Skip to content

Commit ea56e80

Browse files
authored
Rollup merge of #104235 - compiler-errors:more-ct-guar, r=oli-obk
Use `const_error_with_guaranteed` more Better to pass down an ErrorGuaranteed rather than making a new one out of thin air, for some usages. Also for the ones where we *do* need to delay a bug, that delayed bug will have a more descriptive message.
2 parents 77fde5d + 0f89fb1 commit ea56e80

File tree

7 files changed

+51
-15
lines changed

7 files changed

+51
-15
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ use rustc_ast::LitKind;
106106
use rustc_data_structures::fx::FxHashMap;
107107
use rustc_data_structures::sync::{HashMapExt, Lock};
108108
use rustc_data_structures::tiny_list::TinyList;
109+
use rustc_errors::ErrorGuaranteed;
109110
use rustc_hir::def_id::DefId;
110111
use rustc_macros::HashStable;
111112
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -176,7 +177,7 @@ pub enum LitToConstError {
176177
/// This is used for graceful error handling (`delay_span_bug`) in
177178
/// type checking (`Const::from_anon_const`).
178179
TypeError,
179-
Reported,
180+
Reported(ErrorGuaranteed),
180181
}
181182

182183
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]

compiler/rustc_middle/src/mir/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,9 @@ impl<'tcx> ConstantKind<'tcx> {
22512251
match tcx.const_eval_resolve(param_env, uneval, None) {
22522252
Ok(val) => Self::Val(val, ty),
22532253
Err(ErrorHandled::TooGeneric | ErrorHandled::Linted) => self,
2254-
Err(_) => Self::Ty(tcx.const_error(ty)),
2254+
Err(ErrorHandled::Reported(guar)) => {
2255+
Self::Ty(tcx.const_error_with_guaranteed(ty, guar))
2256+
}
22552257
}
22562258
}
22572259
}

