Skip to content

Commit 07e087b

Browse files
committed
auto merge of #5890 : youknowone/rust/const-eval, r=catamorphism
This will help not to meet confusing errors. In issue #5873, the error was "expected constant expr for vector length: Can't cast str to int". It was originally "expected constant expr for vector length: Non-constant path in constant expr" (though still invalid error). This patch make the original error to be printed.
2 parents 7cacd87 + 9b55d86 commit 07e087b

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/librustc/middle/const_eval.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -371,32 +371,31 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
371371
expr_cast(base, _) => {
372372
let ety = ty::expr_ty(tcx, e);
373373
let base = eval_const_expr_partial(tcx, base);
374-
match ty::get(ety).sty {
375-
ty::ty_float(_) => {
376-
match base {
377-
Ok(const_uint(u)) => Ok(const_float(u as f64)),
378-
Ok(const_int(i)) => Ok(const_float(i as f64)),
379-
Ok(const_float(_)) => base,
380-
_ => Err(~"Can't cast float to str")
381-
}
382-
}
383-
ty::ty_uint(_) => {
384-
match base {
385-
Ok(const_uint(_)) => base,
386-
Ok(const_int(i)) => Ok(const_uint(i as u64)),
387-
Ok(const_float(f)) => Ok(const_uint(f as u64)),
388-
_ => Err(~"Can't cast str to uint")
389-
}
390-
}
391-
ty::ty_int(_) | ty::ty_bool => {
392-
match base {
393-
Ok(const_uint(u)) => Ok(const_int(u as i64)),
394-
Ok(const_int(_)) => base,
395-
Ok(const_float(f)) => Ok(const_int(f as i64)),
396-
_ => Err(~"Can't cast str to int")
374+
match /*bad*/copy base {
375+
Err(_) => base,
376+
Ok(val) => {
377+
match ty::get(ety).sty {
378+
ty::ty_float(_) => match val {
379+
const_uint(u) => Ok(const_float(u as f64)),
380+
const_int(i) => Ok(const_float(i as f64)),
381+
const_float(_) => base,
382+
_ => Err(~"Can't cast float to str"),
383+
},
384+
ty::ty_uint(_) => match val {
385+
const_uint(_) => base,
386+
const_int(i) => Ok(const_uint(i as u64)),
387+
const_float(f) => Ok(const_uint(f as u64)),
388+
_ => Err(~"Can't cast str to uint"),
389+
},
390+
ty::ty_int(_) | ty::ty_bool => match val {
391+
const_uint(u) => Ok(const_int(u as i64)),
392+
const_int(_) => base,
393+
const_float(f) => Ok(const_int(f as i64)),
394+
_ => Err(~"Can't cast str to int"),
395+
},
396+
_ => Err(~"Can't cast this type")
397+
}
397398
}
398-
}
399-
_ => Err(~"Can't cast this type")
400399
}
401400
}
402401
expr_path(_) => {

0 commit comments

Comments
 (0)