Skip to content

Commit ed45788

Browse files
authored
Rollup merge of rust-lang#59738 - flip1995:match_path_move, r=eddyb,Manishearth
Move match_path from DefId to lint::LateContext cc rust-lang#59316 (comment) r? @eddyb
2 parents 8159f38 + 278ab19 commit ed45788

File tree

3 files changed

+114
-107
lines changed

3 files changed

+114
-107
lines changed

src/librustc/hir/def_id.rs

+2-104
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use crate::ty::{self, print::Printer, subst::Kind, Ty, TyCtxt};
2-
use crate::hir::map::definitions::{DisambiguatedDefPathData, FIRST_FREE_HIGH_DEF_INDEX};
1+
use crate::ty::{self, TyCtxt};
2+
use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
33
use rustc_data_structures::indexed_vec::Idx;
44
use serialize;
55
use std::fmt;
66
use std::u32;
7-
use syntax::symbol::{LocalInternedString, Symbol};
87

98
newtype_index! {
109
pub struct CrateId {
@@ -252,107 +251,6 @@ impl DefId {
252251
format!("module `{}`", tcx.def_path_str(*self))
253252
}
254253
}
255-
256-
/// Check if a `DefId`'s path matches the given absolute type path usage.
257-
// Uplifted from rust-lang/rust-clippy
258-
pub fn match_path<'a, 'tcx>(self, tcx: TyCtxt<'a, 'tcx, 'tcx>, path: &[&str]) -> bool {
259-
pub struct AbsolutePathPrinter<'a, 'tcx> {
260-
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
261-
}
262-
263-
impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
264-
type Error = !;
265-
266-
type Path = Vec<LocalInternedString>;
267-
type Region = ();
268-
type Type = ();
269-
type DynExistential = ();
270-
271-
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
272-
self.tcx
273-
}
274-
275-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
276-
Ok(())
277-
}
278-
279-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
280-
Ok(())
281-
}
282-
283-
fn print_dyn_existential(
284-
self,
285-
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
286-
) -> Result<Self::DynExistential, Self::Error> {
287-
Ok(())
288-
}
289-
290-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
291-
Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
292-
}
293-
294-
fn path_qualified(
295-
self,
296-
self_ty: Ty<'tcx>,
297-
trait_ref: Option<ty::TraitRef<'tcx>>,
298-
) -> Result<Self::Path, Self::Error> {
299-
if trait_ref.is_none() {
300-
if let ty::Adt(def, substs) = self_ty.sty {
301-
return self.print_def_path(def.did, substs);
302-
}
303-
}
304-
305-
// This shouldn't ever be needed, but just in case:
306-
Ok(vec![match trait_ref {
307-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
308-
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
309-
}])
310-
}
311-
312-
fn path_append_impl(
313-
self,
314-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
315-
_disambiguated_data: &DisambiguatedDefPathData,
316-
self_ty: Ty<'tcx>,
317-
trait_ref: Option<ty::TraitRef<'tcx>>,
318-
) -> Result<Self::Path, Self::Error> {
319-
let mut path = print_prefix(self)?;
320-
321-
// This shouldn't ever be needed, but just in case:
322-
path.push(match trait_ref {
323-
Some(trait_ref) => {
324-
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
325-
},
326-
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
327-
});
328-
329-
Ok(path)
330-
}
331-
332-
fn path_append(
333-
self,
334-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
335-
disambiguated_data: &DisambiguatedDefPathData,
336-
) -> Result<Self::Path, Self::Error> {
337-
let mut path = print_prefix(self)?;
338-
path.push(disambiguated_data.data.as_interned_str().as_str());
339-
Ok(path)
340-
}
341-
342-
fn path_generic_args(
343-
self,
344-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
345-
_args: &[Kind<'tcx>],
346-
) -> Result<Self::Path, Self::Error> {
347-
print_prefix(self)
348-
}
349-
}
350-
351-
let names = AbsolutePathPrinter { tcx }.print_def_path(self, &[]).unwrap();
352-
353-
names.len() == path.len()
354-
&& names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
355-
}
356254
}
357255

358256
impl serialize::UseSpecializedEncodable for DefId {}

src/librustc/lint/context.rs

