Skip to content

Commit 2ea7a37

Browse files
committed
Add DiagCtxt::delayed_bug.
We have `span_delayed_bug` and often pass it a `DUMMY_SP`. This commit adds `delayed_bug`, which matches pairs like `err`/`span_err` and `warn`/`span_warn`.
1 parent 3c4f1d8 commit 2ea7a37

File tree

27 files changed

+103
-146
lines changed

27 files changed

+103
-146
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::ty::TypeVisitor;
2727
use rustc_middle::ty::{self, RegionVid, Ty};
2828
use rustc_middle::ty::{Region, TyCtxt};
2929
use rustc_span::symbol::{kw, Ident};
30-
use rustc_span::{Span, DUMMY_SP};
30+
use rustc_span::Span;
3131

3232
use crate::borrowck_errors;
3333
use crate::session_diagnostics::{
@@ -84,7 +84,7 @@ impl<'tcx> RegionErrors<'tcx> {
8484
#[track_caller]
8585
pub fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
8686
let val = val.into();
87-
self.1.sess.dcx().span_delayed_bug(DUMMY_SP, format!("{val:?}"));
87+
self.1.sess.dcx().delayed_bug(format!("{val:?}"));
8888
self.0.push(val);
8989
}
9090
pub fn is_empty(&self) -> bool {

compiler/rustc_const_eval/src/const_eval/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
391391
if ecx.tcx.is_ctfe_mir_available(def) {
392392
Ok(ecx.tcx.mir_for_ctfe(def))
393393
} else if ecx.tcx.def_kind(def) == DefKind::AssocConst {
394-
let guar = ecx.tcx.dcx().span_delayed_bug(
395-
rustc_span::DUMMY_SP,
396-
"This is likely a const item that is missing from its impl",
397-
);
394+
let guar = ecx
395+
.tcx
396+
.dcx()
397+
.delayed_bug("This is likely a const item that is missing from its impl");
398398
throw_inval!(AlreadyReported(guar.into()));
399399
} else {
400400
// `find_mir_or_eval_fn` checks that this is a const fn before even calling us,

compiler/rustc_errors/src/lib.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,16 @@ impl DiagCtxt {
865865
/// For example, it can be used to create an [`ErrorGuaranteed`]
866866
/// (but you should prefer threading through the [`ErrorGuaranteed`] from an error emission
867867
/// directly).
868-
///
869-
/// If no span is available, use [`DUMMY_SP`].
870-
///
871-
/// [`DUMMY_SP`]: rustc_span::DUMMY_SP
868+
#[track_caller]
869+
pub fn delayed_bug(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
870+
let treat_next_err_as_bug = self.inner.borrow().treat_next_err_as_bug();
871+
if treat_next_err_as_bug {
872+
self.bug(msg);
873+
}
874+
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).emit()
875+
}
876+
877+
/// Like `delayed_bug`, but takes an additional span.
872878
///
873879
/// Note: this function used to be called `delay_span_bug`. It was renamed
874880
/// to match similar functions like `span_err`, `span_warn`, etc.
@@ -882,9 +888,7 @@ impl DiagCtxt {
882888
if treat_next_err_as_bug {
883889
self.span_bug(sp, msg);
884890
}
885-
let mut diagnostic = Diagnostic::new(DelayedBug, msg);
886-
diagnostic.span(sp);
887-
self.emit_diagnostic(diagnostic).unwrap()
891+
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).span_mv(sp).emit()
888892
}
889893

890894
// FIXME(eddyb) note the comment inside `impl Drop for DiagCtxtInner`, that's

compiler/rustc_hir_analysis/src/check/check.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,7 @@ pub(super) fn check_specialization_validity<'tcx>(
659659
if !tcx.is_impl_trait_in_trait(impl_item) {
660660
report_forbidden_specialization(tcx, impl_item, parent_impl);
661661
} else {
662-
tcx.dcx().span_delayed_bug(
663-
DUMMY_SP,
664-
format!("parent item: {parent_impl:?} not marked as default"),
665-
);
662+
tcx.dcx().delayed_bug(format!("parent item: {parent_impl:?} not marked as default"));
666663
}
667664
}
668665
}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::{
1919
self, GenericArgs, Ty, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
2020
};
2121
use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
22-
use rustc_span::{Span, DUMMY_SP};
22+
use rustc_span::Span;
2323
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2424
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
2525
use rustc_trait_selection::traits::{
@@ -942,9 +942,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
942942
.note_mv(format!("hidden type inferred to be `{}`", self.ty))
943943
.emit()
944944
}
945-
_ => {
946-
self.tcx.dcx().span_delayed_bug(DUMMY_SP, "should've been able to remap region")
947-
}
945+
_ => self.tcx.dcx().delayed_bug("should've been able to remap region"),
948946
};
949947
return Err(guar);
950948
};
@@ -1303,8 +1301,7 @@ fn compare_number_of_generics<'tcx>(
13031301
// inheriting the generics from will also have mismatched arguments, and
13041302
// we'll report an error for that instead. Delay a bug for safety, though.
13051303
if trait_.is_impl_trait_in_trait() {
1306-
return Err(tcx.dcx().span_delayed_bug(
1307-
rustc_span::DUMMY_SP,
1304+
return Err(tcx.dcx().delayed_bug(
13081305
"errors comparing numbers of generics of trait/impl functions were not emitted",
13091306
));
13101307
}

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::traits::{ObligationCause, Reveal};
77
use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
99
};
10-
use rustc_span::{Span, DUMMY_SP};
10+
use rustc_span::Span;
1111
use rustc_trait_selection::traits::{
1212
elaborate, normalize_param_env_or_error, outlives_bounds::InferCtxtExt, ObligationCtxt,
1313
};
@@ -153,10 +153,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
153153
trait_m_sig.inputs_and_output,
154154
));
155155
if !ocx.select_all_or_error().is_empty() {
156-
tcx.dcx().span_delayed_bug(
157-
DUMMY_SP,
158-
"encountered errors when checking RPITIT refinement (selection)",
159-
);
156+
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
160157
return;
161158
}
162159
let outlives_env = OutlivesEnvironment::with_bounds(
@@ -165,18 +162,12 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
165162
);
166163
let errors = infcx.resolve_regions(&outlives_env);
167164
if !errors.is_empty() {
168-
tcx.dcx().span_delayed_bug(
169-
DUMMY_SP,
170-
"encountered errors when checking RPITIT refinement (regions)",
171-
);
165+
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)");
172166
return;
173167
}
174168
// Resolve any lifetime variables that may have been introduced during normalization.
175169
let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else {
176-
tcx.dcx().span_delayed_bug(
177-
DUMMY_SP,
178-
"encountered errors when checking RPITIT refinement (resolution)",
179-
);
170+
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (resolution)");
180171
return;
181172
};
182173

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir as hir;
44
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
55
use rustc_session::lint;
66
use rustc_span::def_id::LocalDefId;
7-
use rustc_span::{Symbol, DUMMY_SP};
7+
use rustc_span::Symbol;
88
use rustc_target::abi::FieldIdx;
99
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
1010

