Skip to content

Commit 8578ec9

Browse files
authored
Rollup merge of rust-lang#159893 - mejrs:find_attr_hygiene, r=JonathanBrouwer
Fix `find_attr` hygiene and `rustc_hir` cleanups In particular, `extern crate self as rustc_hir;` leads to very noisy import suggestions if you get an import wrong. Also flatten the `nested_filter` module which seemed to exist just to avoid `None` / `Option:None` nameres conflicts
2 parents 4d77a84 + b41588f commit 8578ec9

8 files changed

Lines changed: 59 additions & 68 deletions

File tree

compiler/rustc_hir/src/arena.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ macro_rules! arena_types {
66
$macro!([
77
// HIR types
88
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
9-
[] attribute: rustc_hir::Attribute,
10-
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
9+
[] attribute: crate::Attribute,
10+
[] owner_info: crate::OwnerInfo<'tcx>,
1111
[] macro_def: rustc_ast::MacroDef,
12-
[] delegation_info: rustc_hir::DelegationInfo,
12+
[] delegation_info: crate::DelegationInfo,
1313
]);
1414
)
1515
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_ast::{AttrStyle, Path, ast};
1212
use rustc_data_structures::Limit;
1313
use rustc_data_structures::fx::FxIndexMap;
1414
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
15-
use rustc_hir::LangItem;
1615
use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash};
1716
use rustc_span::def_id::DefId;
1817
use rustc_span::hygiene::Transparency;
@@ -22,7 +21,7 @@ use thin_vec::ThinVec;
2221

2322
use crate::attrs::diagnostic::*;
2423
use crate::attrs::pretty_printing::PrintAttribute;
25-
use crate::{DefaultBodyStability, PartialConstStability, RustcVersion, Stability};
24+
use crate::{DefaultBodyStability, LangItem, PartialConstStability, RustcVersion, Stability};
2625

2726
#[derive(Copy, Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)]
2827
pub enum EiiImplResolution {

compiler/rustc_hir/src/attrs/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ macro_rules! find_attr {
8585
'done: {
8686
for i in $attributes_list {
8787
#[allow(unused_imports)]
88-
use rustc_hir::attrs::AttributeKind::*;
89-
let i: &rustc_hir::Attribute = i;
88+
use $crate::attrs::AttributeKind::*;
89+
let i: &$crate::Attribute = i;
9090
match i {
91-
rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => {
91+
$crate::Attribute::Parsed($pattern) $(if $guard)? => {
9292
break 'done Some($e);
9393
}
94-
rustc_hir::Attribute::Unparsed(..) => {}
94+
$crate::Attribute::Unparsed(..) => {}
9595
// In lint emitting, there's a specific exception for this warning.
9696
// It's not usually emitted from inside macros from other crates
9797
// (see https://github.com/rust-lang/rust/issues/110613)

compiler/rustc_hir/src/intravisit.rs

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -114,71 +114,65 @@ pub trait HirTyCtxt<'hir> {
114114
fn hir_foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
115115
}
116116

117-
// Used when no tcx is actually available, forcing manual implementation of nested visitors.
117+
/// Used when no tcx is actually available, forcing manual implementation of nested visitors.
118118
impl<'hir> HirTyCtxt<'hir> for ! {
119119
fn hir_node(&self, _: HirId) -> Node<'hir> {
120-
unreachable!();
120+
*self
121121
}
122122
fn hir_body(&self, _: BodyId) -> &'hir Body<'hir> {
123-
unreachable!();
123+
*self
124124
}
125125
fn hir_item(&self, _: ItemId) -> &'hir Item<'hir> {
126-
unreachable!();
126+
*self
127127
}
128128
fn hir_trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
129-
unreachable!();
129+
*self
130130
}
131131
fn hir_impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
132-
unreachable!();
132+
*self
133133
}
134134
fn hir_foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
135-
unreachable!();
135+
*self
136136
}
137137
}
138138