+111-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::lint::levels::{LintLevelSets, LintLevelsBuilder};
2525
use crate::middle::privacy::AccessLevels;
2626
use crate::rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
2727
use crate::session::{config, early_error, Session};
28-
use crate::ty::{self, TyCtxt, Ty};
28+
use crate::ty::{self, print::Printer, subst::Kind, TyCtxt, Ty};
2929
use crate::ty::layout::{LayoutError, LayoutOf, TyLayout};
3030
use crate::util::nodemap::FxHashMap;
3131
use crate::util::common::time;
@@ -36,9 +36,10 @@ use syntax::edition;
3636
use syntax_pos::{MultiSpan, Span, symbol::{LocalInternedString, Symbol}};
3737
use errors::DiagnosticBuilder;
3838
use crate::hir;
39-
use crate::hir::def_id::{DefId, LOCAL_CRATE};
39+
use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
4040
use crate::hir::intravisit as hir_visit;
4141
use crate::hir::intravisit::Visitor;
42+
use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData};
4243
use syntax::util::lev_distance::find_best_match_for_name;
4344
use syntax::visit as ast_visit;
4445

@@ -752,6 +753,114 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
752753
pub fn current_lint_root(&self) -> hir::HirId {
753754
self.last_node_with_lint_attrs
754755
}
756+
757+
/// Check if a `DefId`'s path matches the given absolute type path usage.
758+
// Uplifted from rust-lang/rust-clippy
759+
pub fn match_path(&self, def_id: DefId, path: &[&str]) -> bool {
760+
pub struct AbsolutePathPrinter<'a, 'tcx> {
761+
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
762+
}
763+
764+
impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
765+
type Error = !;
766+
767+
type Path = Vec<LocalInternedString>;
768+
type Region = ();
769+
type Type = ();
770+
type DynExistential = ();
771+
772+
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
773+
self.tcx
774+
}
775+
776+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
777+
Ok(())
778+
}
779+
780+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
781+
Ok(())
782+
}
783+
784+
fn print_dyn_existential(
785+
self,
786+
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
787+
) -> Result<Self::DynExistential, Self::Error> {
788+
Ok(())
789+
}
790+
791+
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
792+
Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
793+
}
794+
795+
fn path_qualified(
796+
self,
797+
self_ty: Ty<'tcx>,
798+
trait_ref: Option<ty::TraitRef<'tcx>>,
799+
) -> Result<Self::Path, Self::Error> {
800+
if trait_ref.is_none() {
801+
if let ty::Adt(def, substs) = self_ty.sty {
802+
return self.print_def_path(def.did, substs);
803+
}
804+
}
805+
806+
// This shouldn't ever be needed, but just in case:
807+
Ok(vec![match trait_ref {
808+
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
809+
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
810+
}])
811+
}
812+
813+
fn path_append_impl(
814+
self,
815+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
816+
_disambiguated_data: &DisambiguatedDefPathData,
817+
self_ty: Ty<'tcx>,
818+
trait_ref: Option<ty::TraitRef<'tcx>>,
819+
) -> Result<Self::Path, Self::Error> {
820+
let mut path = print_prefix(self)?;
821+
822+
// This shouldn't ever be needed, but just in case:
823+
path.push(match trait_ref {
824+
Some(trait_ref) => {
825+
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
826+
},
827+
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
828+
});
829+
830+
Ok(path)
831+
}
832+
833+
fn path_append(
834+
self,
835+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
836+
disambiguated_data: &DisambiguatedDefPathData,
837+
) -> Result<Self::Path, Self::Error> {
838+
let mut path = print_prefix(self)?;
839+
840+
// Skip `::{{constructor}}` on tuple/unit structs.
841+
match disambiguated_data.data {
842+
DefPathData::Ctor => return Ok(path),
843+
_ => {}
844+
}
845+
846+
path.push(disambiguated_data.data.as_interned_str().as_str());
847+
Ok(path)
848+
}
849+
850+
fn path_generic_args(
851+
self,
852+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
853+
_args: &[Kind<'tcx>],
854+
) -> Result<Self::Path, Self::Error> {
855+
print_prefix(self)
856+
}
857+
}
858+
859+
let names = AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap();
860+
861+
names.len() == path.len()
862+
&& names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
863+
}
755864
}
756865

757866
impl<'a, 'tcx> LayoutOf for LateContext<'a, 'tcx> {

src/librustc/lint/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
100100
if segment.ident.as_str() == "TyKind" {
101101
if let Some(def) = segment.def {
102102
if let Some(did) = def.opt_def_id() {
103-
return did.match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]);
103+
return cx.match_path(did, &["rustc", "ty", "sty", "TyKind"]);
104104
}
105105
}
106106
}

0 commit comments

Comments
 (0)