@@ -294,7 +294,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
294294
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
295295
let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
296296
let Some(asm_arch) = self.tcx.sess.asm_arch else {
297-
self.tcx.dcx().span_delayed_bug(DUMMY_SP, "target architecture does not support asm");
297+
self.tcx.dcx().delayed_bug("target architecture does not support asm");
298298
return;
299299
};
300300
for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
2222
use rustc_session::lint;
2323
use rustc_span::def_id::DefId;
2424
use rustc_span::symbol::{sym, Ident};
25-
use rustc_span::{Span, DUMMY_SP};
25+
use rustc_span::Span;
2626
use std::fmt;
2727

2828
use crate::errors;
@@ -335,13 +335,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
335335
// though this may happen when we call `poly_trait_ref_binder_info` with
336336
// an (erroneous, #113423) associated return type bound in an impl header.
337337
if !supertrait_bound_vars.is_empty() {
338-
self.tcx.dcx().span_delayed_bug(
339-
DUMMY_SP,
340-
format!(
341-
"found supertrait lifetimes without a binder to append \
338+
self.tcx.dcx().delayed_bug(format!(
339+
"found supertrait lifetimes without a binder to append \
342340
them to: {supertrait_bound_vars:?}"
343-
),
344-
);
341+
));
345342
}
346343
break (vec![], BinderScopeType::Normal);
347344
}

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ impl CanonicalizeMode for CanonicalizeQueryResponse {
192192
// rust-lang/rust#57464: `impl Trait` can leak local
193193
// scopes (in manner violating typeck). Therefore, use
194194
// `span_delayed_bug` to allow type error over an ICE.
195-
canonicalizer.tcx.dcx().span_delayed_bug(
196-
rustc_span::DUMMY_SP,
197-
format!("unexpected region in query response: `{r:?}`"),
198-
);
195+
canonicalizer
196+
.tcx
197+
.dcx()
198+
.delayed_bug(format!("unexpected region in query response: `{r:?}`"));
199199
r
200200
}
201201
}

