Skip to content

Commit 3a035cd

Browse files
committed
delay expand macro bang when there has indeterminate path
1 parent 9d272a1 commit 3a035cd

21 files changed

+458
-213
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+49-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::token::{self, Delimiter};
55
use rustc_ast::tokenstream::TokenStream;
66
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
77
use rustc_errors::PResult;
8-
use rustc_expand::base::{self, *};
8+
use rustc_expand::base::*;
99
use rustc_index::bit_set::GrowableBitSet;
1010
use rustc_parse::parser::Parser;
1111
use rustc_parse_format as parse;
@@ -443,7 +443,7 @@ fn parse_reg<'a>(
443443
fn expand_preparsed_asm(
444444
ecx: &mut ExtCtxt<'_>,
445445
args: AsmArgs,
446-
) -> Result<ast::InlineAsm, ErrorGuaranteed> {
446+
) -> ExpandResult<Result<ast::InlineAsm, ErrorGuaranteed>, ()> {
447447
let mut template = vec![];
448448
// Register operands are implicitly used since they are not allowed to be
449449
// referenced in the template string.
@@ -465,16 +465,22 @@ fn expand_preparsed_asm(
465465

466466
let msg = "asm template must be a string literal";
467467
let template_sp = template_expr.span;
468-
let (template_str, template_style, template_span) =
469-
match expr_to_spanned_string(ecx, template_expr, msg) {
468+
let (template_str, template_style, template_span) = {
469+
let mac = match expr_to_spanned_string(ecx, template_expr, msg) {
470+
ExpandResult::Ready(ret) => ret,
471+
ExpandResult::Retry(_) => return ExpandResult::Retry(()),
472+
};
473+
match mac {
470474
Ok(template_part) => template_part,
471475
Err(err) => {
472-
return Err(match err {
476+
let err = Err(match err {
473477
Ok((err, _)) => err.emit(),
474478
Err(guar) => guar,
475479
});
480+
return ExpandResult::Ready(err);
476481
}
477-
};
482+
}
483+
};
478484

479485
let str_style = match template_style {
480486
ast::StrStyle::Cooked => None,
@@ -562,7 +568,7 @@ fn expand_preparsed_asm(
562568
e.span_label(err_sp, label);
563569
}
564570
let guar = e.emit();
565-
return Err(guar);
571+
return ExpandResult::Ready(Err(guar));
566572
}
567573

568574
curarg = parser.curarg;
@@ -729,24 +735,28 @@ fn expand_preparsed_asm(
729735
}
730736
}
731737

732-
Ok(ast::InlineAsm {
738+
ExpandResult::Ready(Ok(ast::InlineAsm {
733739
template,
734740
template_strs: template_strs.into_boxed_slice(),
735741
operands: args.operands,
736742
clobber_abis: args.clobber_abis,
737743
options: args.options,
738744
line_spans,
739-
})
745+
}))
740746
}
741747

742748
pub(super) fn expand_asm<'cx>(
743749
ecx: &'cx mut ExtCtxt<'_>,
744750
sp: Span,
745751
tts: TokenStream,
746-
) -> Box<dyn base::MacResult + 'cx> {
747-
match parse_args(ecx, sp, tts, false) {
752+
) -> MacroExpanderResult<'cx> {
753+
let res = match parse_args(ecx, sp, tts, false) {
748754
Ok(args) => {
749-
let expr = match expand_preparsed_asm(ecx, args) {
755+
let mac = match expand_preparsed_asm(ecx, args) {
756+
ExpandResult::Ready(ret) => ret,
757+
ExpandResult::Retry(_) => return ExpandResult::Retry(()),
758+
};
759+
let expr = match mac {
750760
Ok(inline_asm) => P(ast::Expr {
751761
id: ast::DUMMY_NODE_ID,
752762
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
@@ -762,34 +772,42 @@ pub(super) fn expand_asm<'cx>(
762772
let guar = err.emit();
763773
DummyResult::any(sp, guar)
764774
}
765-
}
775+
};
776+
ExpandResult::Ready(res)
766777
}
767778

768779
pub(super) fn expand_global_asm<'cx>(
769780
ecx: &'cx mut ExtCtxt<'_>,
770781
sp: Span,
771782
tts: TokenStream,
772-
) -> Box<dyn base::MacResult + 'cx> {
773-
match parse_args(ecx, sp, tts, true) {
774-
Ok(args) => match expand_preparsed_asm(ecx, args) {
775-
Ok(inline_asm) => MacEager::items(smallvec![P(ast::Item {
776-
ident: Ident::empty(),
777-
attrs: ast::AttrVec::new(),
778-
id: ast::DUMMY_NODE_ID,
779-
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
780-
vis: ast::Visibility {
781-
span: sp.shrink_to_lo(),
782-
kind: ast::VisibilityKind::Inherited,
783+
) -> MacroExpanderResult<'cx> {
784+
let res = match parse_args(ecx, sp, tts, true) {
785+
Ok(args) => {
786+
let mac = match expand_preparsed_asm(ecx, args) {
787+
ExpandResult::Ready(ret) => ret,
788+
ExpandResult::Retry(_) => return ExpandResult::Retry(()),
789+
};
790+
match mac {
791+
Ok(inline_asm) => MacEager::items(smallvec![P(ast::Item {
792+
ident: Ident::empty(),
793+
attrs: ast::AttrVec::new(),
794+
id: ast::DUMMY_NODE_ID,
795+
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
796+
vis: ast::Visibility {
797+
span: sp.shrink_to_lo(),
798+
kind: ast::VisibilityKind::Inherited,
799+
tokens: None,
800+
},
801+
span: sp,
783802
tokens: None,
784-
},
785-
span: sp,
786-
tokens: None,
787-
})]),
788-
Err(guar) => DummyResult::any(sp, guar),
789-
},
803+
})]),
804+
Err(guar) => DummyResult::any(sp, guar),
805+
}
806+
}
790807
Err(err) => {
791808
let guar = err.emit();
792809
DummyResult::any(sp, guar)
793810
}
794-
}
811+
};
812+
ExpandResult::Ready(res)
795813
}

