1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A pass that qualifies constness of temporaries in constants,
12 //! static initializers and functions and also drives promotion.
14 //! The Qualif flags below can be used to also provide better
15 //! diagnostics as to why a constant rvalue wasn't promoted.
17 use rustc_data_structures::bitvec::BitVector;
18 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
19 use rustc::dep_graph::DepNode;
21 use rustc::hir::def_id::DefId;
22 use rustc::hir::intravisit::FnKind;
23 use rustc::hir::map::blocks::FnLikeNode;
24 use rustc::traits::{self, Reveal};
25 use rustc::ty::{self, TyCtxt, Ty};
26 use rustc::ty::cast::CastTy;
27 use rustc::mir::repr::*;
28 use rustc::mir::mir_map::MirMap;
29 use rustc::mir::traversal::{self, ReversePostorder};
30 use rustc::mir::transform::{Pass, MirMapPass, MirPassHook, MirSource};
31 use rustc::mir::visit::{LvalueContext, Visitor};
32 use rustc::util::nodemap::DefIdMap;
34 use syntax::feature_gate::UnstableFeatures;
37 use std::collections::hash_map::Entry;
42 use super::promote_consts::{self, Candidate, TempState};
46 // Const item's qualification while recursing.
47 // Recursive consts are an error.
48 const RECURSIVE = 1 << 0,
50 // Constant containing interior mutability (UnsafeCell).
51 const MUTABLE_INTERIOR = 1 << 1,
53 // Constant containing an ADT that implements Drop.
54 const NEEDS_DROP = 1 << 2,
57 const FN_ARGUMENT = 1 << 3,
59 // Static lvalue or move from a static.
60 const STATIC = 1 << 4,
62 // Reference to a static.
63 const STATIC_REF = 1 << 5,
65 // Not constant at all - non-`const fn` calls, asm!,
66 // pointer comparisons, ptr-to-int casts, etc.
67 const NOT_CONST = 1 << 6,
69 // Refers to temporaries which cannot be promoted as
70 // promote_consts decided they weren't simple enough.
71 const NOT_PROMOTABLE = 1 << 7,
73 // Borrows of temporaries can be promoted only
74 // if they have none of the above qualifications.
75 const NEVER_PROMOTE = !0,
77 // Const items can only have MUTABLE_INTERIOR
78 // and NOT_PROMOTABLE without producing an error.
79 const CONST_ERROR = !Qualif::MUTABLE_INTERIOR.bits &
80 !Qualif::NOT_PROMOTABLE.bits
84 impl<'a, 'tcx> Qualif {
85 /// Remove flags which are impossible for the given type.
86 fn restrict(&mut self, ty: Ty<'tcx>,
87 tcx: TyCtxt<'a, 'tcx, 'tcx>,
88 param_env: &ty::ParameterEnvironment<'tcx>) {
89 if !ty.type_contents(tcx).interior_unsafe() {
90 *self = *self - Qualif::MUTABLE_INTERIOR;
92 if !tcx.type_needs_drop_given_env(ty, param_env) {
93 *self = *self - Qualif::NEEDS_DROP;
98 /// What kind of item we are in.
99 #[derive(Copy, Clone, PartialEq, Eq)]
108 impl fmt::Display for Mode {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111 Mode::Const => write!(f, "constant"),
112 Mode::Static | Mode::StaticMut => write!(f, "static"),
113 Mode::ConstFn => write!(f, "constant function"),
114 Mode::Fn => write!(f, "function")
119 fn is_const_fn(tcx: TyCtxt, def_id: DefId) -> bool {
120 if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
121 let fn_like = FnLikeNode::from_node(tcx.map.get(node_id));
122 match fn_like.map(|f| f.kind()) {
123 Some(FnKind::ItemFn(_, _, _, c, _, _, _)) => {
124 c == hir::Constness::Const
126 Some(FnKind::Method(_, m, _, _)) => {
127 m.constness == hir::Constness::Const
132 tcx.sess.cstore.is_const_fn(def_id)
136 struct Qualifier<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
141 rpo: ReversePostorder<'a, 'tcx>,
142 tcx: TyCtxt<'a, 'gcx, 'tcx>,
143 param_env: ty::ParameterEnvironment<'tcx>,
144 qualif_map: &'a mut DefIdMap<Qualif>,
145 mir_map: Option<&'a MirMap<'tcx>>,
146 temp_qualif: IndexVec<Temp, Option<Qualif>>,
147 return_qualif: Option<Qualif>,
149 const_fn_arg_vars: BitVector,
151 temp_promotion_state: IndexVec<Temp, TempState>,
152 promotion_candidates: Vec<Candidate>
155 impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
156 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
157 param_env: ty::ParameterEnvironment<'tcx>,
158 qualif_map: &'a mut DefIdMap<Qualif>,
159 mir_map: Option<&'a MirMap<'tcx>>,
163 -> Qualifier<'a, 'tcx, 'tcx> {
164 let mut rpo = traversal::reverse_postorder(mir);
165 let temps = promote_consts::collect_temps(mir, &mut rpo);
174 param_env: param_env,
175 qualif_map: qualif_map,
177 temp_qualif: IndexVec::from_elem(None, &mir.temp_decls),
179 qualif: Qualif::empty(),
180 const_fn_arg_vars: BitVector::new(mir.var_decls.len()),
185 temp_promotion_state: temps,
186 promotion_candidates: vec![]
190 // FIXME(eddyb) we could split the errors into meaningful
191 // categories, but enabling full miri would make that
192 // slightly pointless (even with feature-gating).
193 fn not_const(&mut self) {
194 self.add(Qualif::NOT_CONST);
195 if self.mode != Mode::Fn {
196 span_err!(self.tcx.sess, self.span, E0019,
197 "{} contains unimplemented expression type", self.mode);
201 /// Error about extra statements in a constant.
202 fn statement_like(&mut self) {
203 self.add(Qualif::NOT_CONST);
204 if self.mode != Mode::Fn {
205 span_err!(self.tcx.sess, self.span, E0016,
206 "blocks in {}s are limited to items and tail expressions",
211 /// Add the given qualification to self.qualif.
212 fn add(&mut self, qualif: Qualif) {
213 self.qualif = self.qualif | qualif;
216 /// Add the given type's qualification to self.qualif.
217 fn add_type(&mut self, ty: Ty<'tcx>) {
218 self.add(Qualif::MUTABLE_INTERIOR | Qualif::NEEDS_DROP);
219 self.qualif.restrict(ty, self.tcx, &self.param_env);
222 /// Within the provided closure, self.qualif will start
223 /// out empty, and its value after the closure returns will
224 /// be combined with the value before the call to nest.
225 fn nest<F: FnOnce(&mut Self)>(&mut self, f: F) {
226 let original = self.qualif;
227 self.qualif = Qualif::empty();
232 /// Check for NEEDS_DROP (from an ADT or const fn call) and
233 /// error, unless we're in a function, or the feature-gate
234 /// for globals with destructors is enabled.
235 fn deny_drop(&self) {
236 if self.mode == Mode::Fn || !self.qualif.intersects(Qualif::NEEDS_DROP) {
240 // Static and const fn's allow destructors, but they're feature-gated.
241 let msg = if self.mode != Mode::Const {
242 // Feature-gate for globals with destructors is enabled.
243 if self.tcx.sess.features.borrow().drop_types_in_const {
247 // This comes from a macro that has #[allow_internal_unstable].
248 if self.tcx.sess.codemap().span_allows_unstable(self.span) {
252 format!("destructors in {}s are an unstable feature",
255 format!("{}s are not allowed to have destructors",
260 struct_span_err!(self.tcx.sess, self.span, E0493, "{}", msg);
261 if self.mode != Mode::Const {
263 "in Nightly builds, add `#![feature(drop_types_in_const)]` \
264 to the crate attributes to enable");
269 /// Check if an Lvalue with the current qualifications could
270 /// be consumed, by either an operand or a Deref projection.
271 fn try_consume(&mut self) -> bool {
272 if self.qualif.intersects(Qualif::STATIC) && self.mode != Mode::Fn {
273 let msg = if self.mode == Mode::Static ||
274 self.mode == Mode::StaticMut {
275 "cannot refer to other statics by value, use the \
276 address-of operator or a constant instead"
278 "cannot refer to statics by value, use a constant instead"
280 span_err!(self.tcx.sess, self.span, E0394, "{}", msg);
282 // Replace STATIC with NOT_CONST to avoid further errors.
283 self.qualif = self.qualif - Qualif::STATIC;
284 self.add(Qualif::NOT_CONST);
292 /// Assign the current qualification to the given destination.
293 fn assign(&mut self, dest: &Lvalue<'tcx>) {
294 let qualif = self.qualif;
295 let span = self.span;
296 let store = |slot: &mut Option<Qualif>| {
298 span_bug!(span, "multiple assignments to {:?}", dest);
300 *slot = Some(qualif);
303 // Only handle promotable temps in non-const functions.
304 if self.mode == Mode::Fn {
305 if let Lvalue::Temp(index) = *dest {
306 if self.temp_promotion_state[index].is_promotable() {
307 store(&mut self.temp_qualif[index]);
314 Lvalue::Temp(index) => store(&mut self.temp_qualif[index]),
315 Lvalue::ReturnPointer => store(&mut self.return_qualif),
317 Lvalue::Projection(box Projection {
318 base: Lvalue::Temp(index),
319 elem: ProjectionElem::Deref
320 }) if self.mir.temp_decls[index].ty.is_unique()
321 && self.temp_qualif[index].map_or(false, |qualif| {
322 qualif.intersects(Qualif::NOT_CONST)
324 // Part of `box expr`, we should've errored
325 // already for the Box allocation Rvalue.
328 // This must be an explicit assignment.
330 // Catch more errors in the destination.
331 self.visit_lvalue(dest, LvalueContext::Store);
332 self.statement_like();
337 /// Qualify a whole const, static initializer or const fn.
338 fn qualify_const(&mut self) -> Qualif {
341 let mut seen_blocks = BitVector::new(mir.basic_blocks().len());
342 let mut bb = START_BLOCK;
344 seen_blocks.insert(bb.index());
346 self.visit_basic_block_data(bb, &mir[bb]);
348 let target = match mir[bb].terminator().kind {
349 TerminatorKind::Goto { target } |
350 // Drops are considered noops.
351 TerminatorKind::Drop { target, .. } |
352 TerminatorKind::Assert { target, .. } |
353 TerminatorKind::Call { destination: Some((_, target)), .. } => {
357 // Non-terminating calls cannot produce any value.
358 TerminatorKind::Call { destination: None, .. } => {
359 return Qualif::empty();
362 TerminatorKind::If {..} |
363 TerminatorKind::Switch {..} |
364 TerminatorKind::SwitchInt {..} |
365 TerminatorKind::DropAndReplace { .. } |
366 TerminatorKind::Resume |
367 TerminatorKind::Unreachable => None,
369 TerminatorKind::Return => {
370 // Check for unused values. This usually means
371 // there are extra statements in the AST.
372 for temp in mir.temp_decls.indices() {
373 if self.temp_qualif[temp].is_none() {
377 let state = self.temp_promotion_state[temp];
378 if let TempState::Defined { location, uses: 0 } = state {
379 let data = &mir[location.block];
380 let stmt_idx = location.statement_index;
382 // Get the span for the initialization.
383 let source_info = if stmt_idx < data.statements.len() {
384 data.statements[stmt_idx].source_info
386 data.terminator().source_info
388 self.span = source_info.span;
390 // Treat this as a statement in the AST.
391 self.statement_like();
395 // Make sure there are no extra unassigned variables.
396 self.qualif = Qualif::NOT_CONST;
397 for index in 0..mir.var_decls.len() {
398 if !self.const_fn_arg_vars.contains(index) {
399 self.assign(&Lvalue::Var(Var::new(index)));
409 Some(target) if !seen_blocks.contains(target.index()) => {
419 let return_ty = mir.return_ty;
420 self.qualif = self.return_qualif.unwrap_or(Qualif::NOT_CONST);
424 // Check for destructors in static mut.
425 self.add_type(return_ty);
429 // Account for errors in consts by using the
430 // conservative type qualification instead.
431 if self.qualif.intersects(Qualif::CONST_ERROR) {
432 self.qualif = Qualif::empty();
433 self.add_type(return_ty);
441 /// Accumulates an Rvalue or Call's effects in self.qualif.
442 /// For functions (constant or not), it also records
443 /// candidates for promotion in promotion_candidates.
444 impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
445 fn visit_lvalue(&mut self, lvalue: &Lvalue<'tcx>, context: LvalueContext) {
448 self.add(Qualif::FN_ARGUMENT);
451 self.add(Qualif::NOT_CONST);
453 Lvalue::Temp(index) => {
454 if !self.temp_promotion_state[index].is_promotable() {
455 self.add(Qualif::NOT_PROMOTABLE);
458 if let Some(qualif) = self.temp_qualif[index] {
464 Lvalue::Static(_) => {
465 self.add(Qualif::STATIC);
466 if self.mode == Mode::Const || self.mode == Mode::ConstFn {
467 span_err!(self.tcx.sess, self.span, E0013,
468 "{}s cannot refer to statics, use \
469 a constant instead", self.mode);
472 Lvalue::ReturnPointer => {
475 Lvalue::Projection(ref proj) => {
477 this.super_lvalue(lvalue, context);
479 ProjectionElem::Deref => {
480 if !this.try_consume() {
484 if this.qualif.intersects(Qualif::STATIC_REF) {
485 this.qualif = this.qualif - Qualif::STATIC_REF;
486 this.add(Qualif::STATIC);
489 let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
490 if let ty::TyRawPtr(_) = base_ty.sty {
491 this.add(Qualif::NOT_CONST);
492 if this.mode != Mode::Fn {
493 struct_span_err!(this.tcx.sess,
495 "raw pointers cannot be dereferenced in {}s",
497 .span_label(this.span,
498 &format!("dereference of raw pointer in constant"))
504 ProjectionElem::Field(..) |
505 ProjectionElem::Index(_) => {
506 if this.mode != Mode::Fn &&
507 this.qualif.intersects(Qualif::STATIC) {
508 span_err!(this.tcx.sess, this.span, E0494,
509 "cannot refer to the interior of another \
510 static, use a constant instead");
512 let ty = lvalue.ty(this.mir, this.tcx).to_ty(this.tcx);
513 this.qualif.restrict(ty, this.tcx, &this.param_env);
516 ProjectionElem::ConstantIndex {..} |
517 ProjectionElem::Subslice {..} |
518 ProjectionElem::Downcast(..) => {
527 fn visit_operand(&mut self, operand: &Operand<'tcx>) {
529 Operand::Consume(_) => {
531 this.super_operand(operand);
535 Operand::Constant(ref constant) => {
536 // Only functions and methods can have these types.
537 if let ty::TyFnDef(..) = constant.ty.sty {
541 if let Literal::Item { def_id, substs } = constant.literal {
542 // Don't peek inside generic (associated) constants.
543 if !substs.types.is_empty() {
544 self.add_type(constant.ty);
546 let qualif = qualify_const_item_cached(self.tcx,
553 // FIXME(eddyb) check recursive constants here,
554 // instead of rustc_passes::static_recursion.
555 if self.qualif.intersects(Qualif::RECURSIVE) {
556 span_bug!(constant.span,
557 "recursive constant wasn't caught earlier");
560 // Let `const fn` transitively have destructors,
561 // but they do get stopped in `const` or `static`.
562 if self.mode != Mode::ConstFn {
570 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
571 // Recurse through operands and lvalues.
572 self.super_rvalue(rvalue);
577 Rvalue::UnaryOp(..) |
578 Rvalue::CheckedBinaryOp(..) |
579 Rvalue::Cast(CastKind::ReifyFnPointer, _, _) |
580 Rvalue::Cast(CastKind::UnsafeFnPointer, _, _) |
581 Rvalue::Cast(CastKind::Unsize, _, _) => {}
584 // Static lvalues in consts would have errored already,
585 // don't treat length checks as reads from statics.
586 self.qualif = self.qualif - Qualif::STATIC;
589 Rvalue::Ref(_, kind, ref lvalue) => {
590 // Static lvalues in consts would have errored already,
591 // only keep track of references to them here.
592 if self.qualif.intersects(Qualif::STATIC) {
593 self.qualif = self.qualif - Qualif::STATIC;
594 self.add(Qualif::STATIC_REF);
597 let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
598 if kind == BorrowKind::Mut {
599 // In theory, any zero-sized value could be borrowed
600 // mutably without consequences. However, only &mut []
601 // is allowed right now, and only in functions.
602 let allow = if self.mode == Mode::StaticMut {
603 // Inside a `static mut`, &mut [...] is also allowed.
605 ty::TyArray(..) | ty::TySlice(_) => {
606 // Mutating can expose drops, be conservative.
613 } else if let ty::TyArray(_, 0) = ty.sty {
614 self.mode == Mode::Fn
620 self.add(Qualif::NOT_CONST);
621 if self.mode != Mode::Fn {
622 struct_span_err!(self.tcx.sess, self.span, E0017,
623 "references in {}s may only refer \
624 to immutable values", self.mode)
625 .span_label(self.span, &format!("{}s require immutable values",
631 // Constants cannot be borrowed if they contain interior mutability as
632 // it means that our "silent insertion of statics" could change
633 // initializer values (very bad).
634 if self.qualif.intersects(Qualif::MUTABLE_INTERIOR) {
635 // Replace MUTABLE_INTERIOR with NOT_CONST to avoid
636 // duplicate errors (from reborrowing, for example).
637 self.qualif = self.qualif - Qualif::MUTABLE_INTERIOR;
638 self.add(Qualif::NOT_CONST);
639 if self.mode != Mode::Fn {
640 span_err!(self.tcx.sess, self.span, E0492,
641 "cannot borrow a constant which contains \
642 interior mutability, create a static instead");
647 // We might have a candidate for promotion.
648 let candidate = Candidate::Ref(self.location);
649 if self.mode == Mode::Fn || self.mode == Mode::ConstFn {
650 if !self.qualif.intersects(Qualif::NEVER_PROMOTE) {
651 // We can only promote direct borrows of temps.
652 if let Lvalue::Temp(_) = *lvalue {
653 self.promotion_candidates.push(candidate);
659 Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
660 let operand_ty = operand.ty(self.mir, self.tcx);
661 let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
662 let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
663 match (cast_in, cast_out) {
664 (CastTy::Ptr(_), CastTy::Int(_)) |
665 (CastTy::FnPtr, CastTy::Int(_)) => {
666 self.add(Qualif::NOT_CONST);
667 if self.mode != Mode::Fn {
668 span_err!(self.tcx.sess, self.span, E0018,
669 "raw pointers cannot be cast to integers in {}s",
677 Rvalue::BinaryOp(op, ref lhs, _) => {
678 if let ty::TyRawPtr(_) = lhs.ty(self.mir, self.tcx).sty {
679 assert!(op == BinOp::Eq || op == BinOp::Ne ||
680 op == BinOp::Le || op == BinOp::Lt ||
681 op == BinOp::Ge || op == BinOp::Gt);
683 self.add(Qualif::NOT_CONST);
684 if self.mode != Mode::Fn {
685 span_err!(self.tcx.sess, self.span, E0395,
686 "raw pointers cannot be compared in {}s",
693 self.add(Qualif::NOT_CONST);
694 if self.mode != Mode::Fn {
695 struct_span_err!(self.tcx.sess, self.span, E0010,
696 "allocations are not allowed in {}s", self.mode)
697 .span_label(self.span, &format!("allocation not allowed in {}s", self.mode))
702 Rvalue::Aggregate(ref kind, _) => {
703 if let AggregateKind::Adt(def, _, _) = *kind {
705 self.add(Qualif::NEEDS_DROP);
709 if Some(def.did) == self.tcx.lang_items.unsafe_cell_type() {
710 let ty = rvalue.ty(self.mir, self.tcx).unwrap();
712 assert!(self.qualif.intersects(Qualif::MUTABLE_INTERIOR));
713 // Even if the value inside may not need dropping,
714 // mutating it would change that.
715 if !self.qualif.intersects(Qualif::NOT_CONST) {
722 Rvalue::InlineAsm {..} => {
728 fn visit_terminator_kind(&mut self, bb: BasicBlock, kind: &TerminatorKind<'tcx>) {
729 if let TerminatorKind::Call { ref func, ref args, ref destination, .. } = *kind {
730 self.visit_operand(func);
732 let fn_ty = func.ty(self.mir, self.tcx);
733 let (is_shuffle, is_const_fn) = match fn_ty.sty {
734 ty::TyFnDef(def_id, _, f) => {
735 (f.abi == Abi::PlatformIntrinsic &&
736 self.tcx.item_name(def_id).as_str().starts_with("simd_shuffle"),
737 is_const_fn(self.tcx, def_id))
742 for (i, arg) in args.iter().enumerate() {
744 this.visit_operand(arg);
745 if is_shuffle && i == 2 && this.mode == Mode::Fn {
746 let candidate = Candidate::ShuffleIndices(bb);
747 if !this.qualif.intersects(Qualif::NEVER_PROMOTE) {
748 this.promotion_candidates.push(candidate);
750 span_err!(this.tcx.sess, this.span, E0526,
751 "shuffle indices are not constant");
759 // We are in a const or static initializer,
760 if self.mode != Mode::Fn &&
762 // feature-gate is not enabled,
763 !self.tcx.sess.features.borrow().const_fn &&
765 // this doesn't come from a crate with the feature-gate enabled,
766 self.def_id.is_local() &&
768 // this doesn't come from a macro that has #[allow_internal_unstable]
769 !self.tcx.sess.codemap().span_allows_unstable(self.span)
771 let mut err = self.tcx.sess.struct_span_err(self.span,
772 "const fns are an unstable feature");
774 "in Nightly builds, add `#![feature(const_fn)]` \
775 to the crate attributes to enable");
779 self.qualif = Qualif::NOT_CONST;
780 if self.mode != Mode::Fn {
781 // FIXME(#24111) Remove this check when const fn stabilizes
782 let (msg, note) = if let UnstableFeatures::Disallow =
783 self.tcx.sess.opts.unstable_features {
784 (format!("calls in {}s are limited to \
785 struct and enum constructors",
787 Some("a limited form of compile-time function \
788 evaluation is available on a nightly \
789 compiler via `const fn`"))
791 (format!("calls in {}s are limited \
792 to constant functions, \
793 struct and enum constructors",
797 let mut err = struct_span_err!(self.tcx.sess, self.span, E0015, "{}", msg);
798 if let Some(note) = note {
799 err.span_note(self.span, note);
805 if let Some((ref dest, _)) = *destination {
806 // Avoid propagating irrelevant callee/argument qualifications.
807 if self.qualif.intersects(Qualif::CONST_ERROR) {
808 self.qualif = Qualif::NOT_CONST;
810 // Be conservative about the returned value of a const fn.
812 let ty = dest.ty(self.mir, tcx).to_ty(tcx);
813 self.qualif = Qualif::empty();
816 // Let `const fn` transitively have destructors,
817 // but they do get stopped in `const` or `static`.
818 if self.mode != Mode::ConstFn {
825 // Qualify any operands inside other terminators.
826 self.super_terminator_kind(bb, kind);
830 fn visit_assign(&mut self, _: BasicBlock, dest: &Lvalue<'tcx>, rvalue: &Rvalue<'tcx>) {
831 self.visit_rvalue(rvalue);
833 // Check the allowed const fn argument forms.
834 if let (Mode::ConstFn, &Lvalue::Var(index)) = (self.mode, dest) {
835 if self.const_fn_arg_vars.insert(index.index()) {
836 // Direct use of an argument is permitted.
837 if let Rvalue::Use(Operand::Consume(Lvalue::Arg(_))) = *rvalue {
841 // Avoid a generic error for other uses of arguments.
842 if self.qualif.intersects(Qualif::FN_ARGUMENT) {
843 let decl = &self.mir.var_decls[index];
844 span_err!(self.tcx.sess, decl.source_info.span, E0022,
845 "arguments of constant functions can only \
846 be immutable by-value bindings");
855 fn visit_source_info(&mut self, source_info: &SourceInfo) {
856 self.span = source_info.span;
859 fn visit_statement(&mut self, bb: BasicBlock, statement: &Statement<'tcx>) {
860 assert_eq!(self.location.block, bb);
862 this.visit_source_info(&statement.source_info);
863 match statement.kind {
864 StatementKind::Assign(ref lvalue, ref rvalue) => {
865 this.visit_assign(bb, lvalue, rvalue);
867 StatementKind::SetDiscriminant { .. } |
868 StatementKind::StorageLive(_) |
869 StatementKind::StorageDead(_) => {}
872 self.location.statement_index += 1;
875 fn visit_terminator(&mut self, bb: BasicBlock, terminator: &Terminator<'tcx>) {
876 assert_eq!(self.location.block, bb);
877 self.nest(|this| this.super_terminator(bb, terminator));
880 fn visit_basic_block_data(&mut self, bb: BasicBlock, data: &BasicBlockData<'tcx>) {
881 self.location.statement_index = 0;
882 self.location.block = bb;
883 self.super_basic_block_data(bb, data);
887 fn qualify_const_item_cached<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
888 qualif_map: &mut DefIdMap<Qualif>,
889 mir_map: Option<&MirMap<'tcx>>,
892 match qualif_map.entry(def_id) {
893 Entry::Occupied(entry) => return *entry.get(),
894 Entry::Vacant(entry) => {
895 // Guard against `const` recursion.
896 entry.insert(Qualif::RECURSIVE);
901 let param_env_and_mir = if def_id.is_local() {
902 mir_map.and_then(|map| map.map.get(&def_id)).map(|mir| {
903 let node_id = tcx.map.as_local_node_id(def_id).unwrap();
904 (ty::ParameterEnvironment::for_item(tcx, node_id), mir)
906 } else if let Some(mir) = tcx.sess.cstore.maybe_get_item_mir(tcx, def_id) {
907 // These should only be monomorphic constants.
909 Some((tcx.empty_parameter_environment(), &extern_mir))
914 let (param_env, mir) = param_env_and_mir.unwrap_or_else(|| {
915 bug!("missing constant MIR for {}", tcx.item_path_str(def_id))
918 let mut qualifier = Qualifier::new(tcx, param_env, qualif_map, mir_map,
919 def_id, mir, Mode::Const);
920 let qualif = qualifier.qualify_const();
921 qualifier.qualif_map.insert(def_id, qualif);
925 pub struct QualifyAndPromoteConstants;
927 impl Pass for QualifyAndPromoteConstants {}
929 impl<'tcx> MirMapPass<'tcx> for QualifyAndPromoteConstants {
930 fn run_pass<'a>(&mut self,
931 tcx: TyCtxt<'a, 'tcx, 'tcx>,
932 map: &mut MirMap<'tcx>,
933 hooks: &mut [Box<for<'s> MirPassHook<'s>>]) {
934 let mut qualif_map = DefIdMap();
936 // First, visit `const` items, potentially recursing, to get
937 // accurate MUTABLE_INTERIOR and NEEDS_DROP qualifications.
938 let keys = map.map.keys();
939 for &def_id in &keys {
940 let _task = tcx.dep_graph.in_task(DepNode::Mir(def_id));
941 let id = tcx.map.as_local_node_id(def_id).unwrap();
942 let src = MirSource::from_node(tcx, id);
943 if let MirSource::Const(_) = src {
944 qualify_const_item_cached(tcx, &mut qualif_map, Some(map), def_id);
948 // Then, handle everything else, without recursing,
949 // as the MIR map is not shared, since promotion
950 // in functions (including `const fn`) mutates it.
951 for &def_id in &keys {
952 let _task = tcx.dep_graph.in_task(DepNode::Mir(def_id));
953 let id = tcx.map.as_local_node_id(def_id).unwrap();
954 let src = MirSource::from_node(tcx, id);
955 let mode = match src {
956 MirSource::Fn(_) => {
957 if is_const_fn(tcx, def_id) {
963 MirSource::Const(_) => continue,
964 MirSource::Static(_, hir::MutImmutable) => Mode::Static,
965 MirSource::Static(_, hir::MutMutable) => Mode::StaticMut,
966 MirSource::Promoted(..) => bug!()
968 let param_env = ty::ParameterEnvironment::for_item(tcx, id);
970 let mir = map.map.get_mut(&def_id).unwrap();
971 for hook in &mut *hooks {
972 hook.on_mir_pass(tcx, src, mir, self, false);
975 if mode == Mode::Fn || mode == Mode::ConstFn {
976 // This is ugly because Qualifier holds onto mir,
977 // which can't be mutated until its scope ends.
978 let (temps, candidates) = {
979 let mut qualifier = Qualifier::new(tcx, param_env, &mut qualif_map,
980 None, def_id, mir, mode);
981 if mode == Mode::ConstFn {
982 // Enforce a constant-like CFG for `const fn`.
983 qualifier.qualify_const();
985 while let Some((bb, data)) = qualifier.rpo.next() {
986 qualifier.visit_basic_block_data(bb, data);
990 (qualifier.temp_promotion_state,
991 qualifier.promotion_candidates)
994 // Do the actual promotion, now that we know what's viable.
995 promote_consts::promote_candidates(mir, tcx, temps, candidates);
997 let mut qualifier = Qualifier::new(tcx, param_env, &mut qualif_map,
998 None, def_id, mir, mode);
999 qualifier.qualify_const();
1002 for hook in &mut *hooks {
1003 hook.on_mir_pass(tcx, src, mir, self, true);
1006 // Statics must be Sync.
1007 if mode == Mode::Static {
1008 let ty = mir.return_ty;
1009 tcx.infer_ctxt(None, None, Reveal::NotSpecializable).enter(|infcx| {
1010 let cause = traits::ObligationCause::new(mir.span, id, traits::SharedStatic);
1011 let mut fulfillment_cx = traits::FulfillmentContext::new();
1012 fulfillment_cx.register_builtin_bound(&infcx, ty, ty::BoundSync, cause);
1013 if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
1014 infcx.report_fulfillment_errors(&err);
1017 if let Err(errors) = fulfillment_cx.select_rfc1592_obligations(&infcx) {
1018 infcx.report_fulfillment_errors_as_warnings(&errors, id);