Skip to content

Commit 19b8685

Browse files
authored
Rollup merge of #108379 - compiler-errors:hir-error-guaranteed, r=cjgillot
Add `ErrorGuaranteed` to `hir::{Expr,Ty}Kind::Err` variants First step in making the `Err` variants of `ExprKind` and `TyKind` require an `ErrorGuaranteed` during parsing. Making the corresponding AST versions require `ErrorGuaranteed` is a bit harder, whereas it was pretty easy to do this for HIR, so let's do that first. The only weird thing about this PR is that `ErrorGuaranteed` is moved to `rustc_span`. This is *certainly* not the right place to put it, but `rustc_hir` cannot depend on `rustc_error` because the latter already depends on the former. Should I just pull out some of the error machinery from `rustc_error` into an even more minimal crate that `rustc_hir` can depend on? Advice would be appreciated.
2 parents b27f37d + 0f4a7d1 commit 19b8685

File tree

27 files changed

+111
-88
lines changed

27 files changed

+111
-88
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
8888
let kind = hir::ExprKind::Box(self.lower_expr(&inner));
8989
return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
9090
} else {
91-
self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
92-
hir::ExprKind::Err
91+
let guar = self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
92+
hir::ExprKind::Err(guar)
9393
}
9494
} else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
9595
self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
@@ -266,8 +266,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
266266
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
267267
}
268268
ExprKind::Underscore => {
269-
self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
270-
hir::ExprKind::Err
269+
let guar = self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
270+
hir::ExprKind::Err(guar)
271271
}
272272
ExprKind::Path(qself, path) => {
273273
let qpath = self.lower_qpath(
@@ -299,8 +299,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
299299
let rest = match &se.rest {
300300
StructRest::Base(e) => Some(self.lower_expr(e)),
301301
StructRest::Rest(sp) => {
302-
self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
303-
Some(&*self.arena.alloc(self.expr_err(*sp)))
302+
let guar =
303+
self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
304+
Some(&*self.arena.alloc(self.expr_err(*sp, guar)))
304305
}
305306
StructRest::None => None,
306307
};
@@ -318,7 +319,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
318319
)
319320
}
320321
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
321-
ExprKind::Err => hir::ExprKind::Err,
322+
ExprKind::Err => hir::ExprKind::Err(
323+
self.tcx.sess.delay_span_bug(e.span, "lowered ExprKind::Err"),
324+
),
322325
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
323326

324327
ExprKind::Paren(_) | ExprKind::ForLoop(..) => unreachable!("already handled"),
@@ -761,7 +764,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
761764
self.expr_ident_mut(span, task_context_ident, task_context_hid)
762765
} else {
763766
// Use of `await` outside of an async context, we cannot use `task_context` here.
764-
self.expr_err(span)
767+
self.expr_err(span, self.tcx.sess.delay_span_bug(span, "no task_context hir id"))
765768
};
766769
let new_unchecked = self.expr_call_lang_item_fn_mut(
767770
span,

compiler/rustc_ast_lowering/src/format.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ fn make_count<'hir>(
102102
let value = ctx.arena.alloc_from_iter([ctx.expr_usize(sp, i)]);
103103
ctx.expr_call_mut(sp, count_param, value)
104104
} else {
105-
ctx.expr(sp, hir::ExprKind::Err)
105+
ctx.expr(
106+
sp,
107+
hir::ExprKind::Err(
108+
ctx.tcx.sess.delay_span_bug(sp, "lowered bad format_args count"),
109+
),
110+
)
106111
}
107112
}
108113
None => ctx.expr_lang_item_type_relative(sp, hir::LangItem::FormatCount, sym::Implied),
@@ -135,7 +140,10 @@ fn make_format_spec<'hir>(
135140
argmap.insert_full((arg_index, ArgumentType::Format(placeholder.format_trait)));
136141
ctx.expr_usize(sp, i)
137142
}
138-
Err(_) => ctx.expr(sp, hir::ExprKind::Err),
143+
Err(_) => ctx.expr(
144+
sp,
145+
hir::ExprKind::Err(ctx.tcx.sess.delay_span_bug(sp, "lowered bad format_args count")),
146+
),
139147
};
140148
let &FormatOptions {
141149
ref width,
@@ -294,7 +302,12 @@ fn expand_format_args<'hir>(
294302
));
295303
make_argument(ctx, sp, arg, ty)
296304
} else {
297-
ctx.expr(macsp, hir::ExprKind::Err)
305+
ctx.expr(
306+
macsp,
307+
hir::ExprKind::Err(
308+
ctx.tcx.sess.delay_span_bug(macsp, format!("no arg at {arg_index}")),
309+
),
310+
)
298311
}
299312
}));
300313
let elements: Vec<_> = arguments

