2014-12-30 22:36:03 +02:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2014-12-30 22:36:03 +02:00
|
|
|
// Verifies that the types and values of const and static items
|
|
|
|
// are safe. The rules enforced by this module are:
|
|
|
|
//
|
|
|
|
// - For each *mutable* static item, it checks that its **type**:
|
|
|
|
// - doesn't have a destructor
|
2015-06-09 16:26:21 -04:00
|
|
|
// - doesn't own a box
|
2014-12-30 22:36:03 +02:00
|
|
|
//
|
|
|
|
// - For each *immutable* static item, it checks that its **value**:
|
2015-06-09 16:26:21 -04:00
|
|
|
// - doesn't own a box
|
2014-12-30 22:36:03 +02:00
|
|
|
// - doesn't contain a struct literal or a call to an enum variant / struct constructor where
|
|
|
|
// - the type of the struct/enum has a dtor
|
|
|
|
//
|
|
|
|
// Rules Enforced Elsewhere:
|
|
|
|
// - It's not possible to take the address of a static item with unsafe interior. This is enforced
|
|
|
|
// by borrowck::gather_loans
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
use rustc::ty::cast::CastKind;
|
2017-04-13 16:40:03 +03:00
|
|
|
use rustc_const_eval::ConstContext;
|
|
|
|
use rustc::middle::const_val::ConstEvalErr;
|
|
|
|
use rustc::middle::const_val::ErrKind::{IndexOpFeatureGated, UnimplementedConstVal, MiscCatchAll};
|
|
|
|
use rustc::middle::const_val::ErrKind::{ErroneousReferencedConstant, MiscBinaryOp, NonConstPath};
|
2017-06-23 17:48:29 +03:00
|
|
|
use rustc::middle::const_val::ErrKind::{TypeckError, Math, LayoutError};
|
2016-04-26 14:10:07 +02:00
|
|
|
use rustc_const_math::{ConstMathErr, Op};
|
2016-09-15 00:51:46 +03:00
|
|
|
use rustc::hir::def::{Def, CtorKind};
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-12-20 23:05:21 +02:00
|
|
|
use rustc::hir::map::blocks::FnLikeNode;
|
2016-01-21 10:52:37 +01:00
|
|
|
use rustc::middle::expr_use_visitor as euv;
|
|
|
|
use rustc::middle::mem_categorization as mc;
|
|
|
|
use rustc::middle::mem_categorization::Categorization;
|
2016-12-20 23:05:21 +02:00
|
|
|
use rustc::mir::transform::MirSource;
|
2017-05-15 17:58:58 -04:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2017-07-26 15:54:44 +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-12-20 23:05:21 +02:00
|
|
|
use rustc::util::nodemap::NodeSet;
|
2016-01-21 10:52:37 +01:00
|
|
|
use rustc::lint::builtin::CONST_ERR;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2017-01-10 22:13:53 +01:00
|
|
|
use rustc::hir::{self, PatKind, RangeEnd};
|
2014-09-13 20:10:34 +03:00
|
|
|
use syntax::ast;
|
2017-04-17 21:18:56 +03:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2016-12-20 23:05:21 +02:00
|
|
|
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2013-08-13 02:10:10 +02:00
|
|
|
|
2015-01-29 13:57:06 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
2015-06-30 08:53:50 -07:00
|
|
|
use std::cmp::Ordering;
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2014-09-12 13:10:30 +03:00
|
|
|
struct CheckCrateVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-12-20 23:05:21 +02:00
|
|
|
in_fn: bool,
|
|
|
|
promotable: bool,
|
|
|
|
mut_rvalue_borrows: NodeSet,
|
2017-05-15 17:57:30 -04:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-07-26 15:54:44 +03:00
|
|
|
identity_substs: &'tcx Substs<'tcx>,
|
2017-01-25 16:24:00 -05:00
|
|
|
tables: &'a ty::TypeckTables<'tcx>,
|
2013-08-13 02:10:10 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
|
2017-07-26 15:54:44 +03:00
|
|
|
fn const_cx(&self) -> ConstContext<'a, 'gcx> {
|
2017-07-26 15:51:53 +03:00
|
|
|
ConstContext::new(self.tcx, self.param_env.and(self.identity_substs), self.tables)
|
2017-07-26 15:54:44 +03:00
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
fn check_const_eval(&self, expr: &'gcx hir::Expr) {
|
2017-07-26 15:54:44 +03:00
|
|
|
if let Err(err) = self.const_cx().eval(expr) {
|
2016-04-01 09:19:29 +02:00
|
|
|
match err.kind {
|
2016-07-21 07:01:14 +05:30
|
|
|
UnimplementedConstVal(_) => {}
|
|
|
|
IndexOpFeatureGated => {}
|
|
|
|
ErroneousReferencedConstant(_) => {}
|
2017-02-15 15:00:20 +02:00
|
|
|
TypeckError => {}
|
2016-07-21 07:01:14 +05:30
|
|
|
_ => {
|
2017-07-26 21:51:09 -07:00
|
|
|
self.tcx.lint_node(CONST_ERR,
|
|
|
|
expr.id,
|
|
|
|
expr.span,
|
|
|
|
&format!("constant evaluation error: {}. This will \
|
|
|
|
become a HARD ERROR in the future",
|
|
|
|
err.description().into_oneline()));
|
2016-07-21 07:01:14 +05:30
|
|
|
}
|
2016-04-01 09:19:29 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
// Adds the worst effect out of all the values of one type.
|
|
|
|
fn add_type(&mut self, ty: Ty<'gcx>) {
|
2017-05-10 10:28:06 -04:00
|
|
|
if !ty.is_freeze(self.tcx, self.param_env, DUMMY_SP) {
|
2016-12-20 23:05:21 +02:00
|
|
|
self.promotable = false;
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 10:28:06 -04:00
|
|
|
if ty.needs_drop(self.tcx, self.param_env) {
|
2016-12-20 23:05:21 +02:00
|
|
|
self.promotable = false;
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
fn handle_const_fn_call(&mut self, def_id: DefId, ret_ty: Ty<'gcx>) {
|
|
|
|
self.add_type(ret_ty);
|
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
self.promotable &= if let Some(fn_id) = self.tcx.hir.as_local_node_id(def_id) {
|
|
|
|
FnLikeNode::from_node(self.tcx.hir.get(fn_id)).map_or(false, |fn_like| {
|
2016-12-20 23:05:21 +02:00
|
|
|
fn_like.constness() == hir::Constness::Const
|
|
|
|
})
|
|
|
|
} else {
|
2017-06-11 21:16:26 -07:00
|
|
|
self.tcx.is_const_fn(def_id)
|
2016-12-20 23:05:21 +02:00
|
|
|
};
|
2013-08-13 02:10:10 +02:00
|
|
|
}
|
2014-09-12 13:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-10-29 15:01:11 +02:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2016-12-20 23:05:21 +02:00
|
|
|
NestedVisitorMap::None
|
2016-10-29 15:01:11 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
|
|
|
|
match self.tcx.rvalue_promotable_to_static.borrow_mut().entry(body_id.node_id) {
|
2016-12-20 23:05:21 +02:00
|
|
|
Entry::Occupied(_) => return,
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
// Prevent infinite recursion on re-entry.
|
|
|
|
entry.insert(false);
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
2015-01-04 17:58:56 +02:00
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let item_id = self.tcx.hir.body_owner(body_id);
|
2017-07-26 15:54:44 +03:00
|
|
|
let item_def_id = self.tcx.hir.local_def_id(item_id);
|
2015-03-15 19:35:25 -06:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
let outer_in_fn = self.in_fn;
|
2017-07-26 15:54:44 +03:00
|
|
|
let outer_tables = self.tables;
|
|
|
|
let outer_param_env = self.param_env;
|
|
|
|
let outer_identity_substs = self.identity_substs;
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
self.in_fn = match MirSource::from_node(self.tcx, item_id) {
|
|
|
|
MirSource::Fn(_) => true,
|
|
|
|
_ => false
|
|
|
|
};
|
2017-04-10 00:00:08 -07:00
|
|
|
self.tables = self.tcx.typeck_tables_of(item_def_id);
|
2017-07-26 15:54:44 +03:00
|
|
|
self.param_env = self.tcx.param_env(item_def_id);
|
|
|
|
self.identity_substs = Substs::identity_for_item(self.tcx, item_def_id);
|
2017-01-06 21:54:24 +02:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let body = self.tcx.hir.body(body_id);
|
2016-12-20 23:05:21 +02:00
|
|
|
if !self.in_fn {
|
|
|
|
self.check_const_eval(&body.value);
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2017-06-08 23:57:16 +03:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let param_env = self.param_env;
|
|
|
|
let region_maps = self.tcx.region_maps(item_def_id);
|
|
|
|
euv::ExprUseVisitor::new(self, tcx, param_env, ®ion_maps, self.tables)
|
|
|
|
.consume_body(body);
|
2016-12-20 23:05:21 +02:00
|
|
|
|
|
|
|
self.visit_body(body);
|
|
|
|
|
|
|
|
self.in_fn = outer_in_fn;
|
2017-07-26 15:54:44 +03:00
|
|
|
self.tables = outer_tables;
|
|
|
|
self.param_env = outer_param_env;
|
|
|
|
self.identity_substs = outer_identity_substs;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 15:01:11 +02:00
|
|
|
fn visit_pat(&mut self, p: &'tcx hir::Pat) {
|
2015-01-29 13:57:06 +02:00
|
|
|
match p.node {
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Lit(ref lit) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
self.check_const_eval(lit);
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2017-01-10 22:13:53 +01:00
|
|
|
PatKind::Range(ref start, ref end, RangeEnd::Excluded) => {
|
2017-07-26 15:54:44 +03:00
|
|
|
match self.const_cx().compare_lit_exprs(p.span, start, end) {
|
2017-01-10 22:13:53 +01:00
|
|
|
Ok(Ordering::Less) => {}
|
|
|
|
Ok(Ordering::Equal) |
|
|
|
|
Ok(Ordering::Greater) => {
|
|
|
|
span_err!(self.tcx.sess,
|
|
|
|
start.span,
|
|
|
|
E0579,
|
|
|
|
"lower range bound must be less than upper");
|
|
|
|
}
|
|
|
|
Err(ErrorReported) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PatKind::Range(ref start, ref end, RangeEnd::Included) => {
|
2017-07-26 15:54:44 +03:00
|
|
|
match self.const_cx().compare_lit_exprs(p.span, start, end) {
|
2016-07-20 00:02:56 +03:00
|
|
|
Ok(Ordering::Less) |
|
|
|
|
Ok(Ordering::Equal) => {}
|
|
|
|
Ok(Ordering::Greater) => {
|
2016-08-15 00:21:13 -06:00
|
|
|
struct_span_err!(self.tcx.sess, start.span, E0030,
|
|
|
|
"lower range bound must be less than or equal to upper")
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(start.span, "lower bound larger than upper bound")
|
2016-08-15 00:21:13 -06:00
|
|
|
.emit();
|
2015-06-30 08:53:50 -07:00
|
|
|
}
|
2016-07-20 00:02:56 +03:00
|
|
|
Err(ErrorReported) => {}
|
2015-06-30 08:53:50 -07:00
|
|
|
}
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
_ => {}
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
intravisit::walk_pat(self, p);
|
2014-09-12 13:10:30 +03:00
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
|
|
|
|
match stmt.node {
|
|
|
|
hir::StmtDecl(ref decl, _) => {
|
|
|
|
match decl.node {
|
|
|
|
hir::DeclLocal(_) => {
|
|
|
|
self.promotable = false;
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
// Item statements are allowed
|
|
|
|
hir::DeclItem(_) => {}
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
hir::StmtExpr(..) |
|
|
|
|
hir::StmtSemi(..) => {
|
|
|
|
self.promotable = false;
|
|
|
|
}
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
intravisit::walk_stmt(self, stmt);
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 15:01:11 +02:00
|
|
|
fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
|
2016-12-20 23:05:21 +02:00
|
|
|
let outer = self.promotable;
|
|
|
|
self.promotable = true;
|
2015-01-29 13:57:06 +02:00
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
let node_ty = self.tables.node_id_to_type(ex.id);
|
2015-01-29 13:57:06 +02:00
|
|
|
check_expr(self, ex, node_ty);
|
2015-07-07 18:45:52 +03:00
|
|
|
check_adjustments(self, ex);
|
2015-01-29 13:57:06 +02:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
if let hir::ExprMatch(ref discr, ref arms, _) = ex.node {
|
|
|
|
// Compute the most demanding borrow from all the arms'
|
|
|
|
// patterns and set that on the discriminator.
|
|
|
|
let mut mut_borrow = false;
|
|
|
|
for pat in arms.iter().flat_map(|arm| &arm.pats) {
|
|
|
|
if self.mut_rvalue_borrows.remove(&pat.id) {
|
|
|
|
mut_borrow = true;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
if mut_borrow {
|
|
|
|
self.mut_rvalue_borrows.insert(discr.id);
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
intravisit::walk_expr(self, ex);
|
|
|
|
|
2015-01-29 13:57:06 +02:00
|
|
|
// Handle borrows on (or inside the autorefs of) this expression.
|
2016-12-20 23:05:21 +02:00
|
|
|
if self.mut_rvalue_borrows.remove(&ex.id) {
|
|
|
|
self.promotable = false;
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2016-04-26 11:18:48 +02:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
if self.in_fn && self.promotable {
|
2017-07-26 15:54:44 +03:00
|
|
|
match self.const_cx().eval(ex) {
|
2016-04-26 11:18:48 +02:00
|
|
|
Ok(_) => {}
|
2016-07-21 07:01:14 +05:30
|
|
|
Err(ConstEvalErr { kind: UnimplementedConstVal(_), .. }) |
|
|
|
|
Err(ConstEvalErr { kind: MiscCatchAll, .. }) |
|
|
|
|
Err(ConstEvalErr { kind: MiscBinaryOp, .. }) |
|
|
|
|
Err(ConstEvalErr { kind: NonConstPath, .. }) |
|
|
|
|
Err(ConstEvalErr { kind: ErroneousReferencedConstant(_), .. }) |
|
|
|
|
Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shr)), .. }) |
|
|
|
|
Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shl)), .. }) |
|
|
|
|
Err(ConstEvalErr { kind: IndexOpFeatureGated, .. }) => {}
|
2017-02-15 15:00:20 +02:00
|
|
|
Err(ConstEvalErr { kind: TypeckError, .. }) => {}
|
2017-06-23 17:48:29 +03:00
|
|
|
Err(ConstEvalErr {
|
|
|
|
kind: LayoutError(ty::layout::LayoutError::Unknown(_)), ..
|
|
|
|
}) => {}
|
2016-04-26 11:18:48 +02:00
|
|
|
Err(msg) => {
|
2017-07-26 21:51:09 -07:00
|
|
|
self.tcx.lint_node(CONST_ERR,
|
|
|
|
ex.id,
|
|
|
|
msg.span,
|
|
|
|
&msg.description().into_oneline().into_owned());
|
2016-04-26 11:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
self.tcx.rvalue_promotable_to_static.borrow_mut().insert(ex.id, self.promotable);
|
|
|
|
self.promotable &= outer;
|
2013-08-13 02:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-08 18:26:02 -08:00
|
|
|
|
2014-12-30 22:36:03 +02:00
|
|
|
/// This function is used to enforce the constraints on
|
|
|
|
/// const/static items. It walks through the *value*
|
|
|
|
/// of the item walking down the expression and evaluating
|
|
|
|
/// every nested expression. If the expression is not part
|
2015-01-29 13:57:06 +02:00
|
|
|
/// of a const/static item, it is qualified for promotion
|
|
|
|
/// instead of producing errors.
|
2016-07-21 07:01:14 +05:30
|
|
|
fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node_ty: Ty<'tcx>) {
|
2014-12-30 22:36:03 +02:00
|
|
|
match node_ty.sty {
|
2017-02-19 14:46:29 +02:00
|
|
|
ty::TyAdt(def, _) if def.has_dtor(v.tcx) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2014-10-06 21:32:58 -07:00
|
|
|
match e.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprUnary(..) |
|
|
|
|
hir::ExprBinary(..) |
|
2017-05-20 16:11:07 +03:00
|
|
|
hir::ExprIndex(..) if v.tables.is_method_call(e) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2015-09-24 18:00:08 +03:00
|
|
|
hir::ExprBox(_) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprUnary(op, ref inner) => {
|
2017-01-06 21:54:24 +02:00
|
|
|
match v.tables.node_id_to_type(inner.id).sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyRawPtr(_) => {
|
2015-07-31 00:04:06 -07:00
|
|
|
assert!(op == hir::UnDeref);
|
2015-05-28 00:35:56 +03:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2015-05-28 00:35:56 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBinary(op, ref lhs, _) => {
|
2017-01-06 21:54:24 +02:00
|
|
|
match v.tables.node_id_to_type(lhs.id).sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyRawPtr(_) => {
|
2015-07-31 00:04:06 -07:00
|
|
|
assert!(op.node == hir::BiEq || op.node == hir::BiNe ||
|
|
|
|
op.node == hir::BiLe || op.node == hir::BiLt ||
|
|
|
|
op.node == hir::BiGe || op.node == hir::BiGt);
|
2015-05-28 00:35:56 +03:00
|
|
|
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
2012-01-26 12:26:14 +01:00
|
|
|
}
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprCast(ref from, _) => {
|
2015-05-05 19:36:47 +03:00
|
|
|
debug!("Checking const cast(id={})", from.id);
|
2017-01-27 16:16:43 -05:00
|
|
|
match v.tables.cast_kinds.get(&from.id) {
|
2016-03-28 23:03:47 +02:00
|
|
|
None => span_bug!(e.span, "no kind for cast"),
|
2015-05-14 15:04:49 +03:00
|
|
|
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2015-05-05 19:36:47 +03:00
|
|
|
_ => {}
|
2012-03-14 18:04:03 +01:00
|
|
|
}
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
hir::ExprPath(ref qpath) => {
|
2017-08-04 09:49:40 +02:00
|
|
|
let def = v.tables.qpath_def(qpath, e.hir_id);
|
2016-11-25 13:21:19 +02:00
|
|
|
match def {
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::VariantCtor(..) | Def::StructCtor(..) |
|
|
|
|
Def::Fn(..) | Def::Method(..) => {}
|
2016-12-20 23:05:21 +02:00
|
|
|
Def::AssociatedConst(_) => v.add_type(node_ty),
|
|
|
|
Def::Const(did) => {
|
2017-01-26 02:41:06 +02:00
|
|
|
v.promotable &= if let Some(node_id) = v.tcx.hir.as_local_node_id(did) {
|
|
|
|
match v.tcx.hir.expect_item(node_id).node {
|
2016-12-20 23:05:21 +02:00
|
|
|
hir::ItemConst(_, body) => {
|
|
|
|
v.visit_nested_body(body);
|
|
|
|
v.tcx.rvalue_promotable_to_static.borrow()[&body.node_id]
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-28 01:08:48 -07:00
|
|
|
v.tcx.const_is_rvalue_promotable_to_static(did)
|
2016-12-20 23:05:21 +02:00
|
|
|
};
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
2016-05-07 19:14:28 +03:00
|
|
|
_ => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2012-11-30 16:29:28 -08:00
|
|
|
}
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprCall(ref callee, _) => {
|
2015-01-29 13:57:06 +02:00
|
|
|
let mut callee = &**callee;
|
|
|
|
loop {
|
|
|
|
callee = match callee.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBlock(ref block) => match block.expr {
|
2016-02-09 21:30:52 +01:00
|
|
|
Some(ref tail) => &tail,
|
2015-01-29 13:57:06 +02:00
|
|
|
None => break
|
|
|
|
},
|
|
|
|
_ => break
|
|
|
|
};
|
|
|
|
}
|
2016-06-03 23:15:00 +03:00
|
|
|
// The callee is an arbitrary expression, it doesn't necessarily have a definition.
|
2016-11-25 13:21:19 +02:00
|
|
|
let def = if let hir::ExprPath(ref qpath) = callee.node {
|
2017-08-04 09:49:40 +02:00
|
|
|
v.tables.qpath_def(qpath, callee.hir_id)
|
2016-11-25 13:21:19 +02:00
|
|
|
} else {
|
|
|
|
Def::Err
|
|
|
|
};
|
2016-12-20 23:05:21 +02:00
|
|
|
match def {
|
2016-11-25 13:21:19 +02:00
|
|
|
Def::StructCtor(_, CtorKind::Fn) |
|
2016-12-20 23:05:21 +02:00
|
|
|
Def::VariantCtor(_, CtorKind::Fn) => {}
|
2016-11-25 13:21:19 +02:00
|
|
|
Def::Fn(did) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.handle_const_fn_call(did, node_ty)
|
2015-02-25 22:06:08 +02:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
Def::Method(did) => {
|
2016-11-10 02:06:34 +02:00
|
|
|
match v.tcx.associated_item(did).container {
|
2015-08-04 01:16:53 +03:00
|
|
|
ty::ImplContainer(_) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.handle_const_fn_call(did, node_ty)
|
2015-08-04 01:16:53 +03:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
ty::TraitContainer(_) => v.promotable = false
|
2015-08-04 01:16:53 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
_ => v.promotable = false
|
2012-04-04 15:02:25 -07:00
|
|
|
}
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprMethodCall(..) => {
|
2017-08-04 09:49:40 +02:00
|
|
|
v.tables.validate_hir_id(e.hir_id);
|
|
|
|
let def_id = v.tables.type_dependent_defs[&e.hir_id.local_id].def_id();
|
2017-05-20 16:11:07 +03:00
|
|
|
match v.tcx.associated_item(def_id).container {
|
|
|
|
ty::ImplContainer(_) => v.handle_const_fn_call(def_id, node_ty),
|
2016-12-20 23:05:21 +02:00
|
|
|
ty::TraitContainer(_) => v.promotable = false
|
2014-05-04 10:39:11 +02:00
|
|
|
}
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprStruct(..) => {
|
2017-01-06 21:54:24 +02:00
|
|
|
if let ty::TyAdt(adt, ..) = v.tables.expr_ty(e).sty {
|
2016-09-15 00:51:46 +03:00
|
|
|
// unsafe_cell_type doesn't necessarily exist with no_core
|
|
|
|
if Some(adt.did) == v.tcx.lang_items.unsafe_cell_type() {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2016-09-15 00:51:46 +03:00
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprLit(_) |
|
2016-12-20 23:05:21 +02:00
|
|
|
hir::ExprAddrOf(..) |
|
|
|
|
hir::ExprRepeat(..) => {}
|
2015-01-29 13:57:06 +02:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprClosure(..) => {
|
2015-02-25 22:06:08 +02:00
|
|
|
// Paths in constant contexts cannot refer to local variables,
|
2015-01-29 13:57:06 +02:00
|
|
|
// as there are none, and thus closures can't have upvars there.
|
2015-06-25 23:42:17 +03:00
|
|
|
if v.tcx.with_freevars(e.id, |fv| !fv.is_empty()) {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBlock(_) |
|
|
|
|
hir::ExprIndex(..) |
|
|
|
|
hir::ExprField(..) |
|
|
|
|
hir::ExprTupField(..) |
|
2016-09-20 02:14:46 +02:00
|
|
|
hir::ExprArray(_) |
|
2015-02-01 09:59:46 +02:00
|
|
|
hir::ExprType(..) |
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprTup(..) => {}
|
2014-09-13 20:10:34 +03:00
|
|
|
|
2014-12-30 22:36:03 +02:00
|
|
|
// Conditional control flow (possible to implement).
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprMatch(..) |
|
|
|
|
hir::ExprIf(..) |
|
2014-10-06 21:32:58 -07:00
|
|
|
|
2014-12-30 22:36:03 +02:00
|
|
|
// Loops (not very meaningful in constants).
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprWhile(..) |
|
|
|
|
hir::ExprLoop(..) |
|
2014-12-30 22:36:03 +02:00
|
|
|
|
|
|
|
// More control flow (also not very meaningful).
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 15:15:06 -07:00
|
|
|
hir::ExprBreak(..) |
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprAgain(_) |
|
|
|
|
hir::ExprRet(_) |
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2015-02-25 22:06:08 +02:00
|
|
|
// Expressions with side-effects.
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprAssign(..) |
|
|
|
|
hir::ExprAssignOp(..) |
|
2016-03-09 22:17:02 +02:00
|
|
|
hir::ExprInlineAsm(..) => {
|
2016-12-20 23:05:21 +02:00
|
|
|
v.promotable = false;
|
2014-10-06 21:32:58 -07:00
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-16 01:58:51 -05:00
|
|
|
|
2015-07-07 18:45:52 +03:00
|
|
|
/// Check the adjustments of an expression
|
2015-07-31 00:04:06 -07:00
|
|
|
fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr) {
|
2016-10-20 06:33:20 +03:00
|
|
|
use rustc::ty::adjustment::*;
|
|
|
|
|
2017-05-27 10:29:24 +03:00
|
|
|
for adjustment in v.tables.expr_adjustments(e) {
|
|
|
|
match adjustment.kind {
|
|
|
|
Adjust::NeverToAny |
|
|
|
|
Adjust::ReifyFnPointer |
|
|
|
|
Adjust::UnsafeFnPointer |
|
|
|
|
Adjust::ClosureFnPointer |
|
|
|
|
Adjust::MutToConstPointer |
|
|
|
|
Adjust::Borrow(_) |
|
|
|
|
Adjust::Unsize => {}
|
|
|
|
|
|
|
|
Adjust::Deref(ref overloaded) => {
|
|
|
|
if overloaded.is_some() {
|
|
|
|
v.promotable = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-07 18:45:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2017-04-18 05:57:39 -04:00
|
|
|
tcx.hir.krate().visit_all_item_likes(&mut CheckCrateVisitor {
|
|
|
|
tcx: tcx,
|
2017-08-04 09:49:40 +02:00
|
|
|
tables: &ty::TypeckTables::empty(DefId::invalid()),
|
2017-04-18 05:57:39 -04:00
|
|
|
in_fn: false,
|
|
|
|
promotable: false,
|
|
|
|
mut_rvalue_borrows: NodeSet(),
|
2017-05-17 08:01:04 -04:00
|
|
|
param_env: ty::ParamEnv::empty(Reveal::UserFacing),
|
2017-07-26 15:54:44 +03:00
|
|
|
identity_substs: Substs::empty(),
|
2017-04-18 05:57:39 -04:00
|
|
|
}.as_deep_visitor());
|
2014-12-30 22:36:03 +02:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
|
2014-12-30 22:36:03 +02:00
|
|
|
fn consume(&mut self,
|
|
|
|
_consume_id: ast::NodeId,
|
2016-05-07 19:14:28 +03:00
|
|
|
_consume_span: Span,
|
2016-12-20 23:05:21 +02:00
|
|
|
_cmt: mc::cmt,
|
|
|
|
_mode: euv::ConsumeMode) {}
|
2014-12-30 22:36:03 +02:00
|
|
|
|
|
|
|
fn borrow(&mut self,
|
2015-01-29 13:57:06 +02:00
|
|
|
borrow_id: ast::NodeId,
|
2016-05-07 19:14:28 +03:00
|
|
|
_borrow_span: Span,
|
2014-12-30 22:36:03 +02:00
|
|
|
cmt: mc::cmt<'tcx>,
|
2017-04-20 04:45:53 -04:00
|
|
|
_loan_region: ty::Region<'tcx>,
|
2015-01-29 13:57:06 +02:00
|
|
|
bk: ty::BorrowKind,
|
2016-07-21 07:01:14 +05:30
|
|
|
loan_cause: euv::LoanCause) {
|
2015-04-08 04:31:51 -04:00
|
|
|
// Kind of hacky, but we allow Unsafe coercions in constants.
|
|
|
|
// These occur when we convert a &T or *T to a *U, as well as
|
|
|
|
// when making a thin pointer (e.g., `*T`) into a fat pointer
|
|
|
|
// (e.g., `*Trait`).
|
|
|
|
match loan_cause {
|
|
|
|
euv::LoanCause::AutoUnsafe => {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 07:01:14 +05:30
|
|
|
_ => {}
|
2015-04-08 04:31:51 -04:00
|
|
|
}
|
|
|
|
|
2014-12-30 22:36:03 +02:00
|
|
|
let mut cur = &cmt;
|
|
|
|
loop {
|
|
|
|
match cur.cat {
|
2015-10-03 21:36:16 +02:00
|
|
|
Categorization::Rvalue(..) => {
|
2015-01-29 13:57:06 +02:00
|
|
|
if loan_cause == euv::MatchDiscriminant {
|
|
|
|
// Ignore the dummy immutable borrow created by EUV.
|
|
|
|
break;
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
2016-12-20 23:05:21 +02:00
|
|
|
if bk.to_mutbl_lossy() == hir::MutMutable {
|
|
|
|
self.mut_rvalue_borrows.insert(borrow_id);
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-10-03 21:36:16 +02:00
|
|
|
Categorization::StaticItem => {
|
2014-12-30 22:36:03 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-05-16 17:54:03 +03:00
|
|
|
Categorization::Deref(ref cmt, _) |
|
2015-10-03 21:36:16 +02:00
|
|
|
Categorization::Downcast(ref cmt, _) |
|
|
|
|
Categorization::Interior(ref cmt, _) => {
|
2014-12-30 22:36:03 +02:00
|
|
|
cur = cmt;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:36:16 +02:00
|
|
|
Categorization::Upvar(..) |
|
2016-07-21 07:01:14 +05:30
|
|
|
Categorization::Local(..) => break,
|
2014-12-30 22:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {}
|
2014-12-30 22:36:03 +02:00
|
|
|
fn mutate(&mut self,
|
|
|
|
_assignment_id: ast::NodeId,
|
|
|
|
_assignment_span: Span,
|
|
|
|
_assignee_cmt: mc::cmt,
|
2016-07-21 07:01:14 +05:30
|
|
|
_mode: euv::MutateMode) {
|
|
|
|
}
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
fn matched_pat(&mut self, _: &hir::Pat, _: mc::cmt, _: euv::MatchMode) {}
|
2014-12-30 22:36:03 +02:00
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: mc::cmt, _mode: euv::ConsumeMode) {}
|
2015-01-29 13:57:06 +02:00
|
|
|
}
|