Skip to content

Commit 8b0b849

Browse files
committed
Auto merge of #121589 - bvanjoi:fix-98291, r=<try>
try to resolve under eager expander Fixes #98291 I will attempt to clarify the root problem through several examples: Firstly, ```rs // rustc code.rs --edition=2018 macro_rules! wrap { () => { macro_rules! _a { () => { "Hello world" }; } }; } wrap!(); use _a as a; fn main() { format_args!(_a!()); } ``` The above case will compile successfully because `_a` is defined after the `wrap` expaned, ensuring `_a` can be resolved without any issues. And, ```rs // rustc code.rs --edition=2018 macro_rules! wrap { () => { macro_rules! _a { () => { "Hello world" }; } }; } wrap!(); use _a as a; fn main() { format_args!("{}", a!()); } ``` The above example will also compile successfully because the `parse_args` in `expand_format_args_impl` will return a value `MacroInput { fmtstr: Expr::Lit::Str, args: [Expr::MacroCall]}`. Since the graph for `args` will be build lately, `a` will eventually be resolved. However, in the case of: ```rs // rustc code.rs --edition=2018 macro_rules! wrap { () => { macro_rules! _a { () => { "Hello world" }; } }; } wrap!(); use _a as a; fn main() { format_args!(a!()); } ``` The result of `parse_args` is `MacroInput {fmtstr: Expr::Lit::Macro, args: [] }`, we attempt to expand `fmtstr` **eagerly** within `expr_to_spanned_string`. Although we have recorded `(root, _a)` into resolutions, `use _a as a` is an indeterminate import, which will not try to resolve under the conditions of `expander.monotonic = false`. Therefore, I've altered the strategy for resolving indeterminate imports, ensuring it will also resolve during eager expansion. This could be a significant change to the resolution infra. However, I think it's acceptable if the goal of avoiding resolution under eager expansion is to save time. r? `@petrochenkov`
2 parents c7beecf + ee857f7 commit 8b0b849

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use PositionUsedAs::*;
4040

4141
use crate::errors;
4242

43+
#[derive(Debug)]
4344
struct MacroInput {
4445
fmtstr: P<Expr>,
4546
args: FormatArguments,

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
549549
}
550550

551551
fn resolve_imports(&mut self) {
552-
if self.monotonic {
553-
self.cx.resolver.resolve_imports();
554-
}
552+
self.cx.resolver.resolve_imports();
555553
}
556554

557555
/// Collects all macro invocations reachable at this time in this AST fragment, and replace
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@check-pass
2+
//@edition: 2018
3+
4+
// https://github.com/rust-lang/rust/issues/98291
5+
6+
macro_rules! wrap {
7+
() => {
8+
macro_rules! _a {
9+
() => {
10+
"Hello world"
11+
};
12+
}
13+
};
14+
}
15+
16+
wrap!();
17+
18+
use _a as a;
19+
20+
fn main() {
21+
format_args!(a!());
22+
}

0 commit comments

Comments
 (0)