compiler/rustc_ast_lowering/src/item.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::ptr::P;
77
use rustc_ast::visit::AssocCtxt;
88
use rustc_ast::*;
99
use rustc_data_structures::sorted_map::SortedMap;
10+
use rustc_errors::ErrorGuaranteed;
1011
use rustc_hir as hir;
1112
use rustc_hir::def::{DefKind, Res};
1213
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -284,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284285
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
285286
},
286287
ItemKind::GlobalAsm(asm) => hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)),
287-
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty: Some(ty), .. }) => {
288+
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => {
288289
// We lower
289290
//
290291
// type Foo = impl Trait
@@ -299,18 +300,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
299300
&generics,
300301
id,
301302
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
302-
|this| this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy),
303-
);
304-
hir::ItemKind::TyAlias(ty, generics)
305-
}
306-
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty: None, .. }) => {
307-
let mut generics = generics.clone();
308-
add_ty_alias_where_clause(&mut generics, *where_clauses, true);
309-
let (generics, ty) = self.lower_generics(
310-
&generics,
311-
id,
312-
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
313-
|this| this.arena.alloc(this.ty(span, hir::TyKind::Err)),
303+
|this| match ty {
304+
None => {
305+
let guar = this.tcx.sess.delay_span_bug(
306+
span,
307+
"expected to lower type alias type, but it was missing",
308+
);
309+
this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
310+
}
311+
Some(ty) => this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy),
312+
},
314313
);
315314
hir::ItemKind::TyAlias(ty, generics)
316315
}
@@ -798,8 +797,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
798797
}
799798

800799
/// Construct `ExprKind::Err` for the given `span`.
801-
pub(crate) fn expr_err(&mut self, span: Span) -> hir::Expr<'hir> {
802-
self.expr(span, hir::ExprKind::Err)
800+
pub(crate) fn expr_err(&mut self, span: Span, guar: ErrorGuaranteed) -> hir::Expr<'hir> {
801+
self.expr(span, hir::ExprKind::Err(guar))
803802
}
804803

805804
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
@@ -847,7 +846,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
847846
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
848847
|this| match ty {
849848
None => {
850-
let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err));
849+
let guar = this.tcx.sess.delay_span_bug(
850+
i.span,
851+
"expected to lower associated type, but it was missing",
852+
);
853+
let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err(guar)));
851854
hir::ImplItemKind::Type(ty)
852855
}
853856
Some(ty) => {
@@ -973,7 +976,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
973976
fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir> {
974977
match block {
975978
Some(block) => self.lower_block_expr(block),
976-
None => self.expr_err(span),
979+
None => self.expr_err(span, self.tcx.sess.delay_span_bug(span, "no block")),
977980
}
978981
}
979982

@@ -983,7 +986,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
983986
&[],
984987
match expr {
985988
Some(expr) => this.lower_expr_mut(expr),
986-
None => this.expr_err(span),
989+
None => this.expr_err(span, this.tcx.sess.delay_span_bug(span, "no block")),
987990
},
988991
)
989992
})

