Skip to content

Commit

Permalink
Rollup merge of #104554 - BoxyUwU:less_unchecked_pls, r=lcnr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dylan-DPC committed Nov 19, 2022
2 parents 45bfb1c + 45a09a4 commit 7f35493
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 125 deletions.
30 changes: 18 additions & 12 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate tracing;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::graph::dominators::Dominators;
use rustc_data_structures::vec_map::VecMap;
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{Diagnostic, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_index::bit_set::ChunkedBitSet;
Expand Down Expand Up @@ -192,13 +192,13 @@ fn do_mir_borrowck<'tcx>(
}
}

let mut errors = error::BorrowckErrors::new();
let mut errors = error::BorrowckErrors::new(infcx.tcx);

// Gather the upvars of a closure, if any.
let tables = tcx.typeck_opt_const_arg(def);
if let Some(ErrorGuaranteed { .. }) = tables.tainted_by_errors {
infcx.set_tainted_by_errors();
errors.set_tainted_by_errors();
if let Some(e) = tables.tainted_by_errors {
infcx.set_tainted_by_errors(e);
errors.set_tainted_by_errors(e);
}
let upvars: Vec<_> = tables
.closure_min_captures_flattened(def.did)
Expand Down Expand Up @@ -2260,6 +2260,7 @@ mod error {
use super::*;

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

impl BorrowckErrors<'_> {
pub fn new() -> Self {
impl<'tcx> BorrowckErrors<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
BorrowckErrors {
tcx,
buffered_move_errors: BTreeMap::new(),
buffered: Default::default(),
tainted_by_errors: None,
}
}

// FIXME(eddyb) this is a suboptimal API because `tainted_by_errors` is
// set before any emission actually happens (weakening the guarantee).
pub fn buffer_error(&mut self, t: DiagnosticBuilder<'_, ErrorGuaranteed>) {
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
if let None = self.tainted_by_errors {
self.tainted_by_errors = Some(
self.tcx
.sess
.delay_span_bug(t.span.clone(), "diagnostic buffered but not emitted"),
)
}
t.buffer(&mut self.buffered);
}

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

pub fn set_tainted_by_errors(&mut self) {
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
pub fn set_tainted_by_errors(&mut self, e: ErrorGuaranteed) {
self.tainted_by_errors = Some(e);
}
}

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ pub(crate) fn compute_regions<'cx, 'tcx>(

if !nll_errors.is_empty() {
// Suppress unhelpful extra errors in `infer_opaque_types`.
infcx.set_tainted_by_errors();
infcx.set_tainted_by_errors(infcx.tcx.sess.delay_span_bug(
body.span,
"`compute_regions` tainted `infcx` with errors but did not emit any errors",
));
}

let remapped_opaque_tys = regioncx.infer_opaque_types(&infcx, opaque_type_values);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
instantiated_ty: OpaqueHiddenType<'tcx>,
origin: OpaqueTyOrigin,
) -> Ty<'tcx> {
if self.is_tainted_by_errors() {
return self.tcx.ty_error();
if let Some(e) = self.tainted_by_errors() {
return self.tcx.ty_error_with_guaranteed(e);
}

let definition_ty = instantiated_ty
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub trait AstConv<'tcx> {
/// (e.g., resolve) that is translated into a ty-error. This is
/// used to help suppress derived errors typeck might otherwise
/// report.
fn set_tainted_by_errors(&self);
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);

fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
}
Expand Down Expand Up @@ -2620,8 +2620,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
}
Res::Err => {
self.set_tainted_by_errors();
self.tcx().ty_error()
let e = self
.tcx()
.sess
.delay_span_bug(path.span, "path with `Res:Err` but no error emitted");
self.set_tainted_by_errors(e);
self.tcx().ty_error_with_guaranteed(e)
}
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
ty
}

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

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,9 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// Mark that we've failed to coerce the types here to suppress
// any superfluous errors we might encounter while trying to
// emit or provide suggestions on how to fix the initial error.
fcx.set_tainted_by_errors();
fcx.set_tainted_by_errors(
fcx.tcx.sess.delay_span_bug(cause.span, "coercion error but no error emitted"),
);
let (expected, found) = if label_expression_as_expected {
// In the case where this is a "forced unit", like
// `break`, we want to call the `()` "expected"
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Err(e) => e,
};

