Skip to content

Commit 059190a

Browse files
committed
Continue adding associated consts
1 parent 7a4bd7f commit 059190a

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

chalk-integration/src/lowering.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,18 @@ impl LowerWithEnv for Lifetime {
892892
}
893893
}
894894

895-
impl LowerWithEnv for (&Impl, ImplId<ChalkIr>, &AssociatedTyValueIds) {
895+
impl LowerWithEnv
896+
for (
897+
&Impl,
898+
ImplId<ChalkIr>,
899+
&AssociatedTyValueIds,
900+
&AssociatedConstValueIds,
901+
)
902+
{
896903
type Lowered = rust_ir::ImplDatum<ChalkIr>;
897904

898905
fn lower(&self, env: &Env) -> LowerResult<Self::Lowered> {
899-
let (impl_, impl_id, associated_ty_value_ids) = self;
906+
let (impl_, impl_id, associated_ty_value_ids, associated_const_value_ids) = self;
900907

901908
let polarity = impl_.polarity.lower();
902909
let binders = env.in_binders(impl_.all_parameters(), |env| {
@@ -926,13 +933,20 @@ impl LowerWithEnv for (&Impl, ImplId<ChalkIr>, &AssociatedTyValueIds) {
926933
.map(|atv| associated_ty_value_ids[&(*impl_id, atv.name.str.clone())])
927934
.collect();
928935

936+
let associated_const_value_ids = impl_
937+
.assoc_const_values
938+
.iter()
939+
.map(|acv| associated_const_value_ids[&(*impl_id, acv.name.str.clone())])
940+
.collect();
941+
929942
debug!(?associated_ty_value_ids);
930943

931944
Ok(rust_ir::ImplDatum {
932945
polarity,
933946
binders,
934947
impl_type: impl_.impl_type.lower(),
935948
associated_ty_value_ids,
949+
associated_const_value_ids,
936950
})
937951
}
938952
}

chalk-integration/src/lowering/env.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use chalk_ir::{
55
};
66
use chalk_ir::{cast::Cast, ForeignDefId, WithKind};
77
use chalk_parse::ast::*;
8-
use chalk_solve::rust_ir::AssociatedTyValueId;
8+
use chalk_solve::rust_ir::{AssociatedConstValueId, AssociatedTyValueId};
99
use std::collections::BTreeMap;
1010

1111
use crate::error::RustIrError;
@@ -28,6 +28,8 @@ pub type GeneratorKinds = BTreeMap<chalk_ir::GeneratorId<ChalkIr>, TypeKind>;
2828
pub type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
2929
pub type AssociatedTyValueIds =
3030
BTreeMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
31+
pub type AssociatedConstValueIds =
32+
BTreeMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedConstValueId<ChalkIr>>;
3133
pub type ForeignIds = BTreeMap<Ident, chalk_ir::ForeignDefId<ChalkIr>>;
3234

3335
pub type ParameterMap = BTreeMap<Ident, chalk_ir::WithKind<ChalkIr, BoundVar>>;

chalk-integration/src/lowering/program_lowerer.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(super) struct ProgramLowerer {
2525

2626
associated_ty_lookups: AssociatedTyLookups,
2727
associated_ty_value_ids: AssociatedTyValueIds,
28+
associated_const_value_ids: AssociatedConstValueIds,
2829
adt_ids: AdtIds,
2930
fn_def_ids: FnDefIds,
3031
closure_ids: ClosureIds,
@@ -316,7 +317,13 @@ impl ProgramLowerer {
316317
Item::Impl(ref impl_defn) => {
317318
let impl_id = ImplId(raw_id);
318319
let impl_datum = Arc::new(
319-
(impl_defn, impl_id, &self.associated_ty_value_ids).lower(&empty_env)?,
320+
(
321+
impl_defn,
322+
impl_id,
323+
&self.associated_ty_value_ids,
324+
&self.associated_const_value_ids,
325+
)
326+
.lower(&empty_env)?,
320327
);
321328
impl_data.insert(impl_id, impl_datum.clone());
322329
let trait_id = impl_datum.trait_id();

chalk-parse/src/ast.rs

+8
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ pub struct Impl {
276276
pub polarity: Polarity,
277277
pub where_clauses: Vec<QuantifiedWhereClause>,
278278
pub assoc_ty_values: Vec<AssocTyValue>,
279+
pub assoc_const_values: Vec<AssocConstValue>,
279280
pub impl_type: ImplType,
280281
}
281282

@@ -293,6 +294,13 @@ pub struct AssocTyValue {
293294
pub default: bool,
294295
}
295296

297+
#[derive(Clone, PartialEq, Eq, Debug)]
298+
pub struct AssocConstValue {
299+
pub name: Identifier,
300+
pub ty: Ty,
301+
pub value: Const,
302+
}
303+
296304
#[derive(Clone, PartialEq, Eq, Debug)]
297305
pub enum Ty {
298306
Id {

chalk-parse/src/parser.lalrpop

+10-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ QuantifiedInlineBound: QuantifiedInlineBound = {
354354

355355
Impl: Impl = {
356356
<external:UpstreamKeyword?> "impl" <p:Angle<VariableKind>> <mark:"!"?> <t:Id> <a:Angle<GenericArg>> "for" <s:Ty>
357-
<w:QuantifiedWhereClauses> "{" <assoc:AssocTyValue*> "}" =>
357+
<w:QuantifiedWhereClauses> "{" <assoc:AssocTyValue*> <assoc_const:AssocConstValue*> "}" =>
358358
{
359359
let mut args = vec![GenericArg::Ty(s)];
360360
args.extend(a);
@@ -367,6 +367,7 @@ Impl: Impl = {
367367
},
368368
where_clauses: w,
369369
assoc_ty_values: assoc,
370+
assoc_const_values: assoc_const,
370371
impl_type: external.map(|_| ImplType::External).unwrap_or(ImplType::Local),
371372
}
372373
},
@@ -397,6 +398,14 @@ AssocTyValue: AssocTyValue = {
397398
},
398399
};
399400

401+
AssocConstValue: AssocConstValue = {
402+
"const" <n: Id> ":" <ty:Ty> "=" <v:Const> ";" => AssocConstValue {
403+
name:n,
404+
ty: ty,
405+
value:v,
406+
},
407+
};
408+
400409
pub Ty: Ty = {
401410
<n:Id> => Ty::Id { name: n },
402411
TyWithoutId,

chalk-solve/src/rust_ir.rs

+35
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ use std::ops::ControlFlow;
1818
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1919
pub struct AssociatedTyValueId<I: Interner>(pub I::DefId);
2020

21+
/// Identifier for an "associated const value" found in some impl.
22+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
23+
pub struct AssociatedConstValueId<I: Interner>(pub I::DefId);
24+
2125
chalk_ir::id_visit!(AssociatedTyValueId);
2226
chalk_ir::id_fold!(AssociatedTyValueId);
2327

28+
chalk_ir::id_visit!(AssociatedConstValueId);
29+
chalk_ir::id_fold!(AssociatedConstValueId);
30+
2431
#[derive(Clone, Debug, PartialEq, Eq, Hash, Visit)]
2532
pub struct ImplDatum<I: Interner> {
2633
pub polarity: Polarity,
2734
pub binders: Binders<ImplDatumBound<I>>,
2835
pub impl_type: ImplType,
2936
pub associated_ty_value_ids: Vec<AssociatedTyValueId<I>>,
37+
pub associated_const_value_ids: Vec<AssociatedConstValueId<I>>,
3038
}
3139

3240
impl<I: Interner> ImplDatum<I> {
@@ -619,6 +627,33 @@ pub struct AssociatedTyValue<I: Interner> {
619627
pub value: Binders<AssociatedTyValueBound<I>>,
620628
}
621629

630+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, Visit)]
631+
pub struct AssociatedConstValue<I: Interner> {
632+
/// Impl in which this associated type value is found. You might
633+
/// need to look at this to find the generic parameters defined on
634+
/// the impl, for example.
635+
///
636+
/// ```ignore
637+
/// impl Iterator for Foo { // <-- refers to this impl
638+
/// const Item: usize = XXX; // <-- (where this is self)
639+
/// }
640+
/// ```
641+
pub impl_id: ImplId<I>,
642+
643+
/// Associated type being defined.
644+
///
645+
/// ```ignore
646+
/// impl Iterator for Foo {
647+
/// const Item: usize = XXX; // <-- (where this is self)
648+
/// }
649+
/// ...
650+
/// trait Iterator {
651+
/// const Item: usize; // <-- refers to this declaration here!
652+
/// }
653+
/// ```
654+
pub associated_const_id: AssocItemId<I>,
655+
}
656+
622657
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, Visit, HasInterner)]
623658
pub struct AssociatedTyValueBound<I: Interner> {
624659
/// Type that we normalize to. The X in `type Foo<'a> = X`.

0 commit comments

Comments
 (0)