Skip to content

Commit 3afc5ea

Browse files
committed
use def_span and def_kind queries instead of calling tcx.hir() methods
Signed-off-by: Miguel Guarniz <[email protected]>
1 parent ef1d112 commit 3afc5ea

File tree

17 files changed

+31
-104
lines changed

17 files changed

+31
-104
lines changed

compiler/rustc_metadata/src/foreign_modules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_session::cstore::ForeignModule;
66
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
77
let mut modules = Vec::new();
88
for id in tcx.hir().items() {
9-
if !matches!(tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
9+
if !matches!(tcx.def_kind(id.def_id), DefKind::ForeignMod) {
1010
continue;
1111
}
1212
let item = tcx.hir().item(id);

compiler/rustc_metadata/src/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Collector<'tcx> {
3636

3737
impl<'tcx> Collector<'tcx> {
3838
fn process_item(&mut self, id: rustc_hir::ItemId) {
39-
if !matches!(self.tcx.hir().def_kind(id.def_id), DefKind::ForeignMod) {
39+
if !matches!(self.tcx.def_kind(id.def_id), DefKind::ForeignMod) {
4040
return;
4141
}
4242

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18131813
FxHashMap::default();
18141814

18151815
for id in tcx.hir().items() {
1816-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) {
1816+
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
18171817
if let Some(trait_ref) = tcx.impl_trait_ref(id.def_id.to_def_id()) {
18181818
let simplified_self_ty = fast_reject::simplify_type(
18191819
self.tcx,

compiler/rustc_monomorphize/src/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ struct RootCollector<'a, 'tcx> {
11671167

11681168
impl<'v> RootCollector<'_, 'v> {
11691169
fn process_item(&mut self, id: hir::ItemId) {
1170-
match self.tcx.hir().def_kind(id.def_id) {
1170+
match self.tcx.def_kind(id.def_id) {
11711171
DefKind::Enum | DefKind::Struct | DefKind::Union => {
11721172
let item = self.tcx.hir().item(id);
11731173
match item.kind {
@@ -1228,7 +1228,7 @@ impl<'v> RootCollector<'_, 'v> {
12281228
}
12291229

12301230
fn process_impl_item(&mut self, id: hir::ImplItemId) {
1231-
if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) {
1231+
if matches!(self.tcx.def_kind(id.def_id), DefKind::AssocFn) {
12321232
self.push_if_root(id.def_id);
12331233
}
12341234
}

compiler/rustc_passes/src/lang_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
240240
let crate_items = tcx.hir_crate_items(());
241241

242242
for id in crate_items.items() {
243-
collector.check_for_lang(Target::from_def_kind(tcx.hir().def_kind(id.def_id)), id.hir_id());
243+
collector.check_for_lang(Target::from_def_kind(tcx.def_kind(id.def_id)), id.hir_id());
244244

245-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Enum) {
245+
if matches!(tcx.def_kind(id.def_id), DefKind::Enum) {
246246
let item = tcx.hir().item(id);
247247
if let hir::ItemKind::Enum(def, ..) = &item.kind {
248248
for variant in def.variants {

compiler/rustc_typeck/src/check/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
719719
tcx.def_path_str(id.def_id.to_def_id())
720720
);
721721
let _indenter = indenter();
722-
match tcx.hir().def_kind(id.def_id) {
722+
match tcx.def_kind(id.def_id) {
723723
DefKind::Static(..) => {
724724
tcx.ensure().typeck(id.def_id);
725725
maybe_check_static_with_link_section(tcx, id.def_id, tcx.def_span(id.def_id));
@@ -1473,7 +1473,6 @@ pub(super) fn check_type_params_are_used<'tcx>(
14731473
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
14741474
let module = tcx.hir_module_items(module_def_id);
14751475
for id in module.items() {
1476-
// let item = tcx.hir().item(id);
14771476
check_item_type(tcx, id);
14781477
}
14791478
}

compiler/rustc_typeck/src/check_unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1717
}
1818

1919
for id in tcx.hir().items() {
20-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Use) {
20+
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
2121
if tcx.visibility(id.def_id).is_public() {
2222
continue;
2323
}
@@ -101,7 +101,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
101101
let mut crates_to_lint = vec![];
102102

103103
for id in tcx.hir().items() {
104-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::ExternCrate) {
104+
if matches!(tcx.def_kind(id.def_id), DefKind::ExternCrate) {
105105
let item = tcx.hir().item(id);
106106
if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
107107
crates_to_lint.push(ExternCrateToLint {

compiler/rustc_typeck/src/coherence/inherent_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> InherentCollect<'tcx> {
177177
}
178178

179179
fn check_item(&mut self, id: hir::ItemId) {
180-
if !matches!(self.tcx.hir().def_kind(id.def_id), DefKind::Impl) {
180+
if !matches!(self.tcx.def_kind(id.def_id), DefKind::Impl) {
181181
return;
182182
}
183183

compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
126126
}
127127

128128
fn check_item(&mut self, id: hir::ItemId) {
129-
let def_kind = self.tcx.hir().def_kind(id.def_id);
129+
let def_kind = self.tcx.def_kind(id.def_id);
130130
if !matches!(def_kind, DefKind::Enum | DefKind::Struct | DefKind::Trait | DefKind::Union) {
131131
return;
132132
}

compiler/rustc_typeck/src/coherence/unsafety.rs

+1-72
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::TyCtxt;
99

1010
pub fn check(tcx: TyCtxt<'_>) {
1111
for id in tcx.hir().items() {
12-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) {
12+
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
1313
let item = tcx.hir().item(id);
1414
if let hir::ItemKind::Impl(ref impl_) = item.kind {
1515
check_unsafety_coherence(
@@ -83,74 +83,3 @@ fn check_unsafety_coherence<'tcx>(
8383
}
8484
}
8585
}
86-
87-
// struct UnsafetyChecker<'tcx> {
88-
// tcx: TyCtxt<'tcx>,
89-
// }
90-
//
91-
// impl<'tcx> UnsafetyChecker<'tcx> {
92-
// fn check_unsafety_coherence(
93-
// &mut self,
94-
// item: &hir::Item<'_>,
95-
// impl_generics: Option<&hir::Generics<'_>>,
96-
// unsafety: hir::Unsafety,
97-
// polarity: hir::ImplPolarity,
98-
// ) {
99-
// if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id) {
100-
// let trait_def = self.tcx.trait_def(trait_ref.def_id);
101-
// let unsafe_attr = impl_generics.and_then(|generics| {
102-
// generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle")
103-
// });
104-
// match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
105-
// (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
106-
// struct_span_err!(
107-
// self.tcx.sess,
108-
// item.span,
109-
// E0199,
110-
// "implementing the trait `{}` is not unsafe",
111-
// trait_ref.print_only_trait_path()
112-
// )
113-
// .emit();
114-
// }
115-
//
116-
// (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
117-
// struct_span_err!(
118-
// self.tcx.sess,
119-
// item.span,
120-
// E0200,
121-
// "the trait `{}` requires an `unsafe impl` declaration",
122-
// trait_ref.print_only_trait_path()
123-
// )
124-
// .emit();
125-
// }
126-
//
127-
// (
128-
// Unsafety::Normal,
129-
// Some(attr_name),
130-
// Unsafety::Normal,
131-
// hir::ImplPolarity::Positive,
132-
// ) => {
133-
// struct_span_err!(
134-
// self.tcx.sess,
135-
// item.span,
136-
// E0569,
137-
// "requires an `unsafe impl` declaration due to `#[{}]` attribute",
138-
// attr_name
139-
// )
140-
// .emit();
141-
// }
142-
//
143-
// (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
144-
// // Reported in AST validation
145-
// self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
146-
// }
147-
// (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
148-
// | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
149-
// | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
150-
// | (Unsafety::Normal, None, Unsafety::Normal, _) => {
151-
// // OK
152-
// }
153-
// }
154-
// }
155-
// }
156-
// }

compiler/rustc_typeck/src/impl_wf_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
6565
let min_specialization = tcx.features().min_specialization;
6666
let module = tcx.hir_module_items(module_def_id);
6767
for id in module.items() {
68-
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Impl) {
68+
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
6969
let item = tcx.hir().item(id);
7070
if let hir::ItemKind::Impl(ref impl_) = item.kind {
7171
enforce_impl_params_are_constrained(tcx, item.def_id, impl_.items);

compiler/rustc_typeck/src/outlives/implicit_infer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn infer_predicates<'tcx>(
3535
debug!("InferVisitor::visit_item(item={:?})", item_did);
3636

3737
let mut item_required_predicates = RequiredPredicates::default();
38-
match tcx.hir().def_kind(item_did) {
38+
match tcx.def_kind(item_did) {
3939
DefKind::Union | DefKind::Enum | DefKind::Struct => {
4040
let adt_def = tcx.adt_def(item_did.to_def_id());
4141

compiler/rustc_typeck/src/outlives/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn test_inferred_outlives(tcx: TyCtxt<'_>) {
1010
let inferred_outlives_of = tcx.inferred_outlives_of(id.def_id);
1111
struct_span_err!(
1212
tcx.sess,
13-
tcx.hir().span(id.hir_id()),
13+
tcx.def_span(id.def_id),
1414
E0640,
1515
"{:?}",
1616
inferred_outlives_of

compiler/rustc_typeck/src/variance/constraints.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
6969
}
7070

7171
for id in crate_items.trait_items() {
72-
if let DefKind::AssocFn = tcx.hir().def_kind(id.def_id) {
72+
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
7373
constraint_cx.check_node_helper(id.hir_id());
7474
}
7575
}
7676

7777
for id in crate_items.impl_items() {
78-
if let DefKind::AssocFn = tcx.hir().def_kind(id.def_id) {
78+
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
7979
constraint_cx.check_node_helper(id.hir_id());
8080
}
8181
}
8282

8383
for id in crate_items.foreign_items() {
84-
if let DefKind::Fn = tcx.hir().def_kind(id.def_id) {
84+
if let DefKind::Fn = tcx.def_kind(id.def_id) {
8585
constraint_cx.check_node_helper(id.hir_id());
8686
}
8787
}
@@ -91,7 +91,7 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
9191

9292
impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
9393
fn check_item(&mut self, id: hir::ItemId) {
94-
let def_kind = self.tcx().hir().def_kind(id.def_id);
94+
let def_kind = self.tcx().def_kind(id.def_id);
9595
match def_kind {
9696
DefKind::Struct | DefKind::Union => {
9797
let item = self.tcx().hir().item(id);

compiler/rustc_typeck/src/variance/terms.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
8686
}
8787

8888
for id in crate_items.trait_items() {
89-
if let DefKind::AssocFn = tcx.hir().def_kind(id.def_id) {
89+
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
9090
terms_cx.add_inferreds_for_item(id.hir_id());
9191
}
9292
}
9393

9494
for id in crate_items.impl_items() {
95-
if let DefKind::AssocFn = tcx.hir().def_kind(id.def_id) {
95+
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
9696
terms_cx.add_inferreds_for_item(id.hir_id());
9797
}
9898
}
9999

100100
for id in crate_items.foreign_items() {
101-
if let DefKind::Fn = tcx.hir().def_kind(id.def_id) {
101+
if let DefKind::Fn = tcx.def_kind(id.def_id) {
102102
terms_cx.add_inferreds_for_item(id.hir_id());
103103
}
104104
}
@@ -150,7 +150,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
150150
fn check_item(&mut self, id: hir::ItemId) {
151151
debug!("add_inferreds for item {}", self.tcx.hir().node_to_string(id.hir_id()));
152152

153-
let def_kind = self.tcx.hir().def_kind(id.def_id);
153+
let def_kind = self.tcx.def_kind(id.def_id);
154154
match def_kind {
155155
DefKind::Struct | DefKind::Union => {
156156
let item = self.tcx.hir().item(id);

compiler/rustc_typeck/src/variance/test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ pub fn test_variance(tcx: TyCtxt<'_>) {
88
for id in tcx.hir().items() {
99
if tcx.has_attr(id.def_id.to_def_id(), sym::rustc_variance) {
1010
let variances_of = tcx.variances_of(id.def_id);
11-
struct_span_err!(tcx.sess, tcx.hir().span(id.hir_id()), E0208, "{:?}", variances_of)
12-
.emit();
11+
struct_span_err!(tcx.sess, tcx.def_span(id.def_id), E0208, "{:?}", variances_of).emit();
1312
}
1413
}
1514
}

src/tools/clippy/clippy_lints/src/same_name_method.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
5151
let mut map = FxHashMap::<Res, ExistingName>::default();
5252

5353
for id in cx.tcx.hir().items() {
54-
if matches!(cx.tcx.hir().def_kind(id.def_id), DefKind::Impl)
54+
if matches!(cx.tcx.def_kind(id.def_id), DefKind::Impl)
5555
&& let item = cx.tcx.hir().item(id)
5656
&& let ItemKind::Impl(Impl {
57-
items,
58-
of_trait,
59-
self_ty,
60-
..
61-
}) = &item.kind
57+
items,
58+
of_trait,
59+
self_ty,
60+
..
61+
}) = &item.kind
6262
&& let TyKind::Path(QPath::Resolved(_, Path { res, .. })) = self_ty.kind
6363
{
6464
if !map.contains_key(res) {

0 commit comments

Comments
 (0)