Skip to content

Commit

Permalink
Auto merge of #66233 - cjgillot:constkind, r=oli-obk
Browse files Browse the repository at this point in the history
Split ConstValue into two enums

Hello,

Issue #59210 appeared abandoned, so I gave it a go.
Some further cleanup and refactoring may be mandated.

I did not test beyond `x.py check`, since my home computer dies compiling librustc.

Fixes #59210
  • Loading branch information
bors committed Nov 14, 2019
2 parents 3f07f1c + 552fa64 commit 5e380b7
Show file tree
Hide file tree
Showing 54 changed files with 305 additions and 274 deletions.
11 changes: 5 additions & 6 deletions src/librustc/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::infer::canonical::{
OriginalQueryValues,
};
use crate::infer::InferCtxt;
use crate::mir::interpret::ConstValue;
use std::sync::atomic::Ordering;
use crate::ty::fold::{TypeFoldable, TypeFolder};
use crate::ty::subst::GenericArg;
Expand Down Expand Up @@ -441,7 +440,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {

fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
match ct.val {
ConstValue::Infer(InferConst::Var(vid)) => {
ty::ConstKind::Infer(InferConst::Var(vid)) => {
debug!("canonical: const var found with vid {:?}", vid);
match self.infcx.unwrap().probe_const_var(vid) {
Ok(c) => {
Expand All @@ -465,17 +464,17 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
}
}
}
ConstValue::Infer(InferConst::Fresh(_)) => {
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
bug!("encountered a fresh const during canonicalization")
}
ConstValue::Bound(debruijn, _) => {
ty::ConstKind::Bound(debruijn, _) => {
if debruijn >= self.binder_index {
bug!("escaping bound type during canonicalization")
} else {
return ct;
}
}
ConstValue::Placeholder(placeholder) => {
ty::ConstKind::Placeholder(placeholder) => {
return self.canonicalize_const_var(
CanonicalVarInfo {
kind: CanonicalVarKind::PlaceholderConst(placeholder),
Expand Down Expand Up @@ -700,7 +699,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
let var = self.canonical_var(info, const_var.into());
self.tcx().mk_const(
ty::Const {
val: ConstValue::Bound(self.binder_index, var.into()),
val: ty::ConstKind::Bound(self.binder_index, var.into()),
ty: self.fold_ty(const_var.ty),
}
)
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/infer/canonical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{ConstVariableOrigin, ConstVariableOriginKind};
use crate::infer::region_constraints::MemberConstraint;
use crate::mir::interpret::ConstValue;
use rustc_index::vec::IndexVec;
use rustc_macros::HashStable;
use rustc_serialize::UseSpecializedDecodable;
Expand Down Expand Up @@ -447,7 +446,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
};
self.tcx.mk_const(
ty::Const {
val: ConstValue::Placeholder(placeholder_mapped),
val: ty::ConstKind::Placeholder(placeholder_mapped),
ty: self.tcx.types.err, // FIXME(const_generics)
}
).into()
Expand Down Expand Up @@ -510,7 +509,7 @@ impl<'tcx> CanonicalVarValues<'tcx> {
GenericArgKind::Const(ct) => {
tcx.mk_const(ty::Const {
ty: ct.ty,
val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
}).into()
}
})
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::infer::canonical::{
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
use crate::infer::InferCtxtBuilder;
use crate::infer::{InferCtxt, InferOk, InferResult};
use crate::mir::interpret::ConstValue;
use rustc_index::vec::Idx;
use rustc_index::vec::IndexVec;
use std::fmt::Debug;
Expand Down Expand Up @@ -493,7 +492,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
}
}
GenericArgKind::Const(result_value) => {
if let ty::Const { val: ConstValue::Bound(debrujin, b), .. } = result_value {
if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value {
// ...in which case we would set `canonical_vars[0]` to `Some(const X)`.

// We only allow a `ty::INNERMOST` index in substitutions.
Expand Down
17 changes: 8 additions & 9 deletions src/librustc/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use super::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use super::unify_key::replace_if_possible;

use crate::hir::def_id::DefId;
use crate::mir::interpret::ConstValue;
use crate::ty::{IntType, UintType};
use crate::ty::{self, Ty, TyCtxt, InferConst};
use crate::ty::error::TypeError;
Expand Down Expand Up @@ -137,8 +136,8 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
let a_is_expected = relation.a_is_expected();

match (a.val, b.val) {
(ConstValue::Infer(InferConst::Var(a_vid)),
ConstValue::Infer(InferConst::Var(b_vid))) => {
(ty::ConstKind::Infer(InferConst::Var(a_vid)),
ty::ConstKind::Infer(InferConst::Var(b_vid))) => {
self.const_unification_table
.borrow_mut()
.unify_var_var(a_vid, b_vid)
Expand All @@ -147,16 +146,16 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
}

// All other cases of inference with other variables are errors.
(ConstValue::Infer(InferConst::Var(_)), ConstValue::Infer(_)) |
(ConstValue::Infer(_), ConstValue::Infer(InferConst::Var(_))) => {
bug!("tried to combine ConstValue::Infer/ConstValue::Infer(InferConst::Var)")
(ty::ConstKind::Infer(InferConst::Var(_)), ty::ConstKind::Infer(_)) |
(ty::ConstKind::Infer(_), ty::ConstKind::Infer(InferConst::Var(_))) => {
bug!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var)")
}

(ConstValue::Infer(InferConst::Var(vid)), _) => {
(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
return self.unify_const_variable(a_is_expected, vid, b);
}

(_, ConstValue::Infer(InferConst::Var(vid))) => {
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
return self.unify_const_variable(!a_is_expected, vid, a);
}

Expand Down Expand Up @@ -603,7 +602,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==

match c.val {
ConstValue::Infer(InferConst::Var(vid)) => {
ty::ConstKind::Infer(InferConst::Var(vid)) => {
let mut variable_table = self.infcx.const_unification_table.borrow_mut();
let var_value = variable_table.probe_value(vid);
match var_value.val {
Expand Down
17 changes: 7 additions & 10 deletions src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
//! inferencer knows "so far".

use crate::mir::interpret::ConstValue;
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
use crate::ty::fold::TypeFolder;
use crate::util::nodemap::FxHashMap;
Expand Down Expand Up @@ -227,7 +226,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {

fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
match ct.val {
ConstValue::Infer(ty::InferConst::Var(v)) => {
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
let opt_ct = self.infcx.const_unification_table
.borrow_mut()
.probe_value(v)
Expand All @@ -240,7 +239,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
ct.ty,
);
}
ConstValue::Infer(ty::InferConst::Fresh(i)) => {
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
if i >= self.const_freshen_count {
bug!(
"Encountered a freshend const with id {} \
Expand All @@ -252,16 +251,14 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
return ct;
}

ConstValue::Bound(..) |
ConstValue::Placeholder(_) => {
ty::ConstKind::Bound(..) |
ty::ConstKind::Placeholder(_) => {
bug!("unexpected const {:?}", ct)
}

ConstValue::Param(_) |
ConstValue::Scalar(_) |
ConstValue::Slice { .. } |
ConstValue::ByRef { .. } |
ConstValue::Unevaluated(..) => {}
ty::ConstKind::Param(_) |
ty::ConstKind::Value(_) |
ty::ConstKind::Unevaluated(..) => {}
}

ct.super_fold_with(self)
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/fudge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::ty::{self, Ty, TyCtxt, TyVid, IntVid, FloatVid, RegionVid, ConstVid};
use crate::ty::fold::{TypeFoldable, TypeFolder};
use crate::mir::interpret::ConstValue;

use super::InferCtxt;
use super::{RegionVariableOrigin, ConstVariableOrigin};
Expand Down Expand Up @@ -198,7 +197,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
}

fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ty::Const { val: ConstValue::Infer(ty::InferConst::Var(vid)), ty } = ct {
if let ty::Const { val: ty::ConstKind::Infer(ty::InferConst::Var(vid)), ty } = ct {
if self.const_vars.0.contains(&vid) {
// This variable was created during the fudging.
// Recreate it with a fresh variable here.
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/higher_ranked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::{HigherRankedType, InferCtxt, PlaceholderMap};
use crate::infer::CombinedSnapshot;
use crate::ty::relate::{Relate, RelateResult, TypeRelation};
use crate::ty::{self, Binder, TypeFoldable};
use crate::mir::interpret::ConstValue;

impl<'a, 'tcx> CombineFields<'a, 'tcx> {
pub fn higher_ranked_sub<T>(
Expand Down Expand Up @@ -103,7 +102,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let fld_c = |bound_var: ty::BoundVar, ty| {
self.tcx.mk_const(
ty::Const {
val: ConstValue::Placeholder(ty::PlaceholderConst {
val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
universe: next_universe,
name: bound_var,
}),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::infer::unify_key::{ConstVarValue, ConstVariableValue};
use crate::middle::free_region::RegionRelations;
use crate::middle::lang_items;
use crate::middle::region;
use crate::mir::interpret::ConstValue;
use crate::session::config::BorrowckMode;
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
use crate::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
Expand Down Expand Up @@ -1662,7 +1661,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
}

fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = ct {
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
self.infcx.const_unification_table
.borrow_mut()
.probe_value(*vid)
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
use crate::ty::subst::GenericArg;
use crate::ty::{self, Ty, TyCtxt, InferConst};
use crate::infer::{ConstVariableValue, ConstVarValue};
use crate::mir::interpret::ConstValue;
use rustc_data_structures::fx::FxHashMap;
use std::fmt::Debug;

Expand Down Expand Up @@ -626,7 +625,7 @@ where
}

match b.val {
ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
// Forbid inference variables in the RHS.
bug!("unexpected inference var {:?}", b)
}
Expand Down Expand Up @@ -999,13 +998,13 @@ where
_: &'tcx ty::Const<'tcx>,
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
match a.val {
ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
bug!(
"unexpected inference variable encountered in NLL generalization: {:?}",
a
);
}
ConstValue::Infer(InferConst::Var(vid)) => {
ty::ConstKind::Infer(InferConst::Var(vid)) => {
let mut variable_table = self.infcx.const_unification_table.borrow_mut();
let var_value = variable_table.probe_value(vid);
match var_value.val.known() {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::hir::Node;
use crate::infer::outlives::free_region_map::FreeRegionRelations;
use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin, TypeVariableOriginKind};
use crate::middle::region;
use crate::mir::interpret::ConstValue;
use crate::traits::{self, PredicateObligation};
use crate::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder, TypeVisitor};
use crate::ty::subst::{InternalSubsts, GenericArg, SubstsRef, GenericArgKind};
Expand Down Expand Up @@ -945,7 +944,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
trace!("checking const {:?}", ct);
// Find a const parameter
match ct.val {
ConstValue::Param(..) => {
ty::ConstKind::Param(..) => {
// Look it up in the substitution list.
match self.map.get(&ct.into()).map(|k| k.unpack()) {
// Found it in the substitution list, replace with the parameter from the
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/infer/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{InferCtxt, FixupError, FixupResult, Span};
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::mir::interpret::ConstValue;
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst};
use crate::ty::fold::{TypeFolder, TypeVisitor};

Expand Down Expand Up @@ -230,11 +229,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
} else {
let c = self.infcx.shallow_resolve(c);
match c.val {
ConstValue::Infer(InferConst::Var(vid)) => {
ty::ConstKind::Infer(InferConst::Var(vid)) => {
self.err = Some(FixupError::UnresolvedConst(vid));
return self.tcx().consts.err;
}
ConstValue::Infer(InferConst::Fresh(_)) => {
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
bug!("Unexpected const in full const resolver: {:?}", c);
}
_ => {}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/infer/unify_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt, InferConst};
use crate::mir::interpret::ConstValue;
use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable};
use rustc_data_structures::unify::InPlace;
use syntax_pos::{Span, DUMMY_SP};
Expand Down Expand Up @@ -180,7 +179,7 @@ pub fn replace_if_possible(
mut table: RefMut<'_, UnificationTable<InPlace<ty::ConstVid<'tcx>>>>,
c: &'tcx ty::Const<'tcx>
) -> &'tcx ty::Const<'tcx> {
if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = c {
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = c {
match table.probe_value(*vid).val.known() {
Some(c) => c,
None => c,
Expand Down
36 changes: 1 addition & 35 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::fmt;
use rustc_macros::HashStable;
use rustc_apfloat::{Float, ieee::{Double, Single}};

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

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

Expand All @@ -23,18 +20,6 @@ pub struct RawConst<'tcx> {
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
RustcEncodable, RustcDecodable, Hash, HashStable)]
pub enum ConstValue<'tcx> {
/// A const generic parameter.
Param(ParamConst),

/// Infer the value of the const.
Infer(InferConst<'tcx>),

/// Bound const variable, used only when preparing a trait query.
Bound(DebruijnIndex, BoundVar),

/// A placeholder const - universally quantified higher-ranked const.
Placeholder(PlaceholderConst),

/// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
///
/// Not using the enum `Value` to encode that this must not be `Undef`.
Expand All @@ -55,10 +40,6 @@ pub enum ConstValue<'tcx> {
/// Offset into `alloc`
offset: Size,
},

/// 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 @@ -68,26 +49,11 @@ impl<'tcx> ConstValue<'tcx> {
#[inline]
pub fn try_to_scalar(&self) -> Option<Scalar> {
match *self {
ConstValue::Param(_) |
ConstValue::Infer(_) |
ConstValue::Bound(..) |
ConstValue::Placeholder(_) |
ConstValue::ByRef { .. } |
ConstValue::Unevaluated(..) |
ConstValue::Slice { .. } => None,
ConstValue::Scalar(val) => Some(val),
}
}

#[inline]
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
self.try_to_scalar()?.to_bits(size).ok()
}

#[inline]
pub fn try_to_ptr(&self) -> Option<Pointer> {
self.try_to_scalar()?.to_ptr().ok()
}
}

/// A `Scalar` represents an immediate, primitive value existing outside of a
Expand Down
Loading

0 comments on commit 5e380b7

Please sign in to comment.