Skip to content

Commit 3f7be22

Browse files
committed
Simplify the constness checks
1 parent fefe816 commit 3f7be22

35 files changed

+317
-331
lines changed

src/librustc/dep_graph/dep_node.rs

-2
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ define_dep_nodes!( <'tcx>
479479

480480
// Represents the MIR for a fn; also used as the task node for
481481
// things read/modify that MIR.
482-
[] MirConstQualif(DefId),
483482
[] MirBuilt(DefId),
484483
[] MirConst(DefId),
485484
[] MirValidated(DefId),
@@ -562,7 +561,6 @@ define_dep_nodes!( <'tcx>
562561
[input] DefSpan(DefId),
563562
[] LookupStability(DefId),
564563
[] LookupDeprecationEntry(DefId),
565-
[] ConstIsRvaluePromotableToStatic(DefId),
566564
[] RvaluePromotableMap(DefId),
567565
[] ImplParent(DefId),
568566
[] TraitOfItem(DefId),

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#![feature(in_band_lifetimes)]
7575
#![feature(macro_at_most_once_rep)]
7676
#![feature(inclusive_range_methods)]
77+
#![feature(extern_prelude)]
7778

7879
#![recursion_limit="512"]
7980

src/librustc/middle/expr_use_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::rc::Rc;
3232
use syntax::ast;
3333
use syntax::ptr::P;
3434
use syntax_pos::Span;
35-
use util::nodemap::ItemLocalSet;
35+
use util::nodemap::ItemLocalMap;
3636

3737
///////////////////////////////////////////////////////////////////////////
3838
// The Delegate trait
@@ -280,7 +280,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx, 'tcx> {
280280
param_env: ty::ParamEnv<'tcx>,
281281
region_scope_tree: &'a region::ScopeTree,
282282
tables: &'a ty::TypeckTables<'tcx>,
283-
rvalue_promotable_map: Option<Lrc<ItemLocalSet>>)
283+
rvalue_promotable_map: Option<Lrc<ItemLocalMap<ty::Promotability>>>)
284284
-> Self
285285
{
286286
ExprUseVisitor {

src/librustc/middle/mem_categorization.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use std::fmt;
8787
use std::hash::{Hash, Hasher};
8888
use rustc_data_structures::sync::Lrc;
8989
use std::rc::Rc;
90-
use util::nodemap::ItemLocalSet;
90+
use util::nodemap::ItemLocalMap;
9191

9292
#[derive(Clone, Debug, PartialEq)]
9393
pub enum Categorization<'tcx> {
@@ -295,7 +295,7 @@ pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
295295
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
296296
pub region_scope_tree: &'a region::ScopeTree,
297297
pub tables: &'a ty::TypeckTables<'tcx>,
298-
rvalue_promotable_map: Option<Lrc<ItemLocalSet>>,
298+
rvalue_promotable_map: Option<Lrc<ItemLocalMap<ty::Promotability>>>,
299299
infcx: Option<&'a InferCtxt<'a, 'gcx, 'tcx>>,
300300
}
301301

@@ -404,7 +404,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx, 'tcx> {
404404
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
405405
region_scope_tree: &'a region::ScopeTree,
406406
tables: &'a ty::TypeckTables<'tcx>,
407-
rvalue_promotable_map: Option<Lrc<ItemLocalSet>>)
407+
rvalue_promotable_map: Option<Lrc<ItemLocalMap<ty::Promotability>>>)
408408
-> MemCategorizationContext<'a, 'tcx, 'tcx> {
409409
MemCategorizationContext {
410410
tcx,
@@ -946,32 +946,35 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
946946
span,
947947
expr_ty,
948948
);
949-
let promotable = self.rvalue_promotable_map.as_ref().map(|m| m.contains(&hir_id.local_id))
950-
.unwrap_or(false);
949+
use ty::Promotability::*;
950+
let promotability = self
951+
.rvalue_promotable_map
952+
.as_ref()
953+
.and_then(|m| m.get(&hir_id.local_id).cloned())
954+
.unwrap_or(NotPromotable);
951955

952956
debug!(
953957
"cat_rvalue_node: promotable = {:?}",
954-
promotable,
958+
promotability,
955959
);
956960

