Skip to content

Commit

Permalink
rustc_error: make ErrorReported impossible to construct
Browse files Browse the repository at this point in the history
There are a few places were we have to construct it, though, and a few
places that are more invasive to change. To do this, we create a
constructor with a long obvious name.
  • Loading branch information
mark-i-m committed Mar 16, 2022
1 parent 461e807 commit bb8d430
Show file tree
Hide file tree
Showing 104 changed files with 704 additions and 549 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
sess.diagnostic().delay_span_bug(
span,
"unexpected delimiter in key-value attribute's value",
)
);
}
unwrap_single_token(sess, tokens, span)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl<'a> AstValidator<'a> {
attr.span,
"allow, cfg, cfg_attr, deny, \
forbid, and warn are the only allowed built-in attributes in function parameters",
)
);
}
});
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,12 @@ impl<'a> PostExpansionVisitor<'a> {
"wasm ABI is experimental and subject to change"
);
}
abi => self
.sess
.parse_sess
.span_diagnostic
.delay_span_bug(span, &format!("unrecognized ABI not caught in lowering: {}", abi)),
abi => {
self.sess.parse_sess.span_diagnostic.delay_span_bug(
span,
&format!("unrecognized ABI not caught in lowering: {}", abi),
);
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn do_mir_borrowck<'a, '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 {
if let Some(ErrorGuaranteed { .. }) = tables.tainted_by_errors {
infcx.set_tainted_by_errors();
errors.set_tainted_by_errors();
}
Expand Down Expand Up @@ -2274,6 +2274,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
}

mod error {
use rustc_errors::ErrorGuaranteed;

use super::*;

pub struct BorrowckErrors<'tcx> {
Expand Down Expand Up @@ -2311,7 +2313,7 @@ mod error {
// 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 {});
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
t.buffer(&mut self.buffered);
}

Expand All @@ -2320,7 +2322,7 @@ mod error {
}

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

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Handling of `static`s, `const`s and promoted allocations

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorGuaranteed;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::interpret::{
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
Expand Down Expand Up @@ -54,7 +53,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
{
all_constants_ok = false;
match err {
ErrorHandled::Reported(ErrorGuaranteed) | ErrorHandled::Linted => {
ErrorHandled::Reported(_) | ErrorHandled::Linted => {
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
}
ErrorHandled::TooGeneric => {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::traits::*;
use rustc_errors::ErrorGuaranteed;
use rustc_middle::mir;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
Expand Down Expand Up @@ -191,7 +190,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
all_consts_ok = false;
match err {
// errored or at least linted
ErrorHandled::Reported(ErrorGuaranteed) | ErrorHandled::Linted => {}
ErrorHandled::Reported(_) | ErrorHandled::Linted => {}
ErrorHandled::TooGeneric => {
span_bug!(const_.span, "codgen encountered polymorphic constant: {:?}", err)
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::DefKind;
use rustc_middle::mir;
use rustc_middle::ty::{self, Ty};
Expand Down Expand Up @@ -247,11 +246,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
if ecx.tcx.is_ctfe_mir_available(def.did) {
Ok(ecx.tcx.mir_for_ctfe_opt_const_arg(def))
} else if ecx.tcx.def_kind(def.did) == DefKind::AssocConst {
ecx.tcx.sess.delay_span_bug(
let guar = ecx.tcx.sess.delay_span_bug(
rustc_span::DUMMY_SP,
"This is likely a const item that is missing from its impl",
);
throw_inval!(AlreadyReported(ErrorGuaranteed {}));
throw_inval!(AlreadyReported(guar));
} else {
let path = ecx.tcx.def_path_str(def.did);
Err(ConstEvalErrKind::NeedsRfc(format!("calling extern function `{}`", path))
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,11 @@ pub fn intern_const_alloc_recursive<
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
// Codegen does not like dangling pointers, and generally `tcx` assumes that
// all allocations referenced anywhere actually exist. So, make sure we error here.
ecx.tcx.sess.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
return Err(ErrorGuaranteed);
let reported = ecx
.tcx
.sess
.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
return Err(reported);
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() {
// We have hit an `AllocId` that is neither in local or global memory and isn't
// marked as dangling by local memory. That should be impossible.
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
use std::convert::TryFrom;
use std::fmt::Write;

use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::Namespace;
use rustc_macros::HashStable;
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
use rustc_middle::ty::{ConstInt, Ty};
use rustc_middle::ty::{ConstInt, DelaySpanBugEmitted, Ty};
use rustc_middle::{mir, ty};
use rustc_target::abi::{Abi, HasDataLayout, Size, TagEncoding};
use rustc_target::abi::{VariantIdx, Variants};
Expand Down Expand Up @@ -565,7 +564,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
match val.val() {
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorGuaranteed)),
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
throw_inval!(AlreadyReported(reported))
}
ty::ConstKind::Unevaluated(uv) => {
let instance = self.resolve(uv.def, uv.substs)?;
Ok(self.eval_to_allocation(GlobalId { instance, promoted: uv.promoted })?.into())
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
self.tcx.sess.diagnostic().emit_diagnostic(&error);
}
} else {
assert!(self.tcx.sess.has_errors());
assert!(self.tcx.sess.has_errors().is_some());
}
}

Expand Down Expand Up @@ -327,8 +327,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {

match op.importance() {
ops::DiagnosticImportance::Primary => {
self.error_emitted = Some(ErrorGuaranteed);
err.emit();
let reported = err.emit();
self.error_emitted = Some(reported);
}

ops::DiagnosticImportance::Secondary => err.buffer(&mut self.secondary_errors),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fn run_compiler(
};

match make_input(config.opts.error_format, &matches.free) {
Err(ErrorGuaranteed) => return Err(ErrorGuaranteed),
Err(reported) => return Err(reported),
Ok(Some((input, input_file_path))) => {
config.input = input;
config.input_path = input_file_path;
Expand Down Expand Up @@ -465,11 +465,11 @@ fn make_input(
if io::stdin().read_to_string(&mut src).is_err() {
// Immediately stop compilation if there was an issue reading
// the input (for example if the input stream is not UTF-8).
early_error_no_abort(
let reported = early_error_no_abort(
error_format,
"couldn't read from stdin, as it did not contain valid UTF-8",
);
return Err(ErrorGuaranteed);
return Err(reported);
}
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
Expand Down Expand Up @@ -1128,7 +1128,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
if value.is::<rustc_errors::FatalErrorMarker>() {
ErrorGuaranteed
ErrorGuaranteed::unchecked_claim_error_was_emitted()
} else {
panic::resume_unwind(value);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
DiagnosticBuilderState::Emittable(handler) => {
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;

handler.emit_diagnostic(&db.inner.diagnostic);
let guar = handler.emit_diagnostic(&db.inner.diagnostic);

// Only allow a guarantee if the `level` wasn't switched to a
// non-error - the field isn't `pub`, but the whole `Diagnostic`
Expand All @@ -139,7 +139,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
from `DiagnosticBuilder<ErrorGuaranteed>`",
db.inner.diagnostic.level,
);
ErrorGuaranteed
guar.unwrap()
}
// `.emit()` was previously called, disallowed from repeating it,
// but can take advantage of the previous `.emit()`'s guarantee
Expand All @@ -154,7 +154,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
became non-error ({:?}), after original `.emit()`",
db.inner.diagnostic.level,
);
ErrorGuaranteed
ErrorGuaranteed::unchecked_claim_error_was_emitted()
}
}
}
Expand Down
Loading

0 comments on commit bb8d430

Please sign in to comment.