diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index ad0040608ece8..c5fe6cdade6de 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -322,10 +322,9 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P { 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), @@ -560,7 +559,7 @@ pub fn eval_const_expr_partial(tcx: &ty::ctxt, e: &Expr) -> Result 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 { @@ -572,25 +571,28 @@ pub fn eval_const_expr_partial(tcx: &ty::ctxt, e: &Expr) -> Result 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::(n.get()).unwrap() as f64) - } - LitNil => const_nil, - LitBool(b) => const_bool(b) } } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index b9af31665a113..97f28c8e9c312 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -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 { - 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(), @@ -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 { - 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 { diff --git a/src/test/run-pass/issue-16745.rs b/src/test/run-pass/issue-16745.rs new file mode 100644 index 0000000000000..862e859233376 --- /dev/null +++ b/src/test/run-pass/issue-16745.rs @@ -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 or the MIT license +// , 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)); +} diff --git a/src/test/run-pass/issue-17631.rs b/src/test/run-pass/issue-17631.rs new file mode 100644 index 0000000000000..cff440d86d8ff --- /dev/null +++ b/src/test/run-pass/issue-17631.rs @@ -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 or the MIT license +// , 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); +}