Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07c993e

Browse files
committedFeb 23, 2023
Auto merge of #108369 - compiler-errors:ty-error-more, r=BoxyUwU
Use `tcx.ty_error_with_guaranteed` in more places, rename variants 1. Use `ty_error_with_guaranteed` more so we don't delay so many span bugs 2. Rename `ty_error_with_guaranteed` to `ty_error`, `ty_error` to `ty_error_misc`. This is to incentivize using the former over the latter in cases where we already are witness to a `ErrorGuaranteed` token. Second commit is just name replacement, so the first commit can be reviewed on its own with more scrutiny.
2 parents eb909d8 + 298ae8c commit 07c993e

File tree

33 files changed

+374
-313
lines changed

33 files changed

+374
-313
lines changed
 

‎compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
22
use rustc_data_structures::vec_map::VecMap;
3+
use rustc_errors::ErrorGuaranteed;
34
use rustc_hir::def_id::LocalDefId;
45
use rustc_hir::OpaqueTyOrigin;
56
use rustc_infer::infer::TyCtxtInferExt as _;
@@ -149,13 +150,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
149150
// once we convert the generic parameters to those of the opaque type.
150151
if let Some(prev) = result.get_mut(&opaque_type_key.def_id) {
151152
if prev.ty != ty {
152-
if !ty.references_error() {
153+
let guar = ty.error_reported().err().unwrap_or_else(|| {
153154
prev.report_mismatch(
154155
&OpaqueHiddenType { ty, span: concrete_type.span },
155156
infcx.tcx,
156-
);
157-
}
158-
prev.ty = infcx.tcx.ty_error();
157+
)
158+
});
159+
prev.ty = infcx.tcx.ty_error(guar);
159160
}
160161
// Pick a better span if there is one.
161162
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
@@ -247,20 +248,20 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
247248
origin: OpaqueTyOrigin,
248249
) -> Ty<'tcx> {
249250
if let Some(e) = self.tainted_by_errors() {
250-
return self.tcx.ty_error_with_guaranteed(e);
251+
return self.tcx.ty_error(e);
251252
}
252253

253254
let definition_ty = instantiated_ty
254255
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false)
255256
.ty;
256257

257-
if !check_opaque_type_parameter_valid(
258+
if let Err(guar) = check_opaque_type_parameter_valid(
258259
self.tcx,
259260
opaque_type_key,
260261
origin,
261262
instantiated_ty.span,
262263
) {
263-
return self.tcx.ty_error();
264+
return self.tcx.ty_error(guar);
264265
}
265266

266267
// Only check this for TAIT. RPIT already supports `tests/ui/impl-trait/nested-return-type2.rs`
@@ -325,7 +326,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
325326
definition_ty
326327
} else {
327328
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
328-
self.tcx.ty_error_with_guaranteed(reported)
329+
self.tcx.ty_error(reported)
329330
}
330331
}
331332
}
@@ -335,7 +336,7 @@ fn check_opaque_type_parameter_valid(
335336
opaque_type_key: OpaqueTypeKey<'_>,
336337
origin: OpaqueTyOrigin,
337338
span: Span,
338-
) -> bool {
339+
) -> Result<(), ErrorGuaranteed> {
339340
match origin {
340341
// No need to check return position impl trait (RPIT)
341342
// because for type and const parameters they are correct
@@ -358,7 +359,7 @@ fn check_opaque_type_parameter_valid(
358359
// fn foo<l0..'ln>() -> foo::<'static..'static>::Foo<'l0..'lm>.
359360
//
360361
// which would error here on all of the `'static` args.
361-
OpaqueTyOrigin::FnReturn(..) | OpaqueTyOrigin::AsyncFn(..) => return true,
362+
OpaqueTyOrigin::FnReturn(..) | OpaqueTyOrigin::AsyncFn(..) => return Ok(()),
362363
// Check these
363364
OpaqueTyOrigin::TyAlias => {}
364365
}
@@ -379,13 +380,13 @@ fn check_opaque_type_parameter_valid(
379380
// Prevent `fn foo() -> Foo<u32>` from being defining.
380381
let opaque_param = opaque_generics.param_at(i, tcx);
381382
let kind = opaque_param.kind.descr();
382-
tcx.sess.emit_err(NonGenericOpaqueTypeParam {
383+
384+
return Err(tcx.sess.emit_err(NonGenericOpaqueTypeParam {
383385
ty: arg,
384386
kind,
385387
span,
386388
param_span: tcx.def_span(opaque_param.def_id),
387-
});
388-
return false;
389+
}));
389390
}
390391
}
391392

@@ -396,12 +397,13 @@ fn check_opaque_type_parameter_valid(
396397
.into_iter()
397398
.map(|i| tcx.def_span(opaque_generics.param_at(i, tcx).def_id))
398399
.collect();
399-
tcx.sess
400+
return Err(tcx
401+
.sess
400402
.struct_span_err(span, "non-defining opaque type use in defining scope")
401403
.span_note(spans, &format!("{} used multiple times", descr))
402-
.emit();
403-
return false;
404+
.emit());
404405
}
405406
}
406-
true
407+
408+
Ok(())
407409
}

‎compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
270270
.and(type_op::normalize::Normalize::new(ty))
271271
.fully_perform(self.infcx)
272272
.unwrap_or_else(|_| {
273-
self.infcx
273+
let guar = self
274+
.infcx
274275
.tcx
275276
.sess
276277
.delay_span_bug(span, &format!("failed to normalize {:?}", ty));
277278
TypeOpOutput {
278-
output: self.infcx.tcx.ty_error(),
279+
output: self.infcx.tcx.ty_error(guar),
279280
constraints: None,
280281
error_info: None,
281282
}

‎compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
239239
decl.hidden_type.span,
240240
&format!("could not resolve {:#?}", hidden_type.ty.kind()),
241241
);
242-
hidden_type.ty = infcx.tcx.ty_error_with_guaranteed(reported);
242+
hidden_type.ty = infcx.tcx.ty_error(reported);
243243
}
244244

245245
(opaque_type_key, (hidden_type, decl.origin))
@@ -529,9 +529,9 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
529529