compiler/rustc_infer/src/infer/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtx
3838
use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
3939
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef};
4040
use rustc_span::symbol::Symbol;
41-
use rustc_span::{Span, DUMMY_SP};
41+
use rustc_span::Span;
4242

4343
use std::cell::{Cell, RefCell};
4444
use std::fmt;
@@ -1434,10 +1434,8 @@ impl<'tcx> InferCtxt<'tcx> {
14341434
bug!("`{value:?}` is not fully resolved");
14351435
}
14361436
if value.has_infer_regions() {
1437-
let guar = self
1438-
.tcx
1439-
.dcx()
1440-
.span_delayed_bug(DUMMY_SP, format!("`{value:?}` is not fully resolved"));
1437+
let guar =
1438+
self.tcx.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
14411439
Ok(self.tcx.fold_regions(value, |re, _| {
14421440
if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }
14431441
}))

compiler/rustc_infer/src/infer/opaque_types/table.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_data_structures::undo_log::UndoLogs;
22
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
3-
use rustc_span::DUMMY_SP;
43

54
use crate::infer::{InferCtxtUndoLogs, UndoLog};
65

@@ -40,9 +39,7 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
4039
impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
4140
fn drop(&mut self) {
4241
if !self.opaque_types.is_empty() {
43-
ty::tls::with(|tcx| {
44-
tcx.dcx().span_delayed_bug(DUMMY_SP, format!("{:?}", self.opaque_types))
45-
});
42+
ty::tls::with(|tcx| tcx.dcx().delayed_bug(format!("{:?}", self.opaque_types)));
4643
}
4744
}
4845
}

compiler/rustc_infer/src/infer/outlives/verify.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,9 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
175175
// ignore this, we presume it will yield an error
176176
// later, since if a type variable is not resolved by
177177
// this point it never will be
178-
self.tcx.dcx().span_delayed_bug(
179-
rustc_span::DUMMY_SP,
180-
format!("unresolved inference variable in outlives: {v:?}"),
181-
);
178+
self.tcx
179+
.dcx()
180+
.delayed_bug(format!("unresolved inference variable in outlives: {v:?}"));
182181
// add a bound that never holds
183182
VerifyBound::AnyBound(vec![])
184183
}

compiler/rustc_infer/src/infer/relate/combine.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,9 @@ impl<'tcx> InferCtxt<'tcx> {
180180
&mut OriginalQueryValues::default(),
181181
);
182182
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
183-
self.tcx.dcx().span_delayed_bug(
184-
DUMMY_SP,
185-
format!("cannot relate consts of different types (a={a:?}, b={b:?})",),
186-
)
183+
self.tcx.dcx().delayed_bug(format!(
184+
"cannot relate consts of different types (a={a:?}, b={b:?})",
185+
))
187186
})
188187
});
189188

compiler/rustc_middle/src/mir/interpret/allocation.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use either::{Left, Right};
1414

