Skip to content

Commit

Permalink
Auto merge of #125741 - petrochenkov:atvisord, r=davidtwco
Browse files Browse the repository at this point in the history
ast: Standardize visiting order for attributes and node IDs

This should only affect `macro_rules` scopes and order of diagnostics.

Also add a deprecation lint for `macro_rules` called outside of their scope, like in #124535.
  • Loading branch information
bors committed Jun 25, 2024
2 parents c2d2bb3 + c4c7859 commit d929a42
Show file tree
Hide file tree
Showing 21 changed files with 412 additions and 186 deletions.
42 changes: 21 additions & 21 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ pub fn noop_flat_map_pat_field<T: MutVisitor>(
) -> SmallVec<[PatField; 1]> {
let PatField { attrs, id, ident, is_placeholder: _, is_shorthand: _, pat, span } = &mut fp;
vis.visit_id(id);
visit_attrs(attrs, vis);
vis.visit_ident(ident);
vis.visit_pat(pat);
vis.visit_span(span);
visit_attrs(attrs, vis);
smallvec![fp]
}

Expand All @@ -443,8 +443,8 @@ fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
UseTreeKind::Simple(rename) => visit_opt(rename, |rename| vis.visit_ident(rename)),
UseTreeKind::Nested { items, .. } => {
for (tree, id) in items {
vis.visit_use_tree(tree);
vis.visit_id(id);
vis.visit_use_tree(tree);
}
}
UseTreeKind::Glob => {}
Expand All @@ -454,8 +454,8 @@ fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {

pub fn noop_flat_map_arm<T: MutVisitor>(mut arm: Arm, vis: &mut T) -> SmallVec<[Arm; 1]> {
let Arm { attrs, pat, guard, body, span, id, is_placeholder: _ } = &mut arm;
visit_attrs(attrs, vis);
vis.visit_id(id);
visit_attrs(attrs, vis);
vis.visit_pat(pat);
visit_opt(guard, |guard| vis.visit_expr(guard));
visit_opt(body, |body| vis.visit_expr(body));
Expand Down Expand Up @@ -548,10 +548,10 @@ pub fn noop_flat_map_variant<T: MutVisitor>(
visitor: &mut T,
) -> SmallVec<[Variant; 1]> {
let Variant { ident, vis, attrs, id, data, disr_expr, span, is_placeholder: _ } = &mut variant;
visitor.visit_id(id);
visit_attrs(attrs, visitor);
visitor.visit_ident(ident);
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
visitor.visit_id(id);
visitor.visit_variant_data(data);
visit_opt(disr_expr, |disr_expr| visitor.visit_anon_const(disr_expr));
visitor.visit_span(span);
Expand All @@ -565,8 +565,8 @@ fn noop_visit_ident<T: MutVisitor>(Ident { name: _, span }: &mut Ident, vis: &mu
fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens }: &mut Path, vis: &mut T) {
vis.visit_span(span);
for PathSegment { ident, id, args } in segments {
vis.visit_ident(ident);
vis.visit_id(id);
vis.visit_ident(ident);
visit_opt(args, |args| vis.visit_generic_args(args));
}
visit_lazy_tts(tokens, vis);
Expand Down Expand Up @@ -620,6 +620,7 @@ fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(
fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
let Local { id, pat, ty, kind, span, colon_sp, attrs, tokens } = local.deref_mut();
vis.visit_id(id);
visit_attrs(attrs, vis);
vis.visit_pat(pat);
visit_opt(ty, |ty| vis.visit_ty(ty));
match kind {
Expand All @@ -634,7 +635,6 @@ fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
}
vis.visit_span(span);
visit_opt(colon_sp, |sp| vis.visit_span(sp));
visit_attrs(attrs, vis);
visit_lazy_tts(tokens, vis);
}

Expand Down Expand Up @@ -894,9 +894,9 @@ fn noop_visit_coroutine_kind<T: MutVisitor>(coroutine_kind: &mut CoroutineKind,
CoroutineKind::Async { span, closure_id, return_impl_trait_id }
| CoroutineKind::Gen { span, closure_id, return_impl_trait_id }
| CoroutineKind::AsyncGen { span, closure_id, return_impl_trait_id } => {
vis.visit_span(span);
vis.visit_id(closure_id);
vis.visit_id(return_impl_trait_id);
vis.visit_span(span);
}
}
}
Expand Down Expand Up @@ -932,8 +932,8 @@ fn noop_visit_precise_capturing_arg<T: MutVisitor>(arg: &mut PreciseCapturingArg
vis.visit_lifetime(lt);
}
PreciseCapturingArg::Arg(path, id) => {
vis.visit_path(path);
vis.visit_id(id);
vis.visit_path(path);
}
}
}
Expand All @@ -944,11 +944,11 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
) -> SmallVec<[GenericParam; 1]> {
let GenericParam { id, ident, attrs, bounds, kind, colon_span, is_placeholder: _ } = &mut param;
vis.visit_id(id);
visit_attrs(attrs, vis);
vis.visit_ident(ident);
if let Some(colon_span) = colon_span {
vis.visit_span(colon_span);
}
visit_attrs(attrs, vis);
visit_vec(bounds, |bound| noop_visit_param_bound(bound, vis));
match kind {
GenericParamKind::Lifetime => {}
Expand Down Expand Up @@ -1015,16 +1015,16 @@ fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut T)
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
}
VariantData::Tuple(fields, id) => {
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
vis.visit_id(id);
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
}
VariantData::Unit(id) => vis.visit_id(id),
}
}

