From 3e0b541f7f96190c08414a1c97ab5dab5128d66a 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 ++++++-- compiler/rustc_resolve/src/macros.rs | 22 +++++++++- .../attributes/key-value-expansion-scope.rs | 13 +++--- .../key-value-expansion-scope.stderr | 44 +++++-------------- .../proc-macro/ambiguous-builtin-attrs.stderr | 30 ++++++------- 5 files changed, 70 insertions(+), 58 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/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 268e7f06d0423..9feb794a31826 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -18,7 +18,9 @@ use rustc_errors::{Applicability, StashKey}; use rustc_expand::base::{Annotatable, DeriveResolution, Indeterminate, ResolverExpand}; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::compile_declarative_macro; -use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion}; +use rustc_expand::expand::{ + AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion, +}; use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_middle::middle::stability; @@ -247,6 +249,24 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> { force: bool, ) -> Result, Indeterminate> { let invoc_id = invoc.expansion_data.id; + + if let Some(&(parent_def_id, _)) = self.invocation_parents.get(&invoc_id) { + if invoc.fragment_kind == AstFragmentKind::Expr + && self.tcx.def_kind(parent_def_id) == DefKind::Mod + { + self.dcx().span_err(invoc.span(), "tralala"); + } + } else if let Some(&(parent_def_id, _)) = self.invocation_parents.get(&eager_expansion_root) + { + if invoc.fragment_kind == AstFragmentKind::Expr + && self.tcx.def_kind(parent_def_id) == DefKind::Mod + { + self.dcx().span_err(invoc.span(), "trulala"); + } + } else { + // self.dcx().span_err(invoc.span(), "what?!"); + } + let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) { Some(parent_scope) => *parent_scope, None => { 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 diff --git a/tests/ui/proc-macro/ambiguous-builtin-attrs.stderr b/tests/ui/proc-macro/ambiguous-builtin-attrs.stderr index 2690c68a83415..0f4ddc065a742 100644 --- a/tests/ui/proc-macro/ambiguous-builtin-attrs.stderr +++ b/tests/ui/proc-macro/ambiguous-builtin-attrs.stderr @@ -4,21 +4,6 @@ error[E0425]: cannot find value `NonExistent` in this scope LL | NonExistent; | ^^^^^^^^^^^ not found in this scope -error[E0659]: `feature` is ambiguous - --> $DIR/ambiguous-builtin-attrs.rs:3:4 - | -LL | #![feature(decl_macro)] - | ^^^^^^^ ambiguous name - | - = note: ambiguous because of a name conflict with a builtin attribute - = note: `feature` could refer to a built-in attribute -note: `feature` could also refer to the attribute macro imported here - --> $DIR/ambiguous-builtin-attrs.rs:6:5 - | -LL | use builtin_attrs::*; - | ^^^^^^^^^^^^^^^^ - = help: use `crate::feature` to refer to this attribute macro unambiguously - error[E0659]: `repr` is ambiguous --> $DIR/ambiguous-builtin-attrs.rs:9:3 | @@ -94,6 +79,21 @@ LL | use deny as allow; | ^^^^^^^^^^^^^ = help: use `crate::allow` to refer to this built-in attribute unambiguously +error[E0659]: `feature` is ambiguous + --> $DIR/ambiguous-builtin-attrs.rs:3:4 + | +LL | #![feature(decl_macro)] + | ^^^^^^^ ambiguous name + | + = note: ambiguous because of a name conflict with a builtin attribute + = note: `feature` could refer to a built-in attribute +note: `feature` could also refer to the attribute macro imported here + --> $DIR/ambiguous-builtin-attrs.rs:6:5 + | +LL | use builtin_attrs::*; + | ^^^^^^^^^^^^^^^^ + = help: use `crate::feature` to refer to this attribute macro unambiguously + error[E0517]: attribute should be applied to a struct, enum, or union --> $DIR/ambiguous-builtin-attrs.rs:20:39 |