957961
// Always promote `[T; 0]` (even when e.g. borrowed mutably).
958-
let promotable = match expr_ty.sty {
959-
ty::TyArray(_, len) if len.assert_usize(self.tcx) == Some(0) => true,
960-
_ => promotable,
962+
let promotability = match expr_ty.sty {
963+
ty::TyArray(_, len) if len.assert_usize(self.tcx) == Some(0) => Promotable,
964+
_ => promotability,
961965
};
962966

963967
debug!(
964968
"cat_rvalue_node: promotable = {:?} (2)",
965-
promotable,
969+
promotability,
966970
);
967971

968972
// Compute maximum lifetime of this rvalue. This is 'static if
969973
// we can promote to a constant, otherwise equal to enclosing temp
970974
// lifetime.
971-
let re = if promotable {
972-
self.tcx.types.re_static
973-
} else {
974-
self.temporary_scope(hir_id.local_id)
975+
let re = match promotability {
976+
Promotable | NotInspectable => self.tcx.types.re_static,
977+
NotPromotable => self.temporary_scope(hir_id.local_id),
975978
};
976979
let ret = self.cat_rvalue(hir_id, span, re, expr_ty);
977980
debug!("cat_rvalue_node ret {:?}", ret);

src/librustc/ty/mod.rs

+43
Original file line numberDiff line numberDiff line change
@@ -2998,3 +2998,46 @@ impl fmt::Debug for SymbolName {
29982998
fmt::Display::fmt(&self.name, fmt)
29992999
}
30003000
}
3001+
3002+
impl_stable_hash_for!(enum self::Promotability {
3003+
Promotable,
3004+
NotInspectable,
3005+
NotPromotable
3006+
});
3007+
3008+
#[must_use]
3009+
#[derive(Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
3010+
/// A fancy `bool` for making it less fragile to compute the promotability of something
3011+
pub enum Promotability {
3012+
Promotable,
3013+
/// The value is promotable as long as that promotion will not cause parts of it to be inspected
3014+
/// at compile-time. So it can't be used in a BinOp or UnOp, or dereferenced, or...
3015+
NotInspectable,
3016+
NotPromotable
3017+
}
3018+
3019+
impl Promotability {
3020+
pub fn inspect(self) -> Self {
3021+
match self {
3022+
// We are allowed to promote operations inspecting the value on these
3023+
Promotability::Promotable => Promotability::Promotable,
3024+
// We are *not* allowed to inspect values of this
3025+
Promotability::NotInspectable => Promotability::NotPromotable,
3026+
// This isn't promoted anyway
3027+
Promotability::NotPromotable => Promotability::NotPromotable,
3028+
}
3029+
}
3030+
}
3031+
3032+
impl std::ops::BitAnd for Promotability {
3033+
type Output = Self;
3034+
3035+
fn bitand(self, rhs: Self) -> Self {
3036+
use self::Promotability::*;
3037+
match (self, rhs) {
3038+
(NotPromotable, _) | (_, NotPromotable) => NotPromotable,
3039+
(NotInspectable, _) | (_, NotInspectable) => NotInspectable,
3040+
(Promotable, Promotable) => Promotable,
3041+
}
3042+
}
3043+
}

src/librustc/ty/query/config.rs

-20
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::trait_of_item<'tcx> {
375375
}
376376
}
377377

