Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert the LazyConst PR #59178

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt;
use rustc_macros::HashStable;

use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}};
use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
use crate::hir::def_id::DefId;

use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};

Expand Down Expand Up @@ -42,6 +43,10 @@ pub enum ConstValue<'tcx> {
/// An allocation together with a pointer into the allocation.
/// Invariant: the pointer's `AllocId` resolves to the allocation.
ByRef(Pointer, &'tcx Allocation),

/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
/// variants when the code is monomorphic enough for that.
Unevaluated(DefId, SubstsRef<'tcx>),
}

#[cfg(target_arch = "x86_64")]
Expand All @@ -54,6 +59,7 @@ impl<'tcx> ConstValue<'tcx> {
ConstValue::Param(_) |
ConstValue::Infer(_) |
ConstValue::ByRef(..) |
ConstValue::Unevaluated(..) |
ConstValue::Slice(..) => None,
ConstValue::Scalar(val) => Some(val),
}
Expand Down
19 changes: 5 additions & 14 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2179,8 +2179,8 @@ impl<'tcx> Operand<'tcx> {
span,
ty,
user_ty: None,
literal: tcx.mk_lazy_const(
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
literal: tcx.mk_const(
ty::Const::zero_sized(ty),
),
})
}
Expand Down Expand Up @@ -2497,7 +2497,7 @@ pub struct Constant<'tcx> {
/// Needed for NLL to impose user-given type constraints.
pub user_ty: Option<UserTypeAnnotationIndex>,

pub literal: &'tcx ty::LazyConst<'tcx>,
pub literal: &'tcx ty::Const<'tcx>,
}

/// A collection of projections into user types.
Expand Down Expand Up @@ -2696,18 +2696,9 @@ newtype_index! {
impl<'tcx> Debug for Constant<'tcx> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "const ")?;
fmt_lazy_const_val(fmt, self.literal)
fmt_const_val(fmt, *self.literal)
}
}

/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
pub fn fmt_lazy_const_val(f: &mut impl Write, const_val: &ty::LazyConst<'_>) -> fmt::Result {
match *const_val {
ty::LazyConst::Unevaluated(..) => write!(f, "{:?}", const_val),
ty::LazyConst::Evaluated(c) => fmt_const_val(f, c),
}
}

/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Result {
use crate::ty::TyKind::*;
Expand Down Expand Up @@ -2760,7 +2751,7 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Resul
}
}
// just raw dump everything else
write!(f, "{:?}:{}", value, ty)
write!(f, "{:?} : {}", value, ty)
}

fn def_path_str(def_id: DefId) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ macro_rules! make_mir_visitor {
}

fn visit_const(&mut self,
constant: & $($mutability)? &'tcx ty::LazyConst<'tcx>,
constant: & $($mutability)? &'tcx ty::Const<'tcx>,
_: Location) {
self.super_const(constant);
}
Expand Down Expand Up @@ -886,7 +886,7 @@ macro_rules! make_mir_visitor {
fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
}

fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::LazyConst<'tcx>) {
fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::Const<'tcx>) {
}

fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {
Expand Down
11 changes: 6 additions & 5 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::util;
use crate::hir::def_id::DefId;
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
use crate::infer::type_variable::TypeVariableOrigin;
use crate::mir::interpret::{GlobalId};
use crate::mir::interpret::{GlobalId, ConstValue};
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
use rustc_macros::HashStable;
use syntax::ast::Ident;
Expand Down Expand Up @@ -397,8 +397,8 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
}
}

