From 6a3fb269ede7d18de3597cae8d35edbb9b7c6c44 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 23 Mar 2020 14:02:58 +0100 Subject: [PATCH] Rename `Item` to `ConstCx`. This renaming was already done in some modules via import renaming. It's strictly used as a context, and it contains a `TyCtxt`. --- .../transform/check_consts/mod.rs | 6 +- .../transform/check_consts/ops.rs | 124 +++++++++--------- .../transform/check_consts/qualifs.rs | 2 +- .../transform/check_consts/resolver.rs | 30 ++--- .../transform/check_consts/validation.rs | 56 ++++---- src/librustc_mir/transform/mod.rs | 6 +- src/librustc_mir/transform/promote_consts.rs | 16 +-- 7 files changed, 120 insertions(+), 120 deletions(-) diff --git a/src/librustc_mir/transform/check_consts/mod.rs b/src/librustc_mir/transform/check_consts/mod.rs index 8aac5c791ecd..3812872ff785 100644 --- a/src/librustc_mir/transform/check_consts/mod.rs +++ b/src/librustc_mir/transform/check_consts/mod.rs @@ -20,7 +20,7 @@ pub mod validation; /// Information about the item currently being const-checked, as well as a reference to the global /// context. -pub struct Item<'mir, 'tcx> { +pub struct ConstCx<'mir, 'tcx> { pub body: &'mir mir::Body<'tcx>, pub tcx: TyCtxt<'tcx>, pub def_id: DefId, @@ -28,12 +28,12 @@ pub struct Item<'mir, 'tcx> { pub const_kind: Option, } -impl Item<'mir, 'tcx> { +impl ConstCx<'mir, 'tcx> { pub fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'mir mir::Body<'tcx>) -> Self { let param_env = tcx.param_env(def_id); let const_kind = ConstKind::for_item(tcx, def_id); - Item { body, tcx, def_id, param_env, const_kind } + ConstCx { body, tcx, def_id, param_env, const_kind } } /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.). diff --git a/src/librustc_mir/transform/check_consts/ops.rs b/src/librustc_mir/transform/check_consts/ops.rs index c4b94b70938d..60eb51f7ccab 100644 --- a/src/librustc_mir/transform/check_consts/ops.rs +++ b/src/librustc_mir/transform/check_consts/ops.rs @@ -7,7 +7,7 @@ use rustc_session::parse::feature_err; use rustc_span::symbol::sym; use rustc_span::{Span, Symbol}; -use super::{ConstKind, Item}; +use super::{ConstCx, ConstKind}; /// An operation that is not *always* allowed in a const context. pub trait NonConstOp: std::fmt::Debug { @@ -27,19 +27,19 @@ pub trait NonConstOp: std::fmt::Debug { /// /// By default, it returns `true` if and only if this operation has a corresponding feature /// gate and that gate is enabled. - fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool { - Self::feature_gate().map_or(false, |gate| item.tcx.features().enabled(gate)) + fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool { + Self::feature_gate().map_or(false, |gate| ccx.tcx.features().enabled(gate)) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0019, "{} contains unimplemented expression type", - item.const_kind() + ccx.const_kind() ); - if item.tcx.sess.teach(&err.get_code().unwrap()) { + if ccx.tcx.sess.teach(&err.get_code().unwrap()) { err.note( "A function call isn't allowed in the const's initialization expression \ because the expression's value must be known at compile-time.", @@ -66,9 +66,9 @@ impl NonConstOp for Downcast { #[derive(Debug)] pub struct FnCallIndirect; impl NonConstOp for FnCallIndirect { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = - item.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn"); + ccx.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn"); err.emit(); } } @@ -77,14 +77,14 @@ impl NonConstOp for FnCallIndirect { #[derive(Debug)] pub struct FnCallNonConst(pub DefId); impl NonConstOp for FnCallNonConst { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0015, "calls in {}s are limited to constant functions, \ tuple structs and tuple variants", - item.const_kind(), + ccx.const_kind(), ); err.emit(); } @@ -96,12 +96,12 @@ impl NonConstOp for FnCallNonConst { #[derive(Debug)] pub struct FnCallUnstable(pub DefId, pub Symbol); impl NonConstOp for FnCallUnstable { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let FnCallUnstable(def_id, feature) = *self; - let mut err = item.tcx.sess.struct_span_err( + let mut err = ccx.tcx.sess.struct_span_err( span, - &format!("`{}` is not yet stable as a const fn", item.tcx.def_path_str(def_id)), + &format!("`{}` is not yet stable as a const fn", ccx.tcx.def_path_str(def_id)), ); if nightly_options::is_nightly_build() { err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature)); @@ -113,16 +113,16 @@ impl NonConstOp for FnCallUnstable { #[derive(Debug)] pub struct HeapAllocation; impl NonConstOp for HeapAllocation { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0010, "allocations are not allowed in {}s", - item.const_kind() + ccx.const_kind() ); - err.span_label(span, format!("allocation not allowed in {}s", item.const_kind())); - if item.tcx.sess.teach(&err.get_code().unwrap()) { + err.span_label(span, format!("allocation not allowed in {}s", ccx.const_kind())); + if ccx.tcx.sess.teach(&err.get_code().unwrap()) { err.note( "The value of statics and constants must be known at compile time, \ and they live for the entire lifetime of a program. Creating a boxed \ @@ -141,9 +141,9 @@ impl NonConstOp for IfOrMatch { Some(sym::const_if_match) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { // This should be caught by the HIR const-checker. - item.tcx.sess.delay_span_bug(span, "complex control flow is forbidden in a const context"); + ccx.tcx.sess.delay_span_bug(span, "complex control flow is forbidden in a const context"); } } @@ -154,14 +154,14 @@ impl NonConstOp for InlineAsm {} #[derive(Debug)] pub struct LiveDrop; impl NonConstOp for LiveDrop { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0493, "destructors cannot be evaluated at compile-time" ) - .span_label(span, format!("{}s cannot evaluate destructors", item.const_kind())) + .span_label(span, format!("{}s cannot evaluate destructors", ccx.const_kind())) .emit(); } } @@ -173,18 +173,18 @@ impl NonConstOp for Loop { Some(sym::const_loop) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { // This should be caught by the HIR const-checker. - item.tcx.sess.delay_span_bug(span, "complex control flow is forbidden in a const context"); + ccx.tcx.sess.delay_span_bug(span, "complex control flow is forbidden in a const context"); } } #[derive(Debug)] pub struct CellBorrow; impl NonConstOp for CellBorrow { - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0492, "cannot borrow a constant which may contain \ @@ -201,19 +201,19 @@ impl NonConstOp for MutBorrow { Some(sym::const_mut_refs) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_mut_refs, span, &format!( "references in {}s may only refer \ to immutable values", - item.const_kind() + ccx.const_kind() ), ); - err.span_label(span, format!("{}s require immutable values", item.const_kind())); - if item.tcx.sess.teach(&err.get_code().unwrap()) { + err.span_label(span, format!("{}s require immutable values", ccx.const_kind())); + if ccx.tcx.sess.teach(&err.get_code().unwrap()) { err.note( "References in statics and constants may only refer \ to immutable values.\n\n\ @@ -236,12 +236,12 @@ impl NonConstOp for MutAddressOf { Some(sym::const_mut_refs) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_mut_refs, span, - &format!("`&raw mut` is not allowed in {}s", item.const_kind()), + &format!("`&raw mut` is not allowed in {}s", ccx.const_kind()), ) .emit(); } @@ -262,12 +262,12 @@ impl NonConstOp for Panic { Some(sym::const_panic) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_panic, span, - &format!("panicking in {}s is unstable", item.const_kind()), + &format!("panicking in {}s is unstable", ccx.const_kind()), ) .emit(); } @@ -280,12 +280,12 @@ impl NonConstOp for RawPtrComparison { Some(sym::const_compare_raw_pointers) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_compare_raw_pointers, span, - &format!("comparing raw pointers inside {}", item.const_kind()), + &format!("comparing raw pointers inside {}", ccx.const_kind()), ) .emit(); } @@ -298,12 +298,12 @@ impl NonConstOp for RawPtrDeref { Some(sym::const_raw_ptr_deref) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_raw_ptr_deref, span, - &format!("dereferencing raw pointers in {}s is unstable", item.const_kind(),), + &format!("dereferencing raw pointers in {}s is unstable", ccx.const_kind(),), ) .emit(); } @@ -316,12 +316,12 @@ impl NonConstOp for RawPtrToIntCast { Some(sym::const_raw_ptr_to_usize_cast) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_raw_ptr_to_usize_cast, span, - &format!("casting pointers to integers in {}s is unstable", item.const_kind(),), + &format!("casting pointers to integers in {}s is unstable", ccx.const_kind(),), ) .emit(); } @@ -331,22 +331,22 @@ impl NonConstOp for RawPtrToIntCast { #[derive(Debug)] pub struct StaticAccess; impl NonConstOp for StaticAccess { - fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool { - item.const_kind().is_static() + fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool { + ccx.const_kind().is_static() } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { let mut err = struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0013, "{}s cannot refer to statics", - item.const_kind() + ccx.const_kind() ); err.help( "consider extracting the value of the `static` to a `const`, and referring to that", ); - if item.tcx.sess.teach(&err.get_code().unwrap()) { + if ccx.tcx.sess.teach(&err.get_code().unwrap()) { err.note( "`static` and `const` variables can refer to other `const` variables. \ A `const` variable, however, cannot refer to a `static` variable.", @@ -363,9 +363,9 @@ pub struct ThreadLocalAccess; impl NonConstOp for ThreadLocalAccess { const IS_SUPPORTED_IN_MIRI: bool = false; - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { struct_span_err!( - item.tcx.sess, + ccx.tcx.sess, span, E0625, "thread-local statics cannot be \ @@ -378,19 +378,19 @@ impl NonConstOp for ThreadLocalAccess { #[derive(Debug)] pub struct UnionAccess; impl NonConstOp for UnionAccess { - fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool { + fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool { // Union accesses are stable in all contexts except `const fn`. - item.const_kind() != ConstKind::ConstFn - || item.tcx.features().enabled(Self::feature_gate().unwrap()) + ccx.const_kind() != ConstKind::ConstFn + || ccx.tcx.features().enabled(Self::feature_gate().unwrap()) } fn feature_gate() -> Option { Some(sym::const_fn_union) } - fn emit_error(&self, item: &Item<'_, '_>, span: Span) { + fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { feature_err( - &item.tcx.sess.parse_sess, + &ccx.tcx.sess.parse_sess, sym::const_fn_union, span, "unions in const fn are unstable", diff --git a/src/librustc_mir/transform/check_consts/qualifs.rs b/src/librustc_mir/transform/check_consts/qualifs.rs index 5f50e073e29a..f82f06599b74 100644 --- a/src/librustc_mir/transform/check_consts/qualifs.rs +++ b/src/librustc_mir/transform/check_consts/qualifs.rs @@ -6,7 +6,7 @@ use rustc_middle::mir::*; use rustc_middle::ty::{self, AdtDef, Ty}; use rustc_span::DUMMY_SP; -use super::Item as ConstCx; +use super::ConstCx; pub fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> ConstQualifs { ConstQualifs { diff --git a/src/librustc_mir/transform/check_consts/resolver.rs b/src/librustc_mir/transform/check_consts/resolver.rs index 6ae314b973ff..a81d7a23be2f 100644 --- a/src/librustc_mir/transform/check_consts/resolver.rs +++ b/src/librustc_mir/transform/check_consts/resolver.rs @@ -8,7 +8,7 @@ use rustc_middle::mir::{self, BasicBlock, Local, Location}; use std::marker::PhantomData; -use super::{qualifs, Item, Qualif}; +use super::{qualifs, ConstCx, Qualif}; use crate::dataflow; /// A `Visitor` that propagates qualifs between locals. This defines the transfer function of @@ -18,7 +18,7 @@ use crate::dataflow; /// the `MaybeMutBorrowedLocals` dataflow pass to see if a `Local` may have become qualified via /// an indirect assignment or function call. struct TransferFunction<'a, 'mir, 'tcx, Q> { - item: &'a Item<'mir, 'tcx>, + ccx: &'a ConstCx<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet, _qualif: PhantomData, @@ -28,16 +28,16 @@ impl TransferFunction<'a, 'mir, 'tcx, Q> where Q: Qualif, { - fn new(item: &'a Item<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet) -> Self { - TransferFunction { item, qualifs_per_local, _qualif: PhantomData } + fn new(ccx: &'a ConstCx<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet) -> Self { + TransferFunction { ccx, qualifs_per_local, _qualif: PhantomData } } fn initialize_state(&mut self) { self.qualifs_per_local.clear(); - for arg in self.item.body.args_iter() { - let arg_ty = self.item.body.local_decls[arg].ty; - if Q::in_any_value_of_ty(self.item, arg_ty) { + for arg in self.ccx.body.args_iter() { + let arg_ty = self.ccx.body.local_decls[arg].ty; + if Q::in_any_value_of_ty(self.ccx, arg_ty) { self.qualifs_per_local.insert(arg); } } @@ -72,8 +72,8 @@ where ) { // We cannot reason about another function's internals, so use conservative type-based // qualification for the result of a function call. - let return_ty = return_place.ty(self.item.body, self.item.tcx).ty; - let qualif = Q::in_any_value_of_ty(self.item, return_ty); + let return_ty = return_place.ty(self.ccx.body, self.ccx.tcx).ty; + let qualif = Q::in_any_value_of_ty(self.ccx, return_ty); if !return_place.is_indirect() { self.assign_qualif_direct(&return_place, qualif); @@ -108,7 +108,7 @@ where location: Location, ) { let qualif = qualifs::in_rvalue::( - self.item, + self.ccx, &mut |l| self.qualifs_per_local.contains(l), rvalue, ); @@ -127,7 +127,7 @@ where if let mir::TerminatorKind::DropAndReplace { value, location: dest, .. } = kind { let qualif = qualifs::in_operand::( - self.item, + self.ccx, &mut |l| self.qualifs_per_local.contains(l), value, ); @@ -145,7 +145,7 @@ where /// The dataflow analysis used to propagate qualifs on arbitrary CFGs. pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> { - item: &'a Item<'mir, 'tcx>, + ccx: &'a ConstCx<'mir, 'tcx>, _qualif: PhantomData, } @@ -153,15 +153,15 @@ impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> where Q: Qualif, { - pub(super) fn new(_: Q, item: &'a Item<'mir, 'tcx>) -> Self { - FlowSensitiveAnalysis { item, _qualif: PhantomData } + pub(super) fn new(_: Q, ccx: &'a ConstCx<'mir, 'tcx>) -> Self { + FlowSensitiveAnalysis { ccx, _qualif: PhantomData } } fn transfer_function( &self, state: &'a mut BitSet, ) -> TransferFunction<'a, 'mir, 'tcx, Q> { - TransferFunction::::new(self.item, state) + TransferFunction::::new(self.ccx, state) } } diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index ce0cdc2bac07..45d8e1d08b72 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -20,7 +20,7 @@ use std::ops::Deref; use super::ops::{self, NonConstOp}; use super::qualifs::{self, HasMutInterior, NeedsDrop}; use super::resolver::FlowSensitiveAnalysis; -use super::{is_lang_panic_fn, ConstKind, Item, Qualif}; +use super::{is_lang_panic_fn, ConstCx, ConstKind, Qualif}; use crate::const_eval::{is_const_fn, is_unstable_const_fn}; use crate::dataflow::MaybeMutBorrowedLocals; use crate::dataflow::{self, Analysis}; @@ -37,15 +37,15 @@ struct QualifCursor<'a, 'mir, 'tcx, Q: Qualif> { } impl QualifCursor<'a, 'mir, 'tcx, Q> { - pub fn new(q: Q, item: &'a Item<'mir, 'tcx>) -> Self { - let cursor = FlowSensitiveAnalysis::new(q, item) - .into_engine(item.tcx, item.body, item.def_id) + pub fn new(q: Q, ccx: &'a ConstCx<'mir, 'tcx>) -> Self { + let cursor = FlowSensitiveAnalysis::new(q, ccx) + .into_engine(ccx.tcx, ccx.body, ccx.def_id) .iterate_to_fixpoint() - .into_results_cursor(item.body); + .into_results_cursor(ccx.body); - let mut in_any_value_of_ty = BitSet::new_empty(item.body.local_decls.len()); - for (local, decl) in item.body.local_decls.iter_enumerated() { - if Q::in_any_value_of_ty(item, decl.ty) { + let mut in_any_value_of_ty = BitSet::new_empty(ccx.body.local_decls.len()); + for (local, decl) in ccx.body.local_decls.iter_enumerated() { + if Q::in_any_value_of_ty(ccx, decl.ty) { in_any_value_of_ty.insert(local); } } @@ -91,12 +91,12 @@ impl Qualifs<'a, 'mir, 'tcx> { || self.indirectly_mutable(local, location) } - fn in_return_place(&mut self, item: &Item<'_, 'tcx>) -> ConstQualifs { + fn in_return_place(&mut self, ccx: &ConstCx<'_, 'tcx>) -> ConstQualifs { // Find the `Return` terminator if one exists. // // If no `Return` terminator exists, this MIR is divergent. Just return the conservative // qualifs for the return type. - let return_block = item + let return_block = ccx .body .basic_blocks() .iter_enumerated() @@ -107,11 +107,11 @@ impl Qualifs<'a, 'mir, 'tcx> { .map(|(bb, _)| bb); let return_block = match return_block { - None => return qualifs::in_any_value_of_ty(item, item.body.return_ty()), + None => return qualifs::in_any_value_of_ty(ccx, ccx.body.return_ty()), Some(bb) => bb, }; - let return_loc = item.body.terminator_loc(return_block); + let return_loc = ccx.body.terminator_loc(return_block); ConstQualifs { needs_drop: self.needs_drop(RETURN_PLACE, return_loc), @@ -121,7 +121,7 @@ impl Qualifs<'a, 'mir, 'tcx> { } pub struct Validator<'a, 'mir, 'tcx> { - item: &'a Item<'mir, 'tcx>, + ccx: &'a ConstCx<'mir, 'tcx>, qualifs: Qualifs<'a, 'mir, 'tcx>, /// The span of the current statement. @@ -129,19 +129,19 @@ pub struct Validator<'a, 'mir, 'tcx> { } impl Deref for Validator<'_, 'mir, 'tcx> { - type Target = Item<'mir, 'tcx>; + type Target = ConstCx<'mir, 'tcx>; fn deref(&self) -> &Self::Target { - &self.item + &self.ccx } } impl Validator<'a, 'mir, 'tcx> { - pub fn new(item: &'a Item<'mir, 'tcx>) -> Self { - let Item { tcx, body, def_id, param_env, .. } = *item; + pub fn new(ccx: &'a ConstCx<'mir, 'tcx>) -> Self { + let ConstCx { tcx, body, def_id, param_env, .. } = *ccx; - let needs_drop = QualifCursor::new(NeedsDrop, item); - let has_mut_interior = QualifCursor::new(HasMutInterior, item); + let needs_drop = QualifCursor::new(NeedsDrop, ccx); + let has_mut_interior = QualifCursor::new(HasMutInterior, ccx); // We can use `unsound_ignore_borrow_on_drop` here because custom drop impls are not // allowed in a const. @@ -156,11 +156,11 @@ impl Validator<'a, 'mir, 'tcx> { let qualifs = Qualifs { needs_drop, has_mut_interior, indirectly_mutable }; - Validator { span: item.body.span, item, qualifs } + Validator { span: ccx.body.span, ccx, qualifs } } pub fn check_body(&mut self) { - let Item { tcx, body, def_id, const_kind, .. } = *self.item; + let ConstCx { tcx, body, def_id, const_kind, .. } = *self.ccx; let use_min_const_fn_checks = (const_kind == Some(ConstKind::ConstFn) && crate::const_eval::is_min_const_fn(tcx, def_id)) @@ -175,7 +175,7 @@ impl Validator<'a, 'mir, 'tcx> { } } - check_short_circuiting_in_const_local(self.item); + check_short_circuiting_in_const_local(self.ccx); if body.is_cfg_cyclic() { // We can't provide a good span for the error here, but this should be caught by the @@ -196,7 +196,7 @@ impl Validator<'a, 'mir, 'tcx> { } pub fn qualifs_in_return_place(&mut self) -> ConstQualifs { - self.qualifs.in_return_place(self.item) + self.qualifs.in_return_place(self.ccx) } /// Emits an error at the given `span` if an expression cannot be evaluated in the current @@ -344,7 +344,7 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> { Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place) | Rvalue::AddressOf(Mutability::Not, ref place) => { let borrowed_place_has_mut_interior = qualifs::in_place::( - &self.item, + &self.ccx, &mut |local| self.qualifs.has_mut_interior(local, location), place.as_ref(), ); @@ -608,8 +608,8 @@ fn error_min_const_fn_violation(tcx: TyCtxt<'_>, span: Span, msg: Cow<'_, str>) .emit(); } -fn check_short_circuiting_in_const_local(item: &Item<'_, 'tcx>) { - let body = item.body; +fn check_short_circuiting_in_const_local(ccx: &ConstCx<'_, 'tcx>) { + let body = ccx.body; if body.control_flow_destroyed.is_empty() { return; @@ -618,12 +618,12 @@ fn check_short_circuiting_in_const_local(item: &Item<'_, 'tcx>) { let mut locals = body.vars_iter(); if let Some(local) = locals.next() { let span = body.local_decls[local].source_info.span; - let mut error = item.tcx.sess.struct_span_err( + let mut error = ccx.tcx.sess.struct_span_err( span, &format!( "new features like let bindings are not permitted in {}s \ which also use short circuiting operators", - item.const_kind(), + ccx.const_kind(), ), ); for (span, kind) in body.control_flow_destroyed.iter() { diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index b7b67f36ae42..a87955274a77 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -194,10 +194,10 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs { return Default::default(); } - let item = - check_consts::Item { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) }; + let ccx = + check_consts::ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) }; - let mut validator = check_consts::validation::Validator::new(&item); + let mut validator = check_consts::validation::Validator::new(&ccx); validator.check_body(); // We return the qualifs in the return place for every MIR body, even though it is only used diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index 20576d82b1c7..b11dbef3294b 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -30,7 +30,7 @@ use std::cell::Cell; use std::{cmp, iter, mem}; use crate::const_eval::{is_const_fn, is_unstable_const_fn}; -use crate::transform::check_consts::{is_lang_panic_fn, qualifs, ConstKind, Item}; +use crate::transform::check_consts::{is_lang_panic_fn, qualifs, ConstCx, ConstKind}; use crate::transform::{MirPass, MirSource}; /// A `MirPass` for promotion. @@ -264,7 +264,7 @@ pub fn collect_temps_and_candidates( /// /// This wraps an `Item`, and has access to all fields of that `Item` via `Deref` coercion. struct Validator<'a, 'tcx> { - item: Item<'a, 'tcx>, + ccx: ConstCx<'a, 'tcx>, temps: &'a IndexVec, /// Explicit promotion happens e.g. for constant arguments declared via @@ -277,10 +277,10 @@ struct Validator<'a, 'tcx> { } impl std::ops::Deref for Validator<'a, 'tcx> { - type Target = Item<'a, 'tcx>; + type Target = ConstCx<'a, 'tcx>; fn deref(&self) -> &Self::Target { - &self.item + &self.ccx } } @@ -413,7 +413,7 @@ impl<'tcx> Validator<'_, 'tcx> { let statement = &self.body[loc.block].statements[loc.statement_index]; match &statement.kind { StatementKind::Assign(box (_, rhs)) => qualifs::in_rvalue::( - &self.item, + &self.ccx, &mut |l| self.qualif_local::(l), rhs, ), @@ -430,7 +430,7 @@ impl<'tcx> Validator<'_, 'tcx> { match &terminator.kind { TerminatorKind::Call { .. } => { let return_ty = self.body.local_decls[local].ty; - Q::in_any_value_of_ty(&self.item, return_ty) + Q::in_any_value_of_ty(&self.ccx, return_ty) } kind => { span_bug!(terminator.source_info.span, "{:?} not promotable", kind); @@ -723,7 +723,7 @@ pub fn validate_candidates( temps: &IndexVec, candidates: &[Candidate], ) -> Vec { - let mut validator = Validator { item: Item::new(tcx, def_id, body), temps, explicit: false }; + let mut validator = Validator { ccx: ConstCx::new(tcx, def_id, body), temps, explicit: false }; candidates .iter() @@ -1155,7 +1155,7 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>( let mut rpo = traversal::reverse_postorder(&body); let (temps, _) = collect_temps_and_candidates(tcx, &body, &mut rpo); let validator = - Validator { item: Item::new(tcx, mir_def_id, body), temps: &temps, explicit: false }; + Validator { ccx: ConstCx::new(tcx, mir_def_id, body), temps: &temps, explicit: false }; let should_promote = validator.validate_operand(operand).is_ok(); let feature_flag = tcx.features().const_in_array_repeat_expressions;