139-
pub mod nested_filter {
140-
use super::HirTyCtxt;
141-
142-
/// Specifies what nested things a visitor wants to visit. By "nested
143-
/// things", we are referring to bits of HIR that are not directly embedded
144-
/// within one another but rather indirectly, through a table in the crate.
145-
/// This is done to control dependencies during incremental compilation: the
146-
/// non-inline bits of HIR can be tracked and hashed separately.
147-
///
148-
/// The most common choice is `OnlyBodies`, which will cause the visitor to
149-
/// visit fn bodies for fns that it encounters, and closure bodies, but
150-
/// skip over nested item-like things.
151-
///
152-
/// See the comments at [`rustc_hir::intravisit`] for more details on the overall
153-
/// visit strategy.
154-
pub trait NestedFilter<'hir> {
155-
type MaybeTyCtxt: HirTyCtxt<'hir>;
156-
157-
/// Whether the visitor visits nested "item-like" things.
158-
/// E.g., item, impl-item.
159-
const INTER: bool;
160-
/// Whether the visitor visits "intra item-like" things.
161-
/// E.g., function body, closure, `AnonConst`
162-
const INTRA: bool;
163-
}
164-
165-
/// Do not visit any nested things. When you add a new
166-
/// "non-nested" thing, you will want to audit such uses to see if
167-
/// they remain valid.
168-
///
169-
/// Use this if you are only walking some particular kind of tree
170-
/// (i.e., a type, or fn signature) and you don't want to thread a
171-
/// `tcx` around.
172-
pub struct None(());
173-
impl NestedFilter<'_> for None {
174-
type MaybeTyCtxt = !;
175-
const INTER: bool = false;
176-
const INTRA: bool = false;
177-
}
139+
/// Specifies what nested things a visitor wants to visit. By "nested
140+
/// things", we are referring to bits of HIR that are not directly embedded
141+
/// within one another but rather indirectly, through a table in the crate.
142+
/// This is done to control dependencies during incremental compilation: the
143+
/// non-inline bits of HIR can be tracked and hashed separately.
144+
///
145+
/// The most common choice is `OnlyBodies`, which will cause the visitor to
146+
/// visit fn bodies for fns that it encounters, and closure bodies, but
147+
/// skip over nested item-like things.
148+
///
149+
/// See the [module level documentation](self) for more details on the overall
150+
/// visit strategy.
151+
pub trait NestedFilter<'hir> {
152+
type MaybeTyCtxt: HirTyCtxt<'hir>;
153+
154+
/// Whether the visitor visits nested "item-like" things.
155+
/// E.g., item, impl-item.
156+
const INTER: bool;
157+
/// Whether the visitor visits "intra item-like" things.
158+
/// E.g., function body, closure, `AnonConst`
159+
const INTRA: bool;
160+
}
161+
162+
/// Do not visit any nested things. When you add a new
163+
/// "non-nested" thing, you will want to audit such uses to see if
164+
/// they remain valid.
165+
///
166+
/// Use this if you are only walking some particular kind of tree
167+
/// (i.e., a type, or fn signature) and you don't want to thread a
168+
/// `tcx` around.
169+
pub struct IgnoreNested(());
170+
impl NestedFilter<'_> for IgnoreNested {
171+
type MaybeTyCtxt = !;
172+
const INTER: bool = false;
173+
const INTRA: bool = false;
178174
}
179175

180-
use nested_filter::NestedFilter;
181-
182176
/// Each method of the Visitor trait is a hook to be potentially
183177
/// overridden. Each method's default implementation recursively visits
184178
/// the substructure of the input via the corresponding `walk` method;
@@ -215,7 +209,7 @@ pub trait Visitor<'v>: Sized {
215209
/// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
216210
/// added in the future, it will cause a panic which can be detected
217211
/// and fixed appropriately.
218-
type NestedFilter: NestedFilter<'v> = nested_filter::None;
212+
type NestedFilter: NestedFilter<'v> = IgnoreNested;
219213

