From cb297a1ae791fe22067c84ee460913317a1744d5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 7 Jun 2024 20:42:31 +0300 Subject: [PATCH] WIP --- .../rustc_resolve/src/build_reduced_graph.rs | 19 ++++++-- .../attributes/key-value-expansion-scope.rs | 13 +++--- .../key-value-expansion-scope.stderr | 44 +++++-------------- 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 689109b2840f8..82547ac3653f3 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -14,7 +14,7 @@ use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, Modul use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, ResolutionError}; use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, Used, VisResolutionError}; -use rustc_ast::visit::{self, AssocCtxt, Visitor}; +use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind}; use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind}; use rustc_ast::{Block, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId}; use rustc_attr as attr; @@ -1312,7 +1312,17 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> { _ => { let orig_macro_rules_scope = self.parent_scope.macro_rules; self.build_reduced_graph_for_item(item); - visit::walk_item(self, item); + match item.kind { + ItemKind::Mod(..) => { + // Visit attributes after items for backward compatibility. + // This way they can use `macro_rules` defined later. + self.visit_vis(&item.vis); + self.visit_ident(item.ident); + item.kind.walk(item, AssocCtxt::Trait, self); + visit::walk_list!(self, visit_attribute, &item.attrs); + } + _ => visit::walk_item(self, item), + } match item.kind { ItemKind::Mod(..) if self.contains_macro_use(&item.attrs) => { self.parent_scope.macro_rules @@ -1502,7 +1512,10 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> { if krate.is_placeholder { self.visit_invoc_in_module(krate.id); } else { - visit::walk_crate(self, krate); + // Visit attributes after items for backward compatibility. + // This way they can use `macro_rules` defined later. + visit::walk_list!(self, visit_item, &krate.items); + visit::walk_list!(self, visit_attribute, &krate.attrs); self.contains_macro_use(&krate.attrs); } } diff --git a/tests/ui/attributes/key-value-expansion-scope.rs b/tests/ui/attributes/key-value-expansion-scope.rs index 147e9bdbe241c..d079d7344ca1e 100644 --- a/tests/ui/attributes/key-value-expansion-scope.rs +++ b/tests/ui/attributes/key-value-expansion-scope.rs @@ -1,6 +1,6 @@ -#![doc = in_root!()] //~ ERROR cannot find macro `in_root` in this scope +#![doc = in_root!()] // ERROR cannot find macro `in_root` in this scope #![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope -#![doc = in_mod_escape!()] //~ ERROR cannot find macro `in_mod_escape` in this scope +#![doc = in_mod_escape!()] // ERROR cannot find macro `in_mod_escape` in this scope #![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope #[doc = in_root!()] //~ ERROR cannot find macro `in_root` in this scope @@ -16,8 +16,9 @@ fn before() { macro_rules! in_root { () => { "" } } +#[doc = in_mod!()] mod macros_stay { - #![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope + #![doc = in_mod!()] // ERROR cannot find macro `in_mod` in this scope macro_rules! in_mod { () => { "" } } @@ -28,8 +29,9 @@ mod macros_stay { } #[macro_use] +#[doc = in_mod_escape!()] mod macros_escape { - #![doc = in_mod_escape!()] //~ ERROR cannot find macro `in_mod_escape` in this scope + #![doc = in_mod_escape!()] // ERROR cannot find macro `in_mod_escape` in this scope macro_rules! in_mod_escape { () => { "" } } @@ -39,8 +41,9 @@ mod macros_escape { } } +#[doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope fn block() { - #![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope + #![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope macro_rules! in_block { () => { "" } } diff --git a/tests/ui/attributes/key-value-expansion-scope.stderr b/tests/ui/attributes/key-value-expansion-scope.stderr index c7713a0ee09f2..c4a04e14fa6f1 100644 --- a/tests/ui/attributes/key-value-expansion-scope.stderr +++ b/tests/ui/attributes/key-value-expansion-scope.stderr @@ -1,11 +1,3 @@ -error: cannot find macro `in_root` in this scope - --> $DIR/key-value-expansion-scope.rs:1:10 - | -LL | #![doc = in_root!()] - | ^^^^^^^ - | - = help: have you added the `#[macro_use]` on the module/import? - error: cannot find macro `in_mod` in this scope --> $DIR/key-value-expansion-scope.rs:2:10 | @@ -14,14 +6,6 @@ LL | #![doc = in_mod!()] | = help: have you added the `#[macro_use]` on the module/import? -error: cannot find macro `in_mod_escape` in this scope - --> $DIR/key-value-expansion-scope.rs:3:10 - | -LL | #![doc = in_mod_escape!()] - | ^^^^^^^^^^^^^ - | - = help: have you added the `#[macro_use]` on the module/import? - error: cannot find macro `in_block` in this scope --> $DIR/key-value-expansion-scope.rs:4:10 | @@ -94,24 +78,16 @@ LL | #![doc = in_block!()] | = help: have you added the `#[macro_use]` on the module/import? -error: cannot find macro `in_mod` in this scope - --> $DIR/key-value-expansion-scope.rs:20:14 - | -LL | #![doc = in_mod!()] - | ^^^^^^ - | - = help: have you added the `#[macro_use]` on the module/import? - -error: cannot find macro `in_mod_escape` in this scope - --> $DIR/key-value-expansion-scope.rs:32:14 +error: cannot find macro `in_block` in this scope + --> $DIR/key-value-expansion-scope.rs:44:9 | -LL | #![doc = in_mod_escape!()] - | ^^^^^^^^^^^^^ +LL | #[doc = in_block!()] + | ^^^^^^^^ | = help: have you added the `#[macro_use]` on the module/import? error: cannot find macro `in_block` in this scope - --> $DIR/key-value-expansion-scope.rs:43:14 + --> $DIR/key-value-expansion-scope.rs:46:14 | LL | #![doc = in_block!()] | ^^^^^^^^ @@ -119,7 +95,7 @@ LL | #![doc = in_block!()] = help: have you added the `#[macro_use]` on the module/import? error: cannot find macro `in_mod` in this scope - --> $DIR/key-value-expansion-scope.rs:54:9 + --> $DIR/key-value-expansion-scope.rs:57:9 | LL | #[doc = in_mod!()] | ^^^^^^ @@ -127,7 +103,7 @@ LL | #[doc = in_mod!()] = help: have you added the `#[macro_use]` on the module/import? error: cannot find macro `in_block` in this scope - --> $DIR/key-value-expansion-scope.rs:56:9 + --> $DIR/key-value-expansion-scope.rs:59:9 | LL | #[doc = in_block!()] | ^^^^^^^^ @@ -135,7 +111,7 @@ LL | #[doc = in_block!()] = help: have you added the `#[macro_use]` on the module/import? error: cannot find macro `in_mod` in this scope - --> $DIR/key-value-expansion-scope.rs:59:14 + --> $DIR/key-value-expansion-scope.rs:62:14 | LL | #![doc = in_mod!()] | ^^^^^^ @@ -143,12 +119,12 @@ LL | #![doc = in_mod!()] = help: have you added the `#[macro_use]` on the module/import? error: cannot find macro `in_block` in this scope - --> $DIR/key-value-expansion-scope.rs:61:14 + --> $DIR/key-value-expansion-scope.rs:64:14 | LL | #![doc = in_block!()] | ^^^^^^^^ | = help: have you added the `#[macro_use]` on the module/import? -error: aborting due to 19 previous errors +error: aborting due to 16 previous errors