Skip to content

Commit 45cd0c6

Browse files
authored
Rollup merge of rust-lang#103928 - chenyukang:yukang/fix-103874-add-ty_error_with_guaranteed, r=lcnr
Add 'ty_error_with_guaranteed' and 'const_error_with_guaranteed' Part of rust-lang#103874
2 parents 04defb6 + 3320879 commit 45cd0c6

File tree

16 files changed

+71
-50
lines changed

16 files changed

+71
-50
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
299299
if errors.is_empty() {
300300
definition_ty
301301
} else {
302-
infcx.err_ctxt().report_fulfillment_errors(&errors, None);
303-
self.tcx.ty_error()
302+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None);
303+
self.tcx.ty_error_with_guaranteed(reported)
304304
}
305305
}
306306
}

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
247247
.and(type_op::normalize::Normalize::new(ty))
248248
.fully_perform(self.infcx)
249249
.unwrap_or_else(|_| {
250-
self.infcx
250+
let reported = self
251+
.infcx
251252
.tcx
252253
.sess
253254
.delay_span_bug(span, &format!("failed to normalize {:?}", ty));
254255
TypeOpOutput {
255-
output: self.infcx.tcx.ty_error(),
256+
output: self.infcx.tcx.ty_error_with_guaranteed(reported),
256257
constraints: None,
257258
error_info: None,
258259
}

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ pub(crate) fn type_check<'mir, 'tcx>(
233233
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type);
234234
trace!("finalized opaque type {:?} to {:#?}", opaque_type_key, hidden_type.ty.kind());
235235
if hidden_type.has_non_region_infer() {
236-
infcx.tcx.sess.delay_span_bug(
236+
let reported = infcx.tcx.sess.delay_span_bug(
237237
decl.hidden_type.span,
238238
&format!("could not resolve {:#?}", hidden_type.ty.kind()),
239239
);
240-
hidden_type.ty = infcx.tcx.ty_error();
240+
hidden_type.ty = infcx.tcx.ty_error_with_guaranteed(reported);
241241
}
242242

243243
(opaque_type_key, (hidden_type, decl.origin))

compiler/rustc_errors/src/diagnostic_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
482482
/// In the meantime, though, callsites are required to deal with the "bug"
483483
/// locally in whichever way makes the most sense.
484484
#[track_caller]
485-
pub fn delay_as_bug(&mut self) {
485+
pub fn delay_as_bug(&mut self) -> G {
486486
self.downgrade_to_delayed_bug();
487-
self.emit();
487+
self.emit()
488488
}
489489

490490
forward!(

compiler/rustc_hir_analysis/src/astconv/mod.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12011201
(_, _) => {
12021202
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
12031203
let expected = def_kind.descr(assoc_item_def_id);
1204-
tcx.sess
1204+
let reported = tcx
1205+
.sess
12051206
.struct_span_err(
12061207
binding.span,
12071208
&format!("expected {expected} bound, found {got}"),
@@ -1212,11 +1213,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12121213
)
12131214
.emit();
12141215
term = match def_kind {
1215-
hir::def::DefKind::AssocTy => tcx.ty_error().into(),
1216+
hir::def::DefKind::AssocTy => {
1217+
tcx.ty_error_with_guaranteed(reported).into()
1218+
}
12161219
hir::def::DefKind::AssocConst => tcx
1217-
.const_error(
1220+
.const_error_with_guaranteed(
12181221
tcx.bound_type_of(assoc_item_def_id)
12191222
.subst(tcx, projection_ty.skip_binder().substs),
1223+
reported,
12201224
)
12211225
.into(),
12221226
_ => unreachable!(),
@@ -1334,8 +1338,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13341338
.map(|&(trait_ref, _, _)| trait_ref.def_id())
13351339
.find(|&trait_ref| tcx.is_trait_alias(trait_ref))
13361340
.map(|trait_ref| tcx.def_span(trait_ref));
1337-
tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
1338-
return tcx.ty_error();
1341+
let reported =
1342+
tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
1343+
return tcx.ty_error_with_guaranteed(reported);
13391344
}
13401345

13411346
// Check that there are no gross object safety violations;
@@ -1345,14 +1350,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13451350
let object_safety_violations =
13461351
astconv_object_safety_violations(tcx, item.trait_ref().def_id());
13471352
if !object_safety_violations.is_empty() {
1348-
report_object_safety_error(
1353+
let reported = report_object_safety_error(
13491354
tcx,
13501355
span,
13511356
item.trait_ref().def_id(),
13521357
&object_safety_violations,
13531358
)
13541359
.emit();
1355-
return tcx.ty_error();
1360+
return tcx.ty_error_with_guaranteed(reported);
13561361
}
13571362
}
13581363

@@ -2112,13 +2117,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21122117
"Type"
21132118
};
21142119

2115-
self.report_ambiguous_associated_type(
2120+
let reported = self.report_ambiguous_associated_type(
21162121
span,
21172122
type_name,
21182123
&path_str,
21192124
item_segment.ident.name,
21202125
);
2121-
return tcx.ty_error();
2126+
return tcx.ty_error_with_guaranteed(reported)
21222127
};
21232128

21242129
debug!("qpath_to_ty: self_type={:?}", self_ty);
@@ -2560,8 +2565,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25602565
{
25612566
err.span_note(impl_.self_ty.span, "not a concrete type");
25622567
}
2563-
err.emit();
2564-
tcx.ty_error()
2568+
tcx.ty_error_with_guaranteed(err.emit())
25652569
} else {
25662570
self.normalize_ty(span, ty)
25672571
}

compiler/rustc_hir_analysis/src/check/compare_method.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,11 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
611611
collected_tys.insert(def_id, ty);
612612
}
613613
Err(err) => {
614-
tcx.sess.delay_span_bug(
614+
let reported = tcx.sess.delay_span_bug(
615615
return_span,
616616
format!("could not fully resolve: {ty} => {err:?}"),
617617
);
618-
collected_tys.insert(def_id, tcx.ty_error());
618+
collected_tys.insert(def_id, tcx.ty_error_with_guaranteed(reported));
619619
}
620620
}
621621
}

compiler/rustc_hir_analysis/src/collect.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
512512
}
513513
_ => {}
514514
}
515-
err.emit();
516-
self.tcx().ty_error()
515+
self.tcx().ty_error_with_guaranteed(err.emit())
517516
}
518517
}
519518