fn fold_const(&mut self, constant: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
if let ty::LazyConst::Unevaluated(def_id, substs) = *constant {
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
let tcx = self.selcx.tcx().global_tcx();
if let Some(param_env) = self.tcx().lift_to_global(&self.param_env) {
if substs.needs_infer() || substs.has_placeholders() {
Expand All @@ -411,8 +411,9 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
};
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
let substs = tcx.lift_to_global(&substs).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do substs need to be lifted here? If substs needed inference but the const still evaluated successfully, doesn't that mean we can just use InternalSubsts::empty() here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or am I misunderstanding what's happening here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing - InternalSubsts::empty() would be wrong, but I'm wondering why there is any substitution at all, maybe this is from the time when this could return e.g. function pointers referring to generic parameters?

IMO const_eval should either return an (interned) &'tcx ty::Const, or the Scalar | Slice | ByRef enum by value, not what it does now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nevermind, const_eval returns the type as well, which could depend on parameters and whatnot (especially lifetime ones!).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool, makes sense. The reason I ask is that this blows up on array lengths that use generic parameters when they have a real param_env.

let evaluated = tcx.mk_const(evaluated);
let evaluated = evaluated.subst(tcx, substs);
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
return evaluated;
}
}
} else {
Expand All @@ -424,7 +425,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
promoted: None
};
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
return tcx.mk_const(evaluated);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/librustc/traits/query/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::infer::at::At;
use crate::infer::canonical::OriginalQueryValues;
use crate::infer::{InferCtxt, InferOk};
use crate::mir::interpret::GlobalId;
use crate::mir::interpret::{GlobalId, ConstValue};
use crate::traits::project::Normalized;
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
use crate::ty::fold::{TypeFoldable, TypeFolder};
Expand Down Expand Up @@ -188,8 +188,8 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
}
}

fn fold_const(&mut self, constant: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
if let ty::LazyConst::Unevaluated(def_id, substs) = *constant {
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
let tcx = self.infcx.tcx.global_tcx();
if let Some(param_env) = self.tcx().lift_to_global(&self.param_env) {
if substs.needs_infer() || substs.has_placeholders() {
Expand All @@ -202,8 +202,9 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
};
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
let substs = tcx.lift_to_global(&substs).unwrap();
let evaluated = tcx.mk_const(evaluated);
let evaluated = evaluated.subst(tcx, substs);
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
return evaluated;
}
}
} else {
Expand All @@ -215,7 +216,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
promoted: None,
};
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
return tcx.mk_const(evaluated);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ pub fn decode_canonical_var_infos<'a, 'tcx, D>(decoder: &mut D)
}

#[inline]
pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
-> Result<&'tcx ty::LazyConst<'tcx>, D::Error>
pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
-> Result<&'tcx ty::Const<'tcx>, D::Error>
where D: TyDecoder<'a, 'tcx>,
'tcx: 'a,
{
Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
}

#[inline]
Expand Down Expand Up @@ -389,10 +389,10 @@ macro_rules! implement_ty_decoder {
}
}

impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::LazyConst<'tcx>>
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
for $DecoderName<$($typaram),*> {
fn specialized_decode(&mut self) -> Result<&'tcx ty::LazyConst<'tcx>, Self::Error> {
decode_lazy_const(self)
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
decode_const(self)
}
}

Expand Down
43 changes: 17 additions & 26 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::traits;
use crate::traits::{Clause, Clauses, GoalKind, Goal, Goals};
use crate::ty::{self, DefIdTree, Ty, TypeAndMut};
use crate::ty::{TyS, TyKind, List};
use crate::ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorSubsts, Region, Const, LazyConst};
use crate::ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorSubsts, Region, Const};
use crate::ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predicate};
use crate::ty::RegionKind;
use crate::ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid, ConstVid};
Expand Down Expand Up @@ -126,7 +126,7 @@ pub struct CtxtInterners<'tcx> {
goal: InternedSet<'tcx, GoalKind<'tcx>>,
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
const_: InternedSet<'tcx, Const<'tcx>>,
}

impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
Expand All @@ -144,7 +144,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
goal: Default::default(),
goal_list: Default::default(),
projs: Default::default(),
lazy_const: Default::default(),
const_: Default::default(),
}
}

