Skip to content

Commit

Permalink
Add a limited prim type lookup for safer const expr evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfager committed Jan 29, 2014
1 parent edfb546 commit 9b1865a
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 209 deletions.
15 changes: 13 additions & 2 deletions src/librustc/middle/const_eval.rs
Expand Up @@ -11,7 +11,9 @@

use metadata::csearch;
use middle::astencode;

use middle::ty;
use middle::typeck::astconv;
use middle;

use syntax::{ast, ast_map, ast_util};
Expand Down Expand Up @@ -445,8 +447,17 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
_ => Err(~"Bad operands for binary")
}
}
ExprCast(base, _) => {
let ety = tcx.expr_ty(e);
ExprCast(base, target_ty) => {
// This tends to get called w/o the type actually having been
// populated in the ctxt, which was causing things to blow up
// (#5900). Fall back to doing a limited lookup to get past it.
let ety = ty::expr_ty_opt(tcx.ty_ctxt(), e)
.or_else(|| astconv::ast_ty_to_prim_ty(tcx.ty_ctxt(), target_ty))
.unwrap_or_else(|| tcx.ty_ctxt().sess.span_fatal(
target_ty.span,
format!("Target type not found for const cast")
));

let base = eval_const_expr_partial(tcx, base);
match base {
Err(_) => base,
Expand Down
19 changes: 15 additions & 4 deletions src/librustc/middle/ty.rs
Expand Up @@ -2617,17 +2617,24 @@ pub fn node_id_to_trait_ref(cx: ctxt, id: ast::NodeId) -> @ty::TraitRef {
}

pub fn node_id_to_type(cx: ctxt, id: ast::NodeId) -> t {
//printfln!("{:?}/{:?}", id, cx.node_types.len());
let node_types = cx.node_types.borrow();
match node_types.get().find(&(id as uint)) {
Some(&t) => t,
match node_id_to_type_opt(cx, id) {
Some(t) => t,
None => cx.sess.bug(
format!("node_id_to_type: no type for node `{}`",
ast_map::node_id_to_str(cx.items, id,
token::get_ident_interner())))
}
}

pub fn node_id_to_type_opt(cx: ctxt, id: ast::NodeId) -> Option<t> {
let node_types = cx.node_types.borrow();
debug!("id: {:?}, node_types: {:?}", id, node_types);
match node_types.get().find(&(id as uint)) {
Some(&t) => Some(t),
None => None
}
}

// FIXME(pcwalton): Makes a copy, bleh. Probably better to not do that.
pub fn node_id_to_type_params(cx: ctxt, id: ast::NodeId) -> ~[t] {
let node_type_substs = cx.node_type_substs.borrow();
Expand Down Expand Up @@ -2798,6 +2805,10 @@ pub fn expr_ty(cx: ctxt, expr: &ast::Expr) -> t {
return node_id_to_type(cx, expr.id);
}

pub fn expr_ty_opt(cx: ctxt, expr: &ast::Expr) -> Option<t> {
return node_id_to_type_opt(cx, expr.id);
}

pub fn expr_ty_adjusted(cx: ctxt, expr: &ast::Expr) -> t {
/*!
*
Expand Down

25 comments on commit 9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = 47bb4a51

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = 7936bfa7

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = 38bbe76d

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = 078d3ca1

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = ca39f208

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at jfager@9b1865a

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

merging jfager/rust/r5900 = 9b1865a into auto

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

jfager/rust/r5900 = 9b1865a merged ok, testing candidate = 5a61812

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 9b1865a Jan 31, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 5a61812

Please sign in to comment.