Skip to content

Commit 99eba8f

Browse files
committed
Auto merge of #120586 - ShE3py:exprkind-err, r=fmease
Add `ErrorGuaranteed` to `ast::ExprKind::Err` See #119967 for context ``` \ \ _~^~^~_ \) / o o \ (/ '_ - _' / '-----' \ ``` r? fmease
2 parents ee933f6 + 34eae07 commit 99eba8f

File tree

38 files changed

+785
-716
lines changed

38 files changed

+785
-716
lines changed

compiler/rustc_ast/src/ast.rs

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

1303-
pub fn take(&mut self) -> Self {
1304-
mem::replace(
1305-
self,
1306-
Expr {
1307-
id: DUMMY_NODE_ID,
1308-
kind: ExprKind::Err,
1309-
span: DUMMY_SP,
1310-
attrs: AttrVec::new(),
1311-
tokens: None,
1312-
},
1313-
)
1314-
}
1315-
13161303
/// To a first-order approximation, is this a pattern?
13171304
pub fn is_approximately_pattern(&self) -> bool {
13181305
matches!(
@@ -1531,7 +1518,10 @@ pub enum ExprKind {
15311518
FormatArgs(P<FormatArgs>),
15321519

15331520
/// Placeholder for an expression that wasn't syntactically well formed in some way.
1534-
Err,
1521+
Err(ErrorGuaranteed),
1522+
1523+
/// Acts as a null expression. Lowering it will always emit a bug.
1524+
Dummy,
15351525
}
15361526

15371527
/// Used to differentiate between `for` loops and `for await` loops.

compiler/rustc_ast/src/mut_visit.rs

+2-2
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 => {}
1529+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err(_) | ExprKind::Dummy => {}
15301530
}
15311531
vis.visit_id(id);
15321532
vis.visit_span(span);
@@ -1642,7 +1642,7 @@ impl DummyAstNode for Expr {
16421642
fn dummy() -> Self {
16431643
Expr {
16441644
id: DUMMY_NODE_ID,
1645-
kind: ExprKind::Err,
1645+
kind: ExprKind::Dummy,
16461646
span: Default::default(),
16471647
attrs: Default::default(),
16481648
tokens: Default::default(),

compiler/rustc_ast/src/util/classify.rs

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

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 => {}
1066+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err(_) | ExprKind::Dummy => {}
10671067
}
10681068

10691069
visitor.visit_expr_post(expression)

compiler/rustc_ast_lowering/src/expr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_ast::*;
1414
use rustc_data_structures::stack::ensure_sufficient_stack;
1515
use rustc_hir as hir;
1616
use rustc_hir::def::{DefKind, Res};
17+
use rustc_middle::span_bug;
1718
use rustc_session::errors::report_lit_error;
1819
use rustc_span::source_map::{respan, Spanned};
1920
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -328,9 +329,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
328329
)
329330
}
330331
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
331-
ExprKind::Err => {
332-
hir::ExprKind::Err(self.dcx().span_delayed_bug(e.span, "lowered ExprKind::Err"))
332+
ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
333+
334+
ExprKind::Dummy => {
335+
span_bug!(e.span, "lowered ExprKind::Dummy")
333336
}
337+
334338
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
335339

336340
ExprKind::Paren(_) | ExprKind::ForLoop { .. } => {

compiler/rustc_ast_lowering/src/pat.rs

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

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,16 @@ 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()
900900
}
901+
ast::ExprKind::Dummy => {
902+
self.popen();
903+
self.word("/*DUMMY*/");
904+
self.pclose();
905+
}
901906
}
902907

903908
self.ann.post(self, AnnNode::Expr(expr));

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ impl<'cx, 'a> Context<'cx, 'a> {
303303
| ExprKind::Closure(_)
304304
| ExprKind::ConstBlock(_)
305305
| ExprKind::Continue(_)
306-
| ExprKind::Err
306+
| ExprKind::Dummy
307+
| ExprKind::Err(_)
307308
| ExprKind::Field(_, _)
308309
| ExprKind::ForLoop { .. }
309310
| ExprKind::FormatArgs(_)

compiler/rustc_builtin_macros/src/cfg.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ 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::{self, *};
11+
use rustc_expand::base::{DummyResult, ExtCtxt, MacEager, MacResult};
1212
use rustc_span::Span;
1313

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

2121
match parse_cfg(cx, sp, tts) {
@@ -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
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
// The compiler code necessary to support the compile_error! extension.
22

33
use rustc_ast::tokenstream::TokenStream;
4-
use rustc_expand::base::{self, *};
4+
use rustc_expand::base::{get_single_str_from_tts, DummyResult, ExtCtxt, MacResult};
55
use rustc_span::Span;
66

77
pub fn expand_compile_error<'cx>(
88
cx: &'cx mut ExtCtxt<'_>,
99
sp: Span,
1010
tts: TokenStream,
11-
) -> 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);
11+
) -> Box<dyn MacResult + 'cx> {
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
}

0 commit comments

Comments
 (0)