Skip to content

Commit 7167843

Browse files
committed
Auto merge of #43505 - eddyb:poly-const-eval-layout-of, r=nikomatsakis
rustc_const_eval: always require Substs and a ParamEnv. Fixes #43357 by tracking the `Substs` and `ParamEnv` for const-evaluation in generic contexts.
2 parents 4d5150c + 60cf542 commit 7167843

File tree

15 files changed

+199
-136
lines changed

15 files changed

+199
-136
lines changed

src/librustc/middle/const_val.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub use rustc_const_math::ConstInt;
1414
use hir;
1515
use hir::def::Def;
1616
use hir::def_id::DefId;
17-
use ty::{TyCtxt, layout};
17+
use traits::Reveal;
18+
use ty::{self, TyCtxt, layout};
1819
use ty::subst::Substs;
1920
use util::common::ErrorReported;
2021
use rustc_const_math::*;
@@ -229,8 +230,9 @@ pub fn eval_length(tcx: TyCtxt,
229230
{
230231
let count_expr = &tcx.hir.body(count).value;
231232
let count_def_id = tcx.hir.body_owner_def_id(count);
232-
let substs = Substs::empty();
233-
match tcx.at(count_expr.span).const_eval((count_def_id, substs)) {
233+
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
234+
let substs = Substs::identity_for_item(tcx.global_tcx(), count_def_id);
235+
match tcx.at(count_expr.span).const_eval(param_env.and((count_def_id, substs))) {
234236
Ok(Integral(Usize(count))) => {
235237
let val = count.as_u64(tcx.sess.target.uint_type);
236238
assert_eq!(val as usize as u64, val);

src/librustc/ty/maps.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
372372
}
373373

374374
impl<'tcx> QueryDescription for queries::const_eval<'tcx> {
375-
fn describe(tcx: TyCtxt, (def_id, _): (DefId, &'tcx Substs<'tcx>)) -> String {
376-
format!("const-evaluating `{}`", tcx.item_path_str(def_id))
375+
fn describe(tcx: TyCtxt, key: ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>) -> String {
376+
format!("const-evaluating `{}`", tcx.item_path_str(key.value.0))
377377
}
378378
}
379379

@@ -935,7 +935,7 @@ define_maps! { <'tcx>
935935

936936
/// Results of evaluating const items or constants embedded in
937937
/// other items (such as enum variant explicit discriminants).
938-
[] const_eval: const_eval_dep_node((DefId, &'tcx Substs<'tcx>))
938+
[] const_eval: const_eval_dep_node(ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
939939
-> const_val::EvalResult<'tcx>,
940940

941941
/// Performs the privacy check and computes "access levels".
@@ -1032,8 +1032,9 @@ fn typeck_item_bodies_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
10321032
DepConstructor::TypeckBodiesKrate
10331033
}
10341034

1035-
fn const_eval_dep_node<'tcx>((def_id, substs): (DefId, &'tcx Substs<'tcx>))
1035+
fn const_eval_dep_node<'tcx>(key: ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
10361036
-> DepConstructor<'tcx> {
1037+
let (def_id, substs) = key.value;
10371038
DepConstructor::ConstEval { def_id, substs }
10381039
}
10391040

src/librustc/ty/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1582,14 +1582,15 @@ impl<'a, 'gcx, 'tcx> AdtDef {
15821582
#[inline]
15831583
pub fn discriminants(&'a self, tcx: TyCtxt<'a, 'gcx, 'tcx>)
15841584
-> impl Iterator<Item=ConstInt> + 'a {
1585+
let param_env = ParamEnv::empty(traits::Reveal::UserFacing);
15851586
let repr_type = self.repr.discr_type();
15861587
let initial = repr_type.initial_discriminant(tcx.global_tcx());
15871588
let mut prev_discr = None::<ConstInt>;
15881589
self.variants.iter().map(move |v| {
15891590
let mut discr = prev_discr.map_or(initial, |d| d.wrap_incr());
15901591
if let VariantDiscr::Explicit(expr_did) = v.discr {
1591-
let substs = Substs::empty();
1592-
match tcx.const_eval((expr_did, substs)) {
1592+
let substs = Substs::identity_for_item(tcx.global_tcx(), expr_did);
1593+
match tcx.const_eval(param_env.and((expr_did, substs))) {
15931594
Ok(ConstVal::Integral(v)) => {
15941595
discr = v;
15951596
}
@@ -1617,6 +1618,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16171618
tcx: TyCtxt<'a, 'gcx, 'tcx>,
16181619
variant_index: usize)
16191620
-> ConstInt {
1621+
let param_env = ParamEnv::empty(traits::Reveal::UserFacing);
16201622
let repr_type = self.repr.discr_type();
16211623
let mut explicit_value = repr_type.initial_discriminant(tcx.global_tcx());
16221624
let mut explicit_index = variant_index;
@@ -1627,8 +1629,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16271629
explicit_index -= distance;
16281630
}
16291631
ty::VariantDiscr::Explicit(expr_did) => {
1630-
let substs = Substs::empty();
1631-
match tcx.const_eval((expr_did, substs)) {
1632+
let substs = Substs::identity_for_item(tcx.global_tcx(), expr_did);
1633+
match tcx.const_eval(param_env.and((expr_did, substs))) {
16321634
Ok(ConstVal::Integral(v)) => {
16331635
explicit_value = v;
16341636
break;

src/librustc/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ macro_rules! CopyImpls {
398398
}
399399
}
400400

401-
CopyImpls! { (), hir::Unsafety, abi::Abi }
401+
CopyImpls! { (), hir::Unsafety, abi::Abi, hir::def_id::DefId }
402402

403403
impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
404404
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {

src/librustc_const_eval/check_match.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc::middle::mem_categorization::{cmt};
2121
use rustc::middle::region::RegionMaps;
2222
use rustc::session::Session;
2323
use rustc::ty::{self, Ty, TyCtxt};
24+
use rustc::ty::subst::Substs;
2425
use rustc::lint;
2526
use rustc_errors::{Diagnostic, Level, DiagnosticBuilder};
2627

@@ -51,7 +52,8 @@ impl<'a, 'tcx> Visitor<'tcx> for OuterVisitor<'a, 'tcx> {
5152
tcx: self.tcx,
5253
tables: self.tcx.body_tables(b),
5354
region_maps: &self.tcx.region_maps(def_id),
54-
param_env: self.tcx.param_env(def_id)
55+
param_env: self.tcx.param_env(def_id),
56+
identity_substs: Substs::identity_for_item(self.tcx, def_id),
5557
}.visit_body(self.tcx.hir.body(b));
5658
}
5759
}
@@ -69,6 +71,7 @@ struct MatchVisitor<'a, 'tcx: 'a> {
6971
tcx: TyCtxt<'a, 'tcx, 'tcx>,
7072
tables: &'a ty::TypeckTables<'tcx>,
7173
param_env: ty::ParamEnv<'tcx>,
74+
identity_substs: &'tcx Substs<'tcx>,
7275
region_maps: &'a RegionMaps,
7376
}
7477

@@ -110,7 +113,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
110113
}
111114
}
112115

113-
impl<'a, 'gcx, 'tcx> PatternContext<'a, 'gcx, 'tcx> {
116+
impl<'a, 'tcx> PatternContext<'a, 'tcx> {
114117
fn report_inlining_errors(&self, pat_span: Span) {
115118
for error in &self.errors {
116119
match *error {
@@ -162,7 +165,9 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
162165

163166
let inlined_arms : Vec<(Vec<_>, _)> = arms.iter().map(|arm| (
164167
arm.pats.iter().map(|pat| {
165-
let mut patcx = PatternContext::new(self.tcx, self.tables);
168+
let mut patcx = PatternContext::new(self.tcx,
169+
self.param_env.and(self.identity_substs),
170+
self.tables);
166171
let pattern = expand_pattern(cx, patcx.lower_pattern(&pat));
167172
if !patcx.errors.is_empty() {
168173
patcx.report_inlining_errors(pat.span);
@@ -229,7 +234,9 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
229234
fn check_irrefutable(&self, pat: &Pat, origin: &str) {
230235
let module = self.tcx.hir.get_module_parent(pat.id);
231236
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {
232-
let mut patcx = PatternContext::new(self.tcx, self.tables);
237+
let mut patcx = PatternContext::new(self.tcx,
238+
self.param_env.and(self.identity_substs),
239+
self.tables);
233240
let pattern = patcx.lower_pattern(pat);
234241
let pattern_ty = pattern.ty;
235242
let pats : Matrix = vec![vec![

0 commit comments

Comments
 (0)