Skip to content

Commit

Permalink
Prevent eval_const_expr_partial hides prior error in expr_cast arm.
Browse files Browse the repository at this point in the history
This will help not to meet confusing errors.
In issue rust-lang#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"
This patch make the original error to be printed.
  • Loading branch information
youknowone committed Apr 15, 2013
1 parent 1ab1354 commit 9b55d86
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,32 +371,31 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
expr_cast(base, _) => {
let ety = ty::expr_ty(tcx, e);
let base = eval_const_expr_partial(tcx, base);
match ty::get(ety).sty {
ty::ty_float(_) => {
match base {
Ok(const_uint(u)) => Ok(const_float(u as f64)),
Ok(const_int(i)) => Ok(const_float(i as f64)),
Ok(const_float(_)) => base,
_ => Err(~"Can't cast float to str")
}
}
ty::ty_uint(_) => {
match base {
Ok(const_uint(_)) => base,
Ok(const_int(i)) => Ok(const_uint(i as u64)),
Ok(const_float(f)) => Ok(const_uint(f as u64)),
_ => Err(~"Can't cast str to uint")
}
}
ty::ty_int(_) | ty::ty_bool => {
match base {
Ok(const_uint(u)) => Ok(const_int(u as i64)),
Ok(const_int(_)) => base,
Ok(const_float(f)) => Ok(const_int(f as i64)),
_ => Err(~"Can't cast str to int")
match /*bad*/copy base {

This comment has been minimized.

Copy link
@catamorphism

catamorphism Apr 15, 2013

You should be able to rewrite this to avoid the copy, by adding the ref keyword to the binding for base and to all the other sub-patterns here (such as val).

This comment has been minimized.

Copy link
@youknowone

youknowone Apr 16, 2013

Author Owner

@catamorphism I am sorry but I have no idea how to resolve this pattern. I don't know about pointers more than tutorial.
Could you hint me how to do it?
val is ok. The line I cannot understand is 373 and 374.
As I know, though I added ref for base on line 371, it should be derefferenced to be put to eval_const_expr_partial in line 373. Then the name base is still a local value now. I have no idea how to adjust ref to this value or any other way to make it as out value without copy.

This comment has been minimized.

Copy link
@catamorphism

catamorphism Apr 16, 2013

Ah, yeah. I fiddled with it a little more, but I think you actually can't avoid the copy here easily. Sorry for the bad advice. You can remove the /* bad */ comment.

Err(_) => base,
Ok(val) => {
match ty::get(ety).sty {
ty::ty_float(_) => match val {
const_uint(u) => Ok(const_float(u as f64)),
const_int(i) => Ok(const_float(i as f64)),
const_float(_) => base,
_ => Err(~"Can't cast float to str"),
},
ty::ty_uint(_) => match val {
const_uint(_) => base,
const_int(i) => Ok(const_uint(i as u64)),
const_float(f) => Ok(const_uint(f as u64)),
_ => Err(~"Can't cast str to uint"),
},
ty::ty_int(_) | ty::ty_bool => match val {
const_uint(u) => Ok(const_int(u as i64)),
const_int(_) => base,
const_float(f) => Ok(const_int(f as i64)),
_ => Err(~"Can't cast str to int"),
},
_ => Err(~"Can't cast this type")
}
}
}
_ => Err(~"Can't cast this type")
}
}
expr_path(_) => {
Expand Down

1 comment on commit 9b55d86

@catamorphism
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.