530530
for elem in place.projection.iter() {
531531
if place_ty.variant_index.is_none() {
532-
if place_ty.ty.references_error() {
532+
if let Err(guar) = place_ty.ty.error_reported() {
533533
assert!(self.errors_reported);
534-
return PlaceTy::from_ty(self.tcx().ty_error());
534+
return PlaceTy::from_ty(self.tcx().ty_error(guar));
535535
}
536536
}
537537
place_ty = self.sanitize_projection(place_ty, elem, place, location, context);
@@ -763,7 +763,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
763763

764764
fn error(&mut self) -> Ty<'tcx> {
765765
self.errors_reported = true;
766-
self.tcx().ty_error()
766+
self.tcx().ty_error_misc()
767767
}
768768

769769
fn get_ambient_variance(&self, context: PlaceContext) -> ty::Variance {

‎compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
429429
}
430430
if let (hir::TyKind::Infer, false) = (&ty.kind, self.astconv.allow_ty_infer()) {
431431
self.inferred_params.push(ty.span);
432-
tcx.ty_error().into()
432+
tcx.ty_error_misc().into()
433433
} else {
434434
self.astconv.ast_ty_to_ty(ty).into()
435435
}
@@ -502,14 +502,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
502502
_ => false,
503503
}) {
504504
// Avoid ICE #86756 when type error recovery goes awry.
505-
return tcx.ty_error().into();
505+
return tcx.ty_error_misc().into();
506506
}
507507
tcx.at(self.span).type_of(param.def_id).subst(tcx, substs).into()
508508
} else if infer_args {
509509
self.astconv.ty_infer(Some(param), self.span).into()
510510
} else {
511511
// We've already errored above about the mismatch.
512-
tcx.ty_error().into()
512+
tcx.ty_error_misc().into()
513513
}
514514
}
515515
GenericParamDefKind::Const { has_default } => {
@@ -518,8 +518,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
518518
.type_of(param.def_id)
519519
.no_bound_vars()
520520
.expect("const parameter types cannot be generic");
521-
if ty.references_error() {
522-
return tcx.const_error(ty).into();
521+
if let Err(guar) = ty.error_reported() {
522+
return tcx.const_error_with_guaranteed(ty, guar).into();
523523
}
524524
if !infer_args && has_default {
525525
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
@@ -1239,9 +1239,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12391239
}
12401240
let reported = err.emit();
12411241
term = match def_kind {
1242-
hir::def::DefKind::AssocTy => {
1243-
tcx.ty_error_with_guaranteed(reported).into()
1244-
}
1242+
hir::def::DefKind::AssocTy => tcx.ty_error(reported).into(),
12451243
hir::def::DefKind::AssocConst => tcx
12461244
.const_error_with_guaranteed(
12471245
tcx.type_of(assoc_item_def_id)
@@ -1397,7 +1395,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13971395
.map(|trait_ref| tcx.def_span(trait_ref));
13981396
let reported =
13991397
tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
1400-
return tcx.ty_error_with_guaranteed(reported);
1398+
return tcx.ty_error(reported);
14011399
}
14021400

14031401
// Check that there are no gross object safety violations;
@@ -1414,7 +1412,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14141412
&object_safety_violations,
14151413
)
14161414
.emit();
1417-
return tcx.ty_error_with_guaranteed(reported);
1415+
return tcx.ty_error(reported);
14181416
}
14191417
}
14201418

@@ -1523,10 +1521,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15231521
if arg == dummy_self.into() {
15241522
let param = &generics.params[index];
15251523
missing_type_params.push(param.name);
1526-
return tcx.ty_error().into();
1524+
return tcx.ty_error_misc().into();
15271525
} else if arg.walk().any(|arg| arg == dummy_self.into()) {
15281526
references_self = true;
1529-
return tcx.ty_error().into();
1527+
return tcx.ty_error_misc().into();
15301528
}
15311529
arg
15321530
})
@@ -1579,15 +1577,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15791577
false
15801578
});
15811579
if references_self {
1582-
tcx.sess
1580+
let guar = tcx
1581+
.sess
15831582
.delay_span_bug(span, "trait object projection bounds reference `Self`");
15841583
let substs: Vec<_> = b
15851584
.projection_ty
15861585
.substs
15871586
.iter()
15881587
.map(|arg| {
15891588
if arg.walk().any(|arg| arg == dummy_self.into()) {
1590-
return tcx.ty_error().into();
1589+
return tcx.ty_error(guar).into();
15911590
}
15921591
arg
15931592
})
@@ -2473,7 +2472,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24732472
&[path_str],
24742473
item_segment.ident.name,
24752474
);
2476-
return tcx.ty_error_with_guaranteed(reported)
2475+
return tcx.ty_error(reported)
24772476
};
24782477

24792478
debug!("qpath_to_ty: self_type={:?}", self_ty);
@@ -2820,7 +2819,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28202819
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
28212820
tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id))
28222821
}
2823-
Some(rbv::ResolvedArg::Error(guar)) => tcx.ty_error_with_guaranteed(guar),
2822+
Some(rbv::ResolvedArg::Error(guar)) => tcx.ty_error(guar),
28242823
arg => bug!("unexpected bound var resolution for {hir_id:?}: {arg:?}"),
28252824
}
28262825
}
@@ -2932,7 +2931,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29322931
{
29332932
err.span_note(impl_.self_ty.span, "not a concrete type");
29342933
}
2935-
tcx.ty_error_with_guaranteed(err.emit())
2934+
tcx.ty_error(err.emit())
29362935
} else {
29372936
ty
29382937
}
@@ -2985,7 +2984,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29852984
.sess
29862985
.delay_span_bug(path.span, "path with `Res::Err` but no error emitted");
29872986
self.set_tainted_by_errors(e);
2988-
self.tcx().ty_error_with_guaranteed(e)
2987+
self.tcx().ty_error(e)
29892988
}
29902989
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
29912990
}
@@ -3064,7 +3063,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30643063
let ty = self.ast_ty_to_ty_inner(qself, false, true);
30653064
self.associated_path_to_ty(ast_ty.hir_id, ast_ty.span, ty, qself, segment, false)
30663065
.map(|(ty, _, _)| ty)
3067-
.unwrap_or_else(|_| tcx.ty_error())
3066+
.unwrap_or_else(|guar| tcx.ty_error(guar))
30683067
}
30693068
&hir::TyKind::Path(hir::QPath::LangItem(lang_item, span, _)) => {
30703069
let def_id = tcx.require_lang_item(lang_item, Some(span));
@@ -3112,7 +3111,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
31123111
// handled specially and will not descend into this routine.
31133112
self.ty_infer(None, ast_ty.span)
31143113
}
3115-
hir::TyKind::Err => tcx.ty_error(),
3114+
hir::TyKind::Err => tcx.ty_error_misc(),
31163115
};
31173116

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

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
790790
return_span,
791791
format!("could not fully resolve: {ty} => {err:?}"),
792792
);
793-
collected_tys.insert(def_id, tcx.ty_error_with_guaranteed(reported));
793+
collected_tys.insert(def_id, tcx.ty_error(reported));
794794
}
795795
}
796796
}

‎compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
499499
}
500500
_ => {}
501501
}
502-
self.tcx().ty_error_with_guaranteed(err.emit())
502+
self.tcx().ty_error(err.emit())
503503
}
504504
}
505505

