Skip to content

Commit e1bb455

Browse files
authored
Rollup merge of rust-lang#104554 - BoxyUwU:less_unchecked_pls, r=lcnr
Use `ErrorGuaranteed::unchecked_claim_error_was_emitted` less there are only like 3 or 4 call sites left after this but it wasnt obvious to me how to remove them
2 parents 266e957 + 45a09a4 commit e1bb455

File tree

23 files changed

+172
-125
lines changed

23 files changed

+172
-125
lines changed

compiler/rustc_borrowck/src/lib.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate tracing;
1919
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2020
use rustc_data_structures::graph::dominators::Dominators;
2121
use rustc_data_structures::vec_map::VecMap;
22-
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
22+
use rustc_errors::{Diagnostic, DiagnosticBuilder};
2323
use rustc_hir as hir;
2424
use rustc_hir::def_id::LocalDefId;
2525
use rustc_index::bit_set::ChunkedBitSet;
@@ -192,13 +192,13 @@ fn do_mir_borrowck<'tcx>(
192192
}
193193
}
194194

195-
let mut errors = error::BorrowckErrors::new();
195+
let mut errors = error::BorrowckErrors::new(infcx.tcx);
196196

197197
// Gather the upvars of a closure, if any.
198198
let tables = tcx.typeck_opt_const_arg(def);
199-
if let Some(ErrorGuaranteed { .. }) = tables.tainted_by_errors {
200-
infcx.set_tainted_by_errors();
201-
errors.set_tainted_by_errors();
199+
if let Some(e) = tables.tainted_by_errors {
200+
infcx.set_tainted_by_errors(e);
201+
errors.set_tainted_by_errors(e);
202202
}
203203
let upvars: Vec<_> = tables
204204
.closure_min_captures_flattened(def.did)
@@ -2260,6 +2260,7 @@ mod error {
22602260
use super::*;
22612261

22622262
pub struct BorrowckErrors<'tcx> {
2263+
tcx: TyCtxt<'tcx>,
22632264
/// This field keeps track of move errors that are to be reported for given move indices.
22642265
///
22652266
/// There are situations where many errors can be reported for a single move out (see #53807)
@@ -2282,28 +2283,33 @@ mod error {
22822283
tainted_by_errors: Option<ErrorGuaranteed>,
22832284
}
22842285

2285-
impl BorrowckErrors<'_> {
2286-
pub fn new() -> Self {
2286+
impl<'tcx> BorrowckErrors<'tcx> {
2287+
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
22872288
BorrowckErrors {
2289+
tcx,
22882290
buffered_move_errors: BTreeMap::new(),
22892291
buffered: Default::default(),
22902292
tainted_by_errors: None,
22912293
}
22922294
}
22932295

2294-
// FIXME(eddyb) this is a suboptimal API because `tainted_by_errors` is
2295-
// set before any emission actually happens (weakening the guarantee).
22962296
pub fn buffer_error(&mut self, t: DiagnosticBuilder<'_, ErrorGuaranteed>) {
2297-
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
2297+
if let None = self.tainted_by_errors {
2298+
self.tainted_by_errors = Some(
2299+
self.tcx
2300+
.sess
2301+
.delay_span_bug(t.span.clone(), "diagnostic buffered but not emitted"),
2302+
)
2303+
}
22982304
t.buffer(&mut self.buffered);
22992305
}
23002306

23012307
pub fn buffer_non_error_diag(&mut self, t: DiagnosticBuilder<'_, ()>) {
23022308
t.buffer(&mut self.buffered);
23032309
}
23042310

2305-
pub fn set_tainted_by_errors(&mut self) {
2306-
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
2311+
pub fn set_tainted_by_errors(&mut self, e: ErrorGuaranteed) {
2312+
self.tainted_by_errors = Some(e);
23072313
}
23082314
}
23092315

compiler/rustc_borrowck/src/nll.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
303303

304304
if !nll_errors.is_empty() {
305305
// Suppress unhelpful extra errors in `infer_opaque_types`.
306-
infcx.set_tainted_by_errors();
306+
infcx.set_tainted_by_errors(infcx.tcx.sess.delay_span_bug(
307+
body.span,
308+
"`compute_regions` tainted `infcx` with errors but did not emit any errors",
309+
));
307310
}
308311

309312
let remapped_opaque_tys = regioncx.infer_opaque_types(&infcx, opaque_type_values);

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
219219
instantiated_ty: OpaqueHiddenType<'tcx>,
220220
origin: OpaqueTyOrigin,
221221
) -> Ty<'tcx> {
222-
if self.is_tainted_by_errors() {
223-
return self.tcx.ty_error();
222+
if let Some(e) = self.tainted_by_errors() {
223+
return self.tcx.ty_error_with_guaranteed(e);
224224
}
225225

226226
let definition_ty = instantiated_ty

compiler/rustc_hir_analysis/src/astconv/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub trait AstConv<'tcx> {
115115
/// (e.g., resolve) that is translated into a ty-error. This is
116116
/// used to help suppress derived errors typeck might otherwise
117117
/// report.
118-
fn set_tainted_by_errors(&self);
118+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
119119

120120
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
121121
}
@@ -2620,8 +2620,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26202620
}
26212621
}
26222622
Res::Err => {
2623-
self.set_tainted_by_errors();
2624-
self.tcx().ty_error()
2623+
let e = self
2624+
.tcx()
2625+
.sess
2626+
.delay_span_bug(path.span, "path with `Res:Err` but no error emitted");
2627+
self.set_tainted_by_errors(e);
2628+
self.tcx().ty_error_with_guaranteed(e)
26252629
}
26262630
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
26272631
}

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
518518
ty
519519
}
520520