Expand Down Expand Up @@ -874,14 +874,11 @@ impl CanonicalUserType<'gcx> {
_ => false,
},

UnpackedKind::Const(ct) => match ct {
ty::LazyConst::Evaluated(ty::Const {
val: ConstValue::Infer(InferConst::Canonical(debruijn, b)),
..
}) => {
UnpackedKind::Const(ct) => match ct.val {
ConstValue::Infer(InferConst::Canonical(debruijn, b)) => {
// We only allow a `ty::INNERMOST` index in substitutions.
assert_eq!(*debruijn, ty::INNERMOST);
cvar == *b
assert_eq!(debruijn, ty::INNERMOST);
cvar == b
}
_ => false,
},
Expand Down Expand Up @@ -1788,7 +1785,7 @@ macro_rules! nop_list_lift {
nop_lift!{Ty<'a> => Ty<'tcx>}
nop_lift!{Region<'a> => Region<'tcx>}
nop_lift!{Goal<'a> => Goal<'tcx>}
nop_lift!{&'a LazyConst<'a> => &'tcx LazyConst<'tcx>}
nop_lift!{&'a Const<'a> => &'tcx Const<'tcx>}

nop_list_lift!{Goal<'a> => Goal<'tcx>}
nop_list_lift!{Clause<'a> => Clause<'tcx>}
Expand Down Expand Up @@ -2274,12 +2271,6 @@ impl<'tcx: 'lcx, 'lcx> Borrow<GoalKind<'lcx>> for Interned<'tcx, GoalKind<'tcx>>
}
}

impl<'tcx: 'lcx, 'lcx> Borrow<LazyConst<'lcx>> for Interned<'tcx, LazyConst<'tcx>> {
fn borrow<'a>(&'a self) -> &'a LazyConst<'lcx> {
&self.0
}
}

impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
Expand Down Expand Up @@ -2387,7 +2378,7 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
direct_interners!('tcx,
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
lazy_const: mk_lazy_const(|c: &LazyConst<'_>| keep_local(&c)) -> LazyConst<'tcx>
const_: mk_const(|c: &Const<'_>| keep_local(&c)) -> Const<'tcx>
);

macro_rules! slice_interners {
Expand Down Expand Up @@ -2575,8 +2566,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

#[inline]
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
self.mk_ty(Array(ty, self.mk_lazy_const(
ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n))
self.mk_ty(Array(ty, self.mk_const(
ty::Const::from_usize(self.global_tcx(), n)
)))
}

Expand Down Expand Up @@ -2670,11 +2661,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

#[inline]
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx LazyConst<'tcx> {
self.mk_lazy_const(LazyConst::Evaluated(ty::Const {
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx Const<'tcx> {
self.mk_const(ty::Const {
val: ConstValue::Infer(InferConst::Var(v)),
ty,
}))
})
}

#[inline]
Expand Down Expand Up @@ -2705,11 +2696,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
index: u32,
name: InternedString,
ty: Ty<'tcx>
) -> &'tcx LazyConst<'tcx> {
self.mk_lazy_const(LazyConst::Evaluated(ty::Const {
) -> &'tcx Const<'tcx> {
self.mk_const(ty::Const {
val: ConstValue::Param(ParamConst { index, name }),
ty,
}))
})
}

#[inline]
Expand Down
9 changes: 3 additions & 6 deletions src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,9 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {

ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
ty::Array(_, n) => match n {
ty::LazyConst::Evaluated(n) => match n.assert_usize(tcx) {
Some(n) => format!("array of {} elements", n).into(),
None => "array".into(),
},
ty::LazyConst::Unevaluated(..) => "array".into(),
ty::Array(_, n) => match n.assert_usize(tcx) {
Some(n) => format!("array of {} elements", n).into(),
None => "array".into(),
}
ty::Slice(_) => "slice".into(),
ty::RawPtr(_) => "*-ptr".into(),
Expand Down
Loading