Skip to content

Commit 9e8c30c

Browse files
committed
auto merge of #16718 : Sawyer47/rust/bool-cast, r=pcwalton
Current version of rust fails when casting from bool, e.g. ```rust fn main() { let _a = false as uint; let _b = true as uint; let _c: [bool, ..false as uint]; let _d: [bool, ..true as uint]; // _a and _b work, but _c and _d result in an error // error: expected constant expr for vector length: can't cast str to uint } ``` This commit makes it work as expected.
2 parents 17f79af + 46cf384 commit 9e8c30c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/librustc/middle/const_eval.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -517,26 +517,29 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
517517
match ty::get(ety).sty {
518518
ty::ty_float(_) => {
519519
match val {
520+
const_bool(b) => Ok(const_float(b as f64)),
520521
const_uint(u) => Ok(const_float(u as f64)),
521522
const_int(i) => Ok(const_float(i as f64)),
522523
const_float(f) => Ok(const_float(f)),
523-
_ => Err("can't cast float to str".to_string()),
524+
_ => Err("can't cast this type to float".to_string()),
524525
}
525526
}
526527
ty::ty_uint(_) => {
527528
match val {
529+
const_bool(b) => Ok(const_uint(b as u64)),
528530
const_uint(u) => Ok(const_uint(u)),
529531
const_int(i) => Ok(const_uint(i as u64)),
530532
const_float(f) => Ok(const_uint(f as u64)),
531-
_ => Err("can't cast str to uint".to_string()),
533+
_ => Err("can't cast this type to uint".to_string()),
532534
}
533535
}
534-
ty::ty_int(_) | ty::ty_bool => {
536+
ty::ty_int(_) => {
535537
match val {
538+
const_bool(b) => Ok(const_int(b as i64)),
536539
const_uint(u) => Ok(const_int(u as i64)),
537540
const_int(i) => Ok(const_int(i)),
538541
const_float(f) => Ok(const_int(f as i64)),
539-
_ => Err("can't cast str to int".to_string()),
542+
_ => Err("can't cast this type to int".to_string()),
540543
}
541544
}
542545
_ => Err("can't cast this type".to_string())

src/test/run-pass/cast-in-array-size.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ fn main() {
1616
let _a: [bool, ..1 as uint];
1717
let _b: [int, ..SIZE as uint] = [1, ..SIZE as uint];
1818
let _c: [bool, ..'\n' as uint] = [true, ..'\n' as uint];
19+
let _d: [bool, ..true as uint] = [true, ..true as uint];
1920
}

0 commit comments

Comments
 (0)