// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//#![allow(non_camel_case_types)]
-
use rustc::middle::const_val::ConstVal::*;
use rustc::middle::const_val::ConstVal;
use self::ErrKind::*;
use self::EvalHint::*;
-use rustc::hir::map as ast_map;
+use rustc::hir::map as hir_map;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::traits;
use rustc::hir::def::Def;
fn lookup_variant_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
variant_def: DefId)
- -> Option<(&'tcx Expr, Option<&'a ty::Tables<'tcx>>)> {
- if let Some(variant_node_id) = tcx.map.as_local_node_id(variant_def) {
- let enum_node_id = tcx.map.get_parent(variant_node_id);
- if let Some(ast_map::NodeItem(it)) = tcx.map.find(enum_node_id) {
+ -> Option<(&'tcx Expr, Option<&'a ty::TypeckTables<'tcx>>)> {
+ if let Some(variant_node_id) = tcx.hir.as_local_node_id(variant_def) {
+ let enum_node_id = tcx.hir.get_parent(variant_node_id);
+ if let Some(hir_map::NodeItem(it)) = tcx.hir.find(enum_node_id) {
if let hir::ItemEnum(ref edef, _) = it.node {
for variant in &edef.variants {
if variant.node.data.id() == variant_node_id {
return variant.node.disr_expr.map(|e| {
- let def_id = tcx.map.body_owner_def_id(e);
- (&tcx.map.body(e).value,
+ let def_id = tcx.hir.body_owner_def_id(e);
+ (&tcx.hir.body(e).value,
tcx.tables.borrow().get(&def_id).cloned())
});
}
def_id: DefId,
substs: Option<&'tcx Substs<'tcx>>)
-> Option<(&'tcx Expr,
- Option<&'a ty::Tables<'tcx>>,
+ Option<&'a ty::TypeckTables<'tcx>>,
Option<ty::Ty<'tcx>>)> {
- if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
- match tcx.map.find(node_id) {
+ if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
+ match tcx.hir.find(node_id) {
None => None,
- Some(ast_map::NodeItem(&hir::Item {
+ Some(hir_map::NodeItem(&hir::Item {
node: hir::ItemConst(ref ty, body), ..
})) |
- Some(ast_map::NodeImplItem(&hir::ImplItem {
+ Some(hir_map::NodeImplItem(&hir::ImplItem {
node: hir::ImplItemKind::Const(ref ty, body), ..
})) => {
- Some((&tcx.map.body(body).value,
+ Some((&tcx.hir.body(body).value,
tcx.tables.borrow().get(&def_id).cloned(),
tcx.ast_ty_to_prim_ty(ty)))
}
- Some(ast_map::NodeTraitItem(ti)) => match ti.node {
+ Some(hir_map::NodeTraitItem(ti)) => match ti.node {
hir::TraitItemKind::Const(ref ty, default) => {
if let Some(substs) = substs {
// If we have a trait item and the substitutions for it,
// `resolve_trait_associated_const` will select an impl
// or the default.
- let trait_id = tcx.map.get_parent(node_id);
- let trait_id = tcx.map.local_def_id(trait_id);
+ let trait_id = tcx.hir.get_parent(node_id);
+ let trait_id = tcx.hir.local_def_id(trait_id);
let default_value = default.map(|body| {
- (&tcx.map.body(body).value,
+ (&tcx.hir.body(body).value,
tcx.tables.borrow().get(&def_id).cloned(),
tcx.ast_ty_to_prim_ty(ty))
});
}
fn lookup_const_fn_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
- -> Option<(&'tcx hir::Body, Option<&'a ty::Tables<'tcx>>)>
+ -> Option<(&'tcx hir::Body, Option<&'a ty::TypeckTables<'tcx>>)>
{
- if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
- FnLikeNode::from_node(tcx.map.get(node_id)).and_then(|fn_like| {
+ if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
+ FnLikeNode::from_node(tcx.hir.get(node_id)).and_then(|fn_like| {
if fn_like.constness() == hir::Constness::Const {
- Some((tcx.map.body(fn_like.body()),
+ Some((tcx.hir.body(fn_like.body()),
tcx.tables.borrow().get(&def_id).cloned()))
} else {
None
pub struct ConstContext<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
- tables: Option<&'a ty::Tables<'tcx>>,
+ tables: Option<&'a ty::TypeckTables<'tcx>>,
fn_args: Option<DefIdMap<ConstVal>>
}
impl<'a, 'tcx> ConstContext<'a, 'tcx> {
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, body: hir::BodyId) -> Self {
- let def_id = tcx.map.body_owner_def_id(body);
+ let def_id = tcx.hir.body_owner_def_id(body);
ConstContext {
tcx: tcx,
tables: tcx.tables.borrow().get(&def_id).cloned(),
}
}
- pub fn with_tables(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &'a ty::Tables<'tcx>) -> Self {
+ pub fn with_tables(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &'a ty::TypeckTables<'tcx>) -> Self {
ConstContext {
tcx: tcx,
tables: Some(tables),
if let hir::ExprLit(ref lit) = inner.node {
use syntax::ast::*;
use syntax::ast::LitIntType::*;
- const I8_OVERFLOW: u128 = i8::max_value() as u128 + 1;
- const I16_OVERFLOW: u128 = i16::max_value() as u128 + 1;
- const I32_OVERFLOW: u128 = i32::max_value() as u128 + 1;
- const I64_OVERFLOW: u128 = i64::max_value() as u128 + 1;
- const I128_OVERFLOW: u128 = i128::max_value() as u128 + 1;
+ const I8_OVERFLOW: u128 = i8::min_value() as u8 as u128;
+ const I16_OVERFLOW: u128 = i16::min_value() as u16 as u128;
+ const I32_OVERFLOW: u128 = i32::min_value() as u32 as u128;
+ const I64_OVERFLOW: u128 = i64::min_value() as u64 as u128;
+ const I128_OVERFLOW: u128 = i128::min_value() as u128;
match (&lit.node, ety.map(|t| &t.sty)) {
- (&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
+ (&LitKind::Int(I8_OVERFLOW, _), Some(&ty::TyInt(IntTy::I8))) |
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
return Ok(Integral(I8(i8::min_value())))
},
- (&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
+ (&LitKind::Int(I16_OVERFLOW, _), Some(&ty::TyInt(IntTy::I16))) |
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
return Ok(Integral(I16(i16::min_value())))
},
- (&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
+ (&LitKind::Int(I32_OVERFLOW, _), Some(&ty::TyInt(IntTy::I32))) |
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
return Ok(Integral(I32(i32::min_value())))
},
- (&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
+ (&LitKind::Int(I64_OVERFLOW, _), Some(&ty::TyInt(IntTy::I64))) |
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
return Ok(Integral(I64(i64::min_value())))
},
- (&LitKind::Int(I128_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I128))) |
- (&LitKind::Int(I128_OVERFLOW, Signed(IntTy::I128)), _) => {
- return Ok(Integral(I128(i128::min_value())))
+ (&LitKind::Int(n, _), Some(&ty::TyInt(IntTy::I128))) |
+ (&LitKind::Int(n, Signed(IntTy::I128)), _) => {
+ // SNAP: replace n in pattern with I128_OVERFLOW and remove this if.
+ if n == I128_OVERFLOW {
+ return Ok(Integral(I128(i128::min_value())))
+ }
},
- (&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
+ (&LitKind::Int(n, _), Some(&ty::TyInt(IntTy::Is))) |
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
match tcx.sess.target.int_type {
IntTy::I16 => if n == I16_OVERFLOW {
_ => bug!()
}
} else {
- let n = &tcx.map.body(count).value;
+ let n = &tcx.hir.body(count).value;
match ConstContext::new(tcx, count).eval(n, len_hint)? {
Integral(Usize(i)) => i.as_u64(tcx.sess.target.uint_type),
Integral(_) => signal!(e, RepeatCountNotNatural),
fn resolve_trait_associated_const<'a, 'tcx: 'a>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_item_id: DefId,
- default_value: Option<(&'tcx Expr, Option<&'a ty::Tables<'tcx>>, Option<ty::Ty<'tcx>>)>,
+ default_value: Option<(&'tcx Expr, Option<&'a ty::TypeckTables<'tcx>>, Option<ty::Ty<'tcx>>)>,
trait_id: DefId,
rcvr_substs: &'tcx Substs<'tcx>
-) -> Option<(&'tcx Expr, Option<&'a ty::Tables<'tcx>>, Option<ty::Ty<'tcx>>)>
+) -> Option<(&'tcx Expr, Option<&'a ty::TypeckTables<'tcx>>, Option<ty::Ty<'tcx>>)>
{
let trait_ref = ty::Binder(ty::TraitRef::new(trait_id, rcvr_substs));
debug!("resolve_trait_associated_const: trait_ref={:?}",
-> Result<usize, ErrorReported>
{
let hint = UncheckedExprHint(tcx.types.usize);
- let count_expr = &tcx.map.body(count).value;
+ let count_expr = &tcx.hir.body(count).value;
match ConstContext::new(tcx, count).eval(count_expr, hint) {
Ok(Integral(Usize(count))) => {
let val = count.as_u64(tcx.sess.target.uint_type);
if let hir::ExprPath(hir::QPath::Resolved(None, ref path)) = count_expr.node {
if let Def::Local(..) = path.def {
diag.note(&format!("`{}` is a variable",
- tcx.map.node_to_pretty_string(count_expr.id)));
+ tcx.hir.node_to_pretty_string(count_expr.id)));
}
}