compiler/rustc_ast_lowering/src/lib.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10821082
hir::TypeBindingKind::Constraint { bounds }
10831083
}
10841084
DesugarKind::Error(position) => {
1085-
self.tcx.sess.emit_err(errors::MisplacedAssocTyBinding {
1085+
let guar = self.tcx.sess.emit_err(errors::MisplacedAssocTyBinding {
10861086
span: constraint.span,
10871087
position: DiagnosticArgFromDisplay(position),
10881088
});
1089-
let err_ty = &*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err));
1089+
let err_ty =
1090+
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
10901091
hir::TypeBindingKind::Equality { term: err_ty.into() }
10911092
}
10921093
}
@@ -1255,7 +1256,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12551256
fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> {
12561257
let kind = match &t.kind {
12571258
TyKind::Infer => hir::TyKind::Infer,
1258-
TyKind::Err => hir::TyKind::Err,
1259+
TyKind::Err => {
1260+
hir::TyKind::Err(self.tcx.sess.delay_span_bug(t.span, "TyKind::Err lowered"))
1261+
}
12591262
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
12601263
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
12611264
TyKind::Ref(region, mt) => {
@@ -1381,7 +1384,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13811384
path
13821385
}
13831386
ImplTraitContext::FeatureGated(position, feature) => {
1384-
self.tcx
1387+
let guar = self
1388+
.tcx
13851389
.sess
13861390
.create_feature_err(
13871391
MisplacedImplTrait {
@@ -1391,24 +1395,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13911395
*feature,
13921396
)
13931397
.emit();
1394-
hir::TyKind::Err
1398+
hir::TyKind::Err(guar)
13951399
}
13961400
ImplTraitContext::Disallowed(position) => {
1397-
self.tcx.sess.emit_err(MisplacedImplTrait {
1401+
let guar = self.tcx.sess.emit_err(MisplacedImplTrait {
13981402
span: t.span,
13991403
position: DiagnosticArgFromDisplay(position),
14001404
});
1401-
hir::TyKind::Err
1405+
hir::TyKind::Err(guar)
14021406
}
14031407
}
14041408
}
14051409
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
14061410
TyKind::CVarArgs => {
1407-
self.tcx.sess.delay_span_bug(
1411+
let guar = self.tcx.sess.delay_span_bug(
14081412
t.span,
14091413
"`TyKind::CVarArgs` should have been handled elsewhere",
14101414
);
1411-
hir::TyKind::Err
1415+
hir::TyKind::Err(guar)
14121416
}
14131417
};
14141418

compiler/rustc_ast_lowering/src/pat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
330330
ExprKind::Path(..) if allow_paths => {}
331331
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
332332
_ => {
333-
self.tcx.sess.emit_err(ArbitraryExpressionInPattern { span: expr.span });
334-
return self.arena.alloc(self.expr_err(expr.span));
333+
let guar = self.tcx.sess.emit_err(ArbitraryExpressionInPattern { span: expr.span });
334+
return self.arena.alloc(self.expr_err(expr.span, guar));
335335
}
336336
}
337337
self.lower_expr(expr)

compiler/rustc_errors/src/lib.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub use rustc_error_messages::{
4242
pub use rustc_lint_defs::{pluralize, Applicability};
4343
use rustc_macros::fluent_messages;
4444
use rustc_span::source_map::SourceMap;
45-
use rustc_span::HashStableContext;
45+
pub use rustc_span::ErrorGuaranteed;
4646
use rustc_span::{Loc, Span};
4747

4848
use std::borrow::Cow;
@@ -1846,17 +1846,3 @@ pub enum TerminalUrl {
18461846
Yes,
18471847
Auto,
18481848
}
1849-
1850-
/// Useful type to use with `Result<>` indicate that an error has already
1851-
/// been reported to the user, so no need to continue checking.
1852-
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
1853-
#[derive(HashStable_Generic)]
1854-
pub struct ErrorGuaranteed(());
1855-
1856-
impl ErrorGuaranteed {
1857-
/// To be used only if you really know what you are doing... ideally, we would find a way to
1858-
/// eliminate all calls to this method.
1859-
pub fn unchecked_claim_error_was_emitted() -> Self {
1860-
ErrorGuaranteed(())
1861-
}
1862-
}

compiler/rustc_hir/src/hir.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@ impl<'hir> GenericArgs<'hir> {
369369

370370
pub fn has_err(&self) -> bool {
371371
self.args.iter().any(|arg| match arg {
372-
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err),
372+
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err(_)),
373373
_ => false,
374374
}) || self.bindings.iter().any(|arg| match arg.kind {
375-
TypeBindingKind::Equality { term: Term::Ty(ty) } => matches!(ty.kind, TyKind::Err),
375+
TypeBindingKind::Equality { term: Term::Ty(ty) } => matches!(ty.kind, TyKind::Err(_)),
376376
_ => false,
377377
})
378378
}
@@ -1688,7 +1688,7 @@ impl Expr<'_> {
16881688
ExprKind::Struct(..) => ExprPrecedence::Struct,
16891689
ExprKind::Repeat(..) => ExprPrecedence::Repeat,
16901690
ExprKind::Yield(..) => ExprPrecedence::Yield,
1691-
ExprKind::Err => ExprPrecedence::Err,
1691+
ExprKind::Err(_) => ExprPrecedence::Err,
16921692
}
16931693
}
16941694

