Skip to content

Commit 507381e

Browse files
committed
Fix ICE and report a human readable error
1 parent 8f19cad commit 507381e

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/librustc/ty/cast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum CastKind {
5858
}
5959

6060
impl<'tcx> CastTy<'tcx> {
61+
/// Returns `Some` for integral/pointer casts.
62+
/// casts like unsizing casts will return `None`
6163
pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
6264
match t.sty {
6365
ty::Bool => Some(CastTy::Int(IntTy::Bool)),

src/librustc_mir/transform/qualify_min_const_fn.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn check_rvalue(
148148
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) => {
149149
check_place(tcx, mir, place, span, PlaceMode::Read)
150150
}
151-
Rvalue::Cast(_, operand, cast_ty) => {
151+
Rvalue::Cast(CastKind::Misc, operand, cast_ty) => {
152152
use rustc::ty::cast::CastTy;
153153
let cast_in = CastTy::from_ty(operand.ty(mir, tcx)).expect("bad input type for cast");
154154
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
@@ -163,6 +163,10 @@ fn check_rvalue(
163163
_ => check_operand(tcx, mir, operand, span),
164164
}
165165
}
166+
Rvalue::Cast(_, _, _) => Err((
167+
span,
168+
"only int casts are allowed in const fn".into(),
169+
)),
166170
// binops are fine on integers
167171
Rvalue::BinaryOp(_, lhs, rhs) | Rvalue::CheckedBinaryOp(_, lhs, rhs) => {
168172
check_operand(tcx, mir, lhs, span)?;
@@ -177,8 +181,11 @@ fn check_rvalue(
177181
))
178182
}
179183
}
180-
// checked by regular const fn checks
181-
Rvalue::NullaryOp(..) => Ok(()),
184+
Rvalue::NullaryOp(NullOp::SizeOf, _) => Ok(()),
185+
Rvalue::NullaryOp(NullOp::Box, _) => Err((
186+
span,
187+
"heap allocations are not allowed in const fn".into(),
188+
)),
182189
Rvalue::UnaryOp(_, operand) => {
183190
let ty = operand.ty(mir, tcx);
184191
if ty.is_integral() || ty.is_bool() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fn foo(a: i32) -> Vec<i32> {
2+
vec![1, 2, 3] //~ ERROR heap allocations are not allowed in const fn
3+
}
4+
5+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: heap allocations are not allowed in const fn
2+
--> $DIR/bad_const_fn_body_ice.rs:2:5
3+
|
4+
LL | vec![1, 2, 3] //~ ERROR heap allocations are not allowed in const fn
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)