Skip to content

Commit 7e1415e

Browse files
authored
Rollup merge of #93222 - mark-i-m:errorreported, r=oli-obk
Make ErrorReported impossible to construct outside `rustc_errors` 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. cc #69426 `@varkor` `@eddyb` `@estebank` I actually didn't see that I was assigned to this issue until now...
2 parents c8133f6 + bb8d430 commit 7e1415e

File tree

104 files changed

+704
-549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+704
-549
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
952952
sess.diagnostic().delay_span_bug(
953953
span,
954954
"unexpected delimiter in key-value attribute's value",
955-
)
955+
);
956956
}
957957
unwrap_single_token(sess, tokens, span)
958958
}

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'a> AstValidator<'a> {
440440
attr.span,
441441
"allow, cfg, cfg_attr, deny, \
442442
forbid, and warn are the only allowed built-in attributes in function parameters",
443-
)
443+
);
444444
}
445445
});
446446
}

compiler/rustc_ast_passes/src/feature_gate.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ impl<'a> PostExpansionVisitor<'a> {
252252
"wasm ABI is experimental and subject to change"
253253
);
254254
}
255-
abi => self
256-
.sess
257-
.parse_sess
258-
.span_diagnostic
259-
.delay_span_bug(span, &format!("unrecognized ABI not caught in lowering: {}", abi)),
255+
abi => {
256+
self.sess.parse_sess.span_diagnostic.delay_span_bug(
257+
span,
258+
&format!("unrecognized ABI not caught in lowering: {}", abi),
259+
);
260+
}
260261
}
261262
}
262263