@@ -1754,7 +1754,7 @@ impl Expr<'_> {
17541754
| ExprKind::Yield(..)
17551755
| ExprKind::Cast(..)
17561756
| ExprKind::DropTemps(..)
1757-
| ExprKind::Err => false,
1757+
| ExprKind::Err(_) => false,
17581758
}
17591759
}
17601760

@@ -1840,7 +1840,7 @@ impl Expr<'_> {
18401840
| ExprKind::Binary(..)
18411841
| ExprKind::Yield(..)
18421842
| ExprKind::DropTemps(..)
1843-
| ExprKind::Err => true,
1843+
| ExprKind::Err(_) => true,
18441844
}
18451845
}
18461846

@@ -2013,7 +2013,7 @@ pub enum ExprKind<'hir> {
20132013
Yield(&'hir Expr<'hir>, YieldSource),
20142014

20152015
/// A placeholder for an expression that wasn't syntactically well formed in some way.
2016-
Err,
2016+
Err(rustc_span::ErrorGuaranteed),
20172017
}
20182018

20192019
/// Represents an optionally `Self`-qualified value/type path or associated extension.
@@ -2676,7 +2676,7 @@ pub enum TyKind<'hir> {
26762676
/// specified. This can appear anywhere in a type.
26772677
Infer,
26782678
/// Placeholder for a type that has failed to be defined.
2679-
Err,
2679+
Err(rustc_span::ErrorGuaranteed),
26802680
}
26812681

26822682
#[derive(Debug, HashStable_Generic)]

compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
790790
ExprKind::Yield(ref subexpression, _) => {
791791
visitor.visit_expr(subexpression);
792792
}
793-
ExprKind::Lit(_) | ExprKind::Err => {}
793+
ExprKind::Lit(_) | ExprKind::Err(_) => {}
794794
}
795795
}
796796

@@ -844,7 +844,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
844844
visitor.visit_lifetime(lifetime);
845845
}
846846
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
847-
TyKind::Infer | TyKind::Err => {}
847+
TyKind::Infer | TyKind::Err(_) => {}
848848
}
849849
}
850850

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3113,7 +3113,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
31133113
// handled specially and will not descend into this routine.
31143114
self.ty_infer(None, ast_ty.span)
31153115
}
3116-
hir::TyKind::Err => tcx.ty_error_misc(),
3116+
hir::TyKind::Err(guar) => tcx.ty_error(*guar),
31173117
};
31183118

31193119
self.record_ty(ast_ty.hir_id, result_ty, ast_ty.span);

compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'a> State<'a> {
358358
self.print_anon_const(e);
359359
self.word(")");
360360
}
361-
hir::TyKind::Err => {
361+
hir::TyKind::Err(_) => {
362362
self.popen();
363363
self.word("/*ERROR*/");
364364
self.pclose();
@@ -1559,7 +1559,7 @@ impl<'a> State<'a> {
15591559
self.word_space("yield");
15601560
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
15611561
}
1562-
hir::ExprKind::Err => {
1562+
hir::ExprKind::Err(_) => {
15631563
self.popen();
15641564
self.word("/*ERROR*/");
15651565
self.pclose();

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
354354
ExprKind::Field(base, field) => self.check_field(expr, &base, field, expected),
355355
ExprKind::Index(base, idx) => self.check_expr_index(base, idx, expr),
356356
ExprKind::Yield(value, ref src) => self.check_expr_yield(value, expr, src),
357-
hir::ExprKind::Err => tcx.ty_error_misc(),
357+
hir::ExprKind::Err(guar) => tcx.ty_error(guar),
358358
}
359359
}
360360

0 commit comments

Comments
 (0)