compiler/rustc_builtin_macros/src/assert.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_ast::tokenstream::{DelimSpan, TokenStream};
99
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment, UnOp};
1010
use rustc_ast_pretty::pprust;
1111
use rustc_errors::PResult;
12-
use rustc_expand::base::{DummyResult, ExtCtxt, MacEager, MacResult};
12+
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
1313
use rustc_parse::parser::Parser;
1414
use rustc_span::symbol::{sym, Ident, Symbol};
1515
use rustc_span::{Span, DUMMY_SP};
@@ -19,12 +19,12 @@ pub fn expand_assert<'cx>(
1919
cx: &'cx mut ExtCtxt<'_>,
2020
span: Span,
2121
tts: TokenStream,
22-
) -> Box<dyn MacResult + 'cx> {
22+
) -> MacroExpanderResult<'cx> {
2323
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
2424
Ok(assert) => assert,
2525
Err(err) => {
2626
let guar = err.emit();
27-
return DummyResult::any(span, guar);
27+
return ExpandResult::Ready(DummyResult::any(span, guar));
2828
}
2929
};
3030

@@ -92,7 +92,7 @@ pub fn expand_assert<'cx>(
9292
expr_if_not(cx, call_site_span, cond_expr, then, None)
9393
};
9494

95-
MacEager::expr(expr)
95+
ExpandResult::Ready(MacEager::expr(expr))
9696
}
9797

9898
struct Assert {

compiler/rustc_builtin_macros/src/cfg.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use rustc_ast::token;
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_attr as attr;
1010
use rustc_errors::PResult;
11-
use rustc_expand::base::{DummyResult, ExtCtxt, MacEager, MacResult};
11+
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
1212
use rustc_span::Span;
1313

1414
pub fn expand_cfg(
1515
cx: &mut ExtCtxt<'_>,
1616
sp: Span,
1717
tts: TokenStream,
18-
) -> Box<dyn MacResult + 'static> {
18+
) -> MacroExpanderResult<'static> {
1919
let sp = cx.with_def_site_ctxt(sp);
2020