‎compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
319319
ItemKind::Impl(hir::Impl { self_ty, .. }) => {
320320
match self_ty.find_self_aliases() {
321321
spans if spans.len() > 0 => {
322-
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
323-
tcx.ty_error()
322+
let guar = tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: () });
323+
tcx.ty_error(guar)
324324
},
325325
_ => icx.to_ty(*self_ty),
326326
}
@@ -599,8 +599,9 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
599599
// // constant does not contain interior mutability.
600600
// ```
601601
let tables = self.tcx.typeck(item_def_id);
602-
if let Some(_) = tables.tainted_by_errors {
603-
self.found = Some(ty::OpaqueHiddenType { span: DUMMY_SP, ty: self.tcx.ty_error() });
602+
if let Some(guar) = tables.tainted_by_errors {
603+
self.found =
604+
Some(ty::OpaqueHiddenType { span: DUMMY_SP, ty: self.tcx.ty_error(guar) });
604605
return;
605606
}
606607
let Some(&typeck_hidden_ty) = tables.concrete_opaque_types.get(&self.def_id) else {
@@ -618,8 +619,8 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
618619
debug!(?concrete_type, "found constraint");
619620
if let Some(prev) = &mut self.found {
620621
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
621-
prev.report_mismatch(&concrete_type, self.tcx);
622-
prev.ty = self.tcx.ty_error();
622+
let guar = prev.report_mismatch(&concrete_type, self.tcx);
623+
prev.ty = self.tcx.ty_error(guar);
623624
}
624625
} else {
625626
self.found = Some(concrete_type);
@@ -706,7 +707,7 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
706707
_ => "item",
707708
},
708709
});
709-
return tcx.ty_error_with_guaranteed(reported);
710+
return tcx.ty_error(reported);
710711
};
711712

712713
// Only check against typeck if we didn't already error
@@ -814,11 +815,11 @@ fn find_opaque_ty_constraints_for_rpit(
814815

815816
concrete.map(|concrete| concrete.ty).unwrap_or_else(|| {
816817
let table = tcx.typeck(owner_def_id);
817-
if let Some(_) = table.tainted_by_errors {
818+
if let Some(guar) = table.tainted_by_errors {
818819
// Some error in the
819820
// owner fn prevented us from populating
820821
// the `concrete_opaque_types` table.
821-
tcx.ty_error()
822+
tcx.ty_error(guar)
822823
} else {
823824
table.concrete_opaque_types.get(&def_id).map(|ty| ty.ty).unwrap_or_else(|| {
824825
// We failed to resolve the opaque type or it

‎compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438438

439439
let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs);
440440

441-
return self.tcx.ty_error_with_guaranteed(err);
441+
return self.tcx.ty_error(err);
442442
}
443443
};
444444

‎compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
44

55
use hir::def::DefKind;
6+
use rustc_errors::ErrorGuaranteed;
67
use rustc_hir as hir;
78
use rustc_hir::lang_items::LangItem;
89
use rustc_hir_analysis::astconv::AstConv;
@@ -488,17 +489,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
488489
};
489490
let expected_span =
490491
expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
491-
self.report_arg_count_mismatch(
492-
expected_span,
493-
closure_span,
494-
expected_args,
495-
found_args,
496-
true,
497-
closure_arg_span,
498-
)
499-
.emit();
500-
501-
let error_sig = self.error_sig_of_closure(decl);
492+
let guar = self
493+
.report_arg_count_mismatch(
494+
expected_span,
495+
closure_span,
496+
expected_args,
497+
found_args,
498+
true,
499+
closure_arg_span,
500+
)
501+
.emit();
502+
503+
let error_sig = self.error_sig_of_closure(decl, guar);
502504

503505
self.closure_sigs(expr_def_id, body, error_sig)
504506
}
@@ -792,13 +794,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
792794
/// Converts the types that the user supplied, in case that doing
793795
/// so should yield an error, but returns back a signature where
794796
/// all parameters are of type `TyErr`.
795-
fn error_sig_of_closure(&self, decl: &hir::FnDecl<'_>) -> ty::PolyFnSig<'tcx> {
797+
fn error_sig_of_closure(
798+
&self,
799+
decl: &hir::FnDecl<'_>,
800+
guar: ErrorGuaranteed,
801+
) -> ty::PolyFnSig<'tcx> {
796802
let astconv: &dyn AstConv<'_> = self;
803+
let err_ty = self.tcx.ty_error(guar);
797804

798805
let supplied_arguments = decl.inputs.iter().map(|a| {
799806
// Convert the types that the user supplied (if any), but ignore them.
800807
astconv.ast_ty_to_ty(a);
801-
self.tcx.ty_error()
808+
err_ty
802809
});
803810

804811
if let hir::FnRetTy::Return(ref output) = decl.output {
@@ -807,7 +814,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
807814

808815
let result = ty::Binder::dummy(self.tcx.mk_fn_sig(
809816
supplied_arguments,
810-
self.tcx.ty_error(),
817+
err_ty,
811818
decl.c_variadic,
812819
hir::Unsafety::Normal,
813820
Abi::RustCall,

‎compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
170170
debug!("Coerce.tys({:?} => {:?})", a, b);
171171

172172
// Just ignore error types.
173-
if a.references_error() || b.references_error() {
173+
if let Err(guar) = (a, b).error_reported() {
174174
// Best-effort try to unify these types -- we're already on the error path,
175175
// so this will have the side-effect of making sure we have no ambiguities
176176
// due to `[type error]` and `_` not coercing together.
177177
let _ = self.commit_if_ok(|_| {
178178
self.at(&self.cause, self.param_env).define_opaque_types(true).eq(a, b)
179179
});
180-
return success(vec![], self.fcx.tcx.ty_error(), vec![]);
180+
return success(vec![], self.fcx.tcx.ty_error(guar), vec![]);
181181
}
182182

183183
// Coercing from `!` to any type is allowed:
@@ -997,7 +997,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
997997

998998
let (adjustments, _) = self.register_infer_ok_obligations(ok);
999999
self.apply_adjustments(expr, adjustments);
1000-
Ok(if expr_ty.references_error() { self.tcx.ty_error() } else { target })
1000+
Ok(if let Err(guar) = expr_ty.error_reported() { self.tcx.ty_error(guar) } else { target })
10011001
}
10021002

10031003
/// Same as `try_coerce()`, but without side-effects.
@@ -1434,8 +1434,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14341434

14351435
// If we see any error types, just propagate that error
14361436
// upwards.
1437-
if expression_ty.references_error() || self.merged_ty().references_error() {
1438-
self.final_ty = Some(fcx.tcx.ty_error());
1437+
if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1438+
self.final_ty = Some(fcx.tcx.ty_error(guar));
14391439
return;
14401440
}
14411441

@@ -1620,7 +1620,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16201620

16211621
let reported = err.emit_unless(unsized_return);
16221622

1623-
self.final_ty = Some(fcx.tcx.ty_error_with_guaranteed(reported));
1623+
self.final_ty = Some(fcx.tcx.ty_error(reported));
16241624
}
16251625
}
16261626
}

‎compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8888
return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
8989
target.to_owned()
9090
} else {
91-
self.tcx().ty_error_with_guaranteed(reported)
91+
self.tcx().ty_error(reported)
9292
};
9393
}
9494

@@ -313,7 +313,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
313313
tcx.types.never
314314
} else {
315315
// There was an error; make type-check fail.
316-
tcx.ty_error()
316+
tcx.ty_error_misc()
317317
}
318318
}
319319
ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
@@ -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(),
357+
hir::ExprKind::Err => tcx.ty_error_misc(),
358358
}
359359
}
360360

