Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 13, 2024
1 parent a1b7009 commit 3e0b541
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 58 deletions.
19 changes: 16 additions & 3 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -247,6 +249,24 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
force: bool,
) -> Result<Lrc<SyntaxExtension>, 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 => {
Expand Down
13 changes: 8 additions & 5 deletions tests/ui/attributes/key-value-expansion-scope.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 { () => { "" } }

Expand All @@ -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 { () => { "" } }

Expand All @@ -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 { () => { "" } }

Expand Down
44 changes: 10 additions & 34 deletions tests/ui/attributes/key-value-expansion-scope.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand All @@ -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
|
Expand Down Expand Up @@ -94,61 +78,53 @@ 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!()]
| ^^^^^^^^
|
= 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!()]
| ^^^^^^
|
= 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!()]
| ^^^^^^^^
|
= 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!()]
| ^^^^^^
|
= 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

30 changes: 15 additions & 15 deletions tests/ui/proc-macro/ambiguous-builtin-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down Expand Up @@ -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
|
Expand Down

0 comments on commit 3e0b541

Please sign in to comment.