Skip to content

Commit 07c993e

Browse files
committed
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

+19-17
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

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

compiler/rustc_borrowck/src/type_check/mod.rs

+4-4
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

+19-20
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

+1-1
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

+1-1
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

+10-9
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

+1-1
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

0 commit comments

Comments
 (0)