521-
fn set_tainted_by_errors(&self) {
521+
fn set_tainted_by_errors(&self, _: ErrorGuaranteed) {
522522
// There's no obvious place to track this, so just let it go.
523523
}
524524

compiler/rustc_hir_typeck/src/coercion.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,9 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15611561
// Mark that we've failed to coerce the types here to suppress
15621562
// any superfluous errors we might encounter while trying to
15631563
// emit or provide suggestions on how to fix the initial error.
1564-
fcx.set_tainted_by_errors();
1564+
fcx.set_tainted_by_errors(
1565+
fcx.tcx.sess.delay_span_bug(cause.span, "coercion error but no error emitted"),
1566+
);
15651567
let (expected, found) = if label_expression_as_expected {
15661568
// In the case where this is a "forced unit", like
15671569
// `break`, we want to call the `()` "expected"

compiler/rustc_hir_typeck/src/demand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
154154
Err(e) => e,
155155
};
156156

157-
self.set_tainted_by_errors();
157+
self.set_tainted_by_errors(self.tcx.sess.delay_span_bug(
158+
expr.span,
159+
"`TypeError` when attempting coercion but no error emitted",
160+
));
158161
let expr = expr.peel_drop_temps();
159162
let cause = self.misc(expr.span);
160163
let expr_ty = self.resolve_vars_with_obligations(checked_ty);

compiler/rustc_hir_typeck/src/expr.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
527527
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
528528
let ty = match res {
529529
Res::Err => {
530-
self.set_tainted_by_errors();
531-
tcx.ty_error()
530+
let e =
531+
self.tcx.sess.delay_span_bug(qpath.span(), "`Res::Err` but no error emitted");
532+
self.set_tainted_by_errors(e);
533+
tcx.ty_error_with_guaranteed(e)
532534
}
533535
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
534-
report_unexpected_variant_res(tcx, res, qpath, expr.span);
535-
tcx.ty_error()
536+
let e = report_unexpected_variant_res(tcx, res, qpath, expr.span);
537+
tcx.ty_error_with_guaranteed(e)
536538
}
537539
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
538540
};
@@ -1962,7 +1964,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19621964
expr_span: Span,
19631965
) {
19641966
if variant.is_recovered() {
1965-
self.set_tainted_by_errors();
1967+
self.set_tainted_by_errors(
1968+
self.tcx
1969+
.sess
1970+
.delay_span_bug(expr_span, "parser recovered but no error was emitted"),
1971+
);
19661972
return;
19671973
}
19681974
let mut err = self.err_ctxt().type_error_struct_with_diag(

compiler/rustc_hir_typeck/src/fallback.rs

+1-1
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 self.is_tainted_by_errors() => self.tcx.ty_error(),
107+
_ if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(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

+10-10
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140140
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
141141
self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);
142142

143-
if ty.references_error() {
144-
self.set_tainted_by_errors();
143+
if let Err(e) = ty.error_reported() {
144+
self.set_tainted_by_errors(e);
145145
}
146146
}
147147

@@ -528,7 +528,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
528528
pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
529529
match self.typeck_results.borrow().node_types().get(id) {
530530
Some(&t) => t,
531-
None if self.is_tainted_by_errors() => self.tcx.ty_error(),
531+
None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
532532
None => {
533533
bug!(
534534
"no type for node {}: {} in fcx {}",
@@ -543,7 +543,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
543543
pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
544544
match self.typeck_results.borrow().node_types().get(id) {
545545
Some(&t) => Some(t),
546-
None if self.is_tainted_by_errors() => Some(self.tcx.ty_error()),
546+
None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
547547
None => None,
548548
}
549549
}
@@ -1148,9 +1148,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11481148
explicit_late_bound = ExplicitLateBound::Yes;
11491149
}
11501150

1151-
if let Err(GenericArgCountMismatch { reported: Some(_), .. }) = arg_count.correct {
1151+
if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
11521152
infer_args_for_err.insert(index);
1153-
self.set_tainted_by_errors(); // See issue #53251.
1153+
self.set_tainted_by_errors(e); // See issue #53251.
11541154
}
11551155
}
11561156

@@ -1440,12 +1440,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14401440
if !ty.is_ty_var() {
14411441
ty
14421442
} else {
1443-
if !self.is_tainted_by_errors() {
1443+
let e = self.tainted_by_errors().unwrap_or_else(|| {
14441444
self.err_ctxt()
14451445
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
1446-
.emit();
1447-
}
1448-
let err = self.tcx.ty_error();
1446+
.emit()
1447+
});
1448+
let err = self.tcx.ty_error_with_guaranteed(e);
14491449
self.demand_suptype(sp, err, ty);
14501450
err
14511451
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
511511
tys.into_iter().any(|ty| ty.references_error() || ty.is_ty_var())
512512
}
513513

514-
self.set_tainted_by_errors();
515514
let tcx = self.tcx;
515+
// FIXME: taint after emitting errors and pass through an `ErrorGuaranteed`
516+
self.set_tainted_by_errors(
517+
tcx.sess.delay_span_bug(call_span, "no errors reported for args"),
518+
);
516519

517520
// Get the argument span in the context of the call span so that
518521
// suggestions and labels are (more) correct when an arg is a
@@ -1207,7 +1210,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12071210
let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, hir_id);
12081211
let variant = match def {
12091212
Res::Err => {
1210-
self.set_tainted_by_errors();
1213+
self.set_tainted_by_errors(
1214+
self.tcx.sess.delay_span_bug(path_span, "`Res::Err` but no error emitted"),
1215+
);
12111216
return None;
12121217
}
12131218
Res::Def(DefKind::Variant, _) => match ty.kind() {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod checks;
44
mod suggestions;
55

66
pub use _impl::*;
7+
use rustc_errors::ErrorGuaranteed;
78
pub use suggestions::*;
89

910
use crate::coercion::DynamicCoerceMany;
@@ -289,8 +290,8 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
289290
}
290291
}
291292

292-
fn set_tainted_by_errors(&self) {
293-
self.infcx.set_tainted_by_errors()
293+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
294+
self.infcx.set_tainted_by_errors(e)
294295
}
295296

296297
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, _span: Span) {

compiler/rustc_hir_typeck/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::check::check_fn;
5353
use crate::coercion::DynamicCoerceMany;
5454
use crate::gather_locals::GatherLocalsVisitor;
5555
use rustc_data_structures::unord::UnordSet;
56-
use rustc_errors::{struct_span_err, MultiSpan};
56+
use rustc_errors::{struct_span_err, ErrorGuaranteed, MultiSpan};
5757
use rustc_hir as hir;
5858
use rustc_hir::def::Res;
5959
use rustc_hir::intravisit::Visitor;
@@ -344,7 +344,7 @@ fn typeck_with_fallback<'tcx>(
344344

345345
fcx.select_all_obligations_or_error();
346346

347-
if !fcx.infcx.is_tainted_by_errors() {
347+
if let None = fcx.infcx.tainted_by_errors() {
348348
fcx.check_transmutes();
349349
}
350350

@@ -428,7 +428,12 @@ impl<'tcx> EnclosingBreakables<'tcx> {
428428
}
429429
}
430430

431-
fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'_>, span: Span) {
431+
fn report_unexpected_variant_res(
432+
tcx: TyCtxt<'_>,
433+
res: Res,
434+
qpath: &hir::QPath<'_>,
435+
span: Span,
436+
) -> ErrorGuaranteed {
432437
struct_span_err!(
433438
tcx.sess,
434439
span,
@@ -437,7 +442,7 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'
437442
res.descr(),
438443
rustc_hir_pretty::qpath_to_string(qpath),
439444
)
440-
.emit();
445+
.emit()
441446
}
442447

443448
/// Controls whether the arguments are tupled. This is used for the call

compiler/rustc_hir_typeck/src/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
133133
}
134134

135135
fn is_tainted_by_errors(&self) -> bool {
136-
self.infcx.is_tainted_by_errors()
136+
self.infcx.tainted_by_errors().is_some()
137137
}
138138

139139
fn resolve_type_vars_or_error(

0 commit comments

Comments
 (0)