Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve $crate at the expansion-local crate #99445

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustc_span::edition::Edition;
use rustc_span::hygiene::Transparency;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, sym, Ident, MacroRulesNormalizedIdent};
use rustc_span::Span;
use rustc_span::{HashStableContext, Span};

use std::borrow::Cow;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -381,6 +381,7 @@ pub fn compile_declarative_macro(
features: &Features,
def: &ast::Item,
edition: Edition,
ctx: impl HashStableContext,
) -> (SyntaxExtension, Vec<(usize, Span)>) {
debug!("compile_declarative_macro: {:?}", def);
let mk_syn_ext = |expander| {
Expand Down Expand Up @@ -487,6 +488,7 @@ pub fn compile_declarative_macro(
def.id,
features,
edition,
ctx.clone(),
)
.pop()
.unwrap();
Expand All @@ -511,6 +513,7 @@ pub fn compile_declarative_macro(
def.id,
features,
edition,
ctx.clone(),
)
.pop()
.unwrap();
Expand Down
40 changes: 35 additions & 5 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use rustc_ast::{tokenstream, NodeId};
use rustc_ast_pretty::pprust;
use rustc_feature::Features;
use rustc_session::parse::{feature_err, ParseSess};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::symbol::{kw, sym, Ident};

use rustc_span::edition::Edition;
use rustc_span::{Span, SyntaxContext};
use rustc_span::{ExpnId, HashStableContext, LocalExpnId, Span, SyntaxContext};

const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
Expand Down Expand Up @@ -42,6 +43,7 @@ pub(super) fn parse(
node_id: NodeId,
features: &Features,
edition: Edition,
ctx: impl HashStableContext,
) -> Vec<TokenTree> {
// Will contain the final collection of `self::TokenTree`
let mut result = Vec::new();
Expand All @@ -52,7 +54,16 @@ pub(super) fn parse(
while let Some(tree) = trees.next() {
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
let tree = parse_tree(tree, &mut trees, parsing_patterns, sess, node_id, features, edition);
let tree = parse_tree(
tree,
&mut trees,
parsing_patterns,
sess,
node_id,
features,
edition,
ctx.clone(),
);
match tree {
TokenTree::MetaVar(start_sp, ident) if parsing_patterns => {
let span = match trees.next() {
Expand Down Expand Up @@ -142,6 +153,7 @@ fn parse_tree(
node_id: NodeId,
features: &Features,
edition: Edition,
ctx: impl HashStableContext,
) -> TokenTree {
// Depending on what `tree` is, we could be parsing different parts of a macro
match tree {
Expand Down Expand Up @@ -202,7 +214,8 @@ fn parse_tree(
// If we didn't find a metavar expression above, then we must have a
// repetition sequence in the macro (e.g. `$(pat)*`). Parse the
// contents of the sequence itself
let sequence = parse(tts, parsing_patterns, sess, node_id, features, edition);
let sequence =
parse(tts, parsing_patterns, sess, node_id, features, edition, ctx);
// Get the Kleene operator and optional separator
let (separator, kleene) =
parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess);
Expand All @@ -221,7 +234,24 @@ fn parse_tree(
let (ident, is_raw) = token.ident().unwrap();
let span = ident.span.with_lo(span.lo());
if ident.name == kw::Crate && !is_raw {
TokenTree::token(token::Ident(kw::DollarCrate, is_raw), span)
// If we use `ident.span` here, $crate will refer to the crate where
// the crate token appears; we want it to refer to the crate which
// is actually defining this macro (the current local crate). This is
// most easily accomplished with a macro expanded macro_rules! which
// uses `$$crate` to get a `$crate` in the expanded macro definition.
let mut expn_data = span.ctxt().outer_expn_data();
expn_data.parent = ExpnId::root();
let local_span = span.fresh_expansion(LocalExpnId::fresh(expn_data, ctx));

if span.ctxt().outer_mark().0.krate != LOCAL_CRATE {
let old = span.ctxt().outer_mark().0.krate.as_def_id();
let new = LOCAL_CRATE.as_def_id();
tracing::debug!(
"$crate changed its target crate from {old:?} to {new:?}"
);
}

TokenTree::token(token::Ident(kw::DollarCrate, is_raw), local_span)
} else {
TokenTree::MetaVar(span, ident)
}
Expand Down Expand Up @@ -262,7 +292,7 @@ fn parse_tree(
span,
Delimited {
delim,
tts: parse(tts, parsing_patterns, sess, node_id, features, edition),
tts: parse(tts, parsing_patterns, sess, node_id, features, edition, ctx),
},
),
}
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,13 +1722,6 @@ impl<'a> Resolver<'a> {
debug!("resolve_crate_root({:?})", ident);
let mut ctxt = ident.span.ctxt();
let mark = if ident.name == kw::DollarCrate {
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
// as described in `SyntaxContext::apply_mark`, so we ignore prepended opaque marks.
// FIXME: This is only a guess and it doesn't work correctly for `macro_rules!`
// definitions actually produced by `macro` and `macro` definitions produced by
// `macro_rules!`, but at least such configurations are not stable yet.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to test the 3 combinations: macro_rules in macro_rules (existing), macro_rules inside macro, macro inside macro_rules.

ctxt = ctxt.normalize_to_macro_rules();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I removed this because it didn't change the behavior of any test (in the default compiler mode x.py test), not because removing it was necessary.

In general, I'm not certain how much of this arm is still necessary now that we're directly giving $crate a new mark when gluing it but at the very least removing the branch entirely didn't seem to work.

debug!(
"resolve_crate_root: marks={:?}",
ctxt.marks().into_iter().map(|(i, t)| (i.expn_data(), t)).collect::<Vec<_>>()
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ impl<'a> Resolver<'a> {
self.session.features_untracked(),
item,
edition,
self.create_stable_hashing_context(),
);

if let Some(builtin_name) = result.builtin_name {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ impl InnerSpan {
///
/// This is a hack to allow using the [`HashStable_Generic`] derive macro
/// instead of implementing everything in rustc_middle.
pub trait HashStableContext {
pub trait HashStableContext: Clone {
fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
fn hash_spans(&self) -> bool;
/// Accesses `sess.opts.unstable_opts.incremental_ignore_spans` since
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/proc-macro/auxiliary/dollar-dollar-crate-external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(macro_metavar_expr)]

#[macro_export]
macro_rules! define_macro {
($m:ident => $item:ident) => {
macro_rules! $m {
() => {
$$crate::$item
};
}
};
}
32 changes: 16 additions & 16 deletions src/test/ui/proc-macro/dollar-crate-issue-57089.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,79 @@ PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/dollar-crate-issue-57089.rs:17:13: 17:19 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:13: 17:19 (#6),
},
Ident {
ident: "M",
span: $DIR/dollar-crate-issue-57089.rs:17:20: 17:21 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:20: 17:21 (#6),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "$crate",
span: $DIR/dollar-crate-issue-57089.rs:17:22: 17:28 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:22: 17:28 (#7),
},
Punct {
ch: ':',
spacing: Joint,
span: $DIR/dollar-crate-issue-57089.rs:17:28: 17:30 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:28: 17:30 (#6),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/dollar-crate-issue-57089.rs:17:28: 17:30 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:28: 17:30 (#6),
},
Ident {
ident: "S",
span: $DIR/dollar-crate-issue-57089.rs:17:30: 17:31 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:30: 17:31 (#6),
},
],
span: $DIR/dollar-crate-issue-57089.rs:17:21: 17:32 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:21: 17:32 (#6),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/dollar-crate-issue-57089.rs:17:32: 17:33 (#4),
span: $DIR/dollar-crate-issue-57089.rs:17:32: 17:33 (#6),
},
]
PRINT-ATTR INPUT (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/dollar-crate-issue-57089.rs:21:9: 21:15 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:9: 21:15 (#6),
},
Ident {
ident: "A",
span: $DIR/dollar-crate-issue-57089.rs:21:16: 21:17 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:16: 21:17 (#6),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "$crate",
span: $DIR/dollar-crate-issue-57089.rs:21:18: 21:24 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:18: 21:24 (#8),
},
Punct {
ch: ':',
spacing: Joint,
span: $DIR/dollar-crate-issue-57089.rs:21:24: 21:26 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:24: 21:26 (#6),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/dollar-crate-issue-57089.rs:21:24: 21:26 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:24: 21:26 (#6),
},
Ident {
ident: "S",
span: $DIR/dollar-crate-issue-57089.rs:21:26: 21:27 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:26: 21:27 (#6),
},
],
span: $DIR/dollar-crate-issue-57089.rs:21:17: 21:28 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:17: 21:28 (#6),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/dollar-crate-issue-57089.rs:21:28: 21:29 (#4),
span: $DIR/dollar-crate-issue-57089.rs:21:28: 21:29 (#6),
},
]
44 changes: 22 additions & 22 deletions src/test/ui/proc-macro/dollar-crate-issue-62325.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,109 @@ PRINT-ATTR INPUT (DISPLAY): struct A(identity! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/dollar-crate-issue-62325.rs:19:5: 19:11 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:5: 19:11 (#5),
},
Ident {
ident: "A",
span: $DIR/dollar-crate-issue-62325.rs:19:12: 19:13 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:12: 19:13 (#5),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "identity",
span: $DIR/dollar-crate-issue-62325.rs:19:14: 19:22 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:14: 19:22 (#5),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/dollar-crate-issue-62325.rs:19:22: 19:23 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:22: 19:23 (#5),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "$crate",
span: $DIR/dollar-crate-issue-62325.rs:19:24: 19:30 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:24: 19:30 (#6),
},
Punct {
ch: ':',
spacing: Joint,
span: $DIR/dollar-crate-issue-62325.rs:19:30: 19:32 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:30: 19:32 (#5),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/dollar-crate-issue-62325.rs:19:30: 19:32 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:30: 19:32 (#5),
},
Ident {
ident: "S",
span: $DIR/dollar-crate-issue-62325.rs:19:32: 19:33 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:32: 19:33 (#5),
},
],
span: $DIR/dollar-crate-issue-62325.rs:19:23: 19:34 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:23: 19:34 (#5),
},
],
span: $DIR/dollar-crate-issue-62325.rs:19:13: 19:35 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:13: 19:35 (#5),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/dollar-crate-issue-62325.rs:19:35: 19:36 (#4),
span: $DIR/dollar-crate-issue-62325.rs:19:35: 19:36 (#5),
},
]
PRINT-ATTR INPUT (DISPLAY): struct B(identity! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/auxiliary/dollar-crate-external.rs:21:5: 21:11 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:5: 21:11 (#15),
},
Ident {
ident: "B",
span: $DIR/auxiliary/dollar-crate-external.rs:21:12: 21:13 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:12: 21:13 (#15),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "identity",
span: $DIR/auxiliary/dollar-crate-external.rs:21:14: 21:22 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:14: 21:22 (#15),
},
Punct {
ch: '!',
spacing: Alone,
span: $DIR/auxiliary/dollar-crate-external.rs:21:22: 21:23 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:22: 21:23 (#15),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "$crate",
span: $DIR/auxiliary/dollar-crate-external.rs:21:24: 21:30 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:24: 21:30 (#16),
},
Punct {
ch: ':',
spacing: Joint,
span: $DIR/auxiliary/dollar-crate-external.rs:21:30: 21:32 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:30: 21:32 (#15),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/auxiliary/dollar-crate-external.rs:21:30: 21:32 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:30: 21:32 (#15),
},
Ident {
ident: "S",
span: $DIR/auxiliary/dollar-crate-external.rs:21:32: 21:33 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:32: 21:33 (#15),
},
],
span: $DIR/auxiliary/dollar-crate-external.rs:21:23: 21:34 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:23: 21:34 (#15),
},
],
span: $DIR/auxiliary/dollar-crate-external.rs:21:13: 21:35 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:13: 21:35 (#15),
},
Punct {
ch: ';',
spacing: Alone,
span: $DIR/auxiliary/dollar-crate-external.rs:21:35: 21:36 (#12),
span: $DIR/auxiliary/dollar-crate-external.rs:21:35: 21:36 (#15),
},
]
Loading