compiler/rustc_hir_analysis/src/collect/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
698698
}
699699

700700
let Some(hidden) = locator.found else {
701-
tcx.sess.emit_err(UnconstrainedOpaqueType {
701+
let reported = tcx.sess.emit_err(UnconstrainedOpaqueType {
702702
span: tcx.def_span(def_id),
703703
name: tcx.item_name(tcx.local_parent(def_id).to_def_id()),
704704
what: match tcx.hir().get(scope) {
@@ -708,7 +708,7 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T
708708
_ => "item",
709709
},
710710
});
711-
return tcx.ty_error();
711+
return tcx.ty_error_with_guaranteed(reported);
712712
};
713713

714714
// Only check against typeck if we didn't already error

compiler/rustc_hir_typeck/src/coercion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1639,9 +1639,9 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16391639
if visitor.ret_exprs.len() > 0 && let Some(expr) = expression {
16401640
self.note_unreachable_loop_return(&mut err, &expr, &visitor.ret_exprs);
16411641
}
1642-
err.emit_unless(unsized_return);
1642+
let reported = err.emit_unless(unsized_return);
16431643

1644-
self.final_ty = Some(fcx.tcx.ty_error());
1644+
self.final_ty = Some(fcx.tcx.ty_error_with_guaranteed(reported));
16451645
}
16461646
}
16471647
}

compiler/rustc_hir_typeck/src/expr.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8080
// coercions from ! to `expected`.
8181
if ty.is_never() {
8282
if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
83-
self.tcx().sess.delay_span_bug(
83+
let reported = self.tcx().sess.delay_span_bug(
8484
expr.span,
8585
"expression with never type wound up being adjusted",
8686
);
8787
return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
8888
target.to_owned()
8989
} else {
90-
self.tcx().ty_error()
90+
self.tcx().ty_error_with_guaranteed(reported)
9191
};
9292
}
9393

@@ -396,8 +396,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
396396
{
397397
err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
398398
}
399-
err.emit();
400-
oprnd_t = tcx.ty_error();
399+
oprnd_t = tcx.ty_error_with_guaranteed(err.emit());
401400
}
402401
}
403402
hir::UnOp::Not => {
@@ -1097,12 +1096,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10971096

10981097
// If the assignment expression itself is ill-formed, don't
10991098
// bother emitting another error
1100-
if lhs_ty.references_error() || rhs_ty.references_error() {
1101-
err.delay_as_bug()
1102-
} else {
1103-
err.emit();
1104-
}
1105-
return self.tcx.ty_error();
1099+
let reported = err.emit_unless(lhs_ty.references_error() || rhs_ty.references_error());
1100+
return self.tcx.ty_error_with_guaranteed(reported);
11061101
}
11071102