@@ -402,7 +402,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
402402
{
403403
err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
404404
}
405-
oprnd_t = tcx.ty_error_with_guaranteed(err.emit());
405+
oprnd_t = tcx.ty_error(err.emit());
406406
}
407407
}
408408
hir::UnOp::Not => {
@@ -452,7 +452,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
452452

453453
let tm = ty::TypeAndMut { ty, mutbl };
454454
match kind {
455-
_ if tm.ty.references_error() => self.tcx.ty_error(),
455+
_ if tm.ty.references_error() => self.tcx.ty_error_misc(),
456456
hir::BorrowKind::Raw => {
457457
self.check_named_place_expr(oprnd);
458458
self.tcx.mk_ptr(tm)
@@ -531,11 +531,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
531531
let e =
532532
self.tcx.sess.delay_span_bug(qpath.span(), "`Res::Err` but no error emitted");
533533
self.set_tainted_by_errors(e);
534-
tcx.ty_error_with_guaranteed(e)
534+
tcx.ty_error(e)
535535
}
536536
Res::Def(DefKind::Variant, _) => {
537537
let e = report_unexpected_variant_res(tcx, res, qpath, expr.span, "E0533", "value");
538-
tcx.ty_error_with_guaranteed(e)
538+
tcx.ty_error(e)
539539
}
540540
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
541541
};
@@ -634,7 +634,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
634634
// If the loop context is not a `loop { }`, then break with
635635
// a value is illegal, and `opt_coerce_to` will be `None`.
636636
// Just set expectation to error in that case.
637-
let coerce_to = opt_coerce_to.unwrap_or_else(|| tcx.ty_error());
637+
let coerce_to = opt_coerce_to.unwrap_or_else(|| tcx.ty_error_misc());
638638

639639
// Recurse without `enclosing_breakables` borrowed.
640640
e_ty = self.check_expr_with_hint(e, coerce_to);
@@ -1033,7 +1033,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10331033
}
10341034

10351035
let result_ty = coerce.complete(self);
1036-
if cond_ty.references_error() { self.tcx.ty_error() } else { result_ty }
1036+
if let Err(guar) = cond_ty.error_reported() { self.tcx.ty_error(guar) } else { result_ty }
10371037
}
10381038

10391039
/// Type check assignment expression `expr` of form `lhs = rhs`.
@@ -1109,7 +1109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11091109
// If the assignment expression itself is ill-formed, don't
11101110
// bother emitting another error
11111111
let reported = err.emit_unless(lhs_ty.references_error() || rhs_ty.references_error());
1112-
return self.tcx.ty_error_with_guaranteed(reported);
1112+
return self.tcx.ty_error(reported);
11131113
}
11141114

11151115
let lhs_ty = self.check_expr_with_needs(&lhs, Needs::MutPlace);
@@ -1155,8 +1155,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11551155

11561156
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
11571157

1158-
if lhs_ty.references_error() || rhs_ty.references_error() {
1159-
self.tcx.ty_error()
1158+
if let Err(guar) = (lhs_ty, rhs_ty).error_reported() {
1159+
self.tcx.ty_error(guar)
11601160
} else {
11611161
self.tcx.mk_unit()
11621162
}
@@ -1274,8 +1274,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12741274
let t_expr = self.resolve_vars_if_possible(t_expr);
12751275

12761276
// Eagerly check for some obvious errors.
1277-
if t_expr.references_error() || t_cast.references_error() {
1278-
self.tcx.ty_error()
1277+
if let Err(guar) = (t_expr, t_cast).error_reported() {
1278+
self.tcx.ty_error(guar)
12791279
} else {
12801280
// Defer other checks until we're done type checking.
12811281
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
@@ -1296,7 +1296,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12961296
deferred_cast_checks.push(cast_check);
12971297
t_cast
12981298
}
1299-
Err(_) => self.tcx.ty_error(),
1299+
Err(guar) => self.tcx.ty_error(guar),
13001300
}
13011301
}
13021302
}
@@ -1423,8 +1423,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14231423
}
14241424
};
14251425

1426-
if element_ty.references_error() {
1427-
return tcx.ty_error();
1426+
if let Err(guar) = element_ty.error_reported() {
1427+
return tcx.ty_error(guar);
14281428
}
14291429

14301430
self.check_repeat_element_needs_copy_bound(element, count, element_ty);
@@ -1493,8 +1493,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14931493
_ => self.check_expr_with_expectation(&e, NoExpectation),
14941494
});
14951495
let tuple = self.tcx.mk_tup(elt_ts_iter);
1496-
if tuple.references_error() {
1497-
self.tcx.ty_error()
1496+
if let Err(guar) = tuple.error_reported() {
1497+
self.tcx.ty_error(guar)
14981498
} else {
14991499
self.require_type_is_sized(tuple, expr.span, traits::TupleInitializerSized);
15001500
tuple
@@ -1510,9 +1510,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15101510
base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>,
15111511
) -> Ty<'tcx> {
15121512
// Find the relevant variant
1513-
let Some((variant, adt_ty)) = self.check_struct_path(qpath, expr.hir_id) else {
1514-
self.check_struct_fields_on_error(fields, base_expr);
1515-
return self.tcx.ty_error();
1513+
let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) {
1514+
Ok(data) => data,
1515+
Err(guar) => {
1516+
self.check_struct_fields_on_error(fields, base_expr);
1517+
return self.tcx.ty_error(guar);
1518+
}
15161519
};
15171520

15181521
// Prohibit struct expressions when non-exhaustive flag is set.
@@ -1594,12 +1597,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15941597
self.field_ty(field.span, v_field, substs)
15951598
} else {
15961599
error_happened = true;
1597-
if let Some(prev_span) = seen_fields.get(&ident) {
1600+
let guar = if let Some(prev_span) = seen_fields.get(&ident) {
15981601
tcx.sess.emit_err(FieldMultiplySpecifiedInInitializer {
15991602
span: field.ident.span,
16001603
prev_span: *prev_span,
16011604
ident,
1602-
});
1605+
})
16031606
} else {
16041607
self.report_unknown_field(
16051608
adt_ty,
@@ -1608,10 +1611,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16081611
ast_fields,
16091612
adt.variant_descr(),
16101613
expr_span,
1611-
);
1612-
}
1614+
)
1615+
};
16131616

1614-
tcx.ty_error()
1617+
tcx.ty_error(guar)
16151618
};
16161619