1515
use rustc_ast::Mutability;
1616
use rustc_data_structures::intern::Interned;
17-
use rustc_span::DUMMY_SP;
1817
use rustc_target::abi::{Align, HasDataLayout, Size};
1918

2019
use super::{
@@ -314,9 +313,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
314313
/// available to the compiler to do so.
315314
pub fn try_uninit<'tcx>(size: Size, align: Align) -> InterpResult<'tcx, Self> {
316315
Self::uninit_inner(size, align, || {
317-
ty::tls::with(|tcx| {
318-
tcx.dcx().span_delayed_bug(DUMMY_SP, "exhausted memory during interpretation")
319-
});
316+
ty::tls::with(|tcx| tcx.dcx().delayed_bug("exhausted memory during interpretation"));
320317
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted).into()
321318
})
322319
}

compiler/rustc_middle/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ macro_rules! define_feedable {
551551
// We have an inconsistency. This can happen if one of the two
552552
// results is tainted by errors. In this case, delay a bug to
553553
// ensure compilation is doomed, and keep the `old` value.
554-
tcx.dcx().span_delayed_bug(DUMMY_SP, format!(
554+
tcx.dcx().delayed_bug(format!(
555555
"Trying to feed an already recorded value for query {} key={key:?}:\n\
556556
old value: {old:?}\nnew value: {value:?}",
557557
stringify!($name),

compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> {
284284
type TargetDataLayoutRef = &'tcx TargetDataLayout;
285285

286286
fn delayed_bug(&self, txt: String) {
287-
self.tcx.dcx().span_delayed_bug(DUMMY_SP, txt);
287+
self.tcx.dcx().delayed_bug(txt);
288288
}
289289

290290
fn current_data_layout(&self) -> Self::TargetDataLayoutRef {

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::thir::*;
99
use rustc_middle::ty::{
1010
self, CanonicalUserType, CanonicalUserTypeAnnotation, TyCtxt, UserTypeAnnotationIndex,
1111
};
12-
use rustc_span::DUMMY_SP;
1312
use rustc_target::abi::Size;
1413

1514
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -111,15 +110,15 @@ fn lit_to_mir_constant<'tcx>(
111110
let LitToConstInput { lit, ty, neg } = lit_input;
112111
let trunc = |n| {
113112
let param_ty = ty::ParamEnv::reveal_all().and(ty);
114-
let width = tcx
115-
.layout_of(param_ty)
116-
.map_err(|_| {
117-
LitToConstError::Reported(tcx.dcx().span_delayed_bug(
118-
DUMMY_SP,
119-
format!("couldn't compute width of literal: {:?}", lit_input.lit),
120-
))
121-
})?
122-
.size;
113+
let width =
114+
tcx.layout_of(param_ty)
115+
.map_err(|_| {
116+
LitToConstError::Reported(tcx.dcx().delayed_bug(format!(
117+
"couldn't compute width of literal: {:?}",
118+
lit_input.lit
119+
)))
120+
})?
121+
.size;
123122
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
124123
let result = width.truncate(n);
125124
trace!("trunc result: {}", result);
@@ -158,16 +157,16 @@ fn lit_to_mir_constant<'tcx>(
158157
}
159158
(ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float_into_constval(*n, *fty, neg)
160159
.ok_or_else(|| {
161-
LitToConstError::Reported(tcx.dcx().span_delayed_bug(
162-
DUMMY_SP,
163-
format!("couldn't parse float literal: {:?}", lit_input.lit),
164-
))
160+
LitToConstError::Reported(
161+
tcx.dcx()
162+
.delayed_bug(format!("couldn't parse float literal: {:?}", lit_input.lit)),
163+
)
165164
})?,
166165
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
167166
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
168167
(ast::LitKind::Err, _) => {
169168
return Err(LitToConstError::Reported(
170-
tcx.dcx().span_delayed_bug(DUMMY_SP, "encountered LitKind::Err during mir build"),
169+
tcx.dcx().delayed_bug("encountered LitKind::Err during mir build"),
171170
));
172171
}
173172
_ => return Err(LitToConstError::TypeError),

0 commit comments

Comments
 (0)