Skip to content

Commit 1e7ef03

Browse files
Use ty_error_with_guaranteed in many more places
1 parent fdbc432 commit 1e7ef03

File tree

16 files changed

+309
-229
lines changed

16 files changed

+309
-229
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+17-15
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_with_guaranteed(guar);
159160
}
160161
// Pick a better span if there is one.
161162
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
@@ -254,13 +255,13 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
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_with_guaranteed(guar);
264265
}
265266

266267
// Only check this for TAIT. RPIT already supports `tests/ui/impl-trait/nested-return-type2.rs`
@@ -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

+3-2
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_with_guaranteed(guar),
279280
constraints: None,
280281
error_info: None,
281282
}

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -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_with_guaranteed(guar));
535535
}
536536
}
537537
place_ty = self.sanitize_projection(place_ty, elem, place, location, context);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -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()
@@ -1579,15 +1579,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15791579
false
15801580
});
15811581
if references_self {
1582-
tcx.sess
1582+
let guar = tcx
1583+
.sess
15831584
.delay_span_bug(span, "trait object projection bounds reference `Self`");
15841585
let substs: Vec<_> = b
15851586
.projection_ty
15861587
.substs
15871588
.iter()
15881589
.map(|arg| {
15891590
if arg.walk().any(|arg| arg == dummy_self.into()) {
1590-
return tcx.ty_error().into();
1591+
return tcx.ty_error_with_guaranteed(guar).into();
15911592
}
15921593
arg
15931594
})
@@ -3064,7 +3065,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30643065
let ty = self.ast_ty_to_ty_inner(qself, false, true);
30653066
self.associated_path_to_ty(ast_ty.hir_id, ast_ty.span, ty, qself, segment, false)
30663067
.map(|(ty, _, _)| ty)
3067-
.unwrap_or_else(|_| tcx.ty_error())
3068+
.unwrap_or_else(|guar| tcx.ty_error_with_guaranteed(guar))
30683069
}
30693070
&hir::TyKind::Path(hir::QPath::LangItem(lang_item, span, _)) => {
30703071
let def_id = tcx.require_lang_item(lang_item, Some(span));

compiler/rustc_hir_analysis/src/collect/type_of.rs

+11-8
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_with_guaranteed(guar)
324324
},
325325
_ => icx.to_ty(*self_ty),
326326
}
@@ -599,8 +599,11 @@ 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 = Some(ty::OpaqueHiddenType {
604+
span: DUMMY_SP,
605+
ty: self.tcx.ty_error_with_guaranteed(guar),
606+
});
604607
return;
605608
}
606609
let Some(&typeck_hidden_ty) = tables.concrete_opaque_types.get(&self.def_id) else {
@@ -618,8 +621,8 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
618621
debug!(?concrete_type, "found constraint");
619622
if let Some(prev) = &mut self.found {
620623
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();
624+
let guar = prev.report_mismatch(&concrete_type, self.tcx);
625+
prev.ty = self.tcx.ty_error_with_guaranteed(guar);
623626
}
624627
} else {
625628
self.found = Some(concrete_type);
@@ -814,11 +817,11 @@ fn find_opaque_ty_constraints_for_rpit(
814817

815818
concrete.map(|concrete| concrete.ty).unwrap_or_else(|| {
816819
let table = tcx.typeck(owner_def_id);
817-
if let Some(_) = table.tainted_by_errors {
820+
if let Some(guar) = table.tainted_by_errors {
818821
// Some error in the
819822
// owner fn prevented us from populating
820823
// the `concrete_opaque_types` table.
821-
tcx.ty_error()
824+
tcx.ty_error_with_guaranteed(guar)
822825
} else {
823826
table.concrete_opaque_types.get(&def_id).map(|ty| ty.ty).unwrap_or_else(|| {
824827
// We failed to resolve the opaque type or it

compiler/rustc_hir_typeck/src/closure.rs

+21-14
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_with_guaranteed(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

+9-5
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_with_guaranteed(guar), vec![]);
181181
}
182182

183183
// Coercing from `!` to any type is allowed:
@@ -997,7 +997,11 @@ 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() {
1001+
self.tcx.ty_error_with_guaranteed(guar)
1002+
} else {
1003+
target
1004+
})
10011005
}
10021006

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

14351439
// If we see any error types, just propagate that error
14361440
// upwards.
1437-
if expression_ty.references_error() || self.merged_ty().references_error() {
1438-
self.final_ty = Some(fcx.tcx.ty_error());
1441+
if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1442+
self.final_ty = Some(fcx.tcx.ty_error_with_guaranteed(guar));
14391443
return;
14401444
}
14411445

0 commit comments

Comments
 (0)