550 lines
20 KiB
Rust
Raw Normal View History

// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <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.
// 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
// - doesn't own a box
//
// - For each *immutable* static item, it checks that its **value**:
// - doesn't own a box
// - 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
2016-07-21 07:01:14 +05:30
use rustc::ty::cast::CastKind;
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};
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::DefId;
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;
use rustc::mir::transform::MirSource;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
use rustc::traits::Reveal;
use rustc::util::common::ErrorReported;
use rustc::util::nodemap::NodeSet;
2016-01-21 10:52:37 +01:00
use rustc::lint::builtin::CONST_ERR;
use rustc::hir::{self, PatKind, RangeEnd};
use syntax::ast;
use syntax_pos::{Span, DUMMY_SP};
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use std::collections::hash_map::Entry;
use std::cmp::Ordering;
struct CheckCrateVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
in_fn: bool,
promotable: bool,
mut_rvalue_borrows: NodeSet,
param_env: ty::ParamEnv<'tcx>,
identity_substs: &'tcx Substs<'tcx>,
2017-01-25 16:24:00 -05:00
tables: &'a ty::TypeckTables<'tcx>,
}
impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
fn const_cx(&self) -> ConstContext<'a, 'gcx> {
ConstContext::new(self.tcx, self.param_env.and(self.identity_substs), self.tables)
}
fn check_const_eval(&self, expr: &'gcx hir::Expr) {
if let Err(err) = self.const_cx().eval(expr) {
match err.kind {
2016-07-21 07:01:14 +05:30
UnimplementedConstVal(_) => {}
IndexOpFeatureGated => {}
ErroneousReferencedConstant(_) => {}
TypeckError => {}
2016-07-21 07:01:14 +05:30
_ => {
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
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
}
}
}
}
// Adds the worst effect out of all the values of one type.
fn add_type(&mut self, ty: Ty<'gcx>) {
if !ty.is_freeze(self.tcx, self.param_env, DUMMY_SP) {
self.promotable = false;
}
if ty.needs_drop(self.tcx, self.param_env) {
self.promotable = false;
}
}
fn handle_const_fn_call(&mut self, def_id: DefId, ret_ty: Ty<'gcx>) {
self.add_type(ret_ty);
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| {
fn_like.constness() == hir::Constness::Const
})
} else {
2017-06-11 21:16:26 -07:00
self.tcx.is_const_fn(def_id)
};
}
}
2016-10-29 15:01:11 +02:00
impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::None
2016-10-29 15:01:11 +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) {
Entry::Occupied(_) => return,
Entry::Vacant(entry) => {
// Prevent infinite recursion on re-entry.
entry.insert(false);
}
}
let item_id = self.tcx.hir.body_owner(body_id);
let item_def_id = self.tcx.hir.local_def_id(item_id);
let outer_in_fn = self.in_fn;
let outer_tables = self.tables;
let outer_param_env = self.param_env;
let outer_identity_substs = self.identity_substs;
self.in_fn = match MirSource::from_node(self.tcx, item_id) {
MirSource::Fn(_) => true,
_ => false
};
self.tables = self.tcx.typeck_tables_of(item_def_id);
self.param_env = self.tcx.param_env(item_def_id);
self.identity_substs = Substs::identity_for_item(self.tcx, item_def_id);
let body = self.tcx.hir.body(body_id);
if !self.in_fn {
self.check_const_eval(&body.value);
}
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, &region_maps, self.tables)
.consume_body(body);
self.visit_body(body);
self.in_fn = outer_in_fn;
self.tables = outer_tables;
self.param_env = outer_param_env;
self.identity_substs = outer_identity_substs;
}
2016-10-29 15:01:11 +02:00
fn visit_pat(&mut self, p: &'tcx hir::Pat) {
match p.node {
2016-02-14 15:25:12 +03:00
PatKind::Lit(ref lit) => {
self.check_const_eval(lit);
}
PatKind::Range(ref start, ref end, RangeEnd::Excluded) => {
match self.const_cx().compare_lit_exprs(p.span, start, end) {
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) => {
match self.const_cx().compare_lit_exprs(p.span, start, end) {
Ok(Ordering::Less) |
Ok(Ordering::Equal) => {}
Ok(Ordering::Greater) => {
struct_span_err!(self.tcx.sess, start.span, E0030,
"lower range bound must be less than or equal to upper")
.span_label(start.span, "lower bound larger than upper bound")
.emit();
}
Err(ErrorReported) => {}
}
}
_ => {}
}
intravisit::walk_pat(self, p);
}
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
match stmt.node {
hir::StmtDecl(ref decl, _) => {
match decl.node {
hir::DeclLocal(_) => {
self.promotable = false;
}
// Item statements are allowed
hir::DeclItem(_) => {}
}
}
hir::StmtExpr(..) |
hir::StmtSemi(..) => {
self.promotable = false;
}
}
intravisit::walk_stmt(self, stmt);
}
2016-10-29 15:01:11 +02:00
fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
let outer = self.promotable;
self.promotable = true;
let node_ty = self.tables.node_id_to_type(ex.id);
check_expr(self, ex, node_ty);
check_adjustments(self, ex);
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;
}
}
if mut_borrow {
self.mut_rvalue_borrows.insert(discr.id);
}
}
intravisit::walk_expr(self, ex);
// Handle borrows on (or inside the autorefs of) this expression.
if self.mut_rvalue_borrows.remove(&ex.id) {
self.promotable = false;
}
if self.in_fn && self.promotable {
match self.const_cx().eval(ex) {
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, .. }) => {}
Err(ConstEvalErr { kind: TypeckError, .. }) => {}
2017-06-23 17:48:29 +03:00
Err(ConstEvalErr {
kind: LayoutError(ty::layout::LayoutError::Unknown(_)), ..
}) => {}
Err(msg) => {
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
2017-07-26 21:51:09 -07:00
self.tcx.lint_node(CONST_ERR,
ex.id,
msg.span,
&msg.description().into_oneline().into_owned());
}
}
}
self.tcx.rvalue_promotable_to_static.borrow_mut().insert(ex.id, self.promotable);
self.promotable &= outer;
}
}
/// 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
/// 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>) {
match node_ty.sty {
ty::TyAdt(def, _) if def.has_dtor(v.tcx) => {
v.promotable = false;
}
_ => {}
}
match e.node {
2015-07-31 00:04:06 -07:00
hir::ExprUnary(..) |
hir::ExprBinary(..) |
hir::ExprIndex(..) if v.tables.is_method_call(e) => {
v.promotable = false;
}
hir::ExprBox(_) => {
v.promotable = false;
}
2015-07-31 00:04:06 -07:00
hir::ExprUnary(op, ref inner) => {
match v.tables.node_id_to_type(inner.id).sty {
ty::TyRawPtr(_) => {
2015-07-31 00:04:06 -07:00
assert!(op == hir::UnDeref);
v.promotable = false;
}
_ => {}
}
}
2015-07-31 00:04:06 -07:00
hir::ExprBinary(op, ref lhs, _) => {
match v.tables.node_id_to_type(lhs.id).sty {
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);
v.promotable = false;
}
_ => {}
}
}
2015-07-31 00:04:06 -07:00
hir::ExprCast(ref from, _) => {
debug!("Checking const cast(id={})", from.id);
match v.tables.cast_kinds.get(&from.id) {
None => span_bug!(e.span, "no kind for cast"),
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
v.promotable = false;
}
_ => {}
}
}
hir::ExprPath(ref qpath) => {
let def = v.tables.qpath_def(qpath, e.hir_id);
match def {
Def::VariantCtor(..) | Def::StructCtor(..) |
Def::Fn(..) | Def::Method(..) => {}
Def::AssociatedConst(_) => v.add_type(node_ty),
Def::Const(did) => {
v.promotable &= if let Some(node_id) = v.tcx.hir.as_local_node_id(did) {
match v.tcx.hir.expect_item(node_id).node {
hir::ItemConst(_, body) => {
v.visit_nested_body(body);
v.tcx.rvalue_promotable_to_static.borrow()[&body.node_id]
}
_ => false
}
} else {
v.tcx.const_is_rvalue_promotable_to_static(did)
};
}
2016-05-07 19:14:28 +03:00
_ => {
v.promotable = false;
}
}
}
2015-07-31 00:04:06 -07:00
hir::ExprCall(ref callee, _) => {
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,
None => break
},
_ => break
};
}
// The callee is an arbitrary expression, it doesn't necessarily have a definition.
let def = if let hir::ExprPath(ref qpath) = callee.node {
v.tables.qpath_def(qpath, callee.hir_id)
} else {
Def::Err
};
match def {
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => {}
Def::Fn(did) => {
v.handle_const_fn_call(did, node_ty)
}
Def::Method(did) => {
match v.tcx.associated_item(did).container {
ty::ImplContainer(_) => {
v.handle_const_fn_call(did, node_ty)
}
ty::TraitContainer(_) => v.promotable = false
}
}
_ => v.promotable = false
}
}
2015-07-31 00:04:06 -07:00
hir::ExprMethodCall(..) => {
v.tables.validate_hir_id(e.hir_id);
let def_id = v.tables.type_dependent_defs[&e.hir_id.local_id].def_id();
match v.tcx.associated_item(def_id).container {
ty::ImplContainer(_) => v.handle_const_fn_call(def_id, node_ty),
ty::TraitContainer(_) => v.promotable = false
}
}
2015-07-31 00:04:06 -07:00
hir::ExprStruct(..) => {
if let ty::TyAdt(adt, ..) = v.tables.expr_ty(e).sty {
// unsafe_cell_type doesn't necessarily exist with no_core
if Some(adt.did) == v.tcx.lang_items.unsafe_cell_type() {
v.promotable = false;
}
}
}
2015-07-31 00:04:06 -07:00
hir::ExprLit(_) |
hir::ExprAddrOf(..) |
hir::ExprRepeat(..) => {}
2015-07-31 00:04:06 -07:00
hir::ExprClosure(..) => {
// Paths in constant contexts cannot refer to local variables,
// as there are none, and thus closures can't have upvars there.
if v.tcx.with_freevars(e.id, |fv| !fv.is_empty()) {
v.promotable = false;
}
}
2015-07-31 00:04:06 -07:00
hir::ExprBlock(_) |
hir::ExprIndex(..) |
hir::ExprField(..) |
hir::ExprTupField(..) |
hir::ExprArray(_) |
2015-02-01 09:59:46 +02:00
hir::ExprType(..) |
2015-07-31 00:04:06 -07:00
hir::ExprTup(..) => {}
// Conditional control flow (possible to implement).
2015-07-31 00:04:06 -07:00
hir::ExprMatch(..) |
hir::ExprIf(..) |
// Loops (not very meaningful in constants).
2015-07-31 00:04:06 -07:00
hir::ExprWhile(..) |
hir::ExprLoop(..) |
// More control flow (also not very meaningful).
hir::ExprBreak(..) |
2015-07-31 00:04:06 -07:00
hir::ExprAgain(_) |
hir::ExprRet(_) |
// Expressions with side-effects.
2015-07-31 00:04:06 -07:00
hir::ExprAssign(..) |
hir::ExprAssignOp(..) |
hir::ExprInlineAsm(..) => {
v.promotable = false;
}
}
}
/// 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) {
use rustc::ty::adjustment::*;
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;
}
}
}
}
}
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
tcx.hir.krate().visit_all_item_likes(&mut CheckCrateVisitor {
tcx: tcx,
tables: &ty::TypeckTables::empty(DefId::invalid()),
in_fn: false,
promotable: false,
mut_rvalue_borrows: NodeSet(),
param_env: ty::ParamEnv::empty(Reveal::UserFacing),
identity_substs: Substs::empty(),
}.as_deep_visitor());
tcx.sess.abort_if_errors();
}
impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
fn consume(&mut self,
_consume_id: ast::NodeId,
2016-05-07 19:14:28 +03:00
_consume_span: Span,
_cmt: mc::cmt,
_mode: euv::ConsumeMode) {}
fn borrow(&mut self,
borrow_id: ast::NodeId,
2016-05-07 19:14:28 +03:00
_borrow_span: Span,
cmt: mc::cmt<'tcx>,
_loan_region: ty::Region<'tcx>,
bk: ty::BorrowKind,
2016-07-21 07:01:14 +05:30
loan_cause: euv::LoanCause) {
// 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
_ => {}
}
let mut cur = &cmt;
loop {
match cur.cat {
Categorization::Rvalue(..) => {
if loan_cause == euv::MatchDiscriminant {
// Ignore the dummy immutable borrow created by EUV.
break;
}
if bk.to_mutbl_lossy() == hir::MutMutable {
self.mut_rvalue_borrows.insert(borrow_id);
}
break;
}
Categorization::StaticItem => {
break;
}
Categorization::Deref(ref cmt, _) |
Categorization::Downcast(ref cmt, _) |
Categorization::Interior(ref cmt, _) => {
cur = cmt;
}
Categorization::Upvar(..) |
2016-07-21 07:01:14 +05:30
Categorization::Local(..) => break,
}
}
}
2016-07-21 07:01:14 +05:30
fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {}
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) {
}
2016-07-21 07:01:14 +05:30
fn matched_pat(&mut self, _: &hir::Pat, _: mc::cmt, _: euv::MatchMode) {}
2016-07-21 07:01:14 +05:30
fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: mc::cmt, _mode: euv::ConsumeMode) {}
}