casting in a static breaks type inference
d47c004
Opened by Cassie Jones at
Writing the following works perfectly:
static count : uint = 2;
fn main() {
let larger : [uint, ..count*2];
}
While the following:
static count : uint = 2 as uint;
fn main() {
let larger : [uint, ..count*2];
}
Fails with error: expected constant expr for vector length: can't do this op on a uint and int. This looks like a problem with the type inference, and using 2u fixes this example, but breaks the one that previously worked.
When the array is being initialized rather than having the type set:
let larger = [0u, ..count*2];
The error is a much more cryptic error: expected constant integer for repeat count, found variable, and which one fails can again be swapped by multiplying by 2u instead of 2.
See https://github.com/rust-lang/rfcs/pull/259
mahkoh at 2014-10-04 00:00:41
This works on 1.1+:
const count : usize = 2 as usize; fn main() { let larger : [usize; count*2]; }Ariel Ben-Yehuda at 2015-06-29 17:10:58
Regression test added in #26668
Corey Farwell at 2015-06-30 00:12:13