21-
match parse_cfg(cx, sp, tts) {
21+
let res = match parse_cfg(cx, sp, tts) {
2222
Ok(cfg) => {
2323
let matches_cfg = attr::cfg_matches(
2424
&cfg,
@@ -32,7 +32,8 @@ pub fn expand_cfg(
3232
let guar = err.emit();
3333
DummyResult::any(sp, guar)
3434
}
35-
}
35+
};
36+
ExpandResult::Ready(res)
3637
}
3738

3839
fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
// The compiler code necessary to support the compile_error! extension.
22

33
use rustc_ast::tokenstream::TokenStream;
4-
use rustc_expand::base::{get_single_str_from_tts, DummyResult, ExtCtxt, MacResult};
4+
use rustc_expand::base::{
5+
get_single_str_from_tts, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult,
6+
};
57
use rustc_span::Span;
68

79
pub fn expand_compile_error<'cx>(
810
cx: &'cx mut ExtCtxt<'_>,
911
sp: Span,
1012
tts: TokenStream,
11-
) -> Box<dyn MacResult + 'cx> {
12-
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
13+
) -> MacroExpanderResult<'cx> {
14+
let mac = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
15+
ExpandResult::Ready(ret) => ret,
16+
ExpandResult::Retry(_) => return ExpandResult::Retry(()),
17+
};
18+
let var = match mac {
1319
Ok(var) => var,
14-
Err(guar) => return DummyResult::any(sp, guar),
20+
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
1521
};
1622

1723
#[expect(rustc::diagnostic_outside_of_impl, reason = "diagnostic message is specified by user")]
1824
#[expect(rustc::untranslatable_diagnostic, reason = "diagnostic message is specified by user")]
1925
let guar = cx.dcx().span_err(sp, var.to_string());
2026

21-
DummyResult::any(sp, guar)
27+
ExpandResult::Ready(DummyResult::any(sp, guar))
2228
}

compiler/rustc_builtin_macros/src/concat.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_ast::tokenstream::TokenStream;
22
use rustc_ast::{ExprKind, LitKind, UnOp};
3-
use rustc_expand::base::{get_exprs_from_tts, DummyResult, ExtCtxt, MacEager, MacResult};
3+
use rustc_expand::base::{
4+
get_exprs_from_tts, DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult,
5+
};
46
use rustc_session::errors::report_lit_error;
57
use rustc_span::symbol::Symbol;
68

@@ -10,10 +12,13 @@ pub fn expand_concat(
1012
cx: &mut ExtCtxt<'_>,
1113
sp: rustc_span::Span,
1214
tts: TokenStream,
13-
) -> Box<dyn MacResult + 'static> {
14-
let es = match get_exprs_from_tts(cx, tts) {
15+
) -> MacroExpanderResult<'static> {
16+
let ExpandResult::Ready(mac) = get_exprs_from_tts(cx, tts) else {
17+
return ExpandResult::Retry(());
18+
};
19+
let es = match mac {
1520
Ok(es) => es,
16-
Err(guar) => return DummyResult::any(sp, guar),
21+
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
1722
};
1823
let mut accumulator = String::new();
1924
let mut missing_literal = vec![];
@@ -72,10 +77,10 @@ pub fn expand_concat(
7277

7378
if !missing_literal.is_empty() {
7479
let guar = cx.dcx().emit_err(errors::ConcatMissingLiteral { spans: missing_literal });
75-
return DummyResult::any(sp, guar);
80+
return ExpandResult::Ready(DummyResult::any(sp, guar));
7681
} else if let Some(guar) = guar {
77-
return DummyResult::any(sp, guar);
82+
return ExpandResult::Ready(DummyResult::any(sp, guar));
7883
}
7984
let sp = cx.with_def_site_ctxt(sp);
80-
MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
85+
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator))))
8186
}

compiler/rustc_builtin_macros/src/concat_bytes.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_ast::{ptr::P, token, tokenstream::TokenStream, ExprKind, LitIntType, LitKind, UintTy};
2-
use rustc_expand::base::{get_exprs_from_tts, DummyResult, ExtCtxt, MacEager, MacResult};
2+
use rustc_expand::base::{
3+
get_exprs_from_tts, DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult,
4+
};
35
use rustc_session::errors::report_lit_error;
46
use rustc_span::{ErrorGuaranteed, Span};
57

@@ -111,10 +113,13 @@ pub fn expand_concat_bytes(
111113
cx: &mut ExtCtxt<'_>,
112114
sp: Span,
113115
tts: TokenStream,
114-
) -> Box<dyn MacResult + 'static> {
115-
let es = match get_exprs_from_tts(cx, tts) {
116+
) -> MacroExpanderResult<'static> {
117+
let ExpandResult::Ready(mac) = get_exprs_from_tts(cx, tts) else {
118+
return ExpandResult::Retry(());
119+
};
120+
let es = match mac {
116121
Ok(es) => es,
117-
Err(guar) => return DummyResult::any(sp, guar),
122+
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
118123
};
119124
let mut accumulator = Vec::new();
120125
let mut missing_literals = vec![];
@@ -172,10 +177,10 @@ pub fn expand_concat_bytes(
172177
}
173178
if !missing_literals.is_empty() {
174179
let guar = cx.dcx().emit_err(errors::ConcatBytesMissingLiteral { spans: missing_literals });
175-
return MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
180+
return ExpandResult::Ready(MacEager::expr(DummyResult::raw_expr(sp, Some(guar))));
176181
} else if let Some(guar) = guar {
177-
return MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
182+
return ExpandResult::Ready(MacEager::expr(DummyResult::raw_expr(sp, Some(guar))));
178183
}
179184
let sp = cx.with_def_site_ctxt(sp);
180-
MacEager::expr(cx.expr_byte_str(sp, accumulator))
185+
ExpandResult::Ready(MacEager::expr(cx.expr_byte_str(sp, accumulator)))
181186
}

compiler/rustc_builtin_macros/src/concat_idents.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::ptr::P;
22
use rustc_ast::token::{self, Token};
33
use rustc_ast::tokenstream::{TokenStream, TokenTree};
44
use rustc_ast::{AttrVec, Expr, ExprKind, Path, Ty, TyKind, DUMMY_NODE_ID};
5-
use rustc_expand::base::{DummyResult, ExtCtxt, MacResult};
5+
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacResult, MacroExpanderResult};
66
use rustc_span::symbol::{Ident, Symbol};
77
use rustc_span::Span;
88

@@ -12,10 +12,10 @@ pub fn expand_concat_idents<'cx>(
1212
cx: &'cx mut ExtCtxt<'_>,
1313
sp: Span,
1414
tts: TokenStream,
15-
) -> Box<dyn MacResult + 'cx> {
15+
) -> MacroExpanderResult<'cx> {
1616
if tts.is_empty() {
1717
let guar = cx.dcx().emit_err(errors::ConcatIdentsMissingArgs { span: sp });
18-
return DummyResult::any(sp, guar);
18+
return ExpandResult::Ready(DummyResult::any(sp, guar));
1919
}
2020

2121
let mut res_str = String::new();
@@ -25,7 +25,7 @@ pub fn expand_concat_idents<'cx>(
2525
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
2626
_ => {
2727
let guar = cx.dcx().emit_err(errors::ConcatIdentsMissingComma { span: sp });
28-
return DummyResult::any(sp, guar);
28+
return ExpandResult::Ready(DummyResult::any(sp, guar));
2929
}
3030
}
3131
} else {
@@ -37,7 +37,7 @@ pub fn expand_concat_idents<'cx>(
3737
}
3838

3939
let guar = cx.dcx().emit_err(errors::ConcatIdentsIdentArgs { span: sp });
40-
return DummyResult::any(sp, guar);
40+
return ExpandResult::Ready(DummyResult::any(sp, guar));
4141
}
4242
}
4343

@@ -68,5 +68,5 @@ pub fn expand_concat_idents<'cx>(
6868
}
6969
}
7070

71-
Box::new(ConcatIdentsResult { ident })
71+
ExpandResult::Ready(Box::new(ConcatIdentsResult { ident }))
7272
}

0 commit comments

Comments
 (0)