16171620
// Make sure to give a type to the field even if there's
@@ -1994,14 +1997,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19941997
skip_fields: &[hir::ExprField<'_>],
19951998
kind_name: &str,
19961999
expr_span: Span,
1997-
) {
2000+
) -> ErrorGuaranteed {
19982001
if variant.is_recovered() {
1999-
self.set_tainted_by_errors(
2000-
self.tcx
2001-
.sess
2002-
.delay_span_bug(expr_span, "parser recovered but no error was emitted"),
2003-
);
2004-
return;
2002+
let guar = self
2003+
.tcx
2004+
.sess
2005+
.delay_span_bug(expr_span, "parser recovered but no error was emitted");
2006+
self.set_tainted_by_errors(guar);
2007+
return guar;
20052008
}
20062009
let mut err = self.err_ctxt().type_error_struct_with_diag(
20072010
field.ident.span,
@@ -2115,7 +2118,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21152118
};
21162119
}
21172120
}
2118-
err.emit();
2121+
err.emit()
21192122
}
21202123

21212124
// Return a hint about the closest match in field names
@@ -2256,21 +2259,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22562259
// (#90483) apply adjustments to avoid ExprUseVisitor from
22572260
// creating erroneous projection.
22582261
self.apply_adjustments(base, adjustments);
2259-
self.ban_private_field_access(expr, base_ty, field, did, expected.only_has_type(self));
2260-
return self.tcx().ty_error();
2262+
let guar = self.ban_private_field_access(
2263+
expr,
2264+
base_ty,
2265+
field,
2266+
did,
2267+
expected.only_has_type(self),
2268+
);
2269+
return self.tcx().ty_error(guar);
22612270
}
22622271

2263-
if field.name == kw::Empty {
2272+
let guar = if field.name == kw::Empty {
2273+
self.tcx.sess.delay_span_bug(field.span, "field name with no name")
22642274
} else if self.method_exists(
22652275
field,
22662276
base_ty,
22672277
expr.hir_id,
22682278
true,
22692279
expected.only_has_type(self),
22702280
) {
2271-
self.ban_take_value_of_method(expr, base_ty, field);
2281+
self.ban_take_value_of_method(expr, base_ty, field)
22722282
} else if !base_ty.is_primitive_ty() {
2273-
self.ban_nonexisting_field(field, base, expr, base_ty);
2283+
self.ban_nonexisting_field(field, base, expr, base_ty)
22742284
} else {
22752285
let field_name = field.to_string();
22762286
let mut err = type_error_struct!(
@@ -2339,10 +2349,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23392349
);
23402350
}
23412351
}
2342-
err.emit();
2343-
}
2352+
err.emit()
2353+
};
23442354

2345-
self.tcx().ty_error()
2355+
self.tcx().ty_error(guar)
23462356
}
23472357

23482358
fn suggest_await_on_field_access(
@@ -2388,7 +2398,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23882398
base: &'tcx hir::Expr<'tcx>,
23892399
expr: &'tcx hir::Expr<'tcx>,
23902400
base_ty: Ty<'tcx>,
2391-
) {
2401+
) -> ErrorGuaranteed {
23922402
debug!(
23932403
"ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
23942404
ident, base, expr, base_ty
@@ -2436,7 +2446,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24362446
HelpUseLatestEdition::new().add_to_diagnostic(&mut err);
24372447
}
24382448

2439-
err.emit();
2449+
err.emit()
24402450
}
24412451

24422452
fn ban_private_field_access(
@@ -2446,7 +2456,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24462456
field: Ident,
24472457
base_did: DefId,
24482458
return_ty: Option<Ty<'tcx>>,
2449-
) {
2459+
) -> ErrorGuaranteed {
24502460
let struct_path = self.tcx().def_path_str(base_did);
24512461
let kind_name = self.tcx().def_descr(base_did);
24522462
let mut err = struct_span_err!(
@@ -2469,10 +2479,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24692479
None,
24702480
);
24712481
}
2472-
err.emit();
2482+
err.emit()
24732483
}
24742484

2475-
fn ban_take_value_of_method(&self, expr: &hir::Expr<'tcx>, expr_t: Ty<'tcx>, field: Ident) {
2485+
fn ban_take_value_of_method(
2486+
&self,
2487+
expr: &hir::Expr<'tcx>,
2488+
expr_t: Ty<'tcx>,
2489+
field: Ident,
2490+
) -> ErrorGuaranteed {
24762491
let mut err = type_error_struct!(
24772492
self.tcx().sess,
24782493
field.span,
@@ -2544,7 +2559,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25442559
err.help("methods are immutable and cannot be assigned to");
25452560
}
25462561

2547-
err.emit();
2562+
err.emit()
25482563
}
25492564

25502565
fn point_at_param_definition(&self, err: &mut Diagnostic, param: ty::ParamTy) {
@@ -2829,7 +2844,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28292844
}
28302845
}
28312846
let reported = err.emit();
2832-
self.tcx.ty_error_with_guaranteed(reported)
2847+
self.tcx.ty_error(reported)
28332848
}
28342849
}
28352850
}

‎compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
104104
// type, `?T` is not considered unsolved, but `?I` is. The
105105
// same is true for float variables.)
106106
let fallback = match ty.kind() {
107-
_ if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
107+
_ if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error(e),
108108
ty::Infer(ty::IntVar(_)) => self.tcx.types.i32,
109109
ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
110110
_ => match diverging_fallback.get(&ty) {

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
451451
pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
452452
match self.typeck_results.borrow().node_types().get(id) {
453453
Some(&t) => t,
454-
None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
454+
None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error(e),
455455
None => {
456456
bug!(
457457
"no type for node {} in fcx {}",
@@ -465,7 +465,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
465465
pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
466466
match self.typeck_results.borrow().node_types().get(id) {
467467
Some(&t) => Some(t),
468-
None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
468+
None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error(e)),
469469
None => None,
470470
}
471471
}
@@ -701,7 +701,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
701701
}
702702

703703
pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
704-
vec![self.tcx.ty_error(); len]
704+
let ty_error = self.tcx.ty_error_misc();
705+
vec![ty_error; len]
705706
}
706707

707708
/// Unifies the output type with the expected type early, for more coercions
@@ -1161,7 +1162,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11611162
}
11621163
}
11631164
let reported = err.emit();
1164-
return (tcx.ty_error_with_guaranteed(reported), res);
1165+
return (tcx.ty_error(reported), res);
11651166
}
11661167
}
11671168
} else {
@@ -1417,7 +1418,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14171418
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
14181419
.emit()
14191420
});
1420-
let err = self.tcx.ty_error_with_guaranteed(e);
1421+
let err = self.tcx.ty_error(e);
14211422
self.demand_suptype(sp, err, ty);
14221423
err
14231424
}