fn noop_visit_trait_ref<T: MutVisitor>(TraitRef { path, ref_id }: &mut TraitRef, vis: &mut T) {
vis.visit_path(path);
vis.visit_id(ref_id);
vis.visit_path(path);
}

fn noop_visit_poly_trait_ref<T: MutVisitor>(p: &mut PolyTraitRef, vis: &mut T) {
Expand All @@ -1039,12 +1039,12 @@ pub fn noop_flat_map_field_def<T: MutVisitor>(
visitor: &mut T,
) -> SmallVec<[FieldDef; 1]> {
let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut fd;
visitor.visit_id(id);
visit_attrs(attrs, visitor);
visitor.visit_span(span);
visit_opt(ident, |ident| visitor.visit_ident(ident));
visitor.visit_vis(vis);
visitor.visit_id(id);
visitor.visit_ty(ty);
visit_attrs(attrs, visitor);
smallvec![fd]
}

Expand All @@ -1053,11 +1053,11 @@ pub fn noop_flat_map_expr_field<T: MutVisitor>(
vis: &mut T,
) -> SmallVec<[ExprField; 1]> {
let ExprField { ident, expr, span, is_shorthand: _, attrs, id, is_placeholder: _ } = &mut f;
vis.visit_id(id);
visit_attrs(attrs, vis);
vis.visit_ident(ident);
vis.visit_expr(expr);
vis.visit_id(id);
vis.visit_span(span);
visit_attrs(attrs, vis);
smallvec![f]
}

Expand Down Expand Up @@ -1429,6 +1429,8 @@ pub fn noop_visit_expr<T: MutVisitor>(
Expr { kind, id, span, attrs, tokens }: &mut Expr,
vis: &mut T,
) {
vis.visit_id(id);
visit_attrs(attrs, vis);
match kind {
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
ExprKind::ConstBlock(anon_const) => {
Expand All @@ -1449,8 +1451,8 @@ pub fn noop_visit_expr<T: MutVisitor>(
args: call_args,
span,
}) => {
vis.visit_ident(ident);
vis.visit_id(id);
vis.visit_ident(ident);
visit_opt(seg_args, |args| vis.visit_generic_args(args));
vis.visit_method_receiver_expr(receiver);
visit_thin_exprs(call_args, vis);
Expand Down Expand Up @@ -1601,9 +1603,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
ExprKind::TryBlock(body) => vis.visit_block(body),
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err(_) | ExprKind::Dummy => {}
}
vis.visit_id(id);
vis.visit_span(span);
visit_attrs(attrs, vis);
visit_lazy_tts(tokens, vis);
}

Expand Down Expand Up @@ -1645,8 +1645,8 @@ fn noop_flat_map_stmt_kind<T: MutVisitor>(kind: StmtKind, vis: &mut T) -> SmallV
StmtKind::Empty => smallvec![StmtKind::Empty],
StmtKind::MacCall(mut mac) => {
let MacCallStmt { mac: mac_, style: _, attrs, tokens } = mac.deref_mut();
vis.visit_mac_call(mac_);
visit_attrs(attrs, vis);
vis.visit_mac_call(mac_);
visit_lazy_tts(tokens, vis);
smallvec![StmtKind::MacCall(mac)]
}
Expand All @@ -1657,8 +1657,8 @@ fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
match &mut visibility.kind {
VisibilityKind::Public | VisibilityKind::Inherited => {}
VisibilityKind::Restricted { path, id, shorthand: _ } => {
vis.visit_path(path);
vis.visit_id(id);
vis.visit_path(path);
}
}
vis.visit_span(&mut visibility.span);
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ pub trait Visitor<'ast>: Sized {
}

pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
walk_list!(visitor, visit_item, &krate.items);
walk_list!(visitor, visit_attribute, &krate.attrs);
walk_list!(visitor, visit_item, &krate.items);
V::Result::output()
}

Expand Down Expand Up @@ -462,25 +462,25 @@ pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant) -
where
V: Visitor<'a>,
{
walk_list!(visitor, visit_attribute, &variant.attrs);
try_visit!(visitor.visit_ident(variant.ident));
try_visit!(visitor.visit_vis(&variant.vis));
try_visit!(visitor.visit_variant_data(&variant.data));
visit_opt!(visitor, visit_variant_discr, &variant.disr_expr);
walk_list!(visitor, visit_attribute, &variant.attrs);
V::Result::output()
}

pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) -> V::Result {
walk_list!(visitor, visit_attribute, &f.attrs);
try_visit!(visitor.visit_expr(&f.expr));
try_visit!(visitor.visit_ident(f.ident));
walk_list!(visitor, visit_attribute, &f.attrs);
V::Result::output()
}

pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) -> V::Result {
walk_list!(visitor, visit_attribute, &fp.attrs);
try_visit!(visitor.visit_ident(fp.ident));
try_visit!(visitor.visit_pat(&fp.pat));
walk_list!(visitor, visit_attribute, &fp.attrs);
V::Result::output()
}

Expand Down Expand Up @@ -722,8 +722,8 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(
visitor: &mut V,
param: &'a GenericParam,
) -> V::Result {
try_visit!(visitor.visit_ident(param.ident));
walk_list!(visitor, visit_attribute, &param.attrs);
try_visit!(visitor.visit_ident(param.ident));
walk_list!(visitor, visit_param_bound, &param.bounds, BoundKind::Bound);
match &param.kind {
GenericParamKind::Lifetime => (),
Expand Down Expand Up @@ -882,10 +882,10 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
ctxt: AssocCtxt,
) -> V::Result {
let &Item { id: _, span: _, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
try_visit!(visitor.visit_ident(ident));
try_visit!(kind.walk(item, ctxt, visitor));
walk_list!(visitor, visit_attribute, attrs);
V::Result::output()
}

Expand All @@ -898,10 +898,10 @@ pub fn walk_struct_def<'a, V: Visitor<'a>>(
}

pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) -> V::Result {
walk_list!(visitor, visit_attribute, &field.attrs);
try_visit!(visitor.visit_vis(&field.vis));
visit_opt!(visitor, visit_ident, field.ident);
try_visit!(visitor.visit_ty(&field.ty));
walk_list!(visitor, visit_attribute, &field.attrs);
V::Result::output()
}

Expand All @@ -918,8 +918,8 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V:
StmtKind::Empty => {}
StmtKind::MacCall(mac) => {
let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
try_visit!(visitor.visit_mac_call(mac));
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_mac_call(mac));
}
}
V::Result::output()
Expand Down Expand Up @@ -1141,10 +1141,10 @@ pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) -> V::R
}

pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) -> V::Result {
walk_list!(visitor, visit_attribute, &arm.attrs);
try_visit!(visitor.visit_pat(&arm.pat));
visit_opt!(visitor, visit_expr, &arm.guard);
visit_opt!(visitor, visit_expr, &arm.body);
walk_list!(visitor, visit_attribute, &arm.attrs);
V::Result::output()
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
.suggestion = use pat_param to preserve semantics
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
.help = import `macro_rules` with `use` to make it callable above its definition
lint_overflowing_bin_hex = literal out of range for `{$ty}`
.negative_note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`
.negative_becomes_note = and the value `-{$lit}` will become `{$actually}{$ty}`
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
lints::InnerAttributeUnstable::CustomInnerAttribute
}
.decorate_lint(diag),
BuiltinLintDiag::OutOfScopeMacroCalls { path } => {
lints::OutOfScopeMacroCalls { path }.decorate_lint(diag)
}
}
}
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2911,3 +2911,10 @@ pub struct UnsafeAttrOutsideUnsafeSuggestion {
#[suggestion_part(code = ")")]
pub right: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_out_of_scope_macro_calls)]
#[help]
pub struct OutOfScopeMacroCalls {
pub path: String,
}
39 changes: 39 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4945,3 +4945,42 @@ declare_lint! {
reference: "issue #123757 <https://github.com/rust-lang/rust/issues/123757>",
};
}

declare_lint! {
/// The `out_of_scope_macro_calls` lint detects `macro_rules` called when they are not in scope,
/// above their definition, which may happen in key-value attributes.
///
/// ### Example
///
/// ```rust
/// #![doc = in_root!()]
///
/// macro_rules! in_root { () => { "" } }
///
/// fn main() {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// The scope in which a `macro_rules` item is visible starts at that item and continues
/// below it. This is more similar to `let` than to other items, which are in scope both above
/// and below their definition.
/// Due to a bug `macro_rules` were accidentally in scope inside some key-value attributes
/// above their definition. The lint catches such cases.
/// To address the issue turn the `macro_rules` into a regularly scoped item by importing it
/// with `use`.
///
/// This is a [future-incompatible] lint to transition this to a
/// hard error in the future.
///
/// [future-incompatible]: ../index.md#future-incompatible-lints
pub OUT_OF_SCOPE_MACRO_CALLS,
Warn,
"detects out of scope calls to `macro_rules` in key-value attributes",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
reference: "issue #124535 <https://github.com/rust-lang/rust/issues/124535>",
};
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ pub enum BuiltinLintDiag {
InnerAttributeUnstable {
is_macro: bool,
},
OutOfScopeMacroCalls {
path: String,
},
}

/// Lints that are buffered up early on in the `Session` before the
Expand Down
Loading

0 comments on commit d929a42

Please sign in to comment.