Skip to content

Commit c440a5b

Browse files
committed
Add ErrorGuaranteed to ast::ExprKind::Err
1 parent a3fce72 commit c440a5b

File tree

37 files changed

+661
-603
lines changed

37 files changed

+661
-603
lines changed

compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ impl Expr {
12961296
ExprKind::Yeet(..) => ExprPrecedence::Yeet,
12971297
ExprKind::FormatArgs(..) => ExprPrecedence::FormatArgs,
12981298
ExprKind::Become(..) => ExprPrecedence::Become,
1299-
ExprKind::Err | ExprKind::Dummy => ExprPrecedence::Err,
1299+
ExprKind::Err(_) | ExprKind::Dummy => ExprPrecedence::Err,
13001300
}
13011301
}
13021302

@@ -1518,7 +1518,7 @@ pub enum ExprKind {
15181518
FormatArgs(P<FormatArgs>),
15191519

15201520
/// Placeholder for an expression that wasn't syntactically well formed in some way.
1521-
Err,
1521+
Err(ErrorGuaranteed),
15221522

15231523
/// Acts as a null expression. Lowering it will always emit a bug.
15241524
Dummy,

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
15261526
}
15271527
ExprKind::Try(expr) => vis.visit_expr(expr),
15281528
ExprKind::TryBlock(body) => vis.visit_block(body),
1529-
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
1529+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err(_) | ExprKind::Dummy => {}
15301530
}
15311531
vis.visit_id(id);
15321532
vis.visit_span(span);

compiler/rustc_ast/src/util/classify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
8989
| Paren(_)
9090
| Try(_)
9191
| Yeet(None)
92-
| Err
92+
| Err(_)
9393
| Dummy => break None,
9494
}
9595
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
10631063
}
10641064
ExprKind::Try(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
10651065
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
1066-
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
1066+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err(_) | ExprKind::Dummy => {}
10671067
}
10681068

10691069
visitor.visit_expr_post(expression)

compiler/rustc_ast_lowering/src/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
329329
)
330330
}
331331
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
332-
ExprKind::Err => {
333-
hir::ExprKind::Err(self.dcx().span_delayed_bug(e.span, "lowered ExprKind::Err"))
334-
}
332+
ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
335333

336334
ExprKind::Dummy => {
337335
span_bug!(e.span, "lowered ExprKind::Dummy")

compiler/rustc_ast_lowering/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
331331
ExprKind::Lit(..)
332332
| ExprKind::ConstBlock(..)
333333
| ExprKind::IncludedBytes(..)
334-
| ExprKind::Err
334+
| ExprKind::Err(_)
335335
| ExprKind::Dummy => {}
336336
ExprKind::Path(..) if allow_paths => {}
337337
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ impl<'a> State<'a> {
893893
self.word_nbsp("try");
894894
self.print_block_with_attrs(blk, attrs)
895895
}
896-
ast::ExprKind::Err => {
896+
ast::ExprKind::Err(_) => {
897897
self.popen();
898898
self.word("/*ERROR*/");
899899
self.pclose()

compiler/rustc_builtin_macros/src/asm.rs

+35-36
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_session::lint;
1313
use rustc_session::parse::ParseSess;
1414
use rustc_span::symbol::Ident;
1515
use rustc_span::symbol::{kw, sym, Symbol};
16-
use rustc_span::{InnerSpan, Span};
16+
use rustc_span::{ErrorGuaranteed, InnerSpan, Span};
1717
use rustc_target::asm::InlineAsmArch;
1818
use smallvec::smallvec;
1919

@@ -433,7 +433,10 @@ fn parse_reg<'a>(
433433
Ok(result)
434434
}
435435

436-
fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::InlineAsm> {
436+
fn expand_preparsed_asm(
437+
ecx: &mut ExtCtxt<'_>,
438+
args: AsmArgs,
439+
) -> Result<ast::InlineAsm, ErrorGuaranteed> {
437440
let mut template = vec![];
438441
// Register operands are implicitly used since they are not allowed to be
439442
// referenced in the template string.
@@ -459,10 +462,10 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
459462
match expr_to_spanned_string(ecx, template_expr, msg) {
460463
Ok(template_part) => template_part,
461464
Err(err) => {
462-
if let Some((err, _)) = err {
463-
err.emit();
464-
}
465-
return None;
465+
return Err(match err {
466+
Ok((err, _)) => err.emit(),
467+
Err(guar) => guar,
468+
});
466469
}
467470
};
468471

@@ -551,8 +554,8 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
551554
let err_sp = template_span.from_inner(InnerSpan::new(span.start, span.end));
552555
e.span_label(err_sp, label);
553556
}
554-
e.emit();
555-
return None;
557+
let guar = e.emit();
558+
return Err(guar);
556559
}
557560

558561
curarg = parser.curarg;
@@ -719,7 +722,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
719722
}
720723
}
721724

