From ff4d73b9f4315b4d102e1043e0a6ab5694074d3e Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Thu, 11 Mar 2021 20:15:28 +0100 Subject: [PATCH] Add binders from trait --- .../src/lowering/program_lowerer.rs | 20 +++++++++++++------ chalk-solve/src/display/items.rs | 2 +- chalk-solve/src/logging_db/id_collector.rs | 2 +- chalk-solve/src/rust_ir.rs | 17 +++++++++++----- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/chalk-integration/src/lowering/program_lowerer.rs b/chalk-integration/src/lowering/program_lowerer.rs index e5d477981cb..22a39db869d 100644 --- a/chalk-integration/src/lowering/program_lowerer.rs +++ b/chalk-integration/src/lowering/program_lowerer.rs @@ -340,6 +340,16 @@ impl ProgramLowerer { AssocItemDefn::Const(assoc_const_defn) => { let lookup = &self.associated_const_lookups [&(trait_id, assoc_const_defn.name.str.clone())]; + + let variable_kinds = trait_defn.all_parameters(); + let binders = assoc_const_defn + .value + .as_ref() + .map(|value| { + empty_env + .in_binders(variable_kinds, |env| value.lower(&env)) + }) + .transpose()?; associated_const_data.insert( lookup.id, Arc::new(rust_ir::AssociatedConstDatum { @@ -347,11 +357,7 @@ impl ProgramLowerer { id: lookup.id, name: assoc_const_defn.name.str.clone(), ty: assoc_const_defn.ty.lower(&empty_env)?, - value: assoc_const_defn - .value - .clone() - .map(|v| v.lower(&empty_env)) - .transpose()?, + binders, }), ); } @@ -409,7 +415,9 @@ impl ProgramLowerer { let lookup = &self.associated_const_lookups [&(trait_id, acv.name.str.clone())]; - let value = acv.value.lower(&empty_env)?; + let variable_kinds = impl_defn.all_parameters(); + let value = empty_env + .in_binders(variable_kinds, |env| acv.value.lower(env))?; associated_const_values.insert( acv_id, Arc::new(rust_ir::AssociatedConstValue { diff --git a/chalk-solve/src/display/items.rs b/chalk-solve/src/display/items.rs index a25f141cbb1..2967fd32375 100644 --- a/chalk-solve/src/display/items.rs +++ b/chalk-solve/src/display/items.rs @@ -485,7 +485,7 @@ impl RenderAsRust for AssociatedConstValue { write!(f, "{}const {}", s.indent(), assoc_const_data.id.display(s))?; write!(f, ": {}", assoc_const_data.ty.display(s))?; - write!(f, " = {};", self.value.display(s))?; + write!(f, " = {};", self.value.skip_binders().display(s))?; Ok(()) } } diff --git a/chalk-solve/src/logging_db/id_collector.rs b/chalk-solve/src/logging_db/id_collector.rs index f820a80f8da..dc127ceb138 100644 --- a/chalk-solve/src/logging_db/id_collector.rs +++ b/chalk-solve/src/logging_db/id_collector.rs @@ -9,7 +9,7 @@ use chalk_ir::{ use std::collections::BTreeSet; /// Collects the identifiers needed to resolve all the names for a given -/// set of identifers, excluding identifiers we already have. +/// set of identifiers, excluding identifiers we already have. /// /// When recording identifiers to print, the `LoggingRustIrDatabase` only /// records identifiers the solver uses. But the solver assumes well-formedness, diff --git a/chalk-solve/src/rust_ir.rs b/chalk-solve/src/rust_ir.rs index 4af82a110c2..b770c5a866e 100644 --- a/chalk-solve/src/rust_ir.rs +++ b/chalk-solve/src/rust_ir.rs @@ -514,7 +514,13 @@ impl Visit for AssociatedTyDatum { } } -/// Represents an associated const declaration found inside of a trait. +/// Represents an associated const declaration found inside of a trait: +/// +/// ```notrust +/// trait Foo { // P0 is Self +/// const Bar: [type] (= [const]); +/// } +/// ``` #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct AssociatedConstDatum { /// The trait this associated const is defined in. @@ -529,8 +535,9 @@ pub struct AssociatedConstDatum { /// Type of this associated const. pub ty: Ty, - /// Value of this associated const. - pub value: Option>, + /// Value of this associated const with variables `P0...Pn` + /// from the trait the assoc const is in. + pub binders: Option>>, } // Manual implementation to avoid I::Identifier type. @@ -546,7 +553,7 @@ impl Visit for AssociatedConstDatum { try_break!(self.trait_id.visit_with(visitor, outer_binder)); try_break!(self.id.visit_with(visitor, outer_binder)); try_break!(self.ty.visit_with(visitor, outer_binder)); - self.value.visit_with(visitor, outer_binder) + self.binders.visit_with(visitor, outer_binder) } } @@ -663,7 +670,7 @@ pub struct AssociatedTyValueBound { pub struct AssociatedConstValue { pub impl_id: ImplId, pub associated_const_id: AssocConstId, - pub value: Const, + pub value: Binders>, } /// Represents the bounds for an `impl Trait` type.