Skip to content

Commit 8168822

Browse files
committed
fix ICE on type error in promoted
1 parent 9c707a8 commit 8168822

25 files changed

+80
-209
lines changed

Diff for: compiler/rustc_const_eval/src/const_eval/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ where
170170
let reported = if allowed_in_infallible {
171171
ReportedErrorInfo::allowed_in_infallible(g)
172172
} else {
173-
ReportedErrorInfo::from(g)
173+
ReportedErrorInfo::const_eval_error(g)
174174
};
175175
ErrorHandled::Reported(reported, span)
176176
}

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use std::sync::atomic::Ordering::Relaxed;
33
use either::{Left, Right};
44
use rustc_abi::{self as abi, BackendRepr};
55
use rustc_hir::def::DefKind;
6-
use rustc_middle::bug;
7-
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo};
6+
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo, ReportedErrorInfo};
87
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
98
use rustc_middle::query::TyCtxtAt;
109
use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
1110
use rustc_middle::ty::print::with_no_trimmed_paths;
1211
use rustc_middle::ty::{self, Ty, TyCtxt};
12+
use rustc_middle::{bug, throw_inval};
1313
use rustc_span::def_id::LocalDefId;
1414
use rustc_span::{DUMMY_SP, Span};
1515
use tracing::{debug, instrument, trace};
@@ -93,18 +93,18 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>(
9393
match intern_result {
9494
Ok(()) => {}
9595
Err(InternResult::FoundDanglingPointer) => {
96-
return Err(ecx
97-
.tcx
98-
.dcx()
99-
.emit_err(errors::DanglingPtrInFinal { span: ecx.tcx.span, kind: intern_kind }))
100-
.into();
96+
throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(
97+
ecx.tcx
98+
.dcx()
99+
.emit_err(errors::DanglingPtrInFinal { span: ecx.tcx.span, kind: intern_kind }),
100+
)));
101101
}
102102
Err(InternResult::FoundBadMutablePointer) => {
103-
return Err(ecx
104-
.tcx
105-
.dcx()
106-
.emit_err(errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind }))
107-
.into();
103+
throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(
104+
ecx.tcx
105+
.dcx()
106+
.emit_err(errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind }),
107+
)));
108108
}
109109
}
110110

Diff for: compiler/rustc_const_eval/src/const_eval/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry};
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem};
1010
use rustc_middle::mir::AssertMessage;
11+
use rustc_middle::mir::interpret::ReportedErrorInfo;
1112
use rustc_middle::query::TyCtxtAt;
1213
use rustc_middle::ty::layout::{HasTypingEnv, TyAndLayout};
1314
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -563,7 +564,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
563564
.tcx
564565
.dcx()
565566
.span_delayed_bug(span, "The deny lint should have already errored");
566-
throw_inval!(AlreadyReported(guard.into()));
567+
throw_inval!(AlreadyReported(ReportedErrorInfo::allowed_in_infallible(guard)));
567568
}
568569
} else if new_steps > start && new_steps.is_power_of_two() {
569570
// Only report after a certain number of terminators have been evaluated and the

Diff for: compiler/rustc_const_eval/src/const_eval/valtrees.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_abi::{BackendRepr, VariantIdx};
22
use rustc_data_structures::stack::ensure_sufficient_stack;
3-
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
3+
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId, ReportedErrorInfo};
44
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
55
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
66
use rustc_middle::{bug, mir};
@@ -261,7 +261,7 @@ pub(crate) fn eval_to_valtree<'tcx>(
261261
ValTreeCreationError::NodesOverflow => {
262262
let handled =
263263
tcx.dcx().emit_err(MaxNumNodesInConstErr { span, global_const_id });
264-
Err(handled.into())
264+
Err(ReportedErrorInfo::non_const_eval_error(handled).into())
265265
}
266266
ValTreeCreationError::NonSupportedType(ty) => Ok(Err(ty)),
267267
}

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
268268
};
269269
// do not continue if typeck errors occurred (can only occur in local crate)
270270
if let Some(err) = body.tainted_by_errors {
271-
throw_inval!(AlreadyReported(ReportedErrorInfo::from(err)));
271+
throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
272272
}
273273
interp_ok(body)
274274
}
@@ -317,7 +317,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
317317
Ok(None) => throw_inval!(TooGeneric),
318318