220214
/// The result type of the `visit_*` methods. Can be either `()`,
221215
/// or `ControlFlow<T>`.
@@ -226,16 +220,16 @@ pub trait Visitor<'v>: Sized {
226220
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
227221
panic!(
228222
"maybe_tcx must be implemented or consider using \
229-
`type NestedFilter = nested_filter::None` (the default)"
223+
`type NestedFilter = Skip` (the default)"
230224
);
231225
}
232226

233227
/// Invoked when a nested item is encountered. By default, when
234-
/// `Self::NestedFilter` is `nested_filter::None`, this method does
228+
/// `Self::NestedFilter` is `Skip`, this method does
235229
/// nothing. **You probably don't want to override this method** --
236230
/// instead, override [`Self::NestedFilter`] or use the "shallow" or
237231
/// "deep" visit patterns described at
238-
/// [`rustc_hir::intravisit`]. The only reason to override
232+
/// [`intravisit`](self). The only reason to override
239233
/// this method is if you want a nested pattern but cannot supply a
240234
/// `TyCtxt`; see `maybe_tcx` for advice.
241235
fn visit_nested_item(&mut self, id: ItemId) -> Self::Result {

compiler/rustc_hir/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#![recursion_limit = "256"]
1616
// tidy-alphabetical-end
1717

18-
extern crate self as rustc_hir;
19-
2018
mod arena;
2119
pub mod attrs;
2220
pub mod canonical_symbols;

compiler/rustc_middle/src/hir/nested_filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::intravisit::nested_filter::NestedFilter;
1+
use rustc_hir::intravisit::NestedFilter;
22

33
use crate::ty::TyCtxt;
44

src/tools/clippy/clippy_lints/src/lifetimes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::visit::{try_visit, walk_list};
77
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
88
use rustc_errors::Applicability;
99
use rustc_hir::FnRetTy::Return;
10-
use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter};
10+
use rustc_hir::intravisit::{IgnoreNested, NestedFilter};
1111
use rustc_hir::intravisit::{
1212
Visitor, VisitorExt, walk_fn_decl, walk_generic_args, walk_generic_param, walk_generics, walk_impl_item_ref,
1313
walk_param_bound, walk_poly_trait_ref, walk_trait_ref, walk_ty, walk_unambig_ty, walk_where_predicate,
@@ -714,7 +714,7 @@ fn report_extra_trait_object_lifetimes<'tcx>(
714714
generic_params: &'tcx [GenericParam<'_>],
715715
trait_ref: &'tcx TraitRef<'tcx>,
716716
) {
717-
let mut checker = LifetimeChecker::<hir_nested_filter::None>::new(cx, generic_params);
717+
let mut checker = LifetimeChecker::<IgnoreNested>::new(cx, generic_params);
718718

719719
for param in generic_params {
720720
walk_generic_param(&mut checker, param);
@@ -738,7 +738,7 @@ fn report_extra_trait_object_lifetimes<'tcx>(
738738
}
739739

740740
fn report_extra_lifetimes<'tcx>(cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>, generics: &'tcx Generics<'_>) {
741-
let mut checker = LifetimeChecker::<hir_nested_filter::None>::new(cx, generics.params);
741+
let mut checker = LifetimeChecker::<IgnoreNested>::new(cx, generics.params);
742742

743743
walk_generics(&mut checker, generics);
744744
walk_fn_decl(&mut checker, func);

src/tools/clippy/clippy_lints/src/unnecessary_literal_bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct FindNonLiteralReturn;
8181

8282
impl<'hir> Visitor<'hir> for FindNonLiteralReturn {
8383
type Result = std::ops::ControlFlow<()>;
84-
type NestedFilter = intravisit::nested_filter::None;
84+
type NestedFilter = intravisit::IgnoreNested;
8585

8686
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) -> Self::Result {
8787
if let ExprKind::Ret(Some(ret_val_expr)) = expr.kind

0 commit comments

Comments
 (0)