Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid 'no type for expr' error in CastExpr - Closes #5900 #11832

Merged
merged 1 commit into from
Jan 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading