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

Make const_eval of literals more type aware #18538

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 24 additions & 22 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,9 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> {

ExprCall(ref callee, ref args) => {
let def = tcx.def_map.borrow().get_copy(&callee.id);
match tcx.def_map.borrow_mut().entry(expr.id) {
Vacant(entry) => { entry.set(def); }
_ => {}
};
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
entry.set(def);
}
let path = match def {
def::DefStruct(def_id) => def_to_path(tcx, def_id),
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
Expand Down Expand Up @@ -560,7 +559,7 @@ pub fn eval_const_expr_partial(tcx: &ty::ctxt, e: &Expr) -> Result<const_val, St
None => Err("non-constant path in constant expr".to_string())
}
}
ExprLit(ref lit) => Ok(lit_to_const(&**lit)),
ExprLit(ref lit) => Ok(lit_to_const(tcx, e.id, &**lit)),
ExprParen(ref e) => eval_const_expr_partial(tcx, &**e),
ExprBlock(ref block) => {
match block.expr {
Expand All @@ -572,25 +571,28 @@ pub fn eval_const_expr_partial(tcx: &ty::ctxt, e: &Expr) -> Result<const_val, St
}
}

pub fn lit_to_const(lit: &Lit) -> const_val {
pub fn lit_to_const(tcx: &ty::ctxt, id: ast::NodeId, lit: &Lit) -> const_val {
let t = ty::try_node_id_to_type(tcx, id);
match lit.node {
LitStr(ref s, _) => const_str((*s).clone()),
LitBinary(ref data) => {
const_binary(Rc::new(data.iter().map(|x| *x).collect()))
}
LitByte(n) => const_uint(n as u64),
LitChar(n) => const_uint(n as u64),
LitInt(n, ast::SignedIntLit(_, ast::Plus)) |
LitInt(n, ast::UnsuffixedIntLit(ast::Plus)) => const_int(n as i64),
LitInt(n, ast::SignedIntLit(_, ast::Minus)) |
LitInt(n, ast::UnsuffixedIntLit(ast::Minus)) => const_int(-(n as i64)),
LitInt(n, ast::UnsignedIntLit(_)) => const_uint(n),
LitFloat(ref n, _) |
LitFloatUnsuffixed(ref n) => {
LitNil =>
const_nil,
LitBool(b) =>
const_bool(b),
LitByte(n) =>
const_uint(n as u64),
LitInt(n, _) => if t.into_iter().any(ty::type_is_signed) {
const_int(n as i64)
} else {
const_uint(n as u64)
},
LitChar(n) =>
const_uint(n as u64),
LitBinary(ref data) =>
const_binary(Rc::new(data.iter().map(|x| *x).collect())),
LitStr(ref s, _) =>
const_str((*s).clone()),
LitFloat(ref n, _) | LitFloatUnsuffixed(ref n) =>
const_float(from_str::<f64>(n.get()).unwrap() as f64)
}
LitNil => const_nil,
LitBool(b) => const_bool(b)
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3208,13 +3208,6 @@ pub fn node_id_to_type(cx: &ctxt, id: ast::NodeId) -> t {
}
}

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

pub fn node_id_item_substs(cx: &ctxt, id: ast::NodeId) -> ItemSubsts {
match cx.item_substs.borrow().find(&id) {
None => ItemSubsts::empty(),
Expand Down Expand Up @@ -3338,7 +3331,7 @@ pub fn expr_ty(cx: &ctxt, expr: &ast::Expr) -> t {
}

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

pub fn expr_ty_adjusted(cx: &ctxt, expr: &ast::Expr) -> t {
Expand Down
26 changes: 26 additions & 0 deletions src/test/run-pass/issue-16745.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn is_whitespace(b: u8) -> bool {
match b {
b' ' => true,
b'\t' => true,
b'\n' => true,
0x0B => true,
0x0C => true,
b'\r' => true,
_ => false,
}
}

fn main() {
assert!(is_whitespace('\n' as u8));
assert!(!is_whitespace('f' as u8));
}
19 changes: 19 additions & 0 deletions src/test/run-pass/issue-17631.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn main() {
let (a, b) = (1u8, 2u8);

assert_eq!(match (a, b) {
(0x1b, 0x01) => 1u,
(_, 0x05u8) => 2u,
_ => 5u
}, 5u);
}