722-
Some(ast::InlineAsm {
725+
Ok(ast::InlineAsm {
723726
template,
724727
template_strs: template_strs.into_boxed_slice(),
725728
operands: args.operands,
@@ -736,22 +739,21 @@ pub(super) fn expand_asm<'cx>(
736739
) -> Box<dyn base::MacResult + 'cx> {
737740
match parse_args(ecx, sp, tts, false) {
738741
Ok(args) => {
739-
let expr = if let Some(inline_asm) = expand_preparsed_asm(ecx, args) {
740-
P(ast::Expr {
742+
let expr = match expand_preparsed_asm(ecx, args) {
743+
Ok(inline_asm) => P(ast::Expr {
741744
id: ast::DUMMY_NODE_ID,
742745
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
743746
span: sp,
744747
attrs: ast::AttrVec::new(),
745748
tokens: None,
746-
})
747-
} else {
748-
DummyResult::raw_expr(sp, true)
749+
}),
750+
Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
749751
};
750752
MacEager::expr(expr)
751753
}
752754
Err(err) => {
753-
err.emit();
754-
DummyResult::any(sp)
755+
let guar = err.emit();
756+
DummyResult::any(sp, guar)
755757
}
756758
}
757759
}
@@ -762,28 +764,25 @@ pub(super) fn expand_global_asm<'cx>(
762764
tts: TokenStream,
763765
) -> Box<dyn base::MacResult + 'cx> {
764766
match parse_args(ecx, sp, tts, true) {
765-
Ok(args) => {
766-
if let Some(inline_asm) = expand_preparsed_asm(ecx, args) {
767-
MacEager::items(smallvec![P(ast::Item {
768-
ident: Ident::empty(),
769-
attrs: ast::AttrVec::new(),
770-
id: ast::DUMMY_NODE_ID,
771-
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
772-
vis: ast::Visibility {
773-
span: sp.shrink_to_lo(),
774-
kind: ast::VisibilityKind::Inherited,
775-
tokens: None,
776-
},
777-
span: sp,
767+
Ok(args) => match expand_preparsed_asm(ecx, args) {
768+
Ok(inline_asm) => MacEager::items(smallvec![P(ast::Item {
769+
ident: Ident::empty(),
770+
attrs: ast::AttrVec::new(),
771+
id: ast::DUMMY_NODE_ID,
772+
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
773+
vis: ast::Visibility {
774+
span: sp.shrink_to_lo(),
775+
kind: ast::VisibilityKind::Inherited,
778776
tokens: None,
779-
})])
780-
} else {
781-
DummyResult::any(sp)
782-
}
783-
}
777+
},
778+
span: sp,
779+
tokens: None,
780+
})]),
781+
Err(guar) => DummyResult::any(sp, guar),
782+
},
784783
Err(err) => {
785-
err.emit();
786-
DummyResult::any(sp)
784+
let guar = err.emit();
785+
DummyResult::any(sp, guar)
787786
}
788787
}
789788
}

compiler/rustc_builtin_macros/src/assert.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub fn expand_assert<'cx>(
2323
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
2424
Ok(assert) => assert,
2525
Err(err) => {
26-
err.emit();
27-
return DummyResult::any(span);
26+
let guar = err.emit();
27+
return DummyResult::any(span, guar);
2828
}
2929
};
3030

compiler/rustc_builtin_macros/src/assert/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
304304
| ExprKind::ConstBlock(_)
305305
| ExprKind::Continue(_)
306306
| ExprKind::Dummy
307-
| ExprKind::Err
307+
| ExprKind::Err(_)
308308
| ExprKind::Field(_, _)
309309
| ExprKind::ForLoop { .. }
310310
| ExprKind::FormatArgs(_)

compiler/rustc_builtin_macros/src/cfg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub fn expand_cfg(
2929
MacEager::expr(cx.expr_bool(sp, matches_cfg))
3030
}
3131
Err(err) => {
32-
err.emit();
33-
DummyResult::any(sp)
32+
let guar = err.emit();
33+
DummyResult::any(sp, guar)
3434
}
3535
}
3636
}

