Skip to content

Commit

Permalink
Rollup merge of #65660 - varkor:canonical-const-to-bound-const, r=eddyb
Browse files Browse the repository at this point in the history
Rename `ConstValue::Infer(InferConst::Canonical(..))` to `ConstValue::Bound(..)`

It already has the right form, so this is just a renaming. Fixes #65655.

r? @eddyb
  • Loading branch information
Centril authored Oct 21, 2019
2 parents 760cd97 + e9c2685 commit c461d02
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/librustc/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
ConstValue::Infer(InferConst::Fresh(_)) => {
bug!("encountered a fresh const during canonicalization")
}
ConstValue::Infer(InferConst::Canonical(debruijn, _)) => {
ConstValue::Bound(debruijn, _) => {
if debruijn >= self.binder_index {
bug!("escaping bound type during canonicalization")
} else {
Expand Down Expand Up @@ -700,7 +700,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
let var = self.canonical_var(info, const_var.into());
self.tcx().mk_const(
ty::Const {
val: ConstValue::Infer(InferConst::Canonical(self.binder_index, var.into())),
val: ConstValue::Bound(self.binder_index, var.into()),
ty: self.fold_ty(const_var.ty),
}
)
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/infer/canonical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use std::ops::Index;
use syntax::source_map::Span;
use crate::ty::fold::TypeFoldable;
use crate::ty::subst::GenericArg;
use crate::ty::{self, BoundVar, InferConst, Lift, List, Region, TyCtxt};
use crate::ty::{self, BoundVar, Lift, List, Region, TyCtxt};

mod canonicalizer;

Expand Down Expand Up @@ -510,9 +510,7 @@ impl<'tcx> CanonicalVarValues<'tcx> {
GenericArgKind::Const(ct) => {
tcx.mk_const(ty::Const {
ty: ct.ty,
val: ConstValue::Infer(
InferConst::Canonical(ty::INNERMOST, ty::BoundVar::from_u32(i))
),
val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
}).into()
}
})
Expand Down
7 changes: 2 additions & 5 deletions src/librustc/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::traits::TraitEngine;
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
use crate::ty::fold::TypeFoldable;
use crate::ty::subst::{GenericArg, GenericArgKind};
use crate::ty::{self, BoundVar, InferConst, Ty, TyCtxt};
use crate::ty::{self, BoundVar, Ty, TyCtxt};
use crate::util::captures::Captures;

impl<'tcx> InferCtxtBuilder<'tcx> {
Expand Down Expand Up @@ -493,10 +493,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
}
}
GenericArgKind::Const(result_value) => {
if let ty::Const {
val: ConstValue::Infer(InferConst::Canonical(debrujin, b)),
..
} = result_value {
if let ty::Const { val: ConstValue::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
2 changes: 1 addition & 1 deletion src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
return ct;
}

ConstValue::Infer(ty::InferConst::Canonical(..)) |
ConstValue::Bound(..) |
ConstValue::Placeholder(_) => {
bug!("unexpected const {:?}", ct)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::ty::error::TypeError;
use crate::ty::fold::{TypeFoldable, TypeVisitor};
use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
use crate::ty::subst::GenericArg;
use crate::ty::{self, Ty, TyCtxt, InferConst};
use crate::ty::{self, Ty, TyCtxt};
use crate::mir::interpret::ConstValue;
use rustc_data_structures::fx::FxHashMap;
use std::fmt::Debug;
Expand Down Expand Up @@ -618,7 +618,7 @@ where
a: &'tcx ty::Const<'tcx>,
b: &'tcx ty::Const<'tcx>,
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
if let ty::Const { val: ConstValue::Infer(InferConst::Canonical(_, _)), .. } = a {
if let ty::Const { val: ConstValue::Bound(..), .. } = a {
// FIXME(const_generics): I'm unsure how this branch should actually be handled,
// so this is probably not correct.
self.infcx.super_combine_consts(self, a, b)
Expand Down Expand Up @@ -993,7 +993,7 @@ where
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
debug!("TypeGeneralizer::consts(a={:?})", a);

if let ty::Const { val: ConstValue::Infer(InferConst::Canonical(_, _)), .. } = a {
if let ty::Const { val: ConstValue::Bound(..), .. } = a {
bug!(
"unexpected inference variable encountered in NLL generalization: {:?}",
a
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 super::{InterpResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};

Expand All @@ -28,6 +29,9 @@ pub enum ConstValue<'tcx> {
/// 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),

Expand Down Expand Up @@ -66,8 +70,9 @@ impl<'tcx> ConstValue<'tcx> {
match *self {
ConstValue::Param(_) |
ConstValue::Infer(_) |
ConstValue::Bound(..) |
ConstValue::Placeholder(_) |
ConstValue::ByRef{ .. } |
ConstValue::ByRef { .. } |
ConstValue::Unevaluated(..) |
ConstValue::Slice { .. } => None,
ConstValue::Scalar(val) => Some(val),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ impl CanonicalUserType<'tcx> {
},

GenericArgKind::Const(ct) => match ct.val {
ConstValue::Infer(InferConst::Canonical(debruijn, b)) => {
ConstValue::Bound(debruijn, b) => {
// We only allow a `ty::INNERMOST` index in substitutions.
assert_eq!(debruijn, ty::INNERMOST);
cvar == b
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ impl FlagComputation {
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_CT_INFER);
match infer {
InferConst::Fresh(_) => {}
InferConst::Canonical(debruijn, _) => self.add_binder(debruijn),
InferConst::Var(_) => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX),
}
}
ConstValue::Bound(debruijn, _) => self.add_binder(debruijn),
ConstValue::Param(_) => {
self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_PARAMS);
}
Expand Down
23 changes: 11 additions & 12 deletions src/librustc/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'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::Canonical(debruijn, bound_const)),
ty,
} = *ct {
if let ty::Const { val: ConstValue::Bound(debruijn, bound_const), ty } = *ct {
if debruijn == self.current_index {
let fld_c = &mut self.fld_c;
let ct = fld_c(bound_const, ty);
Expand Down Expand Up @@ -570,7 +567,10 @@ impl<'tcx> TyCtxt<'tcx> {
// identity for bound types and consts
let fld_t = |bound_ty| self.mk_ty(ty::Bound(ty::INNERMOST, bound_ty));
let fld_c = |bound_ct, ty| {
self.mk_const_infer(ty::InferConst::Canonical(ty::INNERMOST, bound_ct), ty)
self.mk_const(ty::Const {
val: ConstValue::Bound(ty::INNERMOST, bound_ct),
ty,
})
};
self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t, fld_c)
}
Expand Down Expand Up @@ -801,10 +801,7 @@ impl TypeFolder<'tcx> for Shifter<'tcx> {
}

fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
if let ty::Const {
val: ConstValue::Infer(ty::InferConst::Canonical(debruijn, bound_const)),
ty,
} = *ct {
if let ty::Const { val: ConstValue::Bound(debruijn, bound_ct), ty } = *ct {
if self.amount == 0 || debruijn < self.current_index {
ct
} else {
Expand All @@ -815,7 +812,10 @@ impl TypeFolder<'tcx> for Shifter<'tcx> {
debruijn.shifted_out(self.amount)
}
};
self.tcx.mk_const_infer(ty::InferConst::Canonical(debruijn, bound_const), ty)
self.tcx.mk_const(ty::Const {
val: ConstValue::Bound(debruijn, bound_ct),
ty,
})
}
} else {
ct.super_fold_with(self)
Expand Down Expand Up @@ -919,8 +919,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
// const, as it has types/regions embedded in a lot of other
// places.
match ct.val {
ConstValue::Infer(ty::InferConst::Canonical(debruijn, _))
if debruijn >= self.outer_index => true,
ConstValue::Bound(debruijn, _) if debruijn >= self.outer_index => true,
_ => ct.super_visit_with(self),
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,27 +1379,23 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
match *self {
ConstValue::ByRef { alloc, offset } =>
ConstValue::ByRef { alloc, offset },
ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)),
ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)),
ConstValue::Placeholder(p) => ConstValue::Placeholder(p),
ConstValue::Scalar(a) => ConstValue::Scalar(a),
ConstValue::Slice { data, start, end } => ConstValue::Slice { data, start, end },
ConstValue::Unevaluated(did, substs)
=> ConstValue::Unevaluated(did, substs.fold_with(folder)),
ConstValue::ByRef { .. } | ConstValue::Bound(..) | ConstValue::Placeholder(..)
| ConstValue::Scalar(..) | ConstValue::Slice { .. } => *self,

}
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match *self {
ConstValue::ByRef { .. } => false,
ConstValue::Infer(ic) => ic.visit_with(visitor),
ConstValue::Param(p) => p.visit_with(visitor),
ConstValue::Placeholder(_) => false,
ConstValue::Scalar(_) => false,
ConstValue::Slice { .. } => false,
ConstValue::Unevaluated(_, substs) => substs.visit_with(visitor),
ConstValue::ByRef { .. } | ConstValue::Bound(..) | ConstValue::Placeholder(_)
| ConstValue::Scalar(_) | ConstValue::Slice { .. } => false,
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,4 @@ pub enum InferConst<'tcx> {
Var(ConstVid<'tcx>),
/// A fresh const variable. See `infer::freshen` for more details.
Fresh(u32),
/// Canonicalized const variable, used only when preparing a trait query.
Canonical(DebruijnIndex, BoundVar),
}
6 changes: 2 additions & 4 deletions src/librustc/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::hir::def_id::DefId;
use crate::infer::canonical::Canonical;
use crate::ty::{self, Lift, List, Ty, TyCtxt, InferConst, ParamConst};
use crate::ty::{self, Lift, List, Ty, TyCtxt, ParamConst};
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use crate::mir::interpret::ConstValue;
use crate::ty::sty::{ClosureSubsts, GeneratorSubsts};
Expand Down Expand Up @@ -234,9 +234,7 @@ impl<'a, 'tcx> InternalSubsts<'tcx> {

ty::GenericParamDefKind::Const => {
tcx.mk_const(ty::Const {
val: ConstValue::Infer(
InferConst::Canonical(ty::INNERMOST, ty::BoundVar::from(param.index))
),
val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)),
ty: tcx.type_of(def_id),
}).into()
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_ssa/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
ConstValue::Unevaluated(..) => bug!("unevaluated constant in `OperandRef::from_const`"),
ConstValue::Param(_) => bug!("encountered a ConstValue::Param in codegen"),
ConstValue::Infer(_) => bug!("encountered a ConstValue::Infer in codegen"),
ConstValue::Bound(..) => bug!("encountered a ConstValue::Bound in codegen"),
ConstValue::Placeholder(_) => bug!("encountered a ConstValue::Placeholder in codegen"),
ConstValue::Scalar(x) => {
let scalar = match layout.abi {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let ptr = self.tag_static_base_pointer(Pointer::new(id, offset));
Operand::Indirect(MemPlace::from_ptr(ptr, layout.align.abi))
},
ConstValue::Scalar(x) =>
Operand::Immediate(tag_scalar(x).into()),
ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(x).into()),
ConstValue::Slice { data, start, end } => {
// We rely on mutability being set correctly in `data` to prevent writes
// where none should happen.
Expand All @@ -606,6 +605,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
ConstValue::Param(..) |
ConstValue::Infer(..) |
ConstValue::Bound(..) |
ConstValue::Placeholder(..) |
ConstValue::Unevaluated(..) =>
bug!("eval_const_to_op: Unexpected ConstValue {:?}", val),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_traits/chalk_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc::traits::{
InEnvironment,
ChalkCanonicalGoal,
};
use rustc::ty::{self, TyCtxt, InferConst};
use rustc::ty::{self, TyCtxt};
use rustc::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use rustc::ty::query::Providers;
use rustc::ty::subst::{GenericArg, GenericArgKind};
Expand Down Expand Up @@ -286,7 +286,7 @@ impl context::ContextOps<ChalkArenas<'tcx>> for ChalkContext<'tcx> {
_ => false,
},
GenericArgKind::Const(ct) => match ct.val {
ConstValue::Infer(InferConst::Canonical(debruijn, bound_ct)) => {
ConstValue::Bound(debruijn, bound_ct) => {
debug_assert_eq!(debruijn, ty::INNERMOST);
cvar == bound_ct
}
Expand Down
17 changes: 4 additions & 13 deletions src/librustc_traits/chalk_context/resolvent_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc::traits::{
Environment,
InEnvironment,
};
use rustc::ty::{self, Ty, TyCtxt, InferConst};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::GenericArg;
use rustc::ty::relate::{Relate, RelateResult, TypeRelation};
use rustc::mir::interpret::ConstValue;
Expand Down Expand Up @@ -287,10 +287,7 @@ impl TypeRelation<'tcx> for AnswerSubstitutor<'cx, 'tcx> {
a: &'tcx ty::Const<'tcx>,
b: &'tcx ty::Const<'tcx>,
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
if let ty::Const {
val: ConstValue::Infer(InferConst::Canonical(debruijn, bound_ct)),
..
} = a {
if let ty::Const { val: ConstValue::Bound(debruijn, bound_ct), .. } = a {
if *debruijn == self.binder_index {
self.unify_free_answer_var(*bound_ct, b.into())?;
return Ok(b);
Expand All @@ -299,14 +296,8 @@ impl TypeRelation<'tcx> for AnswerSubstitutor<'cx, 'tcx> {

match (a, b) {
(
ty::Const {
val: ConstValue::Infer(InferConst::Canonical(a_debruijn, a_bound)),
..
},
ty::Const {
val: ConstValue::Infer(InferConst::Canonical(b_debruijn, b_bound)),
..
},
ty::Const { val: ConstValue::Bound(a_debruijn, a_bound), .. },
ty::Const { val: ConstValue::Bound(b_debruijn, b_bound), .. },
) => {
assert_eq!(a_debruijn, b_debruijn);
assert_eq!(a_bound, b_bound);
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/symbol-names/impl1.legacy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ error: def-path(bar::<impl foo::Foo>::baz)
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$_$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17h059bf53000885489E)
error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$_$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17h636bc933fc62ee2fE)
--> $DIR/impl1.rs:61:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; _] as impl1::main::{{closure}}::Bar>::method::h059bf53000885489)
error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; _] as impl1::main::{{closure}}::Bar>::method::h636bc933fc62ee2f)
--> $DIR/impl1.rs:61:13
|
LL | #[rustc_symbol_name]
Expand Down

0 comments on commit c461d02

Please sign in to comment.