forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#121589 - bvanjoi:fix-98291, r=petrochenkov
delay expand macro bang when there has indeterminate path Related rust-lang#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`
- Loading branch information
Showing
22 changed files
with
509 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
// The compiler code necessary to support the compile_error! extension. | ||
|
||
use rustc_ast::tokenstream::TokenStream; | ||
use rustc_expand::base::{get_single_str_from_tts, DummyResult, ExtCtxt, MacResult}; | ||
use rustc_expand::base::get_single_str_from_tts; | ||
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult}; | ||
use rustc_span::Span; | ||
|
||
pub fn expand_compile_error<'cx>( | ||
cx: &'cx mut ExtCtxt<'_>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> Box<dyn MacResult + 'cx> { | ||
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") { | ||
) -> MacroExpanderResult<'cx> { | ||
let ExpandResult::Ready(mac) = get_single_str_from_tts(cx, sp, tts, "compile_error!") else { | ||
return ExpandResult::Retry(()); | ||
}; | ||
let var = match mac { | ||
Ok(var) => var, | ||
Err(guar) => return DummyResult::any(sp, guar), | ||
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)), | ||
}; | ||
|
||
#[expect(rustc::diagnostic_outside_of_impl, reason = "diagnostic message is specified by user")] | ||
#[expect(rustc::untranslatable_diagnostic, reason = "diagnostic message is specified by user")] | ||
let guar = cx.dcx().span_err(sp, var.to_string()); | ||
|
||
DummyResult::any(sp, guar) | ||
ExpandResult::Ready(DummyResult::any(sp, guar)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.