compiler/rustc_builtin_macros/src/compile_error.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ pub fn expand_compile_error<'cx>(
99
sp: Span,
1010
tts: TokenStream,
1111
) -> Box<dyn base::MacResult + 'cx> {
12-
let Some(var) = get_single_str_from_tts(cx, sp, tts, "compile_error!") else {
13-
return DummyResult::any(sp);
12+
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
13+
Ok(var) => var,
14+
Err(guar) => return DummyResult::any(sp, guar),
1415
};
1516

16-
#[expect(
17-
rustc::diagnostic_outside_of_impl,
18-
reason = "diagnostic message is specified by user"
19-
)]
17+
#[expect(rustc::diagnostic_outside_of_impl, reason = "diagnostic message is specified by user")]
2018
#[expect(rustc::untranslatable_diagnostic, reason = "diagnostic message is specified by user")]
21-
cx.dcx().span_err(sp, var.to_string());
19+
let guar = cx.dcx().span_err(sp, var.to_string());
2220

23-
DummyResult::any(sp)
21+
DummyResult::any(sp, guar)
2422
}

compiler/rustc_builtin_macros/src/concat.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ pub fn expand_concat(
1111
sp: rustc_span::Span,
1212
tts: TokenStream,
1313
) -> Box<dyn base::MacResult + 'static> {
14-
let Some(es) = base::get_exprs_from_tts(cx, tts) else {
15-
return DummyResult::any(sp);
14+
let es = match base::get_exprs_from_tts(cx, tts) {
15+
Ok(es) => es,
16+
Err(guar) => return DummyResult::any(sp, guar),
1617
};
1718
let mut accumulator = String::new();
1819
let mut missing_literal = vec![];
19-
let mut has_errors = false;
20+
let mut guar = None;
2021
for e in es {
2122
match e.kind {
2223
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
@@ -33,19 +34,16 @@ pub fn expand_concat(
3334
accumulator.push_str(&b.to_string());
3435
}
3536
Ok(ast::LitKind::CStr(..)) => {
36-
cx.dcx().emit_err(errors::ConcatCStrLit { span: e.span });
37-
has_errors = true;
37+
guar = Some(cx.dcx().emit_err(errors::ConcatCStrLit { span: e.span }));
3838
}
3939
Ok(ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..)) => {
40-
cx.dcx().emit_err(errors::ConcatBytestr { span: e.span });
41-
has_errors = true;
40+
guar = Some(cx.dcx().emit_err(errors::ConcatBytestr { span: e.span }));
4241
}
43-
Ok(ast::LitKind::Err(_)) => {
44-
has_errors = true;
42+
Ok(ast::LitKind::Err(guarantee)) => {
43+
guar = Some(guarantee);
4544
}
4645
Err(err) => {
47-
report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span);
48-
has_errors = true;
46+
guar = Some(report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span));
4947
}
5048
},
5149
// We also want to allow negative numeric literals.
@@ -56,17 +54,16 @@ pub fn expand_concat(
5654
Ok(ast::LitKind::Int(i, _)) => accumulator.push_str(&format!("-{i}")),
5755
Ok(ast::LitKind::Float(f, _)) => accumulator.push_str(&format!("-{f}")),
5856
Err(err) => {
59-
report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span);
60-
has_errors = true;
57+
guar = Some(report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span));
6158
}
6259
_ => missing_literal.push(e.span),
6360
}
6461
}
6562
ast::ExprKind::IncludedBytes(..) => {
6663
cx.dcx().emit_err(errors::ConcatBytestr { span: e.span });
6764
}
68-
ast::ExprKind::Err => {
69-
has_errors = true;
65+
ast::ExprKind::Err(guarantee) => {
66+
guar = Some(guarantee);
7067
}
7168
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
7269
_ => {
@@ -76,10 +73,10 @@ pub fn expand_concat(
7673
}
7774

7875
if !missing_literal.is_empty() {
79-
cx.dcx().emit_err(errors::ConcatMissingLiteral { spans: missing_literal });
80-
return DummyResult::any(sp);
81-
} else if has_errors {
82-
return DummyResult::any(sp);
76+
let guar = cx.dcx().emit_err(errors::ConcatMissingLiteral { spans: missing_literal });
77+
return DummyResult::any(sp, guar);
78+
} else if let Some(guar) = guar {
79+
return DummyResult::any(sp, guar);
8380
}
8481
let sp = cx.with_def_site_ctxt(sp);
8582
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))

0 commit comments

Comments
 (0)