Skip to content

Commit

Permalink
add const types
Browse files Browse the repository at this point in the history
  • Loading branch information
basil-cow committed Apr 30, 2020
1 parent 5d463e9 commit 18487c3
Show file tree
Hide file tree
Showing 26 changed files with 428 additions and 314 deletions.
67 changes: 40 additions & 27 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type TraitKinds = BTreeMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
type AssociatedTyValueIds =
BTreeMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
type ParameterMap = BTreeMap<Ident, chalk_ir::ParameterKind<BoundVar>>;
type ParameterMap = BTreeMap<Ident, chalk_ir::ParameterKind<ChalkIr, BoundVar>>;

pub type LowerResult<T> = Result<T, RustIrError>;

Expand Down Expand Up @@ -65,7 +65,7 @@ impl<'k> Env<'k> {
#[derive(Debug, PartialEq, Eq)]
struct AssociatedTyLookup {
id: chalk_ir::AssocTypeId<ChalkIr>,
addl_parameter_kinds: Vec<chalk_ir::ParameterKind<()>>,
addl_parameter_kinds: Vec<chalk_ir::ParameterKind<ChalkIr, ()>>,
}

enum ApplyTypeLookup {
Expand All @@ -80,16 +80,16 @@ impl<'k> Env<'k> {
let interner = self.interner();

if let Some(p) = self.parameter_map.get(&name.str) {
return match *p {
chalk_ir::ParameterKind::Ty(b) => Ok(chalk_ir::TyData::BoundVar(b)
return match p {
chalk_ir::ParameterKind::Ty(b) => Ok(chalk_ir::TyData::BoundVar(*b)
.intern(interner)
.cast(interner)),
chalk_ir::ParameterKind::Lifetime(b) => Ok(chalk_ir::LifetimeData::BoundVar(b)
.intern(interner)
.cast(interner)),
chalk_ir::ParameterKind::Const(b) => Ok(chalk_ir::ConstData::BoundVar(b)
chalk_ir::ParameterKind::Lifetime(b) => Ok(chalk_ir::LifetimeData::BoundVar(*b)
.intern(interner)
.cast(interner)),
chalk_ir::ParameterKind::Const { ty, value: b } => {
Ok(b.to_const(interner, ty.clone()).cast(interner))
}
};
}

Expand Down Expand Up @@ -173,7 +173,7 @@ impl<'k> Env<'k> {
/// will be assigned in order as they are iterated.
fn introduce<I>(&self, binders: I) -> LowerResult<Self>
where
I: IntoIterator<Item = chalk_ir::ParameterKind<Ident>>,
I: IntoIterator<Item = chalk_ir::ParameterKind<ChalkIr, Ident>>,
I::IntoIter: ExactSizeIterator,
{
// As binders to introduce we recieve `ParameterKind<Ident>`,
Expand All @@ -192,7 +192,7 @@ impl<'k> Env<'k> {
let parameter_map: ParameterMap = self
.parameter_map
.iter()
.map(|(&k, &v)| (k, v.map(|b| b.shifted_in())))
.map(|(k, v)| (*k, v.map_ref(|b| b.shifted_in())))
.chain(binders)
.collect();
if parameter_map.len() != self.parameter_map.len() + len {
Expand All @@ -206,7 +206,7 @@ impl<'k> Env<'k> {

fn in_binders<I, T, OP>(&self, binders: I, op: OP) -> LowerResult<chalk_ir::Binders<T>>
where
I: IntoIterator<Item = chalk_ir::ParameterKind<Ident>>,
I: IntoIterator<Item = chalk_ir::ParameterKind<ChalkIr, Ident>>,
I::IntoIter: ExactSizeIterator,
T: HasInterner<Interner = ChalkIr>,
OP: FnOnce(&Self) -> LowerResult<T>,
Expand Down Expand Up @@ -499,9 +499,9 @@ trait LowerTypeKind {
}

trait LowerParameterMap {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>>;
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>>;
fn declared_parameters(&self) -> &[ParameterKind];
fn all_parameters(&self) -> Vec<chalk_ir::ParameterKind<Ident>> {
fn all_parameters(&self) -> Vec<chalk_ir::ParameterKind<ChalkIr, Ident>> {
self.synthetic_parameters()
.into_iter()
.chain(self.declared_parameters().iter().map(|id| id.lower()))
Expand Down Expand Up @@ -550,7 +550,7 @@ trait LowerParameterMap {
}

impl LowerParameterMap for StructDefn {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
None
}

Expand All @@ -560,7 +560,7 @@ impl LowerParameterMap for StructDefn {
}

impl LowerParameterMap for Impl {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
None
}

Expand All @@ -570,7 +570,7 @@ impl LowerParameterMap for Impl {
}

impl LowerParameterMap for AssocTyDefn {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
None
}

Expand All @@ -580,7 +580,7 @@ impl LowerParameterMap for AssocTyDefn {
}

impl LowerParameterMap for AssocTyValue {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
None
}

Expand All @@ -590,7 +590,7 @@ impl LowerParameterMap for AssocTyValue {
}

impl LowerParameterMap for TraitDefn {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
Some(chalk_ir::ParameterKind::Ty(intern(SELF)))
}

Expand All @@ -600,7 +600,7 @@ impl LowerParameterMap for TraitDefn {
}

impl LowerParameterMap for Clause {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<ChalkIr, Ident>> {
None
}

Expand All @@ -609,16 +609,28 @@ impl LowerParameterMap for Clause {
}
}

fn get_type_of_u32() -> chalk_ir::Ty<ChalkIr> {
chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::U32)),
substitution: Substitution::empty(&ChalkIr),
}
.cast(&ChalkIr)
.intern(&ChalkIr)
}

trait LowerParameterKind {
fn lower(&self) -> chalk_ir::ParameterKind<Ident>;
fn lower(&self) -> chalk_ir::ParameterKind<ChalkIr, Ident>;
}

impl LowerParameterKind for ParameterKind {
fn lower(&self) -> chalk_ir::ParameterKind<Ident> {
fn lower(&self) -> chalk_ir::ParameterKind<ChalkIr, Ident> {
match *self {
ParameterKind::Ty(ref n) => chalk_ir::ParameterKind::Ty(n.str),
ParameterKind::Lifetime(ref n) => chalk_ir::ParameterKind::Lifetime(n.str),
ParameterKind::Const(ref n) => chalk_ir::ParameterKind::Const(n.str),
ParameterKind::Const(ref n) => chalk_ir::ParameterKind::Const {
ty: get_type_of_u32(),
value: n.str,
},
}
}
}
Expand Down Expand Up @@ -1233,9 +1245,10 @@ impl LowerParameter for Parameter {
Parameter::Ty(ref t) => Ok(t.lower(env)?.cast(interner)),
Parameter::Lifetime(ref l) => Ok(l.lower(env)?.cast(interner)),
Parameter::Id(name) => env.lookup_parameter(name),
Parameter::ConstValue(value) => Ok(chalk_ir::ConstData::Concrete(
chalk_ir::ConcreteConst { interned: value },
)
Parameter::ConstValue(value) => Ok(chalk_ir::ConstData {
ty: get_type_of_u32(),
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: value }),
}
.intern(interner)
.cast(interner)),
}
Expand Down Expand Up @@ -1560,12 +1573,12 @@ impl Kinded for ParameterKind {
}
}

impl<T, L, C> Kinded for chalk_ir::ParameterKind<T, L, C> {
impl<I: chalk_ir::interner::Interner, T, L, C> Kinded for chalk_ir::ParameterKind<I, T, L, C> {
fn kind(&self) -> Kind {
match *self {
chalk_ir::ParameterKind::Ty(_) => Kind::Ty,
chalk_ir::ParameterKind::Lifetime(_) => Kind::Lifetime,
chalk_ir::ParameterKind::Const(_) => Kind::Const,
chalk_ir::ParameterKind::Const { .. } => Kind::Const,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion chalk-ir/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ impl<I: Interner> CastTo<Parameter<I>> for Lifetime<I> {

impl<I: Interner> CastTo<Parameter<I>> for Const<I> {
fn cast_to(self, interner: &I) -> Parameter<I> {
Parameter::new(interner, ParameterKind::Const(self))
let ty = self.data(interner).ty.clone();
Parameter::new(interner, ParameterKind::Const { ty, value: self })
}
}

Expand Down
23 changes: 13 additions & 10 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<'a, I: Interner> Debug for ParameterKindsInnerDebug<'a, I> {
match *binder {
ParameterKind::Ty(()) => write!(fmt, "type")?,
ParameterKind::Lifetime(()) => write!(fmt, "lifetime")?,
ParameterKind::Const(()) => write!(fmt, "const")?,
ParameterKind::Const { ref ty, value: () } => write!(fmt, "const: {:?}", ty)?,
}
}
write!(fmt, ">")
Expand All @@ -274,11 +274,11 @@ impl<'a, I: Interner> Debug for ParameterKindsInnerDebug<'a, I> {

impl<I: Interner> Debug for ConstData<I> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
match self {
ConstData::BoundVar(db) => write!(fmt, "{:?}", db),
ConstData::InferenceVar(var) => write!(fmt, "{:?}", var),
ConstData::Placeholder(index) => write!(fmt, "{:?}", index),
ConstData::Concrete(evaluated) => write!(fmt, "{:?}", evaluated),
match &self.value {
ConstValue::BoundVar(db) => write!(fmt, "{:?}", db),
ConstValue::InferenceVar(var) => write!(fmt, "{:?}", var),
ConstValue::Placeholder(index) => write!(fmt, "{:?}", index),
ConstValue::Concrete(evaluated) => write!(fmt, "{:?}", evaluated),
}
}
}
Expand Down Expand Up @@ -338,7 +338,7 @@ impl<'a, I: Interner> Debug for ParameterDataInnerDebug<'a, I> {
match self.0 {
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
ParameterKind::Const(n) => write!(fmt, "{:?}", n),
ParameterKind::Const { value: n, .. } => write!(fmt, "{:?}", n),
}
}
}
Expand Down Expand Up @@ -728,7 +728,7 @@ impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
if i > 0 {
write!(f, ",")?;
}
write!(f, "?{}", pk.into_inner())?;
write!(f, "?{}", pk.as_ref().into_inner())?;
}

write!(f, "> {{ {} }}", value)?;
Expand All @@ -738,12 +738,15 @@ impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
}
}

impl<T: Debug, L: Debug, C: Debug> Debug for ParameterKind<T, L, C> {
impl<I: Interner, T: Debug, L: Debug, C: Debug> Debug for ParameterKind<I, T, L, C> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
match *self {
ParameterKind::Ty(ref n) => write!(fmt, "Ty({:?})", n),
ParameterKind::Lifetime(ref n) => write!(fmt, "Lifetime({:?})", n),
ParameterKind::Const(ref n) => write!(fmt, "Const({:?})", n),
ParameterKind::Const {
ref ty,
value: ref n,
} => write!(fmt, "Const({:?}: {:?})", n, ty),
}
}
}
Expand Down
45 changes: 32 additions & 13 deletions chalk-ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ where

fn fold_free_var_const(
&mut self,
ty: &Ty<I>,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Fallible<Const<TI>> {
Expand All @@ -183,7 +184,11 @@ where
)
} else {
let bound_var = bound_var.shifted_in_from(outer_binder);
Ok(ConstData::<TI>::BoundVar(bound_var).intern(self.target_interner()))
Ok(ConstData {
ty: ty.fold_with(self.as_dyn(), outer_binder)?,
value: ConstValue::<TI>::BoundVar(bound_var),
}
.intern(self.target_interner()))
}
}

Expand Down Expand Up @@ -230,13 +235,17 @@ where
#[allow(unused_variables)]
fn fold_free_placeholder_const(
&mut self,
ty: &Ty<I>,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Fallible<Const<TI>> {
if self.forbid_free_placeholders() {
panic!("unexpected placeholder const `{:?}`", universe)
} else {
Ok(universe.to_const(self.target_interner()))
Ok(universe.to_const(
self.target_interner(),
ty.fold_with(self.as_dyn(), outer_binder)?,
))
}
}

Expand Down Expand Up @@ -284,13 +293,17 @@ where
#[allow(unused_variables)]
fn fold_inference_const(
&mut self,
ty: &Ty<I>,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Fallible<Const<TI>> {
if self.forbid_inference_vars() {
panic!("unexpected inference const `{:?}`", var)
} else {
Ok(var.to_const(self.target_interner()))
Ok(var.to_const(
self.target_interner(),
ty.fold_with(self.as_dyn(), outer_binder)?,
))
}
}

Expand Down Expand Up @@ -507,21 +520,27 @@ where
TI: 'i,
{
let interner = folder.interner();
match self.data(interner) {
ConstData::BoundVar(bound_var) => {
let target_interner = folder.target_interner();
let ConstData { ref ty, ref value } = self.data(interner);
let mut fold_ty = || ty.fold_with(folder, outer_binder);
match value {
ConstValue::BoundVar(bound_var) => {
if let Some(bound_var1) = bound_var.shifted_out_to(outer_binder) {
folder.fold_free_var_const(bound_var1, outer_binder)
folder.fold_free_var_const(ty, bound_var1, outer_binder)
} else {
Ok(ConstData::<TI>::BoundVar(*bound_var).intern(folder.target_interner()))
Ok(bound_var.to_const(target_interner, fold_ty()?))
}
}
ConstData::InferenceVar(var) => folder.fold_inference_const(*var, outer_binder),
ConstData::Placeholder(universe) => {
folder.fold_free_placeholder_const(*universe, outer_binder)
ConstValue::InferenceVar(var) => folder.fold_inference_const(ty, *var, outer_binder),
ConstValue::Placeholder(universe) => {
folder.fold_free_placeholder_const(ty, *universe, outer_binder)
}
ConstValue::Concrete(ev) => Ok(ConstData {
ty: fold_ty()?,
value: ConstValue::Concrete(ConcreteConst {
interned: folder.target_interner().transfer_const(&ev.interned),
}),
}
ConstData::Concrete(ev) => Ok(ConstData::Concrete(ConcreteConst {
interned: folder.target_interner().transfer_const(&ev.interned),
})
.intern(folder.target_interner())),
}
}
Expand Down
9 changes: 6 additions & 3 deletions chalk-ir/src/fold/boring_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ impl<I: Interner, TI: TargetInterner<I>> Fold<I, TI> for PhantomData<I> {
}
}

impl<I: Interner, TI: TargetInterner<I>, T, L, C> Fold<I, TI> for ParameterKind<T, L, C>
impl<I: Interner, TI: TargetInterner<I>, T, L, C> Fold<I, TI> for ParameterKind<I, T, L, C>
where
T: Fold<I, TI>,
L: Fold<I, TI>,
C: Fold<I, TI>,
{
type Result = ParameterKind<T::Result, L::Result, C::Result>;
type Result = ParameterKind<TI, T::Result, L::Result, C::Result>;

fn fold_with<'i>(
&self,
Expand All @@ -353,7 +353,10 @@ where
ParameterKind::Lifetime(a) => {
Ok(ParameterKind::Lifetime(a.fold_with(folder, outer_binder)?))
}
ParameterKind::Const(a) => Ok(ParameterKind::Const(a.fold_with(folder, outer_binder)?)),
ParameterKind::Const { ty, value } => Ok(ParameterKind::Const {
ty: ty.fold_with(folder, outer_binder)?,
value: value.fold_with(folder, outer_binder)?,
}),
}
}
}
Expand Down
Loading

0 comments on commit 18487c3

Please sign in to comment.