2016-03-30 13:43:36 +02:00
|
|
|
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2015-08-26 12:00:14 +02:00
|
|
|
//#![allow(non_camel_case_types)]
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2016-03-30 13:43:36 +02:00
|
|
|
use rustc::middle::const_val::ConstVal::*;
|
|
|
|
use rustc::middle::const_val::ConstVal;
|
2015-03-25 11:10:09 +01:00
|
|
|
use self::ErrKind::*;
|
2015-06-30 08:53:50 -07:00
|
|
|
use self::EvalHint::*;
|
2015-03-25 11:10:09 +01:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::map as ast_map;
|
|
|
|
use rustc::hir::map::blocks::FnLikeNode;
|
2016-07-26 10:58:35 -04:00
|
|
|
use rustc::middle::cstore::InlinedItem;
|
2016-03-11 02:31:38 +02:00
|
|
|
use rustc::traits;
|
2016-06-03 23:15:00 +03:00
|
|
|
use rustc::hir::def::{Def, PathResolution};
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
|
|
|
use rustc::hir::pat_util::def_to_path;
|
2016-08-08 23:39:49 +03:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2016-03-30 13:43:36 +02:00
|
|
|
use rustc::ty::util::IntTypeExt;
|
2016-08-08 23:39:49 +03:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-06-30 21:22:47 +03:00
|
|
|
use rustc::traits::Reveal;
|
2016-07-20 00:02:56 +03:00
|
|
|
use rustc::util::common::ErrorReported;
|
2016-03-30 13:43:36 +02:00
|
|
|
use rustc::util::nodemap::NodeMap;
|
|
|
|
use rustc::lint;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2016-01-15 10:07:52 -08:00
|
|
|
use graphviz::IntoCow;
|
2016-01-14 15:03:48 +01:00
|
|
|
use syntax::ast;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::{Expr, PatKind};
|
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::intravisit::FnKind;
|
2014-09-07 20:09:06 +03:00
|
|
|
use syntax::ptr::P;
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::codemap;
|
2016-02-25 10:13:36 +01:00
|
|
|
use syntax::attr::IntType;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{self, Span};
|
2012-07-30 19:05:56 -07:00
|
|
|
|
2016-01-15 10:07:52 -08:00
|
|
|
use std::borrow::Cow;
|
2015-01-29 13:40:14 +02:00
|
|
|
use std::cmp::Ordering;
|
2014-12-13 11:15:18 -05:00
|
|
|
use std::collections::hash_map::Entry::Vacant;
|
2013-03-21 00:27:26 -07:00
|
|
|
|
2016-03-15 12:33:13 +01:00
|
|
|
use rustc_const_math::*;
|
2016-08-05 15:58:31 -07:00
|
|
|
use rustc_errors::DiagnosticBuilder;
|
2015-12-16 18:44:15 +01:00
|
|
|
|
|
|
|
macro_rules! math {
|
|
|
|
($e:expr, $op:expr) => {
|
|
|
|
match $op {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => signal!($e, Math(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn lookup_variant_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
enum_def: DefId,
|
|
|
|
variant_def: DefId)
|
|
|
|
-> Option<&'tcx Expr> {
|
2015-12-07 17:17:41 +03:00
|
|
|
fn variant_expr<'a>(variants: &'a [hir::Variant], id: ast::NodeId)
|
2014-09-07 20:09:06 +03:00
|
|
|
-> Option<&'a Expr> {
|
2015-01-31 12:20:46 -05:00
|
|
|
for variant in variants {
|
2015-10-10 03:28:40 +03:00
|
|
|
if variant.node.data.id() == id {
|
2014-09-07 20:09:06 +03:00
|
|
|
return variant.node.disr_expr.as_ref().map(|e| &**e);
|
2013-07-16 04:27:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(enum_node_id) = tcx.map.as_local_node_id(enum_def) {
|
|
|
|
let variant_node_id = tcx.map.as_local_node_id(variant_def).unwrap();
|
|
|
|
match tcx.map.find(enum_node_id) {
|
2014-09-07 20:09:06 +03:00
|
|
|
None => None,
|
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemEnum(hir::EnumDef { ref variants }, _) => {
|
2015-12-07 17:17:41 +03:00
|
|
|
variant_expr(variants, variant_node_id)
|
2014-09-07 20:09:06 +03:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
2013-07-16 04:27:54 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-30 08:33:22 -04:00
|
|
|
None
|
2013-07-16 04:27:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:16:24 -04:00
|
|
|
/// * `def_id` is the id of the constant.
|
2016-03-10 02:04:55 +02:00
|
|
|
/// * `substs` is the monomorphized substitutions for the expression.
|
2015-08-03 22:16:24 -04:00
|
|
|
///
|
2016-03-10 02:04:55 +02:00
|
|
|
/// `substs` is optional and is used for associated constants.
|
|
|
|
/// This generally happens in late/trans const evaluation.
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-08-16 06:32:28 -04:00
|
|
|
def_id: DefId,
|
2016-08-08 23:39:49 +03:00
|
|
|
substs: Option<&'tcx Substs<'tcx>>)
|
2016-03-03 14:48:08 +01:00
|
|
|
-> Option<(&'tcx Expr, Option<ty::Ty<'tcx>>)> {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
|
|
|
|
match tcx.map.find(node_id) {
|
2014-09-07 20:09:06 +03:00
|
|
|
None => None,
|
|
|
|
Some(ast_map::NodeItem(it)) => match it.node {
|
2016-03-03 14:48:08 +01:00
|
|
|
hir::ItemConst(ref ty, ref const_expr) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
Some((&const_expr, tcx.ast_ty_to_prim_ty(ty)))
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(ast_map::NodeTraitItem(ti)) => match ti.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
hir::ConstTraitItem(..) => {
|
2016-03-10 02:04:55 +02:00
|
|
|
if let Some(substs) = substs {
|
|
|
|
// If we have a trait item and the substitutions for it,
|
2015-03-21 15:06:28 -06:00
|
|
|
// `resolve_trait_associated_const` will select an impl
|
|
|
|
// or the default.
|
2016-08-08 23:39:49 +03:00
|
|
|
let trait_id = tcx.map.get_parent(node_id);
|
|
|
|
let trait_id = tcx.map.local_def_id(trait_id);
|
2016-03-10 02:04:55 +02:00
|
|
|
resolve_trait_associated_const(tcx, ti, trait_id, substs)
|
|
|
|
} else {
|
2015-03-21 15:06:28 -06:00
|
|
|
// Technically, without knowing anything about the
|
|
|
|
// expression that generates the obligation, we could
|
|
|
|
// still return the default if there is one. However,
|
|
|
|
// it's safer to return `None` than to return some value
|
|
|
|
// that may differ from what you would get from
|
|
|
|
// correctly selecting an impl.
|
2016-03-10 02:04:55 +02:00
|
|
|
None
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(ast_map::NodeImplItem(ii)) => match ii.node {
|
2016-03-03 14:48:08 +01:00
|
|
|
hir::ImplItemKind::Const(ref ty, ref expr) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
Some((&expr, tcx.ast_ty_to_prim_ty(ty)))
|
2014-09-07 20:09:06 +03:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
|
|
|
Some(_) => None
|
2012-10-15 12:27:09 -07:00
|
|
|
}
|
2012-11-12 22:10:15 -08:00
|
|
|
} else {
|
2014-11-06 12:25:16 -05:00
|
|
|
match tcx.extern_const_statics.borrow().get(&def_id) {
|
2016-03-03 14:48:08 +01:00
|
|
|
Some(&None) => return None,
|
|
|
|
Some(&Some((expr_id, ty))) => {
|
|
|
|
return Some((tcx.map.expect_expr(expr_id), ty));
|
2014-09-07 20:09:06 +03:00
|
|
|
}
|
2014-03-20 19:49:20 -07:00
|
|
|
None => {}
|
2013-10-04 15:04:11 -07:00
|
|
|
}
|
2016-03-10 02:04:55 +02:00
|
|
|
let mut used_substs = false;
|
2016-03-03 14:48:08 +01:00
|
|
|
let expr_ty = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
|
2016-08-02 16:31:39 -04:00
|
|
|
Some((&InlinedItem::Item(_, ref item), _)) => match item.node {
|
2016-03-03 14:48:08 +01:00
|
|
|
hir::ItemConst(ref ty, ref const_expr) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
Some((&**const_expr, tcx.ast_ty_to_prim_ty(ty)))
|
2016-03-03 14:48:08 +01:00
|
|
|
},
|
2013-03-21 00:27:26 -07:00
|
|
|
_ => None
|
|
|
|
},
|
2016-07-26 10:58:35 -04:00
|
|
|
Some((&InlinedItem::TraitItem(trait_id, ref ti), _)) => match ti.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
hir::ConstTraitItem(..) => {
|
2016-03-10 02:04:55 +02:00
|
|
|
used_substs = true;
|
|
|
|
if let Some(substs) = substs {
|
2015-03-21 15:06:28 -06:00
|
|
|
// As mentioned in the comments above for in-crate
|
|
|
|
// constants, we only try to find the expression for
|
|
|
|
// a trait-associated const if the caller gives us
|
2016-03-10 02:04:55 +02:00
|
|
|
// the substitutions for the reference to it.
|
|
|
|
resolve_trait_associated_const(tcx, ti, trait_id, substs)
|
|
|
|
} else {
|
|
|
|
None
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
},
|
2016-07-26 10:58:35 -04:00
|
|
|
Some((&InlinedItem::ImplItem(_, ref ii), _)) => match ii.node {
|
2016-03-03 14:48:08 +01:00
|
|
|
hir::ImplItemKind::Const(ref ty, ref expr) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
Some((&**expr, tcx.ast_ty_to_prim_ty(ty)))
|
2016-03-03 14:48:08 +01:00
|
|
|
},
|
2015-03-15 19:35:25 -06:00
|
|
|
_ => None
|
|
|
|
},
|
2013-03-21 00:27:26 -07:00
|
|
|
_ => None
|
2013-10-04 15:04:11 -07:00
|
|
|
};
|
2016-03-10 02:04:55 +02:00
|
|
|
// If we used the substitutions, particularly to choose an impl
|
2015-03-15 19:35:25 -06:00
|
|
|
// of a trait-associated const, don't cache that, because the next
|
|
|
|
// lookup with the same def_id may yield a different result.
|
2016-03-10 02:04:55 +02:00
|
|
|
if !used_substs {
|
2015-03-15 19:35:25 -06:00
|
|
|
tcx.extern_const_statics
|
2016-03-03 14:48:08 +01:00
|
|
|
.borrow_mut()
|
|
|
|
.insert(def_id, expr_ty.map(|(e, t)| (e.id, t)));
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2016-03-03 14:48:08 +01:00
|
|
|
expr_ty
|
2012-10-15 12:27:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn inline_const_fn_from_external_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId)
|
|
|
|
-> Option<ast::NodeId> {
|
2015-02-25 22:06:08 +02:00
|
|
|
match tcx.extern_const_fns.borrow().get(&def_id) {
|
|
|
|
Some(&ast::DUMMY_NODE_ID) => return None,
|
|
|
|
Some(&fn_id) => return Some(fn_id),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
if !tcx.sess.cstore.is_const_fn(def_id) {
|
2015-02-25 22:06:08 +02:00
|
|
|
tcx.extern_const_fns.borrow_mut().insert(def_id, ast::DUMMY_NODE_ID);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
let fn_id = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
|
2016-08-02 16:31:39 -04:00
|
|
|
Some((&InlinedItem::Item(_, ref item), _)) => Some(item.id),
|
2016-07-26 10:58:35 -04:00
|
|
|
Some((&InlinedItem::ImplItem(_, ref item), _)) => Some(item.id),
|
2015-02-25 22:06:08 +02:00
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
tcx.extern_const_fns.borrow_mut().insert(def_id,
|
|
|
|
fn_id.unwrap_or(ast::DUMMY_NODE_ID));
|
|
|
|
fn_id
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn lookup_const_fn_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
|
2016-05-03 04:56:42 +03:00
|
|
|
-> Option<FnLikeNode<'tcx>>
|
2015-05-05 08:47:04 -04:00
|
|
|
{
|
2015-09-04 13:52:28 -04:00
|
|
|
let fn_id = if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
|
|
|
|
node_id
|
|
|
|
} else {
|
2015-02-25 22:06:08 +02:00
|
|
|
if let Some(fn_id) = inline_const_fn_from_external_crate(tcx, def_id) {
|
|
|
|
fn_id
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let fn_like = match FnLikeNode::from_node(tcx.map.get(fn_id)) {
|
|
|
|
Some(fn_like) => fn_like,
|
|
|
|
None => return None
|
|
|
|
};
|
|
|
|
|
|
|
|
match fn_like.kind() {
|
2016-08-26 19:23:42 +03:00
|
|
|
FnKind::ItemFn(_, _, _, hir::Constness::Const, ..) => {
|
2015-02-25 22:06:08 +02:00
|
|
|
Some(fn_like)
|
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
FnKind::Method(_, m, ..) => {
|
2015-07-31 00:04:06 -07:00
|
|
|
if m.constness == hir::Constness::Const {
|
2015-02-25 22:06:08 +02:00
|
|
|
Some(fn_like)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
expr: &Expr,
|
|
|
|
pat_id: ast::NodeId,
|
|
|
|
span: Span)
|
|
|
|
-> Result<P<hir::Pat>, DefId> {
|
2016-03-11 13:30:32 -05:00
|
|
|
let pat_ty = tcx.expr_ty(expr);
|
|
|
|
debug!("expr={:?} pat_ty={:?} pat_id={}", expr, pat_ty, pat_id);
|
|
|
|
match pat_ty.sty {
|
|
|
|
ty::TyFloat(_) => {
|
|
|
|
tcx.sess.add_lint(
|
|
|
|
lint::builtin::ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN,
|
|
|
|
pat_id,
|
|
|
|
span,
|
|
|
|
format!("floating point constants cannot be used in patterns"));
|
|
|
|
}
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(adt_def, _) if adt_def.is_union() => {
|
|
|
|
// Matching on union fields is unsafe, we can't hide it in constants
|
|
|
|
tcx.sess.span_err(span, "cannot use unions in constant patterns");
|
|
|
|
}
|
|
|
|
ty::TyAdt(adt_def, _) => {
|
2016-03-11 13:30:32 -05:00
|
|
|
if !tcx.has_attr(adt_def.did, "structural_match") {
|
|
|
|
tcx.sess.add_lint(
|
|
|
|
lint::builtin::ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN,
|
|
|
|
pat_id,
|
|
|
|
span,
|
|
|
|
format!("to use a constant of type `{}` \
|
|
|
|
in a pattern, \
|
2016-03-25 10:02:56 -04:00
|
|
|
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
|
2016-03-11 13:30:32 -05:00
|
|
|
tcx.item_path_str(adt_def.did),
|
|
|
|
tcx.item_path_str(adt_def.did)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
2014-07-13 15:12:47 +02:00
|
|
|
let pat = match expr.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprTup(ref exprs) =>
|
2016-08-27 07:51:55 -07:00
|
|
|
PatKind::Tuple(exprs.iter()
|
|
|
|
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
|
|
|
|
.collect::<Result<_, _>>()?, None),
|
2014-07-13 15:12:47 +02:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprCall(ref callee, ref args) => {
|
2016-06-03 23:15:00 +03:00
|
|
|
let def = tcx.expect_def(callee.id);
|
2015-01-06 16:36:30 +00:00
|
|
|
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
|
2016-06-03 23:15:00 +03:00
|
|
|
entry.insert(PathResolution::new(def));
|
2014-11-29 16:41:21 -05:00
|
|
|
}
|
2016-06-03 23:15:00 +03:00
|
|
|
let path = match def {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Struct(def_id) => def_to_path(tcx, def_id),
|
|
|
|
Def::Variant(_, variant_did) => def_to_path(tcx, variant_did),
|
2016-04-30 03:30:33 +03:00
|
|
|
Def::Fn(..) | Def::Method(..) => return Ok(P(hir::Pat {
|
2015-12-01 17:37:01 +01:00
|
|
|
id: expr.id,
|
2016-02-14 15:25:12 +03:00
|
|
|
node: PatKind::Lit(P(expr.clone())),
|
2015-12-01 17:37:01 +01:00
|
|
|
span: span,
|
2016-02-03 16:38:48 -05:00
|
|
|
})),
|
2016-03-31 20:07:23 +02:00
|
|
|
_ => bug!()
|
2014-07-13 15:12:47 +02:00
|
|
|
};
|
2016-08-27 07:51:55 -07:00
|
|
|
let pats = args.iter()
|
|
|
|
.map(|expr| const_expr_to_pat(tcx, &**expr, pat_id, span))
|
|
|
|
.collect::<Result<_, _>>()?;
|
2016-03-06 15:54:44 +03:00
|
|
|
PatKind::TupleStruct(path, pats, None)
|
2014-07-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprStruct(ref path, ref fields, None) => {
|
2016-02-03 16:38:48 -05:00
|
|
|
let field_pats =
|
2016-08-27 07:51:55 -07:00
|
|
|
fields.iter()
|
|
|
|
.map(|field| Ok(codemap::Spanned {
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
node: hir::FieldPat {
|
|
|
|
name: field.name.node,
|
|
|
|
pat: const_expr_to_pat(tcx, &field.expr, pat_id, span)?,
|
|
|
|
is_shorthand: false,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
.collect::<Result<_, _>>()?;
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Struct(path.clone(), field_pats, false)
|
2014-07-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprVec(ref exprs) => {
|
2016-08-27 07:51:55 -07:00
|
|
|
let pats = exprs.iter()
|
|
|
|
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
|
|
|
|
.collect::<Result<_, _>>()?;
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Vec(pats, None, hir::HirVec::new())
|
2014-07-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprPath(_, ref path) => {
|
2016-06-03 23:15:00 +03:00
|
|
|
match tcx.expect_def(expr.id) {
|
2016-06-11 18:47:47 +03:00
|
|
|
Def::Struct(..) | Def::Variant(..) => PatKind::Path(None, path.clone()),
|
2016-06-03 23:15:00 +03:00
|
|
|
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
|
2016-03-10 02:04:55 +02:00
|
|
|
let substs = Some(tcx.node_id_item_substs(expr.id).substs);
|
|
|
|
let (expr, _ty) = lookup_const_by_id(tcx, def_id, substs).unwrap();
|
2016-03-11 13:30:32 -05:00
|
|
|
return const_expr_to_pat(tcx, expr, pat_id, span);
|
2016-01-16 14:29:20 +01:00
|
|
|
},
|
2016-03-31 20:07:23 +02:00
|
|
|
_ => bug!(),
|
2014-07-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:25:12 +03:00
|
|
|
_ => PatKind::Lit(P(expr.clone()))
|
2014-07-13 15:12:47 +02:00
|
|
|
};
|
2016-02-03 16:38:48 -05:00
|
|
|
Ok(P(hir::Pat { id: expr.id, node: pat, span: span }))
|
2014-07-13 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
pub fn report_const_eval_err<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
err: &ConstEvalErr,
|
|
|
|
primary_span: Span,
|
|
|
|
primary_kind: &str)
|
|
|
|
-> DiagnosticBuilder<'tcx>
|
|
|
|
{
|
|
|
|
let mut err = err;
|
|
|
|
while let &ConstEvalErr { kind: ErroneousReferencedConstant(box ref i_err), .. } = err {
|
|
|
|
err = i_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut diag = struct_span_err!(tcx.sess, err.span, E0080, "constant evaluation error");
|
|
|
|
note_const_eval_err(tcx, err, primary_span, primary_kind, &mut diag);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fatal_const_eval_err<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
err: &ConstEvalErr,
|
|
|
|
primary_span: Span,
|
|
|
|
primary_kind: &str)
|
|
|
|
-> !
|
|
|
|
{
|
|
|
|
report_const_eval_err(tcx, err, primary_span, primary_kind).emit();
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn note_const_eval_err<'a, 'tcx>(
|
|
|
|
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
err: &ConstEvalErr,
|
|
|
|
primary_span: Span,
|
|
|
|
primary_kind: &str,
|
|
|
|
diag: &mut DiagnosticBuilder)
|
|
|
|
{
|
|
|
|
match err.description() {
|
|
|
|
ConstEvalErrDescription::Simple(message) => {
|
2016-08-05 15:58:31 -07:00
|
|
|
diag.span_label(err.span, &message);
|
2016-07-20 00:02:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !primary_span.contains(err.span) {
|
|
|
|
diag.span_note(primary_span,
|
|
|
|
&format!("for {} here", primary_kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn eval_const_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
e: &Expr) -> ConstVal {
|
2015-10-14 12:30:10 +02:00
|
|
|
match eval_const_expr_partial(tcx, e, ExprTypeChecked, None) {
|
2013-06-13 03:02:55 +10:00
|
|
|
Ok(r) => r,
|
2015-12-27 02:17:16 +01:00
|
|
|
// non-const path still needs to be a fatal error, because enums are funky
|
|
|
|
Err(s) => {
|
2016-07-20 00:02:56 +03:00
|
|
|
report_const_eval_err(tcx, &s, e.span, "expression").emit();
|
2016-04-01 09:19:29 +02:00
|
|
|
match s.kind {
|
|
|
|
NonConstPath |
|
2016-07-20 00:02:56 +03:00
|
|
|
UnimplementedConstVal(_) => tcx.sess.abort_if_errors(),
|
|
|
|
_ => {}
|
2016-04-01 09:19:29 +02:00
|
|
|
}
|
2016-07-20 00:02:56 +03:00
|
|
|
Dummy
|
2015-12-27 02:17:16 +01:00
|
|
|
},
|
2012-10-15 12:27:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:30:10 +02:00
|
|
|
pub type FnArgMap<'a> = Option<&'a NodeMap<ConstVal>>;
|
2015-02-22 16:34:26 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ConstEvalErr {
|
|
|
|
pub span: Span,
|
|
|
|
pub kind: ErrKind,
|
|
|
|
}
|
|
|
|
|
2016-04-11 10:41:48 +02:00
|
|
|
#[derive(Clone)]
|
2015-02-22 16:34:26 +01:00
|
|
|
pub enum ErrKind {
|
|
|
|
CannotCast,
|
|
|
|
CannotCastTo(&'static str),
|
2015-11-12 11:30:04 +01:00
|
|
|
InvalidOpForInts(hir::BinOp_),
|
2015-07-31 00:04:06 -07:00
|
|
|
InvalidOpForBools(hir::BinOp_),
|
|
|
|
InvalidOpForFloats(hir::BinOp_),
|
|
|
|
InvalidOpForIntUint(hir::BinOp_),
|
|
|
|
InvalidOpForUintInt(hir::BinOp_),
|
2015-07-13 10:53:16 +02:00
|
|
|
NegateOn(ConstVal),
|
|
|
|
NotOn(ConstVal),
|
2015-11-27 16:43:24 +01:00
|
|
|
CallOn(ConstVal),
|
2015-02-22 16:34:26 +01:00
|
|
|
|
|
|
|
MissingStructField,
|
|
|
|
NonConstPath,
|
2015-11-27 16:43:24 +01:00
|
|
|
UnimplementedConstVal(&'static str),
|
2015-09-30 15:04:21 -07:00
|
|
|
UnresolvedPath,
|
2015-03-08 16:41:28 +01:00
|
|
|
ExpectedConstTuple,
|
|
|
|
ExpectedConstStruct,
|
2015-02-22 16:34:26 +01:00
|
|
|
TupleIndexOutOfBounds,
|
2015-11-18 10:57:52 +01:00
|
|
|
IndexedNonVec,
|
|
|
|
IndexNegative,
|
|
|
|
IndexNotInt,
|
2016-05-26 22:09:48 +03:00
|
|
|
IndexOutOfBounds { len: u64, index: u64 },
|
2015-11-18 10:57:52 +01:00
|
|
|
RepeatCountNotNatural,
|
|
|
|
RepeatCountNotInt,
|
2015-02-22 16:34:26 +01:00
|
|
|
|
|
|
|
MiscBinaryOp,
|
|
|
|
MiscCatchAll,
|
2015-12-04 15:35:07 +01:00
|
|
|
|
|
|
|
IndexOpFeatureGated,
|
2015-12-16 18:44:15 +01:00
|
|
|
Math(ConstMathErr),
|
2016-02-25 10:13:36 +01:00
|
|
|
|
|
|
|
IntermediateUnsignedNegative,
|
2016-02-25 11:12:18 +01:00
|
|
|
/// Expected, Got
|
|
|
|
TypeMismatch(String, ConstInt),
|
2016-07-20 00:02:56 +03:00
|
|
|
|
2016-02-25 10:13:36 +01:00
|
|
|
BadType(ConstVal),
|
2016-04-11 10:41:48 +02:00
|
|
|
ErroneousReferencedConstant(Box<ConstEvalErr>),
|
2016-05-02 16:38:33 +02:00
|
|
|
CharCast(ConstInt),
|
2015-12-16 18:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ConstMathErr> for ErrKind {
|
|
|
|
fn from(err: ConstMathErr) -> ErrKind {
|
|
|
|
Math(err)
|
|
|
|
}
|
2015-02-22 16:34:26 +01:00
|
|
|
}
|
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum ConstEvalErrDescription<'a> {
|
|
|
|
Simple(Cow<'a, str>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ConstEvalErrDescription<'a> {
|
|
|
|
/// Return a one-line description of the error, for lints and such
|
|
|
|
pub fn into_oneline(self) -> Cow<'a, str> {
|
|
|
|
match self {
|
|
|
|
ConstEvalErrDescription::Simple(simple) => simple,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 16:34:26 +01:00
|
|
|
impl ConstEvalErr {
|
2016-07-20 00:02:56 +03:00
|
|
|
pub fn description(&self) -> ConstEvalErrDescription {
|
2015-02-22 16:34:26 +01:00
|
|
|
use self::ErrKind::*;
|
2016-07-20 00:02:56 +03:00
|
|
|
use self::ConstEvalErrDescription::*;
|
|
|
|
|
|
|
|
macro_rules! simple {
|
|
|
|
($msg:expr) => ({ Simple($msg.into_cow()) });
|
|
|
|
($fmt:expr, $($arg:tt)+) => ({
|
|
|
|
Simple(format!($fmt, $($arg)+).into_cow())
|
|
|
|
})
|
|
|
|
}
|
2015-03-25 11:10:09 +01:00
|
|
|
|
2015-02-22 16:34:26 +01:00
|
|
|
match self.kind {
|
2016-07-20 00:02:56 +03:00
|
|
|
CannotCast => simple!("can't cast this type"),
|
|
|
|
CannotCastTo(s) => simple!("can't cast this type to {}", s),
|
|
|
|
InvalidOpForInts(_) => simple!("can't do this op on integrals"),
|
|
|
|
InvalidOpForBools(_) => simple!("can't do this op on bools"),
|
|
|
|
InvalidOpForFloats(_) => simple!("can't do this op on floats"),
|
|
|
|
InvalidOpForIntUint(..) => simple!("can't do this op on an isize and usize"),
|
|
|
|
InvalidOpForUintInt(..) => simple!("can't do this op on a usize and isize"),
|
|
|
|
NegateOn(ref const_val) => simple!("negate on {}", const_val.description()),
|
|
|
|
NotOn(ref const_val) => simple!("not on {}", const_val.description()),
|
|
|
|
CallOn(ref const_val) => simple!("call on {}", const_val.description()),
|
|
|
|
|
|
|
|
MissingStructField => simple!("nonexistent struct field"),
|
|
|
|
NonConstPath => simple!("non-constant path in constant expression"),
|
2015-11-27 16:43:24 +01:00
|
|
|
UnimplementedConstVal(what) =>
|
2016-07-20 00:02:56 +03:00
|
|
|
simple!("unimplemented constant expression: {}", what),
|
|
|
|
UnresolvedPath => simple!("unresolved path in constant expression"),
|
|
|
|
ExpectedConstTuple => simple!("expected constant tuple"),
|
|
|
|
ExpectedConstStruct => simple!("expected constant struct"),
|
|
|
|
TupleIndexOutOfBounds => simple!("tuple index out of bounds"),
|
|
|
|
IndexedNonVec => simple!("indexing is only supported for arrays"),
|
|
|
|
IndexNegative => simple!("indices must be non-negative integers"),
|
|
|
|
IndexNotInt => simple!("indices must be integers"),
|
2016-05-26 22:09:48 +03:00
|
|
|
IndexOutOfBounds { len, index } => {
|
2016-07-20 00:02:56 +03:00
|
|
|
simple!("index out of bounds: the len is {} but the index is {}",
|
|
|
|
len, index)
|
2016-05-26 22:09:48 +03:00
|
|
|
}
|
2016-07-20 00:02:56 +03:00
|
|
|
RepeatCountNotNatural => simple!("repeat count must be a natural number"),
|
|
|
|
RepeatCountNotInt => simple!("repeat count must be integers"),
|
2015-02-22 16:34:26 +01:00
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
MiscBinaryOp => simple!("bad operands for binary"),
|
|
|
|
MiscCatchAll => simple!("unsupported constant expr"),
|
|
|
|
IndexOpFeatureGated => simple!("the index operation on const values is unstable"),
|
|
|
|
Math(ref err) => Simple(err.description().into_cow()),
|
2016-02-25 10:13:36 +01:00
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
IntermediateUnsignedNegative => simple!(
|
|
|
|
"during the computation of an unsigned a negative \
|
|
|
|
number was encountered. This is most likely a bug in\
|
|
|
|
the constant evaluator"),
|
2016-02-25 10:13:36 +01:00
|
|
|
|
2016-02-25 11:12:18 +01:00
|
|
|
TypeMismatch(ref expected, ref got) => {
|
2016-07-22 23:52:53 +03:00
|
|
|
simple!("expected {}, found {}", expected, got.description())
|
2016-02-25 11:12:18 +01:00
|
|
|
},
|
2016-07-20 00:02:56 +03:00
|
|
|
BadType(ref i) => simple!("value of wrong type: {:?}", i),
|
|
|
|
ErroneousReferencedConstant(_) => simple!("could not evaluate referenced constant"),
|
2016-05-02 16:38:33 +02:00
|
|
|
CharCast(ref got) => {
|
2016-07-20 00:02:56 +03:00
|
|
|
simple!("only `u8` can be cast as `char`, not `{}`", got.description())
|
2016-05-02 16:38:33 +02:00
|
|
|
},
|
2015-02-22 16:34:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 10:31:32 +02:00
|
|
|
pub type EvalResult = Result<ConstVal, ConstEvalErr>;
|
|
|
|
pub type CastResult = Result<ConstVal, ErrKind>;
|
2015-03-25 11:10:09 +01:00
|
|
|
|
2015-06-30 08:53:50 -07:00
|
|
|
// FIXME: Long-term, this enum should go away: trying to evaluate
|
|
|
|
// an expression which hasn't been type-checked is a recipe for
|
|
|
|
// disaster. That said, it's not clear how to fix ast_ty_to_ty
|
|
|
|
// to avoid the ordering issue.
|
|
|
|
|
|
|
|
/// Hint to determine how to evaluate constant expressions which
|
|
|
|
/// might not be type-checked.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum EvalHint<'tcx> {
|
|
|
|
/// We have a type-checked expression.
|
|
|
|
ExprTypeChecked,
|
|
|
|
/// We have an expression which hasn't been type-checked, but we have
|
|
|
|
/// an idea of what the type will be because of the context. For example,
|
|
|
|
/// the length of an array is always `usize`. (This is referred to as
|
|
|
|
/// a hint because it isn't guaranteed to be consistent with what
|
|
|
|
/// type-checking would compute.)
|
|
|
|
UncheckedExprHint(Ty<'tcx>),
|
|
|
|
/// We have an expression which has not yet been type-checked, and
|
|
|
|
/// and we have no clue what the type will be.
|
|
|
|
UncheckedExprNoHint,
|
|
|
|
}
|
|
|
|
|
2015-11-27 17:39:44 +01:00
|
|
|
impl<'tcx> EvalHint<'tcx> {
|
|
|
|
fn erase_hint(&self) -> EvalHint<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ExprTypeChecked => ExprTypeChecked,
|
|
|
|
UncheckedExprHint(_) | UncheckedExprNoHint => UncheckedExprNoHint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn checked_or(&self, ty: Ty<'tcx>) -> EvalHint<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ExprTypeChecked => ExprTypeChecked,
|
|
|
|
_ => UncheckedExprHint(ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 18:16:30 +01:00
|
|
|
macro_rules! signal {
|
2015-03-25 11:10:09 +01:00
|
|
|
($e:expr, $exn:expr) => {
|
|
|
|
return Err(ConstEvalErr { span: $e.span, kind: $exn })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 08:53:50 -07:00
|
|
|
/// Evaluate a constant expression in a context where the expression isn't
|
|
|
|
/// guaranteed to be evaluatable. `ty_hint` is usually ExprTypeChecked,
|
|
|
|
/// but a few places need to evaluate constants during type-checking, like
|
|
|
|
/// computing the length of an array. (See also the FIXME above EvalHint.)
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
e: &Expr,
|
|
|
|
ty_hint: EvalHint<'tcx>,
|
|
|
|
fn_args: FnArgMap) -> EvalResult {
|
2015-06-30 08:53:50 -07:00
|
|
|
// Try to compute the type of the expression based on the EvalHint.
|
|
|
|
// (See also the definition of EvalHint, and the FIXME above EvalHint.)
|
|
|
|
let ety = match ty_hint {
|
|
|
|
ExprTypeChecked => {
|
|
|
|
// After type-checking, expr_ty is guaranteed to succeed.
|
|
|
|
Some(tcx.expr_ty(e))
|
|
|
|
}
|
|
|
|
UncheckedExprHint(ty) => {
|
|
|
|
// Use the type hint; it's not guaranteed to be right, but it's
|
|
|
|
// usually good enough.
|
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
UncheckedExprNoHint => {
|
|
|
|
// This expression might not be type-checked, and we have no hint.
|
|
|
|
// Try to query the context for a type anyway; we might get lucky
|
|
|
|
// (for example, if the expression was imported from another crate).
|
|
|
|
tcx.expr_ty_opt(e)
|
|
|
|
}
|
|
|
|
};
|
2015-03-01 12:29:46 +01:00
|
|
|
let result = match e.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprUnary(hir::UnNeg, ref inner) => {
|
2015-12-16 18:44:15 +01:00
|
|
|
// unary neg literals already got their sign during creation
|
2016-06-27 11:42:52 +02:00
|
|
|
if let hir::ExprLit(ref lit) = inner.node {
|
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ast::LitIntType::*;
|
|
|
|
const I8_OVERFLOW: u64 = ::std::i8::MAX as u64 + 1;
|
|
|
|
const I16_OVERFLOW: u64 = ::std::i16::MAX as u64 + 1;
|
|
|
|
const I32_OVERFLOW: u64 = ::std::i32::MAX as u64 + 1;
|
|
|
|
const I64_OVERFLOW: u64 = ::std::i64::MAX as u64 + 1;
|
|
|
|
match (&lit.node, ety.map(|t| &t.sty)) {
|
|
|
|
(&LitKind::Int(I8_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I8))) |
|
|
|
|
(&LitKind::Int(I8_OVERFLOW, Signed(IntTy::I8)), _) => {
|
|
|
|
return Ok(Integral(I8(::std::i8::MIN)))
|
|
|
|
},
|
|
|
|
(&LitKind::Int(I16_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I16))) |
|
|
|
|
(&LitKind::Int(I16_OVERFLOW, Signed(IntTy::I16)), _) => {
|
|
|
|
return Ok(Integral(I16(::std::i16::MIN)))
|
|
|
|
},
|
|
|
|
(&LitKind::Int(I32_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I32))) |
|
|
|
|
(&LitKind::Int(I32_OVERFLOW, Signed(IntTy::I32)), _) => {
|
|
|
|
return Ok(Integral(I32(::std::i32::MIN)))
|
|
|
|
},
|
|
|
|
(&LitKind::Int(I64_OVERFLOW, Unsuffixed), Some(&ty::TyInt(IntTy::I64))) |
|
|
|
|
(&LitKind::Int(I64_OVERFLOW, Signed(IntTy::I64)), _) => {
|
|
|
|
return Ok(Integral(I64(::std::i64::MIN)))
|
|
|
|
},
|
|
|
|
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
|
|
|
|
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
|
|
|
|
match tcx.sess.target.int_type {
|
|
|
|
IntTy::I16 => if n == I16_OVERFLOW {
|
|
|
|
return Ok(Integral(Isize(Is16(::std::i16::MIN))));
|
|
|
|
},
|
|
|
|
IntTy::I32 => if n == I32_OVERFLOW {
|
|
|
|
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
|
|
|
|
},
|
|
|
|
IntTy::I64 => if n == I64_OVERFLOW {
|
|
|
|
return Ok(Integral(Isize(Is64(::std::i64::MIN))));
|
|
|
|
},
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
match eval_const_expr_partial(tcx, &inner, ty_hint, fn_args)? {
|
2015-06-23 10:31:32 +02:00
|
|
|
Float(f) => Float(-f),
|
2015-12-16 18:44:15 +01:00
|
|
|
Integral(i) => Integral(math!(e, -i)),
|
2015-07-13 10:53:16 +02:00
|
|
|
const_val => signal!(e, NegateOn(const_val)),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprUnary(hir::UnNot, ref inner) => {
|
2016-03-22 22:01:37 -05:00
|
|
|
match eval_const_expr_partial(tcx, &inner, ty_hint, fn_args)? {
|
2015-12-16 18:44:15 +01:00
|
|
|
Integral(i) => Integral(math!(e, !i)),
|
2015-06-23 10:31:32 +02:00
|
|
|
Bool(b) => Bool(!b),
|
2015-07-13 10:53:16 +02:00
|
|
|
const_val => signal!(e, NotOn(const_val)),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 09:19:29 +02:00
|
|
|
hir::ExprUnary(hir::UnDeref, _) => signal!(e, UnimplementedConstVal("deref operation")),
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBinary(op, ref a, ref b) => {
|
2015-01-29 13:40:14 +02:00
|
|
|
let b_ty = match op.node {
|
2016-03-16 10:54:00 +01:00
|
|
|
hir::BiShl | hir::BiShr => ty_hint.erase_hint(),
|
2015-06-30 08:53:50 -07:00
|
|
|
_ => ty_hint
|
2015-01-29 13:40:14 +02:00
|
|
|
};
|
2015-12-16 18:44:15 +01:00
|
|
|
// technically, if we don't have type hints, but integral eval
|
|
|
|
// gives us a type through a type-suffix, cast or const def type
|
|
|
|
// we need to re-eval the other value of the BinOp if it was
|
|
|
|
// not inferred
|
2016-03-22 22:01:37 -05:00
|
|
|
match (eval_const_expr_partial(tcx, &a, ty_hint, fn_args)?,
|
|
|
|
eval_const_expr_partial(tcx, &b, b_ty, fn_args)?) {
|
2015-06-23 10:31:32 +02:00
|
|
|
(Float(a), Float(b)) => {
|
2016-06-01 12:22:07 +03:00
|
|
|
use std::cmp::Ordering::*;
|
2015-01-13 14:24:37 +11:00
|
|
|
match op.node {
|
2016-06-01 12:22:07 +03:00
|
|
|
hir::BiAdd => Float(math!(e, a + b)),
|
|
|
|
hir::BiSub => Float(math!(e, a - b)),
|
|
|
|
hir::BiMul => Float(math!(e, a * b)),
|
|
|
|
hir::BiDiv => Float(math!(e, a / b)),
|
|
|
|
hir::BiRem => Float(math!(e, a % b)),
|
|
|
|
hir::BiEq => Bool(math!(e, a.try_cmp(b)) == Equal),
|
|
|
|
hir::BiLt => Bool(math!(e, a.try_cmp(b)) == Less),
|
|
|
|
hir::BiLe => Bool(math!(e, a.try_cmp(b)) != Greater),
|
|
|
|
hir::BiNe => Bool(math!(e, a.try_cmp(b)) != Equal),
|
|
|
|
hir::BiGe => Bool(math!(e, a.try_cmp(b)) != Less),
|
|
|
|
hir::BiGt => Bool(math!(e, a.try_cmp(b)) == Greater),
|
2015-11-12 11:30:04 +01:00
|
|
|
_ => signal!(e, InvalidOpForFloats(op.node)),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
(Integral(a), Integral(b)) => {
|
|
|
|
use std::cmp::Ordering::*;
|
2015-01-13 14:24:37 +11:00
|
|
|
match op.node {
|
2015-12-16 18:44:15 +01:00
|
|
|
hir::BiAdd => Integral(math!(e, a + b)),
|
|
|
|
hir::BiSub => Integral(math!(e, a - b)),
|
|
|
|
hir::BiMul => Integral(math!(e, a * b)),
|
|
|
|
hir::BiDiv => Integral(math!(e, a / b)),
|
|
|
|
hir::BiRem => Integral(math!(e, a % b)),
|
|
|
|
hir::BiBitAnd => Integral(math!(e, a & b)),
|
|
|
|
hir::BiBitOr => Integral(math!(e, a | b)),
|
|
|
|
hir::BiBitXor => Integral(math!(e, a ^ b)),
|
|
|
|
hir::BiShl => Integral(math!(e, a << b)),
|
|
|
|
hir::BiShr => Integral(math!(e, a >> b)),
|
|
|
|
hir::BiEq => Bool(math!(e, a.try_cmp(b)) == Equal),
|
|
|
|
hir::BiLt => Bool(math!(e, a.try_cmp(b)) == Less),
|
|
|
|
hir::BiLe => Bool(math!(e, a.try_cmp(b)) != Greater),
|
|
|
|
hir::BiNe => Bool(math!(e, a.try_cmp(b)) != Equal),
|
|
|
|
hir::BiGe => Bool(math!(e, a.try_cmp(b)) != Less),
|
|
|
|
hir::BiGt => Bool(math!(e, a.try_cmp(b)) == Greater),
|
2015-11-12 11:30:04 +01:00
|
|
|
_ => signal!(e, InvalidOpForInts(op.node)),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-23 10:31:32 +02:00
|
|
|
(Bool(a), Bool(b)) => {
|
|
|
|
Bool(match op.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::BiAnd => a && b,
|
|
|
|
hir::BiOr => a || b,
|
|
|
|
hir::BiBitXor => a ^ b,
|
|
|
|
hir::BiBitAnd => a & b,
|
|
|
|
hir::BiBitOr => a | b,
|
|
|
|
hir::BiEq => a == b,
|
|
|
|
hir::BiNe => a != b,
|
2015-02-27 18:16:30 +01:00
|
|
|
_ => signal!(e, InvalidOpForBools(op.node)),
|
2015-03-01 12:29:46 +01:00
|
|
|
})
|
2012-08-23 15:51:23 -07:00
|
|
|
}
|
2015-02-22 16:34:26 +01:00
|
|
|
|
2015-02-27 18:16:30 +01:00
|
|
|
_ => signal!(e, MiscBinaryOp),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprCast(ref base, ref target_ty) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let ety = tcx.ast_ty_to_prim_ty(&target_ty).or(ety)
|
2014-05-16 10:45:16 -07:00
|
|
|
.unwrap_or_else(|| {
|
2014-04-22 15:56:37 +03:00
|
|
|
tcx.sess.span_fatal(target_ty.span,
|
|
|
|
"target type not found for const cast")
|
2014-05-16 10:45:16 -07:00
|
|
|
});
|
2015-03-25 11:10:09 +01:00
|
|
|
|
2015-06-30 08:53:50 -07:00
|
|
|
let base_hint = if let ExprTypeChecked = ty_hint {
|
|
|
|
ExprTypeChecked
|
|
|
|
} else {
|
|
|
|
match tcx.expr_ty_opt(&base) {
|
|
|
|
Some(t) => UncheckedExprHint(t),
|
|
|
|
None => ty_hint
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-25 10:14:10 +01:00
|
|
|
let val = match eval_const_expr_partial(tcx, &base, base_hint, fn_args) {
|
|
|
|
Ok(val) => val,
|
2016-04-11 10:41:48 +02:00
|
|
|
Err(ConstEvalErr { kind: ErroneousReferencedConstant(
|
|
|
|
box ConstEvalErr { kind: TypeMismatch(_, val), .. }), .. }) |
|
2016-02-25 11:12:18 +01:00
|
|
|
Err(ConstEvalErr { kind: TypeMismatch(_, val), .. }) => {
|
2016-02-25 10:14:10 +01:00
|
|
|
// Something like `5i8 as usize` doesn't need a type hint for the base
|
|
|
|
// instead take the type hint from the inner value
|
|
|
|
let hint = match val.int_type() {
|
|
|
|
Some(IntType::UnsignedInt(ty)) => ty_hint.checked_or(tcx.mk_mach_uint(ty)),
|
|
|
|
Some(IntType::SignedInt(ty)) => ty_hint.checked_or(tcx.mk_mach_int(ty)),
|
|
|
|
// we had a type hint, so we can't have an unknown type
|
2016-03-31 20:07:23 +02:00
|
|
|
None => bug!(),
|
2016-02-25 10:14:10 +01:00
|
|
|
};
|
2016-03-22 22:01:37 -05:00
|
|
|
eval_const_expr_partial(tcx, &base, hint, fn_args)?
|
2016-02-25 10:14:10 +01:00
|
|
|
},
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2015-03-31 17:55:28 +02:00
|
|
|
match cast_const(tcx, val, ety) {
|
2015-03-01 12:29:46 +01:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(kind) => return Err(ConstEvalErr { span: e.span, kind: kind }),
|
2015-02-22 16:34:26 +01:00
|
|
|
}
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprPath(..) => {
|
2016-06-03 23:15:00 +03:00
|
|
|
// This function can be used before type checking when not all paths are fully resolved.
|
|
|
|
// FIXME: There's probably a better way to make sure we don't panic here.
|
|
|
|
let resolution = tcx.expect_resolution(e.id);
|
|
|
|
if resolution.depth != 0 {
|
|
|
|
signal!(e, UnresolvedPath);
|
|
|
|
}
|
|
|
|
match resolution.base_def {
|
2016-03-03 14:48:08 +01:00
|
|
|
Def::Const(def_id) |
|
|
|
|
Def::AssociatedConst(def_id) => {
|
2016-03-10 02:04:55 +02:00
|
|
|
let substs = if let ExprTypeChecked = ty_hint {
|
|
|
|
Some(tcx.node_id_item_substs(e.id).substs)
|
2016-03-03 14:48:08 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-04-11 10:41:48 +02:00
|
|
|
if let Some((expr, ty)) = lookup_const_by_id(tcx, def_id, substs) {
|
2016-03-03 14:48:08 +01:00
|
|
|
let item_hint = match ty {
|
|
|
|
Some(ty) => ty_hint.checked_or(ty),
|
|
|
|
None => ty_hint,
|
|
|
|
};
|
2016-04-11 10:41:48 +02:00
|
|
|
match eval_const_expr_partial(tcx, expr, item_hint, None) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => {
|
|
|
|
debug!("bad reference: {:?}, {:?}", err.description(), err.span);
|
|
|
|
signal!(e, ErroneousReferencedConstant(box err))
|
|
|
|
},
|
|
|
|
}
|
2015-01-29 13:40:14 +02:00
|
|
|
} else {
|
2016-03-03 14:48:08 +01:00
|
|
|
signal!(e, NonConstPath);
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2016-03-03 14:48:08 +01:00
|
|
|
},
|
|
|
|
Def::Variant(enum_def, variant_def) => {
|
|
|
|
if let Some(const_expr) = lookup_variant_by_id(tcx, enum_def, variant_def) {
|
2016-04-11 10:41:48 +02:00
|
|
|
match eval_const_expr_partial(tcx, const_expr, ty_hint, None) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => {
|
|
|
|
debug!("bad reference: {:?}, {:?}", err.description(), err.span);
|
|
|
|
signal!(e, ErroneousReferencedConstant(box err))
|
|
|
|
},
|
|
|
|
}
|
2015-03-15 19:35:25 -06:00
|
|
|
} else {
|
2016-04-01 09:19:29 +02:00
|
|
|
signal!(e, UnimplementedConstVal("enum variants"));
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-03 14:48:08 +01:00
|
|
|
Def::Struct(..) => {
|
|
|
|
ConstVal::Struct(e.id)
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
2016-08-31 14:08:22 +03:00
|
|
|
Def::Local(def_id) => {
|
|
|
|
let id = tcx.map.as_local_node_id(def_id).unwrap();
|
2016-01-20 22:31:10 +03:00
|
|
|
debug!("Def::Local({:?}): {:?}", id, fn_args);
|
2015-10-14 12:30:10 +02:00
|
|
|
if let Some(val) = fn_args.and_then(|args| args.get(&id)) {
|
2016-03-03 14:48:08 +01:00
|
|
|
val.clone()
|
2015-10-14 12:30:10 +02:00
|
|
|
} else {
|
2016-03-03 14:48:08 +01:00
|
|
|
signal!(e, NonConstPath);
|
2015-10-14 12:30:10 +02:00
|
|
|
}
|
|
|
|
},
|
2016-03-03 14:48:08 +01:00
|
|
|
Def::Method(id) | Def::Fn(id) => Function(id),
|
|
|
|
_ => signal!(e, NonConstPath),
|
|
|
|
}
|
2012-10-15 12:27:09 -07:00
|
|
|
}
|
2015-10-14 12:30:10 +02:00
|
|
|
hir::ExprCall(ref callee, ref args) => {
|
2015-11-27 17:39:44 +01:00
|
|
|
let sub_ty_hint = ty_hint.erase_hint();
|
2016-03-22 22:01:37 -05:00
|
|
|
let callee_val = eval_const_expr_partial(tcx, callee, sub_ty_hint, fn_args)?;
|
2016-01-14 15:03:48 +01:00
|
|
|
let did = match callee_val {
|
|
|
|
Function(did) => did,
|
2016-04-01 09:19:29 +02:00
|
|
|
Struct(_) => signal!(e, UnimplementedConstVal("tuple struct constructors")),
|
2016-01-14 15:03:48 +01:00
|
|
|
callee => signal!(e, CallOn(callee)),
|
|
|
|
};
|
|
|
|
let (decl, result) = if let Some(fn_like) = lookup_const_fn_by_id(tcx, did) {
|
|
|
|
(fn_like.decl(), &fn_like.body().expr)
|
|
|
|
} else {
|
|
|
|
signal!(e, NonConstPath)
|
|
|
|
};
|
|
|
|
let result = result.as_ref().expect("const fn has no result expression");
|
2015-10-14 12:30:10 +02:00
|
|
|
assert_eq!(decl.inputs.len(), args.len());
|
|
|
|
|
|
|
|
let mut call_args = NodeMap();
|
|
|
|
for (arg, arg_expr) in decl.inputs.iter().zip(args.iter()) {
|
2016-02-24 16:07:31 +01:00
|
|
|
let arg_hint = ty_hint.erase_hint();
|
2016-03-22 22:01:37 -05:00
|
|
|
let arg_val = eval_const_expr_partial(
|
2015-10-14 12:30:10 +02:00
|
|
|
tcx,
|
|
|
|
arg_expr,
|
2015-12-16 18:44:15 +01:00
|
|
|
arg_hint,
|
2015-10-14 12:30:10 +02:00
|
|
|
fn_args
|
2016-03-22 22:01:37 -05:00
|
|
|
)?;
|
2015-10-14 12:30:10 +02:00
|
|
|
debug!("const call arg: {:?}", arg);
|
|
|
|
let old = call_args.insert(arg.pat.id, arg_val);
|
|
|
|
assert!(old.is_none());
|
|
|
|
}
|
|
|
|
debug!("const call({:?})", call_args);
|
2016-03-22 22:01:37 -05:00
|
|
|
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))?
|
2015-10-14 12:30:10 +02:00
|
|
|
},
|
2016-05-02 16:40:40 +02:00
|
|
|
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety, lit.span) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => signal!(e, err),
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBlock(ref block) => {
|
2014-05-04 10:39:11 +02:00
|
|
|
match block.expr {
|
2016-03-22 22:01:37 -05:00
|
|
|
Some(ref expr) => eval_const_expr_partial(tcx, &expr, ty_hint, fn_args)?,
|
2016-04-01 09:19:29 +02:00
|
|
|
None => signal!(e, UnimplementedConstVal("empty block")),
|
2014-05-04 10:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
hir::ExprType(ref e, _) => eval_const_expr_partial(tcx, &e, ty_hint, fn_args)?,
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprTup(_) => Tuple(e.id),
|
|
|
|
hir::ExprStruct(..) => Struct(e.id),
|
2015-11-18 10:57:52 +01:00
|
|
|
hir::ExprIndex(ref arr, ref idx) => {
|
2015-11-20 10:43:04 +01:00
|
|
|
if !tcx.sess.features.borrow().const_indexing {
|
2015-12-04 15:35:07 +01:00
|
|
|
signal!(e, IndexOpFeatureGated);
|
2015-11-20 10:43:04 +01:00
|
|
|
}
|
2015-11-27 17:39:44 +01:00
|
|
|
let arr_hint = ty_hint.erase_hint();
|
2016-03-22 22:01:37 -05:00
|
|
|
let arr = eval_const_expr_partial(tcx, arr, arr_hint, fn_args)?;
|
2015-11-27 17:39:44 +01:00
|
|
|
let idx_hint = ty_hint.checked_or(tcx.types.usize);
|
2016-03-22 22:01:37 -05:00
|
|
|
let idx = match eval_const_expr_partial(tcx, idx, idx_hint, fn_args)? {
|
2015-12-16 18:44:15 +01:00
|
|
|
Integral(Usize(i)) => i.as_u64(tcx.sess.target.uint_type),
|
2016-03-31 20:07:23 +02:00
|
|
|
Integral(_) => bug!(),
|
2015-11-18 10:57:52 +01:00
|
|
|
_ => signal!(idx, IndexNotInt),
|
|
|
|
};
|
2015-12-16 18:44:15 +01:00
|
|
|
assert_eq!(idx as usize as u64, idx);
|
2015-11-18 10:57:52 +01:00
|
|
|
match arr {
|
2016-05-26 22:09:48 +03:00
|
|
|
Array(_, n) if idx >= n => {
|
|
|
|
signal!(e, IndexOutOfBounds { len: n, index: idx })
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
Array(v, n) => if let hir::ExprVec(ref v) = tcx.map.expect_expr(v).node {
|
|
|
|
assert_eq!(n as usize as u64, n);
|
2016-03-22 22:01:37 -05:00
|
|
|
eval_const_expr_partial(tcx, &v[idx as usize], ty_hint, fn_args)?
|
2015-11-18 10:57:52 +01:00
|
|
|
} else {
|
2016-03-31 20:07:23 +02:00
|
|
|
bug!()
|
2015-11-18 10:57:52 +01:00
|
|
|
},
|
|
|
|
|
2016-05-26 22:09:48 +03:00
|
|
|
Repeat(_, n) if idx >= n => {
|
|
|
|
signal!(e, IndexOutOfBounds { len: n, index: idx })
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
Repeat(elem, _) => eval_const_expr_partial(
|
2015-11-18 10:57:52 +01:00
|
|
|
tcx,
|
2016-02-09 22:00:20 +01:00
|
|
|
&tcx.map.expect_expr(elem),
|
2015-11-18 10:57:52 +01:00
|
|
|
ty_hint,
|
|
|
|
fn_args,
|
2016-03-22 22:01:37 -05:00
|
|
|
)?,
|
2015-11-18 10:57:52 +01:00
|
|
|
|
2016-05-26 22:09:48 +03:00
|
|
|
ByteStr(ref data) if idx >= data.len() as u64 => {
|
|
|
|
signal!(e, IndexOutOfBounds { len: data.len() as u64, index: idx })
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
ByteStr(data) => {
|
|
|
|
Integral(U8(data[idx as usize]))
|
|
|
|
},
|
2015-11-18 10:57:52 +01:00
|
|
|
|
|
|
|
_ => signal!(e, IndexedNonVec),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::ExprVec(ref v) => Array(e.id, v.len() as u64),
|
|
|
|
hir::ExprRepeat(_, ref n) => {
|
2015-11-27 17:39:44 +01:00
|
|
|
let len_hint = ty_hint.checked_or(tcx.types.usize);
|
2015-11-18 10:57:52 +01:00
|
|
|
Repeat(
|
|
|
|
e.id,
|
2016-03-22 22:01:37 -05:00
|
|
|
match eval_const_expr_partial(tcx, &n, len_hint, fn_args)? {
|
2015-12-16 18:44:15 +01:00
|
|
|
Integral(Usize(i)) => i.as_u64(tcx.sess.target.uint_type),
|
|
|
|
Integral(_) => signal!(e, RepeatCountNotNatural),
|
2015-11-18 10:57:52 +01:00
|
|
|
_ => signal!(e, RepeatCountNotInt),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprTupField(ref base, index) => {
|
2015-11-27 17:39:44 +01:00
|
|
|
let base_hint = ty_hint.erase_hint();
|
2016-03-22 22:01:37 -05:00
|
|
|
let c = eval_const_expr_partial(tcx, base, base_hint, fn_args)?;
|
2015-11-27 17:40:18 +01:00
|
|
|
if let Tuple(tup_id) = c {
|
|
|
|
if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
|
|
|
|
if index.node < fields.len() {
|
2016-03-22 22:01:37 -05:00
|
|
|
eval_const_expr_partial(tcx, &fields[index.node], ty_hint, fn_args)?
|
2015-03-08 16:41:28 +01:00
|
|
|
} else {
|
2015-11-27 17:40:18 +01:00
|
|
|
signal!(e, TupleIndexOutOfBounds);
|
2015-03-08 16:41:28 +01:00
|
|
|
}
|
2014-11-24 12:46:02 +01:00
|
|
|
} else {
|
2016-03-31 20:07:23 +02:00
|
|
|
bug!()
|
2014-11-24 12:46:02 +01:00
|
|
|
}
|
2015-03-08 16:41:28 +01:00
|
|
|
} else {
|
2015-11-27 17:40:18 +01:00
|
|
|
signal!(base, ExpectedConstTuple);
|
2014-11-24 12:46:02 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprField(ref base, field_name) => {
|
2015-11-27 17:39:44 +01:00
|
|
|
let base_hint = ty_hint.erase_hint();
|
2014-11-24 12:46:02 +01:00
|
|
|
// Get the base expression if it is a struct and it is constant
|
2016-03-22 22:01:37 -05:00
|
|
|
let c = eval_const_expr_partial(tcx, base, base_hint, fn_args)?;
|
2015-11-27 17:40:18 +01:00
|
|
|
if let Struct(struct_id) = c {
|
|
|
|
if let hir::ExprStruct(_, ref fields, _) = tcx.map.expect_expr(struct_id).node {
|
|
|
|
// Check that the given field exists and evaluate it
|
|
|
|
// if the idents are compared run-pass/issue-19244 fails
|
|
|
|
if let Some(f) = fields.iter().find(|f| f.name.node
|
|
|
|
== field_name.node) {
|
2016-03-22 22:01:37 -05:00
|
|
|
eval_const_expr_partial(tcx, &f.expr, ty_hint, fn_args)?
|
2015-03-08 16:41:28 +01:00
|
|
|
} else {
|
2015-11-27 17:40:18 +01:00
|
|
|
signal!(e, MissingStructField);
|
2015-03-08 16:41:28 +01:00
|
|
|
}
|
2014-11-24 12:46:02 +01:00
|
|
|
} else {
|
2016-03-31 20:07:23 +02:00
|
|
|
bug!()
|
2014-11-24 12:46:02 +01:00
|
|
|
}
|
2015-03-08 16:41:28 +01:00
|
|
|
} else {
|
2015-11-27 17:40:18 +01:00
|
|
|
signal!(base, ExpectedConstStruct);
|
2014-11-24 12:46:02 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 09:19:29 +02:00
|
|
|
hir::ExprAddrOf(..) => signal!(e, UnimplementedConstVal("address operator")),
|
2015-02-27 18:16:30 +01:00
|
|
|
_ => signal!(e, MiscCatchAll)
|
2015-03-01 12:29:46 +01:00
|
|
|
};
|
|
|
|
|
2016-02-25 10:13:36 +01:00
|
|
|
match (ety.map(|t| &t.sty), result) {
|
2016-05-02 16:40:40 +02:00
|
|
|
(Some(ref ty_hint), Integral(i)) => match infer(i, tcx, ty_hint) {
|
|
|
|
Ok(inferred) => Ok(Integral(inferred)),
|
|
|
|
Err(err) => signal!(e, err),
|
|
|
|
},
|
2016-02-25 10:13:36 +01:00
|
|
|
(_, result) => Ok(result),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
fn infer<'a, 'tcx>(i: ConstInt,
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
ty_hint: &ty::TypeVariants<'tcx>)
|
|
|
|
-> Result<ConstInt, ErrKind> {
|
2016-02-25 10:13:36 +01:00
|
|
|
use syntax::ast::*;
|
|
|
|
|
|
|
|
match (ty_hint, i) {
|
|
|
|
(&ty::TyInt(IntTy::I8), result @ I8(_)) => Ok(result),
|
|
|
|
(&ty::TyInt(IntTy::I16), result @ I16(_)) => Ok(result),
|
|
|
|
(&ty::TyInt(IntTy::I32), result @ I32(_)) => Ok(result),
|
|
|
|
(&ty::TyInt(IntTy::I64), result @ I64(_)) => Ok(result),
|
|
|
|
(&ty::TyInt(IntTy::Is), result @ Isize(_)) => Ok(result),
|
|
|
|
|
|
|
|
(&ty::TyUint(UintTy::U8), result @ U8(_)) => Ok(result),
|
|
|
|
(&ty::TyUint(UintTy::U16), result @ U16(_)) => Ok(result),
|
|
|
|
(&ty::TyUint(UintTy::U32), result @ U32(_)) => Ok(result),
|
|
|
|
(&ty::TyUint(UintTy::U64), result @ U64(_)) => Ok(result),
|
|
|
|
(&ty::TyUint(UintTy::Us), result @ Usize(_)) => Ok(result),
|
|
|
|
|
2016-03-09 19:27:36 +01:00
|
|
|
(&ty::TyInt(IntTy::I8), Infer(i)) => Ok(I8(i as i64 as i8)),
|
|
|
|
(&ty::TyInt(IntTy::I16), Infer(i)) => Ok(I16(i as i64 as i16)),
|
|
|
|
(&ty::TyInt(IntTy::I32), Infer(i)) => Ok(I32(i as i64 as i32)),
|
|
|
|
(&ty::TyInt(IntTy::I64), Infer(i)) => Ok(I64(i as i64)),
|
|
|
|
(&ty::TyInt(IntTy::Is), Infer(i)) => {
|
2016-06-09 17:01:05 -04:00
|
|
|
Ok(Isize(ConstIsize::new_truncating(i as i64, tcx.sess.target.int_type)))
|
2016-02-25 10:13:36 +01:00
|
|
|
},
|
|
|
|
|
2016-03-09 19:27:36 +01:00
|
|
|
(&ty::TyInt(IntTy::I8), InferSigned(i)) => Ok(I8(i as i8)),
|
|
|
|
(&ty::TyInt(IntTy::I16), InferSigned(i)) => Ok(I16(i as i16)),
|
|
|
|
(&ty::TyInt(IntTy::I32), InferSigned(i)) => Ok(I32(i as i32)),
|
2016-02-25 10:13:36 +01:00
|
|
|
(&ty::TyInt(IntTy::I64), InferSigned(i)) => Ok(I64(i)),
|
|
|
|
(&ty::TyInt(IntTy::Is), InferSigned(i)) => {
|
2016-06-09 17:01:05 -04:00
|
|
|
Ok(Isize(ConstIsize::new_truncating(i, tcx.sess.target.int_type)))
|
2016-02-25 10:13:36 +01:00
|
|
|
},
|
|
|
|
|
2016-03-09 19:27:36 +01:00
|
|
|
(&ty::TyUint(UintTy::U8), Infer(i)) => Ok(U8(i as u8)),
|
|
|
|
(&ty::TyUint(UintTy::U16), Infer(i)) => Ok(U16(i as u16)),
|
|
|
|
(&ty::TyUint(UintTy::U32), Infer(i)) => Ok(U32(i as u32)),
|
2016-02-25 10:13:36 +01:00
|
|
|
(&ty::TyUint(UintTy::U64), Infer(i)) => Ok(U64(i)),
|
|
|
|
(&ty::TyUint(UintTy::Us), Infer(i)) => {
|
2016-06-09 17:01:05 -04:00
|
|
|
Ok(Usize(ConstUsize::new_truncating(i, tcx.sess.target.uint_type)))
|
2016-02-25 10:13:36 +01:00
|
|
|
},
|
2016-05-02 16:40:40 +02:00
|
|
|
(&ty::TyUint(_), InferSigned(_)) => Err(IntermediateUnsignedNegative),
|
2016-02-25 10:13:36 +01:00
|
|
|
|
2016-05-02 16:40:40 +02:00
|
|
|
(&ty::TyInt(ity), i) => Err(TypeMismatch(ity.to_string(), i)),
|
|
|
|
(&ty::TyUint(ity), i) => Err(TypeMismatch(ity.to_string(), i)),
|
2016-02-25 10:13:36 +01:00
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
(&ty::TyAdt(adt, _), i) if adt.is_enum() => {
|
2016-02-25 10:13:36 +01:00
|
|
|
let hints = tcx.lookup_repr_hints(adt.did);
|
|
|
|
let int_ty = tcx.enum_repr_type(hints.iter().next());
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(i, tcx, &int_ty.to_ty(tcx).sty)
|
2016-02-25 10:13:36 +01:00
|
|
|
},
|
2016-05-02 16:40:40 +02:00
|
|
|
(_, i) => Err(BadType(ConstVal::Integral(i))),
|
2016-02-25 10:13:36 +01:00
|
|
|
}
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn resolve_trait_associated_const<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-07-31 00:04:06 -07:00
|
|
|
ti: &'tcx hir::TraitItem,
|
2015-08-16 06:32:28 -04:00
|
|
|
trait_id: DefId,
|
2016-08-08 23:39:49 +03:00
|
|
|
rcvr_substs: &'tcx Substs<'tcx>)
|
2016-03-03 14:48:08 +01:00
|
|
|
-> Option<(&'tcx Expr, Option<ty::Ty<'tcx>>)>
|
2015-03-15 19:35:25 -06:00
|
|
|
{
|
2016-08-08 23:39:49 +03:00
|
|
|
let trait_ref = ty::Binder(ty::TraitRef::new(trait_id, rcvr_substs));
|
2016-01-21 14:26:35 +02:00
|
|
|
debug!("resolve_trait_associated_const: trait_ref={:?}",
|
|
|
|
trait_ref);
|
2015-03-15 19:35:25 -06:00
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
tcx.populate_implementations_for_trait_if_necessary(trait_id);
|
2016-06-30 21:22:47 +03:00
|
|
|
tcx.infer_ctxt(None, None, Reveal::NotSpecializable).enter(|infcx| {
|
2016-03-25 05:22:44 +02:00
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx);
|
|
|
|
let obligation = traits::Obligation::new(traits::ObligationCause::dummy(),
|
|
|
|
trait_ref.to_poly_trait_predicate());
|
|
|
|
let selection = match selcx.select(&obligation) {
|
|
|
|
Ok(Some(vtable)) => vtable,
|
|
|
|
// Still ambiguous, so give up and let the caller decide whether this
|
|
|
|
// expression is really needed yet. Some associated constant values
|
|
|
|
// can't be evaluated until monomorphization is done in trans.
|
|
|
|
Ok(None) => {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
};
|
2015-03-15 19:35:25 -06:00
|
|
|
|
2016-03-25 05:22:44 +02:00
|
|
|
// NOTE: this code does not currently account for specialization, but when
|
2016-06-30 21:22:47 +03:00
|
|
|
// it does so, it should hook into the Reveal to determine when the
|
2016-03-25 05:22:44 +02:00
|
|
|
// constant should resolve; this will also require plumbing through to this
|
2016-06-30 21:22:47 +03:00
|
|
|
// function whether we are in "trans mode" to pick the right Reveal
|
2016-03-25 05:22:44 +02:00
|
|
|
// when constructing the inference context above.
|
|
|
|
match selection {
|
|
|
|
traits::VtableImpl(ref impl_data) => {
|
|
|
|
match tcx.associated_consts(impl_data.impl_def_id)
|
|
|
|
.iter().find(|ic| ic.name == ti.name) {
|
|
|
|
Some(ic) => lookup_const_by_id(tcx, ic.def_id, None),
|
|
|
|
None => match ti.node {
|
|
|
|
hir::ConstTraitItem(ref ty, Some(ref expr)) => {
|
|
|
|
Some((&*expr, tcx.ast_ty_to_prim_ty(ty)))
|
|
|
|
},
|
|
|
|
_ => None,
|
2016-03-03 14:48:08 +01:00
|
|
|
},
|
2016-03-25 05:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
span_bug!(ti.span,
|
|
|
|
"resolve_trait_associated_const: unexpected vtable type")
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 05:22:44 +02:00
|
|
|
})
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstInt, ty: ty::Ty) -> CastResult {
|
2015-12-16 18:44:15 +01:00
|
|
|
let v = val.to_u64_unchecked();
|
|
|
|
match ty.sty {
|
|
|
|
ty::TyBool if v == 0 => Ok(Bool(false)),
|
|
|
|
ty::TyBool if v == 1 => Ok(Bool(true)),
|
2016-03-09 19:27:36 +01:00
|
|
|
ty::TyInt(ast::IntTy::I8) => Ok(Integral(I8(v as i64 as i8))),
|
|
|
|
ty::TyInt(ast::IntTy::I16) => Ok(Integral(I16(v as i64 as i16))),
|
|
|
|
ty::TyInt(ast::IntTy::I32) => Ok(Integral(I32(v as i64 as i32))),
|
2015-12-16 18:44:15 +01:00
|
|
|
ty::TyInt(ast::IntTy::I64) => Ok(Integral(I64(v as i64))),
|
|
|
|
ty::TyInt(ast::IntTy::Is) => {
|
2016-06-09 17:01:05 -04:00
|
|
|
Ok(Integral(Isize(ConstIsize::new_truncating(v as i64, tcx.sess.target.int_type))))
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
|
|
|
ty::TyUint(ast::UintTy::U8) => Ok(Integral(U8(v as u8))),
|
|
|
|
ty::TyUint(ast::UintTy::U16) => Ok(Integral(U16(v as u16))),
|
|
|
|
ty::TyUint(ast::UintTy::U32) => Ok(Integral(U32(v as u32))),
|
2016-03-09 19:27:36 +01:00
|
|
|
ty::TyUint(ast::UintTy::U64) => Ok(Integral(U64(v))),
|
2015-12-16 18:44:15 +01:00
|
|
|
ty::TyUint(ast::UintTy::Us) => {
|
2016-06-09 17:01:05 -04:00
|
|
|
Ok(Integral(Usize(ConstUsize::new_truncating(v, tcx.sess.target.uint_type))))
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
2016-05-02 11:26:29 +02:00
|
|
|
ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
|
2016-06-01 12:22:07 +03:00
|
|
|
Infer(u) => Ok(Float(F64(u as f64))),
|
|
|
|
InferSigned(i) => Ok(Float(F64(i as f64))),
|
2016-05-02 16:38:33 +02:00
|
|
|
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"),
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
2016-05-02 11:26:29 +02:00
|
|
|
ty::TyFloat(ast::FloatTy::F32) => match val.erase_type() {
|
2016-06-01 12:22:07 +03:00
|
|
|
Infer(u) => Ok(Float(F32(u as f32))),
|
|
|
|
InferSigned(i) => Ok(Float(F32(i as f32))),
|
2016-05-02 16:38:33 +02:00
|
|
|
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"),
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
2016-04-01 09:19:29 +02:00
|
|
|
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting an address to a raw ptr")),
|
2016-05-02 16:38:33 +02:00
|
|
|
ty::TyChar => match infer(val, tcx, &ty::TyUint(ast::UintTy::U8)) {
|
|
|
|
Ok(U8(u)) => Ok(Char(u as char)),
|
|
|
|
// can only occur before typeck, typeck blocks `T as char` for `T` != `u8`
|
|
|
|
_ => Err(CharCast(val)),
|
|
|
|
},
|
2015-12-16 18:44:15 +01:00
|
|
|
_ => Err(CannotCast),
|
2015-03-31 17:55:28 +02:00
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
}
|
2015-03-31 17:55:28 +02:00
|
|
|
|
2016-06-01 12:22:07 +03:00
|
|
|
fn cast_const_float<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
val: ConstFloat,
|
|
|
|
ty: ty::Ty) -> CastResult {
|
2015-12-16 18:44:15 +01:00
|
|
|
match ty.sty {
|
2016-06-01 12:22:07 +03:00
|
|
|
ty::TyInt(_) | ty::TyUint(_) => {
|
|
|
|
let i = match val {
|
|
|
|
F32(f) if f >= 0.0 => Infer(f as u64),
|
|
|
|
FInfer { f64: f, .. } |
|
|
|
|
F64(f) if f >= 0.0 => Infer(f as u64),
|
|
|
|
|
|
|
|
F32(f) => InferSigned(f as i64),
|
|
|
|
FInfer { f64: f, .. } |
|
|
|
|
F64(f) => InferSigned(f as i64)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let (InferSigned(_), &ty::TyUint(_)) = (i, &ty.sty) {
|
|
|
|
return Err(CannotCast);
|
|
|
|
}
|
|
|
|
|
|
|
|
cast_const_int(tcx, i, ty)
|
|
|
|
}
|
|
|
|
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(F64(match val {
|
|
|
|
F32(f) => f as f64,
|
|
|
|
FInfer { f64: f, .. } | F64(f) => f
|
|
|
|
}))),
|
|
|
|
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(F32(match val {
|
|
|
|
F64(f) => f as f32,
|
|
|
|
FInfer { f32: f, .. } | F32(f) => f
|
|
|
|
}))),
|
2015-12-16 18:44:15 +01:00
|
|
|
_ => Err(CannotCast),
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
}
|
2015-01-29 13:40:14 +02:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn cast_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstVal, ty: ty::Ty) -> CastResult {
|
2015-12-16 18:44:15 +01:00
|
|
|
match val {
|
|
|
|
Integral(i) => cast_const_int(tcx, i, ty),
|
|
|
|
Bool(b) => cast_const_int(tcx, Infer(b as u64), ty),
|
|
|
|
Float(f) => cast_const_float(tcx, f, ty),
|
|
|
|
Char(c) => cast_const_int(tcx, Infer(c as u64), ty),
|
2016-04-01 09:19:29 +02:00
|
|
|
Function(_) => Err(UnimplementedConstVal("casting fn pointers")),
|
2016-07-22 09:34:44 +02:00
|
|
|
ByteStr(b) => match ty.sty {
|
2016-05-08 10:52:17 +02:00
|
|
|
ty::TyRawPtr(_) => {
|
|
|
|
Err(ErrKind::UnimplementedConstVal("casting a bytestr to a raw ptr"))
|
|
|
|
},
|
2016-07-22 09:34:44 +02:00
|
|
|
ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty {
|
|
|
|
ty::TyArray(ty, n) if ty == tcx.types.u8 && n == b.len() => Ok(ByteStr(b)),
|
|
|
|
ty::TySlice(_) => {
|
|
|
|
Err(ErrKind::UnimplementedConstVal("casting a bytestr to slice"))
|
|
|
|
},
|
|
|
|
_ => Err(CannotCast),
|
|
|
|
},
|
|
|
|
_ => Err(CannotCast),
|
|
|
|
},
|
|
|
|
Str(s) => match ty.sty {
|
|
|
|
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting a str to a raw ptr")),
|
|
|
|
ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty {
|
|
|
|
ty::TyStr => Ok(Str(s)),
|
|
|
|
_ => Err(CannotCast),
|
|
|
|
},
|
2016-05-08 10:52:17 +02:00
|
|
|
_ => Err(CannotCast),
|
|
|
|
},
|
2015-12-16 18:44:15 +01:00
|
|
|
_ => Err(CannotCast),
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
fn lit_to_const<'a, 'tcx>(lit: &ast::LitKind,
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
ty_hint: Option<Ty<'tcx>>,
|
|
|
|
span: Span)
|
|
|
|
-> Result<ConstVal, ErrKind> {
|
2015-12-16 18:44:15 +01:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ast::LitIntType::*;
|
|
|
|
match *lit {
|
|
|
|
LitKind::Str(ref s, _) => Ok(Str((*s).clone())),
|
|
|
|
LitKind::ByteStr(ref data) => Ok(ByteStr(data.clone())),
|
|
|
|
LitKind::Byte(n) => Ok(Integral(U8(n))),
|
2016-03-09 19:27:36 +01:00
|
|
|
LitKind::Int(n, Signed(ity)) => {
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral)
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
LitKind::Int(n, Unsuffixed) => {
|
|
|
|
match ty_hint.map(|t| &t.sty) {
|
|
|
|
Some(&ty::TyInt(ity)) => {
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral)
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
|
|
|
Some(&ty::TyUint(uty)) => {
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(Infer(n), tcx, &ty::TyUint(uty)).map(Integral)
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
|
|
|
None => Ok(Integral(Infer(n))),
|
2016-09-06 01:26:02 +03:00
|
|
|
Some(&ty::TyAdt(adt, _)) => {
|
2015-12-16 18:44:15 +01:00
|
|
|
let hints = tcx.lookup_repr_hints(adt.did);
|
|
|
|
let int_ty = tcx.enum_repr_type(hints.iter().next());
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(Infer(n), tcx, &int_ty.to_ty(tcx).sty).map(Integral)
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
2016-03-31 20:07:23 +02:00
|
|
|
Some(ty_hint) => bug!("bad ty_hint: {:?}, {:?}", ty_hint, lit),
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
2016-03-09 19:27:36 +01:00
|
|
|
LitKind::Int(n, Unsigned(ity)) => {
|
2016-05-02 16:40:40 +02:00
|
|
|
infer(Infer(n), tcx, &ty::TyUint(ity)).map(Integral)
|
2015-12-16 18:44:15 +01:00
|
|
|
},
|
|
|
|
|
2016-06-01 12:22:07 +03:00
|
|
|
LitKind::Float(ref n, fty) => {
|
|
|
|
Ok(Float(parse_float(n, Some(fty), span)))
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
LitKind::FloatUnsuffixed(ref n) => {
|
2016-06-01 12:22:07 +03:00
|
|
|
let fty_hint = match ty_hint.map(|t| &t.sty) {
|
|
|
|
Some(&ty::TyFloat(fty)) => Some(fty),
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
Ok(Float(parse_float(n, fty_hint, span)))
|
2014-01-15 17:15:39 -08:00
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
LitKind::Bool(b) => Ok(Bool(b)),
|
|
|
|
LitKind::Char(c) => Ok(Char(c)),
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 12:22:07 +03:00
|
|
|
fn parse_float(num: &str, fty_hint: Option<ast::FloatTy>, span: Span) -> ConstFloat {
|
|
|
|
let val = match fty_hint {
|
|
|
|
Some(ast::FloatTy::F32) => num.parse::<f32>().map(F32),
|
|
|
|
Some(ast::FloatTy::F64) => num.parse::<f64>().map(F64),
|
|
|
|
None => {
|
|
|
|
num.parse::<f32>().and_then(|f32| {
|
|
|
|
num.parse::<f64>().map(|f64| {
|
|
|
|
FInfer { f32: f32, f64: f64 }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
val.unwrap_or_else(|_| {
|
|
|
|
// FIXME(#31407) this is only necessary because float parsing is buggy
|
|
|
|
span_bug!(span, "could not evaluate float literal (see issue #31407)");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
pub fn compare_const_vals(tcx: TyCtxt, span: Span, a: &ConstVal, b: &ConstVal)
|
|
|
|
-> Result<Ordering, ErrorReported>
|
|
|
|
{
|
|
|
|
let result = match (a, b) {
|
2015-12-16 18:44:15 +01:00
|
|
|
(&Integral(a), &Integral(b)) => a.try_cmp(b).ok(),
|
2016-06-01 12:22:07 +03:00
|
|
|
(&Float(a), &Float(b)) => a.try_cmp(b).ok(),
|
2015-12-16 18:44:15 +01:00
|
|
|
(&Str(ref a), &Str(ref b)) => Some(a.cmp(b)),
|
|
|
|
(&Bool(a), &Bool(b)) => Some(a.cmp(&b)),
|
|
|
|
(&ByteStr(ref a), &ByteStr(ref b)) => Some(a.cmp(b)),
|
|
|
|
(&Char(a), &Char(ref b)) => Some(a.cmp(b)),
|
|
|
|
_ => None,
|
2016-07-20 00:02:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Some(result) => Ok(result),
|
|
|
|
None => {
|
|
|
|
// FIXME: can this ever be reached?
|
|
|
|
span_err!(tcx.sess, span, E0298,
|
|
|
|
"type mismatch comparing {} and {}",
|
|
|
|
a.description(),
|
|
|
|
b.description());
|
|
|
|
Err(ErrorReported)
|
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
}
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn compare_lit_exprs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-07-20 00:02:56 +03:00
|
|
|
span: Span,
|
2016-05-03 04:56:42 +03:00
|
|
|
a: &Expr,
|
2016-07-20 00:02:56 +03:00
|
|
|
b: &Expr) -> Result<Ordering, ErrorReported> {
|
2015-10-14 12:30:10 +02:00
|
|
|
let a = match eval_const_expr_partial(tcx, a, ExprTypeChecked, None) {
|
2015-01-29 13:40:14 +02:00
|
|
|
Ok(a) => a,
|
2015-02-22 16:34:26 +01:00
|
|
|
Err(e) => {
|
2016-07-20 00:02:56 +03:00
|
|
|
report_const_eval_err(tcx, &e, a.span, "expression").emit();
|
|
|
|
return Err(ErrorReported);
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
|
|
|
};
|
2015-10-14 12:30:10 +02:00
|
|
|
let b = match eval_const_expr_partial(tcx, b, ExprTypeChecked, None) {
|
2015-01-29 13:40:14 +02:00
|
|
|
Ok(b) => b,
|
2015-02-22 16:34:26 +01:00
|
|
|
Err(e) => {
|
2016-07-20 00:02:56 +03:00
|
|
|
report_const_eval_err(tcx, &e, b.span, "expression").emit();
|
|
|
|
return Err(ErrorReported);
|
2015-01-29 13:40:14 +02:00
|
|
|
}
|
|
|
|
};
|
2016-07-20 00:02:56 +03:00
|
|
|
compare_const_vals(tcx, span, &a, &b)
|
2012-03-22 14:56:56 -07:00
|
|
|
}
|
2016-03-30 13:43:36 +02:00
|
|
|
|
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
/// Returns the value of the length-valued expression
|
|
|
|
pub fn eval_length<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
count_expr: &hir::Expr,
|
|
|
|
reason: &str)
|
|
|
|
-> Result<usize, ErrorReported>
|
|
|
|
{
|
2016-03-30 13:43:36 +02:00
|
|
|
let hint = UncheckedExprHint(tcx.types.usize);
|
|
|
|
match eval_const_expr_partial(tcx, count_expr, hint, None) {
|
|
|
|
Ok(Integral(Usize(count))) => {
|
|
|
|
let val = count.as_u64(tcx.sess.target.uint_type);
|
|
|
|
assert_eq!(val as usize as u64, val);
|
2016-07-20 00:02:56 +03:00
|
|
|
Ok(val as usize)
|
2016-03-30 13:43:36 +02:00
|
|
|
},
|
|
|
|
Ok(const_val) => {
|
2016-08-05 16:54:05 +02:00
|
|
|
struct_span_err!(tcx.sess, count_expr.span, E0306,
|
|
|
|
"expected `usize` for {}, found {}",
|
|
|
|
reason,
|
|
|
|
const_val.description())
|
|
|
|
.span_label(count_expr.span, &format!("expected `usize`"))
|
|
|
|
.emit();
|
|
|
|
|
2016-07-20 00:02:56 +03:00
|
|
|
Err(ErrorReported)
|
2016-03-30 13:43:36 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2016-07-20 00:02:56 +03:00
|
|
|
let mut diag = report_const_eval_err(
|
|
|
|
tcx, &err, count_expr.span, reason);
|
|
|
|
|
|
|
|
match count_expr.node {
|
2016-03-30 13:43:36 +02:00
|
|
|
hir::ExprPath(None, hir::Path {
|
|
|
|
global: false,
|
|
|
|
ref segments,
|
|
|
|
..
|
2016-07-20 00:02:56 +03:00
|
|
|
}) if segments.len() == 1 => {
|
|
|
|
if let Some(Def::Local(..)) = tcx.expect_def_or_none(count_expr.id) {
|
|
|
|
diag.note(&format!("`{}` is a variable", segments[0].name));
|
|
|
|
}
|
2016-03-30 13:43:36 +02:00
|
|
|
}
|
2016-07-20 00:02:56 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
diag.emit();
|
|
|
|
Err(ErrorReported)
|
2016-03-30 13:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|