‎compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use crate::{
1010
};
1111
use rustc_ast as ast;
1212
use rustc_data_structures::fx::FxIndexSet;
13-
use rustc_errors::{pluralize, Applicability, Diagnostic, DiagnosticId, MultiSpan};
13+
use rustc_errors::{
14+
pluralize, Applicability, Diagnostic, DiagnosticId, ErrorGuaranteed, MultiSpan,
15+
};
1416
use rustc_hir as hir;
1517
use rustc_hir::def::{CtorOf, DefKind, Res};
1618
use rustc_hir::def_id::DefId;
@@ -72,7 +74,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7274
let ty = self.typeck_results.borrow().expr_ty_adjusted(expr);
7375
let ty = self.resolve_vars_if_possible(ty);
7476
if ty.has_non_region_infer() {
75-
self.tcx.ty_error()
77+
self.tcx.ty_error_misc()
7678
} else {
7779
self.tcx.erase_regions(ty)
7880
}
@@ -113,7 +115,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
113115
tuple_arguments,
114116
method.ok().map(|method| method.def_id),
115117
);
116-
return self.tcx.ty_error();
118+
return self.tcx.ty_error_misc();
117119
}
118120

119121
let method = method.unwrap();
@@ -533,7 +535,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
533535
.typeck_results
534536
.borrow()
535537
.expr_ty_adjusted_opt(*expr)
536-
.unwrap_or_else(|| tcx.ty_error());
538+
.unwrap_or_else(|| tcx.ty_error_misc());
537539
(self.resolve_vars_if_possible(ty), normalize_span(expr.span))
538540
})
539541
.collect();
@@ -1286,23 +1288,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12861288
opt_ty.unwrap_or_else(|| self.next_float_var())
12871289
}
12881290
ast::LitKind::Bool(_) => tcx.types.bool,
1289-
ast::LitKind::Err => tcx.ty_error(),
1291+
ast::LitKind::Err => tcx.ty_error_misc(),
12901292
}
12911293
}
12921294

12931295
pub fn check_struct_path(
12941296
&self,
12951297
qpath: &QPath<'_>,
12961298
hir_id: hir::HirId,
1297-
) -> Option<(&'tcx ty::VariantDef, Ty<'tcx>)> {
1299+
) -> Result<(&'tcx ty::VariantDef, Ty<'tcx>), ErrorGuaranteed> {
12981300
let path_span = qpath.span();
12991301
let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, hir_id);
13001302
let variant = match def {
13011303
Res::Err => {
1302-
self.set_tainted_by_errors(
1303-
self.tcx.sess.delay_span_bug(path_span, "`Res::Err` but no error emitted"),
1304-
);
1305-
return None;
1304+
let guar =
1305+
self.tcx.sess.delay_span_bug(path_span, "`Res::Err` but no error emitted");
1306+
self.set_tainted_by_errors(guar);
1307+
return Err(guar);
13061308
}
13071309
Res::Def(DefKind::Variant, _) => match ty.normalized.ty_adt_def() {
13081310
Some(adt) => {
@@ -1330,28 +1332,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13301332
// Check bounds on type arguments used in the path.
13311333
self.add_required_obligations_for_hir(path_span, did, substs, hir_id);
13321334

1333-
Some((variant, ty.normalized))
1335+
Ok((variant, ty.normalized))
13341336
} else {
1335-
match ty.normalized.kind() {
1336-
ty::Error(_) => {
1337+
Err(match *ty.normalized.kind() {
1338+
ty::Error(guar) => {
13371339
// E0071 might be caused by a spelling error, which will have
13381340
// already caused an error message and probably a suggestion
13391341
// elsewhere. Refrain from emitting more unhelpful errors here
13401342
// (issue #88844).
1343+
guar
13411344
}
1342-
_ => {
1343-
struct_span_err!(
1344-
self.tcx.sess,
1345-
path_span,
1346-
E0071,
1347-
"expected struct, variant or union type, found {}",
1348-
ty.normalized.sort_string(self.tcx)
1349-
)
1350-
.span_label(path_span, "not a struct")
1351-
.emit();
1352-
}
1353-
}
1354-
None
1345+
_ => struct_span_err!(
1346+
self.tcx.sess,
1347+
path_span,
1348+
E0071,
1349+
"expected struct, variant or union type, found {}",
1350+
ty.normalized.sort_string(self.tcx)
1351+
)
1352+
.span_label(path_span, "not a struct")
1353+
.emit(),
1354+
})
13551355
}
13561356
}
13571357

@@ -1715,9 +1715,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17151715
pat: &'tcx hir::Pat<'tcx>,
17161716
ty: Ty<'tcx>,
17171717
) {
1718-
if ty.references_error() {
1718+
if let Err(guar) = ty.error_reported() {
17191719
// Override the types everywhere with `err()` to avoid knock on errors.
1720-
let err = self.tcx.ty_error();
1720+
let err = self.tcx.ty_error(guar);
17211721
self.write_ty(hir_id, err);
17221722
self.write_ty(pat.hir_id, err);
17231723
let local_ty = LocalTy { decl_ty: err, revealed_ty: err };
@@ -1746,7 +1746,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17461746
let result = self
17471747
.astconv()
17481748
.associated_path_to_ty(hir_id, path_span, ty.raw, qself, segment, true);
1749-
let ty = result.map(|(ty, _, _)| ty).unwrap_or_else(|_| self.tcx().ty_error());
1749+
let ty =
1750+
result.map(|(ty, _, _)| ty).unwrap_or_else(|guar| self.tcx().ty_error(guar));
17501751
let ty = self.handle_raw_ty(path_span, ty);
17511752
let result = result.map(|(_, kind, def_id)| (kind, def_id));
17521753

‎compiler/rustc_hir_typeck/src/op.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
297297
method.sig.output()
298298
}
299299
// error types are considered "builtin"
300-
Err(_) if lhs_ty.references_error() || rhs_ty.references_error() => self.tcx.ty_error(),
300+
Err(_) if lhs_ty.references_error() || rhs_ty.references_error() => {
301+
self.tcx.ty_error_misc()
302+
}
301303
Err(errors) => {
302304
let (_, trait_def_id) =
303305
lang_item_for_op(self.tcx, Op::Binary(op, is_assign), op.span);
@@ -518,7 +520,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
518520
}
519521
}
520522
let reported = err.emit();
521-
self.tcx.ty_error_with_guaranteed(reported)
523+
self.tcx.ty_error(reported)
522524
}
523525
};
524526

@@ -631,7 +633,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
631633
}
632634
Err(errors) => {
633635
let actual = self.resolve_vars_if_possible(operand_ty);
634-
if !actual.references_error() {
636+
let guar = actual.error_reported().err().unwrap_or_else(|| {
635637
let mut err = struct_span_err!(
636638
self.tcx.sess,
637639
ex.span,
@@ -701,9 +703,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
701703
}
702704
}
703705
}
704-
err.emit();
705-
}
706-
self.tcx.ty_error()
706+
err.emit()
707+
});
708+
self.tcx.ty_error(guar)
707709
}
708710
}
709711
}

‎compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 141 additions & 107 deletions
Large diffs are not rendered by default.