319319
// FIXME(eddyb) this could be a bit more specific than `AlreadyReported`.
320-
Err(error_reported) => throw_inval!(AlreadyReported(error_reported.into())),
320+
Err(error_guaranteed) => throw_inval!(AlreadyReported(
321+
ReportedErrorInfo::non_const_eval_error(error_guaranteed)
322+
)),
321323
}
322324
}
323325

Diff for: compiler/rustc_middle/src/mir/consts.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_session::config::RemapPathScopeComponents;
88
use rustc_span::{DUMMY_SP, Span};
99
use rustc_type_ir::visit::TypeVisitableExt;
1010

11+
use super::interpret::ReportedErrorInfo;
1112
use crate::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, Scalar, alloc_range};
1213
use crate::mir::{Promoted, pretty_print_const_value};
1314
use crate::ty::print::{pretty_print_const, with_no_trimmed_paths};
@@ -331,7 +332,10 @@ impl<'tcx> Const<'tcx> {
331332
ConstKind::Expr(_) => {
332333
bug!("Normalization of `ty::ConstKind::Expr` is unimplemented")
333334
}
334-
_ => Err(tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body").into()),
335+
_ => Err(ReportedErrorInfo::non_const_eval_error(
336+
tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body"),
337+
)
338+
.into()),
335339
}
336340
}
337341
Const::Unevaluated(uneval, _) => {

Diff for: compiler/rustc_middle/src/mir/interpret/error.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ pub enum ErrorHandled {
2828
TooGeneric(Span),
2929
}
3030

31-
impl From<ErrorGuaranteed> for ErrorHandled {
31+
impl From<ReportedErrorInfo> for ErrorHandled {
3232
#[inline]
33-
fn from(error: ErrorGuaranteed) -> ErrorHandled {
34-
ErrorHandled::Reported(error.into(), DUMMY_SP)
33+
fn from(error: ReportedErrorInfo) -> ErrorHandled {
34+
ErrorHandled::Reported(error, DUMMY_SP)
3535
}
3636
}
3737

@@ -64,6 +64,20 @@ pub struct ReportedErrorInfo {
6464
}
6565

6666
impl ReportedErrorInfo {
67+
#[inline]
68+
pub fn const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
69+
ReportedErrorInfo { allowed_in_infallible: false, error }
70+
}
71+
72+
/// Use this when the error that led to this is *not* a const-eval error
73+
/// (e.g., a layout or type checking error).
74+
#[inline]
75+
pub fn non_const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
76+
ReportedErrorInfo { allowed_in_infallible: true, error }
77+
}
78+
79+
/// Use this when the error that led to this *is* a const-eval error, but
80+
/// we do allow it to occur in infallible constants (e.g., resource exhaustion).
6781
#[inline]
6882
pub fn allowed_in_infallible(error: ErrorGuaranteed) -> ReportedErrorInfo {
6983
ReportedErrorInfo { allowed_in_infallible: true, error }
@@ -74,13 +88,6 @@ impl ReportedErrorInfo {
7488
}
7589
}
7690

77-
impl From<ErrorGuaranteed> for ReportedErrorInfo {
78-
#[inline]
79-
fn from(error: ErrorGuaranteed) -> ReportedErrorInfo {
80-
ReportedErrorInfo { allowed_in_infallible: false, error }
81-
}
82-
}
83-
8491
impl Into<ErrorGuaranteed> for ReportedErrorInfo {
8592
#[inline]
8693
fn into(self) -> ErrorGuaranteed {
@@ -180,11 +187,11 @@ fn print_backtrace(backtrace: &Backtrace) {
180187
eprintln!("\n\nAn error occurred in the MIR interpreter:\n{backtrace}");
181188
}
182189

183-
impl From<ErrorGuaranteed> for InterpErrorInfo<'_> {
190+
/*impl From<ErrorGuaranteed> for InterpErrorInfo<'_> {
184191
fn from(err: ErrorGuaranteed) -> Self {
185192
InterpErrorKind::InvalidProgram(InvalidProgramInfo::AlreadyReported(err.into())).into()
186193
}
187-
}
194+
}*/
188195

189196
impl From<ErrorHandled> for InterpErrorInfo<'_> {
190197
fn from(err: ErrorHandled) -> Self {

Diff for: compiler/rustc_middle/src/mir/interpret/queries.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tracing::{debug, instrument};
66

77
use super::{
88
ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, GlobalId,
9+
ReportedErrorInfo,
910
};
1011
use crate::mir;
1112
use crate::query::TyCtxtEnsure;
@@ -81,7 +82,9 @@ impl<'tcx> TyCtxt<'tcx> {
8182
// For errors during resolution, we deliberately do not point at the usage site of the constant,
8283
// since for these errors the place the constant is used shouldn't matter.
8384
Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)),
84-
Err(err) => Err(ErrorHandled::Reported(err.into(), DUMMY_SP)),
85+
Err(err) => {
86+
Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP))
87+
}
8588
}
8689
}
8790

