2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2014-03-21 20:05:05 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-10-27 17:37:07 -05:00
|
|
|
#![allow(unsigned_negation)]
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::const_val::*;
|
|
|
|
|
2013-03-21 02:27:26 -05:00
|
|
|
use metadata::csearch;
|
2014-11-23 05:14:35 -06:00
|
|
|
use middle::{astencode, def};
|
2014-07-13 08:12:47 -05:00
|
|
|
use middle::pat_util::def_to_path;
|
2015-01-29 05:40:14 -06:00
|
|
|
use middle::ty::{self, Ty};
|
2015-03-08 10:41:28 -05:00
|
|
|
use middle::astconv_util::ast_ty_to_prim_ty;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2015-01-03 21:42:21 -06:00
|
|
|
use syntax::ast::{self, Expr};
|
2015-02-05 11:26:58 -06:00
|
|
|
use syntax::codemap::Span;
|
2014-01-10 16:02:36 -06:00
|
|
|
use syntax::parse::token::InternedString;
|
2014-09-07 12:09:06 -05:00
|
|
|
use syntax::ptr::P;
|
2014-09-13 12:10:34 -05:00
|
|
|
use syntax::{ast_map, ast_util, codemap};
|
2012-07-30 21:05:56 -05:00
|
|
|
|
2015-02-22 09:34:26 -06:00
|
|
|
use std::borrow::{Cow, IntoCow};
|
2015-02-20 17:35:20 -06:00
|
|
|
use std::num::wrapping::OverflowingOps;
|
2015-01-29 05:40:14 -06:00
|
|
|
use std::cmp::Ordering;
|
2014-12-13 10:15:18 -06:00
|
|
|
use std::collections::hash_map::Entry::Vacant;
|
2015-01-29 05:43:55 -06:00
|
|
|
use std::{i8, i16, i32, i64};
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::rc::Rc;
|
2013-03-21 02:27:26 -05:00
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
|
2015-02-16 22:44:23 -06:00
|
|
|
let opt_def = tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
|
2013-12-23 13:15:16 -06:00
|
|
|
match opt_def {
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
Some(def::DefConst(def_id)) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
lookup_const_by_id(tcx, def_id)
|
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
Some(def::DefVariant(enum_def, variant_def, _)) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
lookup_variant_by_id(tcx, enum_def, variant_def)
|
|
|
|
}
|
2012-11-13 00:10:15 -06:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
|
2013-09-01 20:45:37 -05:00
|
|
|
enum_def: ast::DefId,
|
|
|
|
variant_def: ast::DefId)
|
2014-09-07 12:09:06 -05:00
|
|
|
-> Option<&'a Expr> {
|
|
|
|
fn variant_expr<'a>(variants: &'a [P<ast::Variant>], id: ast::NodeId)
|
|
|
|
-> Option<&'a Expr> {
|
2015-01-31 11:20:46 -06:00
|
|
|
for variant in variants {
|
2013-07-16 03:27:54 -05:00
|
|
|
if variant.node.id == id {
|
2014-09-07 12:09:06 -05:00
|
|
|
return variant.node.disr_expr.as_ref().map(|e| &**e);
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
if ast_util::is_local(enum_def) {
|
2014-09-07 12:09:06 -05:00
|
|
|
match tcx.map.find(enum_def.node) {
|
|
|
|
None => None,
|
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ItemEnum(ast::EnumDef { ref variants }, _) => {
|
2015-02-18 13:48:57 -06:00
|
|
|
variant_expr(&variants[..], variant_def.node)
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
2014-11-06 11:25:16 -06:00
|
|
|
match tcx.extern_const_variants.borrow().get(&variant_def) {
|
2014-09-07 12:09:06 -05:00
|
|
|
Some(&ast::DUMMY_NODE_ID) => return None,
|
|
|
|
Some(&expr_id) => {
|
|
|
|
return Some(tcx.map.expect_expr(expr_id));
|
|
|
|
}
|
2014-03-15 15:29:34 -05:00
|
|
|
None => {}
|
2013-10-04 17:04:11 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
let expr_id = match csearch::maybe_get_item_ast(tcx, enum_def,
|
2015-02-15 02:52:21 -06:00
|
|
|
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ItemEnum(ast::EnumDef { ref variants }, _) => {
|
2014-09-07 12:09:06 -05:00
|
|
|
// NOTE this doesn't do the right thing, it compares inlined
|
|
|
|
// NodeId's to the original variant_def's NodeId, but they
|
|
|
|
// come from different crates, so they will likely never match.
|
2015-02-18 13:48:57 -06:00
|
|
|
variant_expr(&variants[..], variant_def.node).map(|e| e.id)
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
_ => None
|
2013-10-04 17:04:11 -05:00
|
|
|
};
|
2014-09-07 12:09:06 -05:00
|
|
|
tcx.extern_const_variants.borrow_mut().insert(variant_def,
|
|
|
|
expr_id.unwrap_or(ast::DUMMY_NODE_ID));
|
|
|
|
expr_id.map(|id| tcx.map.expect_expr(id))
|
2013-07-16 03:27:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
pub fn lookup_const_by_id<'a>(tcx: &'a ty::ctxt, def_id: ast::DefId)
|
|
|
|
-> Option<&'a Expr> {
|
2012-11-13 00:10:15 -06:00
|
|
|
if ast_util::is_local(def_id) {
|
2014-09-07 12:09:06 -05:00
|
|
|
match tcx.map.find(def_id.node) {
|
|
|
|
None => None,
|
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ItemConst(_, ref const_expr) => {
|
2014-09-07 12:09:06 -05:00
|
|
|
Some(&**const_expr)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
2012-11-13 00:10:15 -06:00
|
|
|
} else {
|
2014-11-06 11:25:16 -06:00
|
|
|
match tcx.extern_const_statics.borrow().get(&def_id) {
|
2014-09-07 12:09:06 -05:00
|
|
|
Some(&ast::DUMMY_NODE_ID) => return None,
|
|
|
|
Some(&expr_id) => {
|
|
|
|
return Some(tcx.map.expect_expr(expr_id));
|
|
|
|
}
|
2014-03-20 21:49:20 -05:00
|
|
|
None => {}
|
2013-10-04 17:04:11 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
let expr_id = match csearch::maybe_get_item_ast(tcx, def_id,
|
2015-02-15 02:52:21 -06:00
|
|
|
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
|
2015-01-24 11:58:07 -06:00
|
|
|
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ItemConst(_, ref const_expr) => Some(const_expr.id),
|
2013-03-21 02:27:26 -05:00
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
_ => None
|
2013-10-04 17:04:11 -05:00
|
|
|
};
|
2014-09-07 12:09:06 -05:00
|
|
|
tcx.extern_const_statics.borrow_mut().insert(def_id,
|
|
|
|
expr_id.unwrap_or(ast::DUMMY_NODE_ID));
|
|
|
|
expr_id.map(|id| tcx.map.expect_expr(id))
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone, PartialEq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum const_val {
|
2012-06-04 19:26:17 -05:00
|
|
|
const_float(f64),
|
2012-03-22 16:56:56 -05:00
|
|
|
const_int(i64),
|
|
|
|
const_uint(u64),
|
2014-01-10 16:02:36 -06:00
|
|
|
const_str(InternedString),
|
2015-03-08 10:41:28 -05:00
|
|
|
const_binary(Rc<Vec<u8>>),
|
|
|
|
const_bool(bool),
|
|
|
|
Struct(ast::NodeId),
|
|
|
|
Tuple(ast::NodeId)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2015-02-05 11:26:58 -06:00
|
|
|
pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<ast::Pat> {
|
2014-07-13 08:12:47 -05:00
|
|
|
let pat = match expr.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprTup(ref exprs) =>
|
2015-02-05 11:26:58 -06:00
|
|
|
ast::PatTup(exprs.iter().map(|expr| const_expr_to_pat(tcx, &**expr, span)).collect()),
|
2014-07-13 08:12:47 -05:00
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprCall(ref callee, ref args) => {
|
2015-03-21 20:15:47 -05:00
|
|
|
let def = *tcx.def_map.borrow().get(&callee.id).unwrap();
|
2015-01-06 10:36:30 -06:00
|
|
|
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
|
2015-01-04 13:07:32 -06:00
|
|
|
entry.insert(def);
|
2014-11-29 15:41:21 -06:00
|
|
|
}
|
2015-02-16 22:44:23 -06:00
|
|
|
let path = match def.full_def() {
|
2014-07-13 08:12:47 -05:00
|
|
|
def::DefStruct(def_id) => def_to_path(tcx, def_id),
|
|
|
|
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
|
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2015-02-05 11:26:58 -06:00
|
|
|
let pats = args.iter().map(|expr| const_expr_to_pat(tcx, &**expr, span)).collect();
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatEnum(path, Some(pats))
|
2014-07-13 08:12:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprStruct(ref path, ref fields, None) => {
|
2014-10-05 19:36:53 -05:00
|
|
|
let field_pats = fields.iter().map(|field| codemap::Spanned {
|
|
|
|
span: codemap::DUMMY_SP,
|
2014-09-13 12:10:34 -05:00
|
|
|
node: ast::FieldPat {
|
2014-10-05 19:36:53 -05:00
|
|
|
ident: field.ident.node,
|
2015-02-05 11:26:58 -06:00
|
|
|
pat: const_expr_to_pat(tcx, &*field.expr, span),
|
2014-10-27 02:11:26 -05:00
|
|
|
is_shorthand: false,
|
2014-10-05 19:36:53 -05:00
|
|
|
},
|
2014-07-13 08:12:47 -05:00
|
|
|
}).collect();
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatStruct(path.clone(), field_pats, false)
|
2014-07-13 08:12:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprVec(ref exprs) => {
|
2015-02-05 11:26:58 -06:00
|
|
|
let pats = exprs.iter().map(|expr| const_expr_to_pat(tcx, &**expr, span)).collect();
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatVec(pats, None, vec![])
|
2014-07-13 08:12:47 -05:00
|
|
|
}
|
|
|
|
|
2015-02-17 11:29:13 -06:00
|
|
|
ast::ExprPath(_, ref path) => {
|
2015-02-16 22:44:23 -06:00
|
|
|
let opt_def = tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def());
|
2014-07-13 08:12:47 -05:00
|
|
|
match opt_def {
|
|
|
|
Some(def::DefStruct(..)) =>
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatStruct(path.clone(), vec![], false),
|
2014-07-13 08:12:47 -05:00
|
|
|
Some(def::DefVariant(..)) =>
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::PatEnum(path.clone(), None),
|
2014-07-13 08:12:47 -05:00
|
|
|
_ => {
|
2014-09-07 12:09:06 -05:00
|
|
|
match lookup_const(tcx, expr) {
|
2015-02-05 11:26:58 -06:00
|
|
|
Some(actual) => return const_expr_to_pat(tcx, actual, span),
|
2014-07-13 08:12:47 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 12:10:34 -05:00
|
|
|
_ => ast::PatLit(P(expr.clone()))
|
2014-07-13 08:12:47 -05:00
|
|
|
};
|
2015-02-05 11:26:58 -06:00
|
|
|
P(ast::Pat { id: expr.id, node: pat, span: span })
|
2014-07-13 08:12:47 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
pub fn eval_const_expr(tcx: &ty::ctxt, e: &Expr) -> const_val {
|
2015-01-29 05:40:14 -06:00
|
|
|
match eval_const_expr_partial(tcx, e, None) {
|
2013-06-12 12:02:55 -05:00
|
|
|
Ok(r) => r,
|
2015-03-07 20:08:48 -06:00
|
|
|
Err(s) => tcx.sess.span_fatal(s.span, &s.description())
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 09:34:26 -06:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ConstEvalErr {
|
|
|
|
pub span: Span,
|
|
|
|
pub kind: ErrKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum ErrKind {
|
|
|
|
CannotCast,
|
|
|
|
CannotCastTo(&'static str),
|
|
|
|
InvalidOpForBools(ast::BinOp_),
|
|
|
|
InvalidOpForFloats(ast::BinOp_),
|
|
|
|
InvalidOpForIntUint(ast::BinOp_),
|
|
|
|
InvalidOpForUintInt(ast::BinOp_),
|
|
|
|
NegateOnString,
|
|
|
|
NegateOnBoolean,
|
2015-03-01 05:29:46 -06:00
|
|
|
NegateOnBinary,
|
2015-03-08 10:41:28 -05:00
|
|
|
NegateOnStruct,
|
|
|
|
NegateOnTuple,
|
2015-02-22 09:34:26 -06:00
|
|
|
NotOnFloat,
|
|
|
|
NotOnString,
|
2015-03-01 05:29:46 -06:00
|
|
|
NotOnBinary,
|
2015-03-08 10:41:28 -05:00
|
|
|
NotOnStruct,
|
|
|
|
NotOnTuple,
|
2015-02-22 09:34:26 -06:00
|
|
|
|
|
|
|
AddiWithOverflow(i64, i64),
|
|
|
|
SubiWithOverflow(i64, i64),
|
|
|
|
MuliWithOverflow(i64, i64),
|
|
|
|
AdduWithOverflow(u64, u64),
|
|
|
|
SubuWithOverflow(u64, u64),
|
|
|
|
MuluWithOverflow(u64, u64),
|
|
|
|
DivideByZero,
|
|
|
|
DivideWithOverflow,
|
|
|
|
ModuloByZero,
|
|
|
|
ModuloWithOverflow,
|
|
|
|
MissingStructField,
|
|
|
|
NonConstPath,
|
2015-03-08 10:41:28 -05:00
|
|
|
ExpectedConstTuple,
|
|
|
|
ExpectedConstStruct,
|
2015-02-22 09:34:26 -06:00
|
|
|
TupleIndexOutOfBounds,
|
|
|
|
|
|
|
|
MiscBinaryOp,
|
|
|
|
MiscCatchAll,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConstEvalErr {
|
|
|
|
pub fn description(&self) -> Cow<str> {
|
|
|
|
use self::ErrKind::*;
|
|
|
|
match self.kind {
|
|
|
|
CannotCast => "can't cast this type".into_cow(),
|
|
|
|
CannotCastTo(s) => format!("can't cast this type to {}", s).into_cow(),
|
|
|
|
InvalidOpForBools(_) => "can't do this op on bools".into_cow(),
|
|
|
|
InvalidOpForFloats(_) => "can't do this op on floats".into_cow(),
|
|
|
|
InvalidOpForIntUint(..) => "can't do this op on an int and uint".into_cow(),
|
|
|
|
InvalidOpForUintInt(..) => "can't do this op on a uint and int".into_cow(),
|
|
|
|
NegateOnString => "negate on string".into_cow(),
|
|
|
|
NegateOnBoolean => "negate on boolean".into_cow(),
|
2015-03-01 05:29:46 -06:00
|
|
|
NegateOnBinary => "negate on binary literal".into_cow(),
|
2015-03-08 10:41:28 -05:00
|
|
|
NegateOnStruct => "negate on struct".into_cow(),
|
|
|
|
NegateOnTuple => "negate on tuple".into_cow(),
|
2015-02-22 09:34:26 -06:00
|
|
|
NotOnFloat => "not on float or string".into_cow(),
|
|
|
|
NotOnString => "not on float or string".into_cow(),
|
2015-03-01 05:29:46 -06:00
|
|
|
NotOnBinary => "not on binary literal".into_cow(),
|
2015-03-08 10:41:28 -05:00
|
|
|
NotOnStruct => "not on struct".into_cow(),
|
|
|
|
NotOnTuple => "not on tuple".into_cow(),
|
2015-02-22 09:34:26 -06:00
|
|
|
|
|
|
|
AddiWithOverflow(..) => "attempted to add with overflow".into_cow(),
|
|
|
|
SubiWithOverflow(..) => "attempted to sub with overflow".into_cow(),
|
|
|
|
MuliWithOverflow(..) => "attempted to mul with overflow".into_cow(),
|
|
|
|
AdduWithOverflow(..) => "attempted to add with overflow".into_cow(),
|
|
|
|
SubuWithOverflow(..) => "attempted to sub with overflow".into_cow(),
|
|
|
|
MuluWithOverflow(..) => "attempted to mul with overflow".into_cow(),
|
|
|
|
DivideByZero => "attempted to divide by zero".into_cow(),
|
|
|
|
DivideWithOverflow => "attempted to divide with overflow".into_cow(),
|
|
|
|
ModuloByZero => "attempted remainder with a divisor of zero".into_cow(),
|
|
|
|
ModuloWithOverflow => "attempted remainder with overflow".into_cow(),
|
|
|
|
MissingStructField => "nonexistent struct field".into_cow(),
|
|
|
|
NonConstPath => "non-constant path in constant expr".into_cow(),
|
2015-03-08 10:41:28 -05:00
|
|
|
ExpectedConstTuple => "expected constant tuple".into_cow(),
|
|
|
|
ExpectedConstStruct => "expected constant struct".into_cow(),
|
2015-02-22 09:34:26 -06:00
|
|
|
TupleIndexOutOfBounds => "tuple index out of bounds".into_cow(),
|
|
|
|
|
|
|
|
MiscBinaryOp => "bad operands for binary".into_cow(),
|
|
|
|
MiscCatchAll => "unsupported constant expr".into_cow(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 11:16:30 -06:00
|
|
|
macro_rules! signal {
|
|
|
|
($e:expr, $ctor:ident) => {
|
|
|
|
return Err(ConstEvalErr { span: $e.span, kind: ErrKind::$ctor })
|
|
|
|
};
|
2015-02-22 09:34:26 -06:00
|
|
|
|
2015-02-27 11:16:30 -06:00
|
|
|
($e:expr, $ctor:ident($($arg:expr),*)) => {
|
|
|
|
return Err(ConstEvalErr { span: $e.span, kind: ErrKind::$ctor($($arg),*) })
|
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn checked_add_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_add(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_int(ret)) } else { signal!(e, AddiWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
fn checked_sub_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_sub(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_int(ret)) } else { signal!(e, SubiWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
fn checked_mul_int(e: &Expr, a: i64, b: i64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_mul(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_int(ret)) } else { signal!(e, MuliWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
|
|
|
|
2015-02-22 09:34:26 -06:00
|
|
|
fn checked_add_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_add(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_uint(ret)) } else { signal!(e, AdduWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
fn checked_sub_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_sub(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_uint(ret)) } else { signal!(e, SubuWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
fn checked_mul_uint(e: &Expr, a: u64, b: u64) -> Result<const_val, ConstEvalErr> {
|
2015-02-20 17:35:20 -06:00
|
|
|
let (ret, oflo) = a.overflowing_mul(b);
|
2015-02-27 11:16:30 -06:00
|
|
|
if !oflo { Ok(const_uint(ret)) } else { signal!(e, MuluWithOverflow(a, b)) }
|
2015-02-20 17:35:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-29 05:40:14 -06:00
|
|
|
pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
e: &Expr,
|
|
|
|
ty_hint: Option<Ty<'tcx>>)
|
2015-02-22 09:34:26 -06:00
|
|
|
-> Result<const_val, ConstEvalErr> {
|
2015-03-01 05:29:46 -06:00
|
|
|
fn fromb(b: bool) -> const_val { const_int(b as i64) }
|
2015-01-29 05:40:14 -06:00
|
|
|
|
|
|
|
let ety = ty_hint.or_else(|| ty::expr_ty_opt(tcx, e));
|
|
|
|
|
2015-03-01 05:29:46 -06:00
|
|
|
let result = match e.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprUnary(ast::UnNeg, ref inner) => {
|
2015-03-01 05:29:46 -06:00
|
|
|
match try!(eval_const_expr_partial(tcx, &**inner, ety)) {
|
|
|
|
const_float(f) => const_float(-f),
|
|
|
|
const_int(i) => const_int(-i),
|
|
|
|
const_uint(i) => const_uint(-i),
|
|
|
|
const_str(_) => signal!(e, NegateOnString),
|
|
|
|
const_bool(_) => signal!(e, NegateOnBoolean),
|
|
|
|
const_binary(_) => signal!(e, NegateOnBinary),
|
2015-03-08 10:41:28 -05:00
|
|
|
const_val::Tuple(_) => signal!(e, NegateOnTuple),
|
|
|
|
const_val::Struct(..) => signal!(e, NegateOnStruct),
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprUnary(ast::UnNot, ref inner) => {
|
2015-03-01 05:29:46 -06:00
|
|
|
match try!(eval_const_expr_partial(tcx, &**inner, ety)) {
|
|
|
|
const_int(i) => const_int(!i),
|
|
|
|
const_uint(i) => const_uint(!i),
|
|
|
|
const_bool(b) => const_bool(!b),
|
|
|
|
const_str(_) => signal!(e, NotOnString),
|
|
|
|
const_float(_) => signal!(e, NotOnFloat),
|
|
|
|
const_binary(_) => signal!(e, NotOnBinary),
|
2015-03-08 10:41:28 -05:00
|
|
|
const_val::Tuple(_) => signal!(e, NotOnTuple),
|
|
|
|
const_val::Struct(..) => signal!(e, NotOnStruct),
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprBinary(op, ref a, ref b) => {
|
2015-01-29 05:40:14 -06:00
|
|
|
let b_ty = match op.node {
|
|
|
|
ast::BiShl | ast::BiShr => Some(tcx.types.uint),
|
|
|
|
_ => ety
|
|
|
|
};
|
2015-03-01 05:29:46 -06:00
|
|
|
match (try!(eval_const_expr_partial(tcx, &**a, ety)),
|
|
|
|
try!(eval_const_expr_partial(tcx, &**b, b_ty))) {
|
|
|
|
(const_float(a), const_float(b)) => {
|
2015-01-12 21:24:37 -06:00
|
|
|
match op.node {
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiAdd => const_float(a + b),
|
|
|
|
ast::BiSub => const_float(a - b),
|
|
|
|
ast::BiMul => const_float(a * b),
|
|
|
|
ast::BiDiv => const_float(a / b),
|
|
|
|
ast::BiRem => const_float(a % b),
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::BiEq => fromb(a == b),
|
|
|
|
ast::BiLt => fromb(a < b),
|
|
|
|
ast::BiLe => fromb(a <= b),
|
|
|
|
ast::BiNe => fromb(a != b),
|
|
|
|
ast::BiGe => fromb(a >= b),
|
|
|
|
ast::BiGt => fromb(a > b),
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, InvalidOpForFloats(op.node))
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
(const_int(a), const_int(b)) => {
|
2015-02-27 20:21:09 -06:00
|
|
|
let is_a_min_value = || {
|
2015-01-29 05:43:55 -06:00
|
|
|
let int_ty = match ty::expr_ty_opt(tcx, e).map(|ty| &ty.sty) {
|
|
|
|
Some(&ty::ty_int(int_ty)) => int_ty,
|
|
|
|
_ => return false
|
|
|
|
};
|
2015-03-24 15:26:16 -05:00
|
|
|
let int_ty = if let ast::TyIs = int_ty {
|
2015-01-29 05:43:55 -06:00
|
|
|
tcx.sess.target.int_type
|
|
|
|
} else {
|
|
|
|
int_ty
|
|
|
|
};
|
|
|
|
match int_ty {
|
|
|
|
ast::TyI8 => (a as i8) == i8::MIN,
|
|
|
|
ast::TyI16 => (a as i16) == i16::MIN,
|
|
|
|
ast::TyI32 => (a as i32) == i32::MIN,
|
|
|
|
ast::TyI64 => (a as i64) == i64::MIN,
|
2015-03-24 15:26:16 -05:00
|
|
|
ast::TyIs => unreachable!()
|
2015-01-29 05:43:55 -06:00
|
|
|
}
|
|
|
|
};
|
2015-01-12 21:24:37 -06:00
|
|
|
match op.node {
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiAdd => try!(checked_add_int(e, a, b)),
|
|
|
|
ast::BiSub => try!(checked_sub_int(e, a, b)),
|
|
|
|
ast::BiMul => try!(checked_mul_int(e, a, b)),
|
2015-01-29 05:43:55 -06:00
|
|
|
ast::BiDiv => {
|
|
|
|
if b == 0 {
|
2015-02-27 11:16:30 -06:00
|
|
|
signal!(e, DivideByZero);
|
2015-01-29 05:43:55 -06:00
|
|
|
} else if b == -1 && is_a_min_value() {
|
2015-02-27 11:16:30 -06:00
|
|
|
signal!(e, DivideWithOverflow);
|
2015-01-29 05:43:55 -06:00
|
|
|
} else {
|
2015-03-01 05:29:46 -06:00
|
|
|
const_int(a / b)
|
2015-01-29 05:43:55 -06:00
|
|
|
}
|
2014-05-09 20:45:36 -05:00
|
|
|
}
|
2015-01-29 05:43:55 -06:00
|
|
|
ast::BiRem => {
|
|
|
|
if b == 0 {
|
2015-02-27 11:16:30 -06:00
|
|
|
signal!(e, ModuloByZero)
|
2015-01-29 05:43:55 -06:00
|
|
|
} else if b == -1 && is_a_min_value() {
|
2015-02-27 11:16:30 -06:00
|
|
|
signal!(e, ModuloWithOverflow)
|
2015-01-29 05:43:55 -06:00
|
|
|
} else {
|
2015-03-01 05:29:46 -06:00
|
|
|
const_int(a % b)
|
2015-01-29 05:43:55 -06:00
|
|
|
}
|
2014-05-09 20:45:36 -05:00
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiAnd | ast::BiBitAnd => const_int(a & b),
|
|
|
|
ast::BiOr | ast::BiBitOr => const_int(a | b),
|
|
|
|
ast::BiBitXor => const_int(a ^ b),
|
|
|
|
ast::BiShl => const_int(a << b as uint),
|
|
|
|
ast::BiShr => const_int(a >> b as uint),
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::BiEq => fromb(a == b),
|
|
|
|
ast::BiLt => fromb(a < b),
|
|
|
|
ast::BiLe => fromb(a <= b),
|
|
|
|
ast::BiNe => fromb(a != b),
|
|
|
|
ast::BiGe => fromb(a >= b),
|
|
|
|
ast::BiGt => fromb(a > b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
(const_uint(a), const_uint(b)) => {
|
2015-01-12 21:24:37 -06:00
|
|
|
match op.node {
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiAdd => try!(checked_add_uint(e, a, b)),
|
|
|
|
ast::BiSub => try!(checked_sub_uint(e, a, b)),
|
|
|
|
ast::BiMul => try!(checked_mul_uint(e, a, b)),
|
2015-02-27 11:16:30 -06:00
|
|
|
ast::BiDiv if b == 0 => signal!(e, DivideByZero),
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiDiv => const_uint(a / b),
|
2015-02-27 11:16:30 -06:00
|
|
|
ast::BiRem if b == 0 => signal!(e, ModuloByZero),
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiRem => const_uint(a % b),
|
|
|
|
ast::BiAnd | ast::BiBitAnd => const_uint(a & b),
|
|
|
|
ast::BiOr | ast::BiBitOr => const_uint(a | b),
|
|
|
|
ast::BiBitXor => const_uint(a ^ b),
|
|
|
|
ast::BiShl => const_uint(a << b as uint),
|
|
|
|
ast::BiShr => const_uint(a >> b as uint),
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::BiEq => fromb(a == b),
|
|
|
|
ast::BiLt => fromb(a < b),
|
|
|
|
ast::BiLe => fromb(a <= b),
|
|
|
|
ast::BiNe => fromb(a != b),
|
|
|
|
ast::BiGe => fromb(a >= b),
|
|
|
|
ast::BiGt => fromb(a > b),
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-30 17:03:24 -05:00
|
|
|
// shifts can have any integral type as their rhs
|
2015-03-01 05:29:46 -06:00
|
|
|
(const_int(a), const_uint(b)) => {
|
2015-01-12 21:24:37 -06:00
|
|
|
match op.node {
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiShl => const_int(a << b as uint),
|
|
|
|
ast::BiShr => const_int(a >> b as uint),
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, InvalidOpForIntUint(op.node)),
|
2012-05-30 17:03:24 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
(const_uint(a), const_int(b)) => {
|
2015-01-12 21:24:37 -06:00
|
|
|
match op.node {
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::BiShl => const_uint(a << b as uint),
|
|
|
|
ast::BiShr => const_uint(a >> b as uint),
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, InvalidOpForUintInt(op.node)),
|
2012-05-30 17:03:24 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
(const_bool(a), const_bool(b)) => {
|
|
|
|
const_bool(match op.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::BiAnd => a && b,
|
|
|
|
ast::BiOr => a || b,
|
|
|
|
ast::BiBitXor => a ^ b,
|
|
|
|
ast::BiBitAnd => a & b,
|
|
|
|
ast::BiBitOr => a | b,
|
|
|
|
ast::BiEq => a == b,
|
|
|
|
ast::BiNe => a != b,
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, InvalidOpForBools(op.node)),
|
2015-03-01 05:29:46 -06:00
|
|
|
})
|
2012-08-23 17:51:23 -05:00
|
|
|
}
|
2015-02-22 09:34:26 -06:00
|
|
|
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, MiscBinaryOp),
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprCast(ref base, ref target_ty) => {
|
2014-01-28 23:05:11 -06:00
|
|
|
// 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.
|
2015-01-29 05:40:14 -06:00
|
|
|
let ety = ety.or_else(|| ast_ty_to_prim_ty(tcx, &**target_ty))
|
2014-05-16 12:45:16 -05:00
|
|
|
.unwrap_or_else(|| {
|
2014-04-22 07:56:37 -05:00
|
|
|
tcx.sess.span_fatal(target_ty.span,
|
|
|
|
"target type not found for const cast")
|
2014-05-16 12:45:16 -05:00
|
|
|
});
|
2015-01-29 05:40:14 -06:00
|
|
|
// Prefer known type to noop, but always have a type hint.
|
|
|
|
let base_hint = ty::expr_ty_opt(tcx, &**base).unwrap_or(ety);
|
|
|
|
let val = try!(eval_const_expr_partial(tcx, &**base, Some(base_hint)));
|
2015-02-22 09:34:26 -06:00
|
|
|
match cast_const(val, ety) {
|
2015-03-01 05:29:46 -06:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(kind) => return Err(ConstEvalErr { span: e.span, kind: kind }),
|
2015-02-22 09:34:26 -06:00
|
|
|
}
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
2015-02-17 11:29:13 -06:00
|
|
|
ast::ExprPath(..) => {
|
2015-02-16 22:44:23 -06:00
|
|
|
let opt_def = tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
|
2015-01-29 05:40:14 -06:00
|
|
|
let (const_expr, const_ty) = match opt_def {
|
|
|
|
Some(def::DefConst(def_id)) => {
|
|
|
|
if ast_util::is_local(def_id) {
|
|
|
|
match tcx.map.find(def_id.node) {
|
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
|
|
|
ast::ItemConst(ref ty, ref expr) => {
|
|
|
|
(Some(&**expr), Some(&**ty))
|
|
|
|
}
|
|
|
|
_ => (None, None)
|
|
|
|
},
|
|
|
|
_ => (None, None)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(lookup_const_by_id(tcx, def_id), None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(def::DefVariant(enum_def, variant_def, _)) => {
|
|
|
|
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
|
|
|
|
}
|
|
|
|
_ => (None, None)
|
|
|
|
};
|
|
|
|
let const_expr = match const_expr {
|
|
|
|
Some(actual_e) => actual_e,
|
2015-02-27 11:16:30 -06:00
|
|
|
None => signal!(e, NonConstPath)
|
2015-01-29 05:40:14 -06:00
|
|
|
};
|
|
|
|
let ety = ety.or_else(|| const_ty.and_then(|ty| ast_ty_to_prim_ty(tcx, ty)));
|
2015-03-01 05:29:46 -06:00
|
|
|
try!(eval_const_expr_partial(tcx, const_expr, ety))
|
2012-10-15 14:27:09 -05:00
|
|
|
}
|
2015-01-29 05:40:14 -06:00
|
|
|
ast::ExprLit(ref lit) => {
|
2015-03-01 05:29:46 -06:00
|
|
|
lit_to_const(&**lit, ety)
|
2015-01-29 05:40:14 -06:00
|
|
|
}
|
2015-03-01 05:29:46 -06:00
|
|
|
ast::ExprParen(ref e) => try!(eval_const_expr_partial(tcx, &**e, ety)),
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::ExprBlock(ref block) => {
|
2014-05-04 03:39:11 -05:00
|
|
|
match block.expr {
|
2015-03-01 05:29:46 -06:00
|
|
|
Some(ref expr) => try!(eval_const_expr_partial(tcx, &**expr, ety)),
|
2015-03-03 02:42:26 -06:00
|
|
|
None => const_int(0)
|
2014-05-04 03:39:11 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-08 10:41:28 -05:00
|
|
|
ast::ExprTup(_) => {
|
|
|
|
const_val::Tuple(e.id)
|
|
|
|
}
|
|
|
|
ast::ExprStruct(..) => {
|
|
|
|
const_val::Struct(e.id)
|
|
|
|
}
|
2014-11-24 05:46:02 -06:00
|
|
|
ast::ExprTupField(ref base, index) => {
|
2015-03-08 10:41:28 -05:00
|
|
|
if let Ok(c) = eval_const_expr_partial(tcx, base, None) {
|
|
|
|
if let const_val::Tuple(tup_id) = c {
|
|
|
|
if let ast::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
|
|
|
|
if index.node < fields.len() {
|
|
|
|
return eval_const_expr_partial(tcx, &fields[index.node], None)
|
|
|
|
} else {
|
|
|
|
signal!(e, TupleIndexOutOfBounds);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2014-11-24 05:46:02 -06:00
|
|
|
} else {
|
2015-03-08 10:41:28 -05:00
|
|
|
signal!(base, ExpectedConstTuple);
|
2014-11-24 05:46:02 -06:00
|
|
|
}
|
2015-03-08 10:41:28 -05:00
|
|
|
} else {
|
|
|
|
signal!(base, NonConstPath)
|
2014-11-24 05:46:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::ExprField(ref base, field_name) => {
|
|
|
|
// Get the base expression if it is a struct and it is constant
|
2015-03-08 10:41:28 -05:00
|
|
|
if let Ok(c) = eval_const_expr_partial(tcx, base, None) {
|
|
|
|
if let const_val::Struct(struct_id) = c {
|
|
|
|
if let ast::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
|
|
|
|
// Check that the given field exists and evaluate it
|
|
|
|
if let Some(f) = fields.iter().find(|f| f.ident.node.as_str()
|
|
|
|
== field_name.node.as_str()) {
|
|
|
|
return eval_const_expr_partial(tcx, &*f.expr, None)
|
|
|
|
} else {
|
|
|
|
signal!(e, MissingStructField);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2014-11-24 05:46:02 -06:00
|
|
|
} else {
|
2015-03-08 10:41:28 -05:00
|
|
|
signal!(base, ExpectedConstStruct);
|
2014-11-24 05:46:02 -06:00
|
|
|
}
|
2015-03-08 10:41:28 -05:00
|
|
|
} else {
|
|
|
|
signal!(base, NonConstPath);
|
2014-11-24 05:46:02 -06:00
|
|
|
}
|
|
|
|
}
|
2015-02-27 11:16:30 -06:00
|
|
|
_ => signal!(e, MiscCatchAll)
|
2015-03-01 05:29:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2015-02-22 09:34:26 -06:00
|
|
|
fn cast_const(val: const_val, ty: Ty) -> Result<const_val, ErrKind> {
|
2015-01-29 05:40:14 -06:00
|
|
|
macro_rules! define_casts {
|
|
|
|
($($ty_pat:pat => (
|
|
|
|
$intermediate_ty:ty,
|
|
|
|
$const_type:ident,
|
|
|
|
$target_ty:ty
|
|
|
|
)),*) => (match ty.sty {
|
|
|
|
$($ty_pat => {
|
|
|
|
match val {
|
|
|
|
const_bool(b) => Ok($const_type(b as $intermediate_ty as $target_ty)),
|
|
|
|
const_uint(u) => Ok($const_type(u as $intermediate_ty as $target_ty)),
|
|
|
|
const_int(i) => Ok($const_type(i as $intermediate_ty as $target_ty)),
|
|
|
|
const_float(f) => Ok($const_type(f as $intermediate_ty as $target_ty)),
|
2015-02-22 09:34:26 -06:00
|
|
|
_ => Err(ErrKind::CannotCastTo(stringify!($const_type))),
|
2015-01-29 05:40:14 -06:00
|
|
|
}
|
|
|
|
},)*
|
2015-02-22 09:34:26 -06:00
|
|
|
_ => Err(ErrKind::CannotCast),
|
2015-01-29 05:40:14 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
define_casts!{
|
2015-03-24 15:26:16 -05:00
|
|
|
ty::ty_int(ast::TyIs) => (int, const_int, i64),
|
2015-01-29 05:40:14 -06:00
|
|
|
ty::ty_int(ast::TyI8) => (i8, const_int, i64),
|
|
|
|
ty::ty_int(ast::TyI16) => (i16, const_int, i64),
|
|
|
|
ty::ty_int(ast::TyI32) => (i32, const_int, i64),
|
|
|
|
ty::ty_int(ast::TyI64) => (i64, const_int, i64),
|
2015-03-24 15:26:16 -05:00
|
|
|
ty::ty_uint(ast::TyUs) => (uint, const_uint, u64),
|
2015-01-29 05:40:14 -06:00
|
|
|
ty::ty_uint(ast::TyU8) => (u8, const_uint, u64),
|
|
|
|
ty::ty_uint(ast::TyU16) => (u16, const_uint, u64),
|
|
|
|
ty::ty_uint(ast::TyU32) => (u32, const_uint, u64),
|
|
|
|
ty::ty_uint(ast::TyU64) => (u64, const_uint, u64),
|
|
|
|
ty::ty_float(ast::TyF32) => (f32, const_float, f64),
|
|
|
|
ty::ty_float(ast::TyF64) => (f64, const_float, f64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> const_val {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lit.node {
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::LitStr(ref s, _) => const_str((*s).clone()),
|
|
|
|
ast::LitBinary(ref data) => {
|
2015-02-13 01:33:44 -06:00
|
|
|
const_binary(data.clone())
|
2014-02-28 17:25:15 -06:00
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::LitByte(n) => const_uint(n as u64),
|
|
|
|
ast::LitChar(n) => const_uint(n as u64),
|
2015-01-29 05:40:14 -06:00
|
|
|
ast::LitInt(n, ast::SignedIntLit(_, ast::Plus)) => const_int(n as i64),
|
|
|
|
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Plus)) => {
|
|
|
|
match ty_hint.map(|ty| &ty.sty) {
|
|
|
|
Some(&ty::ty_uint(_)) => const_uint(n),
|
|
|
|
_ => const_int(n as i64)
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::LitInt(n, ast::SignedIntLit(_, ast::Minus)) |
|
|
|
|
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Minus)) => const_int(-(n as i64)),
|
|
|
|
ast::LitInt(n, ast::UnsignedIntLit(_)) => const_uint(n),
|
|
|
|
ast::LitFloat(ref n, _) |
|
|
|
|
ast::LitFloatUnsuffixed(ref n) => {
|
2015-02-03 17:03:39 -06:00
|
|
|
const_float(n.parse::<f64>().unwrap() as f64)
|
2014-01-15 19:15:39 -06:00
|
|
|
}
|
2014-09-13 12:10:34 -05:00
|
|
|
ast::LitBool(b) => const_bool(b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 05:40:14 -06:00
|
|
|
pub fn compare_const_vals(a: &const_val, b: &const_val) -> Option<Ordering> {
|
|
|
|
Some(match (a, b) {
|
|
|
|
(&const_int(a), &const_int(b)) => a.cmp(&b),
|
|
|
|
(&const_uint(a), &const_uint(b)) => a.cmp(&b),
|
|
|
|
(&const_float(a), &const_float(b)) => {
|
|
|
|
// This is pretty bad but it is the existing behavior.
|
|
|
|
if a == b {
|
|
|
|
Ordering::Equal
|
|
|
|
} else if a < b {
|
|
|
|
Ordering::Less
|
|
|
|
} else {
|
|
|
|
Ordering::Greater
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(&const_str(ref a), &const_str(ref b)) => a.cmp(b),
|
|
|
|
(&const_bool(a), &const_bool(b)) => a.cmp(&b),
|
|
|
|
(&const_binary(ref a), &const_binary(ref b)) => a.cmp(b),
|
|
|
|
_ => return None
|
|
|
|
})
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|
|
|
|
|
2015-01-29 05:40:14 -06:00
|
|
|
pub fn compare_lit_exprs<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
a: &Expr,
|
|
|
|
b: &Expr,
|
|
|
|
ty_hint: Option<Ty<'tcx>>)
|
|
|
|
-> Option<Ordering> {
|
|
|
|
let a = match eval_const_expr_partial(tcx, a, ty_hint) {
|
|
|
|
Ok(a) => a,
|
2015-02-22 09:34:26 -06:00
|
|
|
Err(e) => {
|
2015-03-07 20:08:48 -06:00
|
|
|
tcx.sess.span_err(a.span, &e.description());
|
2015-01-29 05:40:14 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let b = match eval_const_expr_partial(tcx, b, ty_hint) {
|
|
|
|
Ok(b) => b,
|
2015-02-22 09:34:26 -06:00
|
|
|
Err(e) => {
|
2015-03-07 20:08:48 -06:00
|
|
|
tcx.sess.span_err(b.span, &e.description());
|
2015-01-29 05:40:14 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
compare_const_vals(&a, &b)
|
2012-03-22 16:56:56 -05:00
|
|
|
}
|