compiler/rustc_borrowck/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn do_mir_borrowck<'a, 'tcx>(
178178

179179
// Gather the upvars of a closure, if any.
180180
let tables = tcx.typeck_opt_const_arg(def);
181-
if let Some(ErrorGuaranteed) = tables.tainted_by_errors {
181+
if let Some(ErrorGuaranteed { .. }) = tables.tainted_by_errors {
182182
infcx.set_tainted_by_errors();
183183
errors.set_tainted_by_errors();
184184
}
@@ -2274,6 +2274,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22742274
}
22752275

22762276
mod error {
2277+
use rustc_errors::ErrorGuaranteed;
2278+
22772279
use super::*;
22782280

22792281
pub struct BorrowckErrors<'tcx> {
@@ -2311,7 +2313,7 @@ mod error {
23112313
// FIXME(eddyb) this is a suboptimal API because `tainted_by_errors` is
23122314
// set before any emission actually happens (weakening the guarantee).
23132315
pub fn buffer_error(&mut self, t: DiagnosticBuilder<'_, ErrorGuaranteed>) {
2314-
self.tainted_by_errors = Some(ErrorGuaranteed {});
2316+
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
23152317
t.buffer(&mut self.buffered);
23162318
}
23172319

@@ -2320,7 +2322,7 @@ mod error {
23202322
}
23212323

23222324
pub fn set_tainted_by_errors(&mut self) {
2323-
self.tainted_by_errors = Some(ErrorGuaranteed {});
2325+
self.tainted_by_errors = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
23242326
}
23252327
}
23262328

compiler/rustc_codegen_cranelift/src/constant.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Handling of `static`s, `const`s and promoted allocations
22
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4-
use rustc_errors::ErrorGuaranteed;
54
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
65
use rustc_middle::mir::interpret::{
76
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
@@ -54,7 +53,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
5453
{
5554
all_constants_ok = false;
5655
match err {
57-
ErrorHandled::Reported(ErrorGuaranteed) | ErrorHandled::Linted => {
56+
ErrorHandled::Reported(_) | ErrorHandled::Linted => {
5857
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
5958
}
6059
ErrorHandled::TooGeneric => {

compiler/rustc_codegen_ssa/src/mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::traits::*;
2-
use rustc_errors::ErrorGuaranteed;
32
use rustc_middle::mir;
43
use rustc_middle::mir::interpret::ErrorHandled;
54
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
@@ -191,7 +190,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
191190
all_consts_ok = false;
192191
match err {
193192
// errored or at least linted
194-
ErrorHandled::Reported(ErrorGuaranteed) | ErrorHandled::Linted => {}
193+
ErrorHandled::Reported(_) | ErrorHandled::Linted => {}
195194
ErrorHandled::TooGeneric => {
196195
span_bug!(const_.span, "codgen encountered polymorphic constant: {:?}", err)
197196
}

compiler/rustc_const_eval/src/const_eval/machine.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_errors::ErrorGuaranteed;
21
use rustc_hir::def::DefKind;
32
use rustc_middle::mir;
43
use rustc_middle::ty::{self, Ty};
@@ -247,11 +246,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
247246
if ecx.tcx.is_ctfe_mir_available(def.did) {
248247
Ok(ecx.tcx.mir_for_ctfe_opt_const_arg(def))
249248
} else if ecx.tcx.def_kind(def.did) == DefKind::AssocConst {
250-
ecx.tcx.sess.delay_span_bug(
249+
let guar = ecx.tcx.sess.delay_span_bug(
251250
rustc_span::DUMMY_SP,
252251
"This is likely a const item that is missing from its impl",
253252
);
254-
throw_inval!(AlreadyReported(ErrorGuaranteed {}));
253+
throw_inval!(AlreadyReported(guar));
255254
} else {
256255
let path = ecx.tcx.def_path_str(def.did);
257256
Err(ConstEvalErrKind::NeedsRfc(format!("calling extern function `{}`", path))

compiler/rustc_const_eval/src/interpret/intern.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,11 @@ pub fn intern_const_alloc_recursive<
406406
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
407407
// Codegen does not like dangling pointers, and generally `tcx` assumes that
408408
// all allocations referenced anywhere actually exist. So, make sure we error here.
409-
ecx.tcx.sess.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
410-
return Err(ErrorGuaranteed);
409+
let reported = ecx
410+
.tcx
411+
.sess
412+
.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
413+
return Err(reported);
411414
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() {
412415
// We have hit an `AllocId` that is neither in local or global memory and isn't
413416
// marked as dangling by local memory. That should be impossible.

compiler/rustc_const_eval/src/interpret/operand.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
use std::convert::TryFrom;
55
use std::fmt::Write;
66

7-
use rustc_errors::ErrorGuaranteed;
87
use rustc_hir::def::Namespace;
98
use rustc_macros::HashStable;
109
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
1110
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
12-
use rustc_middle::ty::{ConstInt, Ty};
11+
use rustc_middle::ty::{ConstInt, DelaySpanBugEmitted, Ty};
1312
use rustc_middle::{mir, ty};
1413
use rustc_target::abi::{Abi, HasDataLayout, Size, TagEncoding};
1514
use rustc_target::abi::{VariantIdx, Variants};
@@ -565,7 +564,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
565564
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
566565
match val.val() {
567566
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
568-
ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorGuaranteed)),
567+
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
568+
throw_inval!(AlreadyReported(reported))
569+
}
569570
ty::ConstKind::Unevaluated(uv) => {
570571
let instance = self.resolve(uv.def, uv.substs)?;
571572
Ok(self.eval_to_allocation(GlobalId { instance, promoted: uv.promoted })?.into())

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
259259
self.tcx.sess.diagnostic().emit_diagnostic(&error);
260260
}
261261
} else {
262-
assert!(self.tcx.sess.has_errors());
262+
assert!(self.tcx.sess.has_errors().is_some());
263263
}
264264
}
265265

@@ -327,8 +327,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
327327

328328
match op.importance() {
329329
ops::DiagnosticImportance::Primary => {
330-
self.error_emitted = Some(ErrorGuaranteed);
331-
err.emit();
330+
let reported = err.emit();
331+
self.error_emitted = Some(reported);
332332
}
333333

334334
ops::DiagnosticImportance::Secondary => err.buffer(&mut self.secondary_errors),

compiler/rustc_driver/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn run_compiler(
235235
};
236236

237237
match make_input(config.opts.error_format, &matches.free) {
238-
Err(ErrorGuaranteed) => return Err(ErrorGuaranteed),
238+
Err(reported) => return Err(reported),
239239
Ok(Some((input, input_file_path))) => {
240240
config.input = input;
241241
config.input_path = input_file_path;
@@ -465,11 +465,11 @@ fn make_input(
465465
if io::stdin().read_to_string(&mut src).is_err() {
466466
// Immediately stop compilation if there was an issue reading
467467
// the input (for example if the input stream is not UTF-8).
468-
early_error_no_abort(
468+
let reported = early_error_no_abort(
469469
error_format,
470470
"couldn't read from stdin, as it did not contain valid UTF-8",
471471
);
472-
return Err(ErrorGuaranteed);
472+
return Err(reported);
473473
}
474474
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
475475
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
@@ -1128,7 +1128,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
11281128
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
11291129
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
11301130
if value.is::<rustc_errors::FatalErrorMarker>() {
1131-
ErrorGuaranteed
1131+
ErrorGuaranteed::unchecked_claim_error_was_emitted()
11321132
} else {
11331133
panic::resume_unwind(value);
11341134
}

compiler/rustc_errors/src/diagnostic_builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
128128
DiagnosticBuilderState::Emittable(handler) => {
129129
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
130130

131-
handler.emit_diagnostic(&db.inner.diagnostic);
131+
let guar = handler.emit_diagnostic(&db.inner.diagnostic);
132132

133133
// Only allow a guarantee if the `level` wasn't switched to a
134134
// non-error - the field isn't `pub`, but the whole `Diagnostic`
@@ -139,7 +139,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
139139
from `DiagnosticBuilder<ErrorGuaranteed>`",
140140
db.inner.diagnostic.level,
141141
);
142-
ErrorGuaranteed
142+
guar.unwrap()
143143
}
144144
// `.emit()` was previously called, disallowed from repeating it,
145145
// but can take advantage of the previous `.emit()`'s guarantee
@@ -154,7 +154,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
154154
became non-error ({:?}), after original `.emit()`",
155155
db.inner.diagnostic.level,
156156
);
157-
ErrorGuaranteed
157+
ErrorGuaranteed::unchecked_claim_error_was_emitted()
158158
}
159159
}
160160
}

0 commit comments

Comments
 (0)