@@ -138,7 +141,9 @@ impl<'tcx> TyCtxt<'tcx> {
138141
// For errors during resolution, we deliberately do not point at the usage site of the constant,
139142
// since for these errors the place the constant is used shouldn't matter.
140143
Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)),
141-
Err(err) => Err(ErrorHandled::Reported(err.into(), DUMMY_SP)),
144+
Err(err) => {
145+
Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP))
146+
}
142147
}
143148
}
144149

Diff for: src/tools/clippy/clippy_lints/src/non_copy_const.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::{
1111
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
1212
};
1313
use rustc_lint::{LateContext, LateLintPass, Lint};
14-
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult, GlobalId};
14+
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult, GlobalId, ReportedErrorInfo};
1515
use rustc_middle::ty::adjustment::Adjust;
1616
use rustc_middle::ty::{self, Ty, TyCtxt};
1717
use rustc_session::impl_lint_pass;
@@ -302,7 +302,10 @@ impl<'tcx> NonCopyConst<'tcx> {
302302
tcx.const_eval_global_id_for_typeck(typing_env, cid, span)
303303
},
304304
Ok(None) => Err(ErrorHandled::TooGeneric(span)),
305-
Err(err) => Err(ErrorHandled::Reported(err.into(), span)),
305+
Err(err) => Err(ErrorHandled::Reported(
306+
ReportedErrorInfo::non_const_eval_error(err),
307+
span,
308+
)),
306309
}
307310
}
308311
}

Diff for: tests/ui/const-generics/generic_const_exprs/unevaluated-const-ice-119731.stderr

-14
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,6 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
7272
LL + #![feature(adt_const_params)]
7373
|
7474

