Skip to content

Commit c98a80e

Browse files
author
Jakub Wieczorek
committed
Fix casts in constant expressions
Fixes #17074.
1 parent d7502ac commit c98a80e

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -502,42 +502,44 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
502502
const cast")
503503
});
504504

505-
let base = eval_const_expr_partial(tcx, &**base);
506-
match base {
507-
Err(_) => base,
508-
Ok(val) => {
509-
match ty::get(ety).sty {
510-
ty::ty_float(_) => {
511-
match val {
512-
const_bool(b) => Ok(const_float(b as f64)),
513-
const_uint(u) => Ok(const_float(u as f64)),
514-
const_int(i) => Ok(const_float(i as f64)),
515-
const_float(f) => Ok(const_float(f)),
516-
_ => Err("can't cast this type to float".to_string()),
517-
}
505+
macro_rules! define_casts(
506+
($val:ident, {
507+
$($ty_pat:pat => (
508+
$intermediate_ty:ty,
509+
$const_type:ident,
510+
$target_ty:ty
511+
)),*
512+
}) => (match ty::get(ety).sty {
513+
$($ty_pat => {
514+
match $val {
515+
const_bool(b) => Ok($const_type(b as $intermediate_ty as $target_ty)),
516+
const_uint(u) => Ok($const_type(u as $intermediate_ty as $target_ty)),
517+
const_int(i) => Ok($const_type(i as $intermediate_ty as $target_ty)),
518+
const_float(f) => Ok($const_type(f as $intermediate_ty as $target_ty)),
519+
_ => Err(concat!(
520+
"can't cast this type to ", stringify!($const_type)
521+
).to_string())
518522
}
519-
ty::ty_uint(_) => {
520-
match val {
521-
const_bool(b) => Ok(const_uint(b as u64)),
522-
const_uint(u) => Ok(const_uint(u)),
523-
const_int(i) => Ok(const_uint(i as u64)),
524-
const_float(f) => Ok(const_uint(f as u64)),
525-
_ => Err("can't cast this type to uint".to_string()),
526-
}
527-
}
528-
ty::ty_int(_) => {
529-
match val {
530-
const_bool(b) => Ok(const_int(b as i64)),
531-
const_uint(u) => Ok(const_int(u as i64)),
532-
const_int(i) => Ok(const_int(i)),
533-
const_float(f) => Ok(const_int(f as i64)),
534-
_ => Err("can't cast this type to int".to_string()),
535-
}
536-
}
537-
_ => Err("can't cast this type".to_string())
538-
}
539-
}
540-
}
523+
},)*
524+
_ => Err("can't cast this type".to_string())
525+
})
526+
)
527+
528+
eval_const_expr_partial(tcx, &**base)
529+
.and_then(|val| define_casts!(val, {
530+
ty::ty_int(ast::TyI) => (int, const_int, i64),
531+
ty::ty_int(ast::TyI8) => (i8, const_int, i64),
532+
ty::ty_int(ast::TyI16) => (i16, const_int, i64),
533+
ty::ty_int(ast::TyI32) => (i32, const_int, i64),
534+
ty::ty_int(ast::TyI64) => (i64, const_int, i64),
535+
ty::ty_uint(ast::TyU) => (uint, const_uint, u64),
536+
ty::ty_uint(ast::TyU8) => (u8, const_uint, u64),
537+
ty::ty_uint(ast::TyU16) => (u16, const_uint, u64),
538+
ty::ty_uint(ast::TyU32) => (u32, const_uint, u64),
539+
ty::ty_uint(ast::TyU64) => (u64, const_uint, u64),
540+
ty::ty_float(ast::TyF32) => (f32, const_float, f64),
541+
ty::ty_float(ast::TyF64) => (f64, const_float, f64)
542+
}))
541543
}
542544
ExprPath(_) => {
543545
match lookup_const(tcx.ty_ctxt(), e) {

src/test/run-pass/issue-17074.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
static X: u64 = -1 as u16 as u64;
12+
static Y: u64 = -1 as u32 as u64;
13+
14+
fn main() {
15+
assert_eq!(match 1 {
16+
X => unreachable!(),
17+
Y => unreachable!(),
18+
_ => 1i
19+
}, 1);
20+
}

0 commit comments

Comments
 (0)