11081103
let lhs_ty = self.check_expr_with_needs(&lhs, Needs::MutPlace);
@@ -2777,8 +2772,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27772772
);
27782773
}
27792774
}
2780-
err.emit();
2781-
self.tcx.ty_error()
2775+
let reported = err.emit();
2776+
self.tcx.ty_error_with_guaranteed(reported)
27822777
}
27832778
}
27842779
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12121212
}
12131213
}
12141214
}
1215-
err.emit();
1216-
1217-
return (tcx.ty_error(), res);
1215+
let reported = err.emit();
1216+
return (tcx.ty_error_with_guaranteed(reported), res);
12181217
}
12191218
}
12201219
} else {

compiler/rustc_hir_typeck/src/op.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
529529
}
530530
}
531531
}
532-
err.emit();
533-
self.tcx.ty_error()
532+
let reported = err.emit();
533+
self.tcx.ty_error_with_guaranteed(reported)
534534
}
535535
};
536536

compiler/rustc_hir_typeck/src/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1278,12 +1278,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12781278
let element_tys = tcx.mk_type_list(element_tys_iter);
12791279
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
12801280
if let Some(mut err) = self.demand_eqtype_pat_diag(span, expected, pat_ty, ti) {
1281-
err.emit();
1281+
let reported = err.emit();
12821282
// Walk subpatterns with an expected type of `err` in this case to silence
12831283
// further errors being emitted when using the bindings. #50333
1284-
let element_tys_iter = (0..max_len).map(|_| tcx.ty_error());
1284+
let element_tys_iter = (0..max_len).map(|_| tcx.ty_error_with_guaranteed(reported));
12851285
for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
1286-
self.check_pat(elem, tcx.ty_error(), def_bm, ti);
1286+
self.check_pat(elem, tcx.ty_error_with_guaranteed(reported), def_bm, ti);
12871287
}
12881288
tcx.mk_tup(element_tys_iter)
12891289
} else {

compiler/rustc_hir_typeck/src/place_op.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9090
Applicability::MachineApplicable,
9191
);
9292
}
93-
err.emit();
94-
Some((self.tcx.ty_error(), self.tcx.ty_error()))
93+
let reported = err.emit();
94+
Some((
95+
self.tcx.ty_error_with_guaranteed(reported),
96+
self.tcx.ty_error_with_guaranteed(reported),
97+
))
9598
}
9699

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

compiler/rustc_middle/src/ty/context.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,12 @@ impl<'tcx> TyCtxt<'tcx> {
12831283
}
12841284
}
12851285

1286+
/// Constructs a `TyKind::Error` type with current `ErrorGuaranteed`
1287+
#[track_caller]
1288+
pub fn ty_error_with_guaranteed(self, reported: ErrorGuaranteed) -> Ty<'tcx> {
1289+
self.mk_ty(Error(reported))
1290+
}
1291+
12861292
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
12871293
#[track_caller]
12881294
pub fn ty_error(self) -> Ty<'tcx> {
@@ -1297,6 +1303,16 @@ impl<'tcx> TyCtxt<'tcx> {
12971303
self.mk_ty(Error(reported))
12981304
}
12991305

1306+
/// Like [TyCtxt::ty_error] but for constants, with current `ErrorGuaranteed`
1307+
#[track_caller]
1308+
pub fn const_error_with_guaranteed(
1309+
self,
1310+
ty: Ty<'tcx>,
1311+
reported: ErrorGuaranteed,
1312+
) -> Const<'tcx> {
1313+
self.mk_const(ty::ConstKind::Error(reported), ty)
1314+
}
1315+
13001316
/// Like [TyCtxt::ty_error] but for constants.
13011317
#[track_caller]
13021318
pub fn const_error(self, ty: Ty<'tcx>) -> Const<'tcx> {

compiler/rustc_middle/src/ty/visit.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
9797
}
9898
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
9999
if self.references_error() {
100-
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
100+
if let Some(reported) = ty::tls::with(|tcx| tcx.sess.has_errors()) {
101+
Err(reported)
102+
} else {
103+
bug!("expect tcx.sess.has_errors return true");
104+
}
101105
} else {
102106
Ok(())
103107
}

0 commit comments

Comments
 (0)