75-
note: erroneous constant encountered
76-
--> $DIR/unevaluated-const-ice-119731.rs:22:19
77-
|
78-
LL | impl v17<512, v0> {
79-
| ^^
80-
81-
note: erroneous constant encountered
82-
--> $DIR/unevaluated-const-ice-119731.rs:22:19
83-
|
84-
LL | impl v17<512, v0> {
85-
| ^^
86-
|
87-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
88-
8975
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
9076
--> $DIR/unevaluated-const-ice-119731.rs:28:37
9177
|

Diff for: tests/ui/consts/const-integer-bool-ops.stderr

-60
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ error[E0308]: mismatched types
1616
LL | const X: usize = 42 && 39;
1717
| ^^^^^^^^ expected `usize`, found `bool`
1818

19-
note: erroneous constant encountered
20-
--> $DIR/const-integer-bool-ops.rs:8:18
21-
|
22-
LL | const ARR: [i32; X] = [99; 34];
23-
| ^
24-
2519
error[E0308]: mismatched types
2620
--> $DIR/const-integer-bool-ops.rs:10:19
2721
|
@@ -40,12 +34,6 @@ error[E0308]: mismatched types
4034
LL | const X1: usize = 42 || 39;
4135
| ^^^^^^^^ expected `usize`, found `bool`
4236

43-
note: erroneous constant encountered
44-
--> $DIR/const-integer-bool-ops.rs:17:19
45-
|
46-
LL | const ARR1: [i32; X1] = [99; 47];
47-
| ^^
48-
4937
error[E0308]: mismatched types
5038
--> $DIR/const-integer-bool-ops.rs:19:19
5139
|
@@ -64,12 +52,6 @@ error[E0308]: mismatched types
6452
LL | const X2: usize = -42 || -39;
6553
| ^^^^^^^^^^ expected `usize`, found `bool`
6654

67-
note: erroneous constant encountered
68-
--> $DIR/const-integer-bool-ops.rs:26:19
69-
|
70-
LL | const ARR2: [i32; X2] = [99; 18446744073709551607];
71-
| ^^
72-
7355
error[E0308]: mismatched types
7456
--> $DIR/const-integer-bool-ops.rs:28:19
7557
|
@@ -88,84 +70,42 @@ error[E0308]: mismatched types
8870
LL | const X3: usize = -42 && -39;
8971
| ^^^^^^^^^^ expected `usize`, found `bool`
9072

91-
note: erroneous constant encountered
92-
--> $DIR/const-integer-bool-ops.rs:35:19
93-
|
94-
LL | const ARR3: [i32; X3] = [99; 6];
95-
| ^^
96-
9773
error[E0308]: mismatched types
9874
--> $DIR/const-integer-bool-ops.rs:37:18
9975
|
10076
LL | const Y: usize = 42.0 == 42.0;
10177
| ^^^^^^^^^^^^ expected `usize`, found `bool`
10278

103-
note: erroneous constant encountered
104-
--> $DIR/const-integer-bool-ops.rs:40:19
105-
|
106-
LL | const ARRR: [i32; Y] = [99; 1];
107-
| ^
108-
10979
error[E0308]: mismatched types
11080
--> $DIR/const-integer-bool-ops.rs:42:19
11181
|
11282
LL | const Y1: usize = 42.0 >= 42.0;
11383
| ^^^^^^^^^^^^ expected `usize`, found `bool`
11484

115-
note: erroneous constant encountered
116-
--> $DIR/const-integer-bool-ops.rs:45:20
117-
|
118-
LL | const ARRR1: [i32; Y1] = [99; 1];
119-
| ^^
120-
12185
error[E0308]: mismatched types
12286
--> $DIR/const-integer-bool-ops.rs:47:19
12387
|
12488
LL | const Y2: usize = 42.0 <= 42.0;
12589
| ^^^^^^^^^^^^ expected `usize`, found `bool`
12690

127-
note: erroneous constant encountered
128-
--> $DIR/const-integer-bool-ops.rs:50:20
129-
|
130-
LL | const ARRR2: [i32; Y2] = [99; 1];
131-
| ^^
132-
13391
error[E0308]: mismatched types
13492
--> $DIR/const-integer-bool-ops.rs:52:19
13593
|
13694
LL | const Y3: usize = 42.0 > 42.0;
13795
| ^^^^^^^^^^^ expected `usize`, found `bool`
13896

139-
note: erroneous constant encountered
140-
--> $DIR/const-integer-bool-ops.rs:55:20
141-
|
142-
LL | const ARRR3: [i32; Y3] = [99; 0];
143-
| ^^
144-
14597
error[E0308]: mismatched types
14698
--> $DIR/const-integer-bool-ops.rs:57:19
14799
|
148100
LL | const Y4: usize = 42.0 < 42.0;
149101
| ^^^^^^^^^^^ expected `usize`, found `bool`
150102

151-
note: erroneous constant encountered
152-
--> $DIR/const-integer-bool-ops.rs:60:20
153-
|
154-
LL | const ARRR4: [i32; Y4] = [99; 0];
155-
| ^^
156-
157103
error[E0308]: mismatched types
158104
--> $DIR/const-integer-bool-ops.rs:62:19
159105
|
160106
LL | const Y5: usize = 42.0 != 42.0;
161107
| ^^^^^^^^^^^^ expected `usize`, found `bool`
162108

163-
note: erroneous constant encountered
164-
--> $DIR/const-integer-bool-ops.rs:65:20
165-
|
166-
LL | const ARRR5: [i32; Y5] = [99; 0];
167-
| ^^
168-
169109
error: aborting due to 18 previous errors
170110

171111
For more information about this error, try `rustc --explain E0308`.

Diff for: tests/ui/consts/const-mut-refs/issue-76510.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0764]: mutable references are not allowed in the final value of constants
44
LL | const S: &'static mut str = &mut " hello ";
55
| ^^^^^^^^^^^^^^
66

7-
note: erroneous constant encountered
8-
--> $DIR/issue-76510.rs:7:70
9-
|
10-
LL | let s = transmute::<(*const u8, usize), &ManuallyDrop<str>>((S.as_ptr(), 3));
11-
| ^
12-
137
error: aborting due to 1 previous error
148

159
For more information about this error, try `rustc --explain E0764`.

0 commit comments

Comments
 (0)