self.set_tainted_by_errors();
self.set_tainted_by_errors(self.tcx.sess.delay_span_bug(
expr.span,
"`TypeError` when attempting coercion but no error emitted",
));
let expr = expr.peel_drop_temps();
let cause = self.misc(expr.span);
let expr_ty = self.resolve_vars_with_obligations(checked_ty);
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
let ty = match res {
Res::Err => {
self.set_tainted_by_errors();
tcx.ty_error()
let e =
self.tcx.sess.delay_span_bug(qpath.span(), "`Res::Err` but no error emitted");
self.set_tainted_by_errors(e);
tcx.ty_error_with_guaranteed(e)
}
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
report_unexpected_variant_res(tcx, res, qpath, expr.span);
tcx.ty_error()
let e = report_unexpected_variant_res(tcx, res, qpath, expr.span);
tcx.ty_error_with_guaranteed(e)
}
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
};
Expand Down Expand Up @@ -1962,7 +1964,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr_span: Span,
) {
if variant.is_recovered() {
self.set_tainted_by_errors();
self.set_tainted_by_errors(
self.tcx
.sess
.delay_span_bug(expr_span, "parser recovered but no error was emitted"),
);
return;
}
let mut err = self.err_ctxt().type_error_struct_with_diag(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
// type, `?T` is not considered unsolved, but `?I` is. The
// same is true for float variables.)
let fallback = match ty.kind() {
_ if self.is_tainted_by_errors() => self.tcx.ty_error(),
_ if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
ty::Infer(ty::IntVar(_)) => self.tcx.types.i32,
ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
_ => match diverging_fallback.get(&ty) {
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);

if ty.references_error() {
self.set_tainted_by_errors();
if let Err(e) = ty.error_reported() {
self.set_tainted_by_errors(e);
}
}

Expand Down Expand Up @@ -528,7 +528,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
match self.typeck_results.borrow().node_types().get(id) {
Some(&t) => t,
None if self.is_tainted_by_errors() => self.tcx.ty_error(),
None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
None => {
bug!(
"no type for node {}: {} in fcx {}",
Expand All @@ -543,7 +543,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
match self.typeck_results.borrow().node_types().get(id) {
Some(&t) => Some(t),
None if self.is_tainted_by_errors() => Some(self.tcx.ty_error()),
None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
None => None,
}
}
Expand Down Expand Up @@ -1148,9 +1148,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
explicit_late_bound = ExplicitLateBound::Yes;
}

if let Err(GenericArgCountMismatch { reported: Some(_), .. }) = arg_count.correct {
if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
infer_args_for_err.insert(index);
self.set_tainted_by_errors(); // See issue #53251.
self.set_tainted_by_errors(e); // See issue #53251.
}
}

Expand Down Expand Up @@ -1440,12 +1440,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !ty.is_ty_var() {
ty
} else {
if !self.is_tainted_by_errors() {
let e = self.tainted_by_errors().unwrap_or_else(|| {
self.err_ctxt()
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
.emit();
}
let err = self.tcx.ty_error();
.emit()
});
let err = self.tcx.ty_error_with_guaranteed(e);
self.demand_suptype(sp, err, ty);
err
}
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
tys.into_iter().any(|ty| ty.references_error() || ty.is_ty_var())
}

self.set_tainted_by_errors();
let tcx = self.tcx;
// FIXME: taint after emitting errors and pass through an `ErrorGuaranteed`
self.set_tainted_by_errors(
tcx.sess.delay_span_bug(call_span, "no errors reported for args"),
);

// Get the argument span in the context of the call span so that
// suggestions and labels are (more) correct when an arg is a
Expand Down Expand Up @@ -1207,7 +1210,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, hir_id);
let variant = match def {
Res::Err => {
self.set_tainted_by_errors();
self.set_tainted_by_errors(
self.tcx.sess.delay_span_bug(path_span, "`Res::Err` but no error emitted"),
);
return None;
}
Res::Def(DefKind::Variant, _) => match ty.kind() {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod checks;
mod suggestions;

pub use _impl::*;
use rustc_errors::ErrorGuaranteed;
pub use suggestions::*;

use crate::coercion::DynamicCoerceMany;
Expand Down Expand Up @@ -289,8 +290,8 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
}
}

fn set_tainted_by_errors(&self) {
self.infcx.set_tainted_by_errors()
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
self.infcx.set_tainted_by_errors(e)
}

fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, _span: Span) {
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use crate::check::check_fn;
use crate::coercion::DynamicCoerceMany;
use crate::gather_locals::GatherLocalsVisitor;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{struct_span_err, MultiSpan};
use rustc_errors::{struct_span_err, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::intravisit::Visitor;
Expand Down Expand Up @@ -344,7 +344,7 @@ fn typeck_with_fallback<'tcx>(

fcx.select_all_obligations_or_error();

if !fcx.infcx.is_tainted_by_errors() {
if let None = fcx.infcx.tainted_by_errors() {
fcx.check_transmutes();
}

Expand Down Expand Up @@ -428,7 +428,12 @@ impl<'tcx> EnclosingBreakables<'tcx> {
}
}

fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'_>, span: Span) {
fn report_unexpected_variant_res(
tcx: TyCtxt<'_>,
res: Res,
qpath: &hir::QPath<'_>,
span: Span,
) -> ErrorGuaranteed {
struct_span_err!(
tcx.sess,
span,
Expand All @@ -437,7 +442,7 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'
res.descr(),
rustc_hir_pretty::qpath_to_string(qpath),
)
.emit();
.emit()
}

/// Controls whether the arguments are tupled. This is used for the call
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}

fn is_tainted_by_errors(&self) -> bool {
self.infcx.is_tainted_by_errors()
self.infcx.tainted_by_errors().is_some()
}

fn resolve_type_vars_or_error(
Expand Down
Loading

0 comments on commit 7f35493

Please sign in to comment.