compiler/rustc_middle/src/ty/consts.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::mir::interpret::LitToConstInput;
22
use crate::mir::ConstantKind;
33
use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt};
44
use rustc_data_structures::intern::Interned;
5-
use rustc_errors::ErrorGuaranteed;
65
use rustc_hir as hir;
76
use rustc_hir::def_id::{DefId, LocalDefId};
87
use rustc_macros::HashStable;
@@ -225,7 +224,7 @@ impl<'tcx> Const<'tcx> {
225224
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
226225
match val {
227226
Ok(val) => Const::from_value(tcx, val, self.ty()),
228-
Err(ErrorGuaranteed { .. }) => tcx.const_error(self.ty()),
227+
Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar),
229228
}
230229
} else {
231230
// Either the constant isn't evaluatable or ValTree creation failed.
@@ -240,7 +239,7 @@ impl<'tcx> Const<'tcx> {
240239
if let Some(val) = self.kind().try_eval_for_mir(tcx, param_env) {
241240
match val {
242241
Ok(const_val) => ConstantKind::from_value(const_val, self.ty()),
243-
Err(ErrorGuaranteed { .. }) => ConstantKind::Ty(tcx.const_error(self.ty())),
242+
Err(guar) => ConstantKind::Ty(tcx.const_error_with_guaranteed(self.ty(), guar)),
244243
}
245244
} else {
246245
ConstantKind::Ty(self)

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

+25-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::mir::interpret::{
99
use rustc_middle::mir::*;
1010
use rustc_middle::thir::*;
1111
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, TyCtxt};
12+
use rustc_span::DUMMY_SP;
1213
use rustc_target::abi::Size;
1314

1415
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -26,7 +27,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2627
let literal =
2728
match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
2829
Ok(c) => c,
29-
Err(LitToConstError::Reported) => ConstantKind::Ty(tcx.const_error(ty)),
30+
Err(LitToConstError::Reported(guar)) => {
31+
ConstantKind::Ty(tcx.const_error_with_guaranteed(ty, guar))
32+
}
3033
Err(LitToConstError::TypeError) => {
3134
bug!("encountered type error in `lit_to_mir_constant")
3235
}
@@ -105,7 +108,15 @@ pub(crate) fn lit_to_mir_constant<'tcx>(
105108
let LitToConstInput { lit, ty, neg } = lit_input;
106109
let trunc = |n| {
107110
let param_ty = ty::ParamEnv::reveal_all().and(ty);
108-
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
111+
let width = tcx
112+
.layout_of(param_ty)
113+
.map_err(|_| {
114+
LitToConstError::Reported(tcx.sess.delay_span_bug(
115+
DUMMY_SP,
116+
format!("couldn't compute width of literal: {:?}", lit_input.lit),
117+
))
118+
})?
119+
.size;
109120
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
110121
let result = width.truncate(n);
111122
trace!("trunc result: {}", result);
@@ -136,12 +147,20 @@ pub(crate) fn lit_to_mir_constant<'tcx>(
136147
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
137148
trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
138149
}
139-
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
140-
parse_float_into_constval(*n, *fty, neg).ok_or(LitToConstError::Reported)?
141-
}
150+
(ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float_into_constval(*n, *fty, neg)
151+
.ok_or_else(|| {
152+
LitToConstError::Reported(tcx.sess.delay_span_bug(
153+
DUMMY_SP,
154+
format!("couldn't parse float literal: {:?}", lit_input.lit),
155+
))
156+
})?,
142157
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
143158
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
144-
(ast::LitKind::Err, _) => return Err(LitToConstError::Reported),
159+
(ast::LitKind::Err, _) => {
160+
return Err(LitToConstError::Reported(
161+
tcx.sess.delay_span_bug(DUMMY_SP, "encountered LitKind::Err during mir build"),
162+
));
163+
}
145164
_ => return Err(LitToConstError::TypeError),
146165
};
147166

compiler/rustc_mir_build/src/thir/constant.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
33
use rustc_middle::ty::{self, ParamEnv, ScalarInt, TyCtxt};
4+
use rustc_span::DUMMY_SP;
45

56
pub(crate) fn lit_to_const<'tcx>(
67
tcx: TyCtxt<'tcx>,
@@ -10,7 +11,15 @@ pub(crate) fn lit_to_const<'tcx>(
1011

1112
let trunc = |n| {
1213
let param_ty = ParamEnv::reveal_all().and(ty);
13-
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
14+
let width = tcx
15+
.layout_of(param_ty)
16+
.map_err(|_| {
17+
LitToConstError::Reported(tcx.sess.delay_span_bug(
18+
DUMMY_SP,
19+
format!("couldn't compute width of literal: {:?}", lit_input.lit),
20+
))
21+
})?
22+
.size;
1423
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
1524
let result = width.truncate(n);
1625
trace!("trunc result: {}", result);
@@ -44,7 +53,11 @@ pub(crate) fn lit_to_const<'tcx>(
4453
}
4554
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
4655
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
47-
(ast::LitKind::Err, _) => return Err(LitToConstError::Reported),
56+
(ast::LitKind::Err, _) => {
57+
return Err(LitToConstError::Reported(
58+
tcx.sess.delay_span_bug(DUMMY_SP, "encountered LitKind::Err during mir build"),
59+
));
60+
}
4861
_ => return Err(LitToConstError::TypeError),
4962
};
5063

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
614614
LitToConstInput { lit: &lit.node, ty: self.typeck_results.expr_ty(expr), neg };
615615
match self.tcx.at(expr.span).lit_to_mir_constant(lit_input) {
616616
Ok(constant) => self.const_to_pat(constant, expr.hir_id, lit.span, false).kind,
617-
Err(LitToConstError::Reported) => PatKind::Wild,
617+
Err(LitToConstError::Reported(_)) => PatKind::Wild,
618618
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
619619
}
620620
}

compiler/rustc_ty_utils/src/consts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
235235
neg,
236236
}) {
237237
Ok(c) => c,
238-
Err(LitToConstError::Reported) => self.tcx.const_error(node.ty),
238+
Err(LitToConstError::Reported(guar)) => {
239+
self.tcx.const_error_with_guaranteed(node.ty, guar)
240+
}
239241
Err(LitToConstError::TypeError) => {
240242
bug!("encountered type error in lit_to_const")
241243
}

0 commit comments

Comments
 (0)