378-
impl<'tcx> QueryDescription<'tcx> for queries::const_is_rvalue_promotable_to_static<'tcx> {
379-
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
380-
format!("const checking if rvalue is promotable to static `{}`",
381-
tcx.item_path_str(def_id))
382-
}
383-
384-
#[inline]
385-
fn cache_on_disk(_: Self::Key) -> bool {
386-
true
387-
}
388-
389-
#[inline]
390-
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
391-
id: SerializedDepNodeIndex)
392-
-> Option<Self::Value> {
393-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
394-
}
395-
}
396-
397378
impl<'tcx> QueryDescription<'tcx> for queries::rvalue_promotable_map<'tcx> {
398379
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
399380
format!("checking which parts of `{}` are promotable to static",
@@ -842,7 +823,6 @@ macro_rules! impl_disk_cacheable_query(
842823
impl_disk_cacheable_query!(unsafety_check_result, |def_id| def_id.is_local());
843824
impl_disk_cacheable_query!(borrowck, |def_id| def_id.is_local());
844825
impl_disk_cacheable_query!(mir_borrowck, |def_id| def_id.is_local());
845-
impl_disk_cacheable_query!(mir_const_qualif, |def_id| def_id.is_local());
846826
impl_disk_cacheable_query!(check_match, |def_id| def_id.is_local());
847827
impl_disk_cacheable_query!(def_symbol_name, |_| true);
848828
impl_disk_cacheable_query!(type_of, |def_id| def_id.is_local());

src/librustc/ty/query/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ use traits::Clauses;
4444
use ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
4545
use ty::steal::Steal;
4646
use ty::subst::Substs;
47-
use util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};
47+
use util::nodemap::{DefIdSet, DefIdMap, ItemLocalMap};
4848
use util::common::{ErrorReported};
4949

50-
use rustc_data_structures::indexed_set::IdxSetBuf;
5150
use rustc_target::spec::PanicStrategy;
5251
use rustc_data_structures::indexed_vec::IndexVec;
5352
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -203,11 +202,6 @@ define_queries! { <'tcx>
203202
/// constructors.
204203
[] fn mir_keys: mir_keys(CrateNum) -> Lrc<DefIdSet>,
205204

206-
/// Maps DefId's that have an associated Mir to the result
207-
/// of the MIR qualify_consts pass. The actual meaning of
208-
/// the value isn't known except to the pass itself.
209-
[] fn mir_const_qualif: MirConstQualif(DefId) -> (u8, Lrc<IdxSetBuf<mir::Local>>),
210-
211205
/// Fetch the MIR for a given def-id right after it's built - this includes
212206
/// unreachable code.
213207
[] fn mir_built: MirBuilt(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
@@ -332,8 +326,8 @@ define_queries! { <'tcx>
332326

333327
TypeChecking {
334328
[] fn trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
335-
[] fn const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
336-
[] fn rvalue_promotable_map: RvaluePromotableMap(DefId) -> Lrc<ItemLocalSet>,
329+
[] fn rvalue_promotable_map: RvaluePromotableMap(DefId)
330+
-> Lrc<ItemLocalMap<ty::Promotability>>,
337331
},
338332

339333
Codegen {

src/librustc/ty/query/on_disk_cache.rs

-2
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ impl<'sess> OnDiskCache<'sess> {
223223
encode_query_results::<unsafety_check_result, _>(tcx, enc, qri)?;
224224
encode_query_results::<borrowck, _>(tcx, enc, qri)?;
225225
encode_query_results::<mir_borrowck, _>(tcx, enc, qri)?;
226-
encode_query_results::<mir_const_qualif, _>(tcx, enc, qri)?;
227226
encode_query_results::<def_symbol_name, _>(tcx, enc, qri)?;
228-
encode_query_results::<const_is_rvalue_promotable_to_static, _>(tcx, enc, qri)?;
229227
encode_query_results::<symbol_name, _>(tcx, enc, qri)?;
230228
encode_query_results::<check_match, _>(tcx, enc, qri)?;
231229
encode_query_results::<codegen_fn_attrs, _>(tcx, enc, qri)?;

src/librustc/ty/query/plumbing.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
10691069
},
10701070
DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
10711071
DepKind::MirBuilt => { force!(mir_built, def_id!()); }
1072-
DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
10731072
DepKind::MirConst => { force!(mir_const, def_id!()); }
10741073
DepKind::MirValidated => { force!(mir_validated, def_id!()); }
10751074
DepKind::MirOptimized => { force!(optimized_mir, def_id!()); }
@@ -1121,9 +1120,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
11211120
DepKind::LookupDeprecationEntry => {
11221121
force!(lookup_deprecation_entry, def_id!());
11231122
}
1124-
DepKind::ConstIsRvaluePromotableToStatic => {
1125-
force!(const_is_rvalue_promotable_to_static, def_id!());
1126-
}
11271123
DepKind::RvaluePromotableMap => { force!(rvalue_promotable_map, def_id!()); }
11281124
DepKind::ImplParent => { force!(impl_parent, def_id!()); }
11291125
DepKind::TraitOfItem => { force!(trait_of_item, def_id!()); }
@@ -1285,9 +1281,7 @@ impl_load_from_cache!(
12851281
UnsafetyCheckResult => unsafety_check_result,
12861282
BorrowCheck => borrowck,
12871283
MirBorrowCheck => mir_borrowck,
1288-
MirConstQualif => mir_const_qualif,
12891284
SymbolName => def_symbol_name,
1290-
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
12911285
CheckMatch => check_match,
12921286
TypeOfItem => type_of,
12931287
GenericsOfItem => generics_of,

src/librustc_metadata/cstore_impl.rs

-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use syntax::edition::Edition;
4242
use syntax::parse::filemap_to_stream;
4343
use syntax::symbol::Symbol;
4444
use syntax_pos::{Span, NO_EXPANSION, FileName};
45-
use rustc_data_structures::indexed_set::IdxSetBuf;
4645
use rustc::hir;
4746

4847
macro_rules! provide {
@@ -140,9 +139,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
140139

141140
mir
142141
}
143-
mir_const_qualif => {
144-
(cdata.mir_const_qualif(def_id.index), Lrc::new(IdxSetBuf::new_empty(0)))
145-
}
146142
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
147143
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
148144
is_const_fn => { cdata.is_const_fn(def_id.index) }
@@ -164,9 +160,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
164160
rendered_const => { cdata.get_rendered_const(def_id.index) }
165161
impl_parent => { cdata.get_parent_impl(def_id.index) }
166162
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
167-
const_is_rvalue_promotable_to_static => {
168-
cdata.const_is_rvalue_promotable_to_static(def_id.index)
169-
}
170163
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
171164

172165
dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }

src/librustc_metadata/decoder.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,6 @@ impl<'a, 'tcx> CrateMetadata {
770770
}
771771
}
772772

773-
pub fn const_is_rvalue_promotable_to_static(&self, id: DefIndex) -> bool {
774-
match self.entry(id).kind {
775-
EntryKind::AssociatedConst(_, data, _) |
776-
EntryKind::Const(data, _) => data.ast_promotable,
777-
_ => bug!(),
778-
}
779-
}
780-
781773
pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
782774
!self.is_proc_macro(id) &&
783775
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
@@ -793,25 +785,14 @@ impl<'a, 'tcx> CrateMetadata {
793785
}
794786
}
795787

796-
pub fn mir_const_qualif(&self, id: DefIndex) -> u8 {
797-
match self.entry(id).kind {
798-
EntryKind::Const(qualif, _) |
799-
EntryKind::AssociatedConst(AssociatedContainer::ImplDefault, qualif, _) |
800-
EntryKind::AssociatedConst(AssociatedContainer::ImplFinal, qualif, _) => {
801-
qualif.mir
802-
}
803-
_ => bug!(),
804-
}
805-
}
806-
807788
pub fn get_associated_item(&self, id: DefIndex) -> ty::AssociatedItem {
808789
let item = self.entry(id);
809790
let def_key = self.def_key(id);
810791
let parent = self.local_def_id(def_key.parent.unwrap());
811792
let name = def_key.disambiguated_data.data.get_opt_name().unwrap();
812793

813794
let (kind, container, has_self) = match item.kind {
814-
EntryKind::AssociatedConst(container, _, _) => {
795+
EntryKind::AssociatedConst(container, _) => {
815796
(ty::AssociatedKind::Const, container, false)
816797
}
817798
EntryKind::Method(data) => {
@@ -1006,8 +987,8 @@ impl<'a, 'tcx> CrateMetadata {
1006987

1007988
pub fn get_rendered_const(&self, id: DefIndex) -> String {
1008989
match self.entry(id).kind {
1009-
EntryKind::Const(_, data) |
1010-
EntryKind::AssociatedConst(_, _, data) => data.decode(self).0,
990+
EntryKind::Const(data) |
991+
EntryKind::AssociatedConst(_, data) => data.decode(self).0,
1011992
_ => bug!(),
1012993
}
1013994
}

0 commit comments

Comments
 (0)