‎compiler/rustc_hir_typeck/src/place_op.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9191
);
9292
}
9393
let reported = err.emit();
94-
Some((
95-
self.tcx.ty_error_with_guaranteed(reported),
96-
self.tcx.ty_error_with_guaranteed(reported),
97-
))
94+
Some((self.tcx.ty_error(reported), self.tcx.ty_error(reported)))
9895
}
9996

10097
/// To type-check `base_expr[index_expr]`, we progressively autoderef

‎compiler/rustc_hir_typeck/src/writeback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> {
797797
debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t);
798798
let e = self.report_error(t);
799799
self.replaced_with_error = Some(e);
800-
self.interner().ty_error_with_guaranteed(e)
800+
self.interner().ty_error(e)
801801
}
802802
}
803803
}

‎compiler/rustc_infer/src/infer/sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
126126

127127
(&ty::Error(e), _) | (_, &ty::Error(e)) => {
128128
infcx.set_tainted_by_errors(e);
129-
Ok(self.tcx().ty_error_with_guaranteed(e))
129+
Ok(self.tcx().ty_error(e))
130130
}
131131

132132
(

‎compiler/rustc_middle/src/ty/_match.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
8989
Err(TypeError::Sorts(relate::expected_found(self, a, b)))
9090
}
9191

92-
(&ty::Error(guar), _) | (_, &ty::Error(guar)) => {
93-
Ok(self.tcx().ty_error_with_guaranteed(guar))
94-
}
92+
(&ty::Error(guar), _) | (_, &ty::Error(guar)) => Ok(self.tcx().ty_error(guar)),
9593

9694
_ => relate::super_relate_tys(self, a, b),
9795
}

‎compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,13 @@ impl<'tcx> TyCtxt<'tcx> {
718718

719719
/// Constructs a `TyKind::Error` type with current `ErrorGuaranteed`
720720
#[track_caller]
721-
pub fn ty_error_with_guaranteed(self, reported: ErrorGuaranteed) -> Ty<'tcx> {
721+
pub fn ty_error(self, reported: ErrorGuaranteed) -> Ty<'tcx> {
722722
self.mk_ty(Error(reported))
723723
}
724724

725725
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
726726
#[track_caller]
727-
pub fn ty_error(self) -> Ty<'tcx> {
727+
pub fn ty_error_misc(self) -> Ty<'tcx> {
728728
self.ty_error_with_message(DUMMY_SP, "TyKind::Error constructed but no error reported")
729729
}
730730

@@ -2458,7 +2458,7 @@ impl<'tcx> TyCtxt<'tcx> {
24582458
impl<'tcx> TyCtxtAt<'tcx> {
24592459
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
24602460
#[track_caller]
2461-
pub fn ty_error(self) -> Ty<'tcx> {
2461+
pub fn ty_error_misc(self) -> Ty<'tcx> {
24622462
self.tcx.ty_error_with_message(self.span, "TyKind::Error constructed but no error reported")
24632463
}
24642464

‎compiler/rustc_middle/src/ty/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl GenericParamDef {
101101
) -> ty::GenericArg<'tcx> {
102102
match &self.kind {
103103
ty::GenericParamDefKind::Lifetime => tcx.mk_re_error_misc().into(),
104-
ty::GenericParamDefKind::Type { .. } => tcx.ty_error().into(),
104+
ty::GenericParamDefKind::Type { .. } => tcx.ty_error_misc().into(),
105105
ty::GenericParamDefKind::Const { .. } => {
106106
tcx.const_error(tcx.type_of(self.def_id).subst(tcx, preceding_substs)).into()
107107
}

‎compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3535
use rustc_data_structures::intern::Interned;
3636
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3737
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
38+
use rustc_errors::ErrorGuaranteed;
3839
use rustc_hir as hir;
3940
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
4041
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
@@ -1358,7 +1359,7 @@ pub struct OpaqueHiddenType<'tcx> {
13581359
}
13591360

13601361
impl<'tcx> OpaqueHiddenType<'tcx> {
1361-
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) {
1362+
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) -> ErrorGuaranteed {
13621363
// Found different concrete types for the opaque type.
13631364
let sub_diag = if self.span == other.span {
13641365
TypeMismatchReason::ConflictType { span: self.span }
@@ -1370,7 +1371,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
13701371
other_ty: other.ty,
13711372
other_span: other.span,
13721373
sub: sub_diag,
1373-
});
1374+
})
13741375
}
13751376

13761377
#[instrument(level = "debug", skip(tcx), ret)]

‎compiler/rustc_middle/src/ty/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> {
186186
.emit();
187187
}
188188

189-
self.interner().ty_error()
189+
self.interner().ty_error_misc()
190190
}
191191
}
192192
}

‎compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
412412
bug!("bound types encountered in super_relate_tys")
413413
}
414414

415-
(&ty::Error(guar), _) | (_, &ty::Error(guar)) => Ok(tcx.ty_error_with_guaranteed(guar)),
415+
(&ty::Error(guar), _) | (_, &ty::Error(guar)) => Ok(tcx.ty_error(guar)),
416416

417417
(&ty::Never, _)
418418
| (&ty::Char, _)

‎compiler/rustc_middle/src/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Ty<'_> {
1616
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo<DepKind>]) -> Self {
1717
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
1818
// FIXME: Represent the above fact in the trait system somehow.
19-
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) }
19+
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error_misc()) }
2020
}
2121
}
2222

@@ -34,7 +34,7 @@ impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::SymbolName<'_> {
3434

3535
impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::Binder<'_, ty::FnSig<'_>> {
3636
fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo<DepKind>]) -> Self {
37-
let err = tcx.ty_error();
37+
let err = tcx.ty_error_misc();
3838

3939
let arity = if let Some(frame) = stack.get(0)
4040
&& frame.query.dep_kind == DepKind::fn_sig

‎compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ fn construct_error(
639639
let hir_id = tcx.hir().local_def_id_to_hir_id(def);
640640
let generator_kind = tcx.generator_kind(def);
641641

642-
let ty = tcx.ty_error();
642+
let ty = tcx.ty_error(err);
643643
let num_params = match body_owner_kind {
644644
hir::BodyOwnerKind::Fn => tcx.fn_sig(def).skip_binder().inputs().skip_binder().len(),
645645
hir::BodyOwnerKind::Closure => {

‎compiler/rustc_trait_selection/src/solve/project_goals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
316316
| ty::Never
317317
| ty::Foreign(..) => tcx.types.unit,
318318

319-
ty::Error(e) => tcx.ty_error_with_guaranteed(*e),
319+
ty::Error(e) => tcx.ty_error(*e),
320320

321321
ty::Str | ty::Slice(_) => tcx.types.usize,
322322

‎compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,7 +3534,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35343534
{
35353535
if let hir::Expr { kind: hir::ExprKind::Block(..), .. } = expr {
35363536
let expr = expr.peel_blocks();
3537-
let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error());
3537+
let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error_misc());
35383538
let span = expr.span;
35393539
if Some(span) != err.span.primary_span() {
35403540
err.span_label(
@@ -3637,7 +3637,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
36373637
let mut assocs = vec![];
36383638
let mut expr = expr;
36393639
let mut prev_ty = self.resolve_vars_if_possible(
3640-
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error()),
3640+
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error_misc()),
36413641
);
36423642
while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, span) = expr.kind {
36433643
// Point at every method call in the chain with the resulting type.
@@ -3648,7 +3648,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
36483648
self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
36493649
assocs.push(assocs_in_this_method);
36503650
prev_ty = self.resolve_vars_if_possible(
3651-
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error()),
3651+
typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error_misc()),
36523652
);
36533653

36543654
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
@@ -3666,7 +3666,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
36663666
if let hir::Node::Param(param) = parent {
36673667
// ...and it is a an fn argument.
36683668
let prev_ty = self.resolve_vars_if_possible(
3669-
typeck_results.node_type_opt(param.hir_id).unwrap_or(tcx.ty_error()),
3669+
typeck_results.node_type_opt(param.hir_id).unwrap_or(tcx.ty_error_misc()),
36703670
);
36713671
let assocs_in_this_method = self.probe_assoc_types_at_expr(&type_diffs, param.ty_span, prev_ty, param.hir_id, param_env);
36723672
if assocs_in_this_method.iter().any(|a| a.is_some()) {

‎compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,8 @@ struct Progress<'tcx> {
12131213
}
12141214

12151215
impl<'tcx> Progress<'tcx> {
1216-
fn error(tcx: TyCtxt<'tcx>) -> Self {
1217-
Progress { term: tcx.ty_error().into(), obligations: vec![] }
1216+
fn error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
1217+
Progress { term: tcx.ty_error(guar).into(), obligations: vec![] }
12181218
}
12191219

12201220
fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
@@ -1240,8 +1240,8 @@ fn project<'cx, 'tcx>(
12401240
)));
12411241
}
12421242

1243-
if obligation.predicate.references_error() {
1244-
return Ok(Projected::Progress(Progress::error(selcx.tcx())));
1243+
if let Err(guar) = obligation.predicate.error_reported() {
1244+
return Ok(Projected::Progress(Progress::error(selcx.tcx(), guar)));
12451245
}
12461246

12471247
let mut candidates = ProjectionCandidateSet::None;
@@ -2097,8 +2097,9 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20972097
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
20982098

20992099
let param_env = obligation.param_env;
2100-
let Ok(assoc_ty) = specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) else {
2101-
return Progress { term: tcx.ty_error().into(), obligations: nested };
2100+
let assoc_ty = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {
2101+
Ok(assoc_ty) => assoc_ty,
2102+
Err(guar) => return Progress::error(tcx, guar),
21022103
};
21032104

21042105
if !assoc_ty.item.defaultness(tcx).has_value() {
@@ -2110,7 +2111,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
21102111
"confirm_impl_candidate: no associated type {:?} for {:?}",
21112112
assoc_ty.item.name, obligation.predicate
21122113
);
2113-
return Progress { term: tcx.ty_error().into(), obligations: nested };
2114+
return Progress { term: tcx.ty_error_misc().into(), obligations: nested };
21142115
}
21152116
// If we're trying to normalize `<Vec<u32> as X>::A<S>` using
21162117
//`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
@@ -2194,11 +2195,12 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
21942195
let mut obligations = data.nested;
21952196

21962197
let trait_fn_def_id = tcx.impl_trait_in_trait_parent(obligation.predicate.def_id);
2197-
let Ok(leaf_def) = specialization_graph::assoc_def(tcx, data.impl_def_id, trait_fn_def_id) else {
2198-
return Progress { term: tcx.ty_error().into(), obligations };
2198+
let leaf_def = match specialization_graph::assoc_def(tcx, data.impl_def_id, trait_fn_def_id) {
2199+
Ok(assoc_ty) => assoc_ty,
2200+
Err(guar) => return Progress::error(tcx, guar),
21992201
};
22002202
if !leaf_def.item.defaultness(tcx).has_value() {
2201-
return Progress { term: tcx.ty_error().into(), obligations };
2203+
return Progress { term: tcx.ty_error_misc().into(), obligations };
22022204
}
22032205

22042206
// Use the default `impl Trait` for the trait, e.g., for a default trait body
@@ -2269,7 +2271,7 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
22692271
obligation.recursion_depth + 1,
22702272
tcx.bound_return_position_impl_trait_in_trait_tys(impl_fn_def_id)
22712273
.map_bound(|tys| {
2272-
tys.map_or_else(|_| tcx.ty_error(), |tys| tys[&obligation.predicate.def_id])
2274+
tys.map_or_else(|guar| tcx.ty_error(guar), |tys| tys[&obligation.predicate.def_id])
22732275
})
22742276
.subst(tcx, impl_fn_substs),
22752277
&mut obligations,

‎compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,15 +2443,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
24432443
// the placeholder trait ref may fail due the Generalizer relation
24442444
// raising a CyclicalTy error due to a sub_root_var relation
24452445
// for a variable being generalized...
2446-
self.infcx.tcx.sess.delay_span_bug(
2446+
let guar = self.infcx.tcx.sess.delay_span_bug(
24472447
obligation.cause.span,
24482448
&format!(
24492449
"Impl {:?} was matchable against {:?} but now is not",
24502450
impl_def_id, obligation
24512451
),
24522452
);
24532453
let value = self.infcx.fresh_substs_for_item(obligation.cause.span, impl_def_id);
2454-
let err = self.tcx().ty_error();
2454+
let err = self.tcx().ty_error(guar);
24552455
let value = value.fold_with(&mut BottomUpFolder {
24562456
tcx: self.tcx(),
24572457
ty_op: |_| err,

‎compiler/rustc_traits/src/chalk/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty<RustInterner<'tcx>> {
455455
interner.tcx.mk_alias_ty(assoc_ty.0, substitution.lower_into(interner)),
456456
),
457457
TyKind::Foreign(def_id) => ty::Foreign(def_id.0),
458-
TyKind::Error => return interner.tcx.ty_error(),
458+
TyKind::Error => return interner.tcx.ty_error_misc(),
459459
TyKind::Alias(alias_ty) => match alias_ty {
460460
chalk_ir::AliasTy::Projection(projection) => ty::Alias(
461461
ty::Projection,

‎compiler/rustc_ty_utils/src/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
9898
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
9999
if let Some(def_id) = def_id.as_local() {
100100
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
101-
return tcx.intern_type_list(&[tcx.ty_error()]);
101+
return tcx.intern_type_list(&[tcx.ty_error_misc()]);
102102
}
103103
}
104104
let def = tcx.adt_def(def_id);

0 commit comments

Comments
 (0)
Please sign in to comment.