Skip to content

Commit 20da7b0

Browse files
committed
Use indexmap throughout
use `DiGraph`specialization forest
1 parent e0d7768 commit 20da7b0

File tree

8 files changed

+89
-80
lines changed

8 files changed

+89
-80
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chalk-integration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ chalk-solve = { version = "0.76.0-dev.0", path = "../chalk-solve" }
2020
chalk-recursive = { version = "0.76.0-dev.0", path = "../chalk-recursive" }
2121
chalk-engine = { version = "0.76.0-dev.0", path = "../chalk-engine" }
2222
chalk-parse = { version = "0.76.0-dev.0", path = "../chalk-parse" }
23+
indexmap = "1.7.0"

chalk-integration/src/lowering.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use chalk_ir::{
99
use chalk_parse::ast::*;
1010
use chalk_solve::rust_ir::{self, IntoWhereClauses};
1111
use program_lowerer::ProgramLowerer;
12-
use std::collections::BTreeMap;
1312
use string_cache::DefaultAtom as Atom;
1413
use tracing::debug;
1514

@@ -564,8 +563,6 @@ impl LowerWithEnv for [QuantifiedInlineBound] {
564563
}
565564
}
566565

567-
auto_traits.sort_by_key(|b| b.1);
568-
569566
regular_traits
570567
.iter()
571568
.chain(auto_traits.iter())
@@ -1014,7 +1011,7 @@ impl LowerWithEnv for (&TraitDefn, chalk_ir::TraitId<ChalkIr>) {
10141011

10151012
pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult<chalk_ir::Goal<ChalkIr>> {
10161013
let interner = ChalkIr;
1017-
let associated_ty_lookups: BTreeMap<_, _> = program
1014+
let associated_ty_lookups: indexmap::IndexMap<_, _> = program
10181015
.associated_ty_data
10191016
.iter()
10201017
.map(|(&associated_ty_id, datum)| {
@@ -1052,7 +1049,7 @@ pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult<chalk_ir
10521049
opaque_ty_kinds: &program.opaque_ty_kinds,
10531050
associated_ty_lookups: &associated_ty_lookups,
10541051
foreign_ty_ids: &program.foreign_ty_ids,
1055-
parameter_map: BTreeMap::new(),
1052+
parameter_map: indexmap::IndexMap::new(),
10561053
auto_traits: &auto_traits,
10571054
};
10581055

chalk-integration/src/lowering/env.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,32 @@ use chalk_ir::{
66
use chalk_ir::{cast::Cast, ForeignDefId, WithKind};
77
use chalk_parse::ast::*;
88
use chalk_solve::rust_ir::AssociatedTyValueId;
9-
use std::collections::BTreeMap;
9+
// use std::collections::BTreeMap;
1010

1111
use crate::error::RustIrError;
1212
use crate::interner::ChalkIr;
1313
use crate::{Identifier as Ident, TypeKind};
1414

15-
pub type AdtIds = BTreeMap<Ident, chalk_ir::AdtId<ChalkIr>>;
16-
pub type FnDefIds = BTreeMap<Ident, chalk_ir::FnDefId<ChalkIr>>;
17-
pub type ClosureIds = BTreeMap<Ident, chalk_ir::ClosureId<ChalkIr>>;
18-
pub type TraitIds = BTreeMap<Ident, chalk_ir::TraitId<ChalkIr>>;
19-
pub type GeneratorIds = BTreeMap<Ident, chalk_ir::GeneratorId<ChalkIr>>;
20-
pub type OpaqueTyIds = BTreeMap<Ident, chalk_ir::OpaqueTyId<ChalkIr>>;
21-
pub type AdtKinds = BTreeMap<chalk_ir::AdtId<ChalkIr>, TypeKind>;
22-
pub type FnDefKinds = BTreeMap<chalk_ir::FnDefId<ChalkIr>, TypeKind>;
23-
pub type ClosureKinds = BTreeMap<chalk_ir::ClosureId<ChalkIr>, TypeKind>;
24-
pub type TraitKinds = BTreeMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
25-
pub type AutoTraits = BTreeMap<chalk_ir::TraitId<ChalkIr>, bool>;
26-
pub type OpaqueTyVariableKinds = BTreeMap<chalk_ir::OpaqueTyId<ChalkIr>, TypeKind>;
27-
pub type GeneratorKinds = BTreeMap<chalk_ir::GeneratorId<ChalkIr>, TypeKind>;
28-
pub type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
15+
pub type AdtIds = indexmap::IndexMap<Ident, chalk_ir::AdtId<ChalkIr>>;
16+
pub type FnDefIds = indexmap::IndexMap<Ident, chalk_ir::FnDefId<ChalkIr>>;
17+
pub type ClosureIds = indexmap::IndexMap<Ident, chalk_ir::ClosureId<ChalkIr>>;
18+
pub type TraitIds = indexmap::IndexMap<Ident, chalk_ir::TraitId<ChalkIr>>;
19+
pub type GeneratorIds = indexmap::IndexMap<Ident, chalk_ir::GeneratorId<ChalkIr>>;
20+
pub type OpaqueTyIds = indexmap::IndexMap<Ident, chalk_ir::OpaqueTyId<ChalkIr>>;
21+
pub type AdtKinds = indexmap::IndexMap<chalk_ir::AdtId<ChalkIr>, TypeKind>;
22+
pub type FnDefKinds = indexmap::IndexMap<chalk_ir::FnDefId<ChalkIr>, TypeKind>;
23+
pub type ClosureKinds = indexmap::IndexMap<chalk_ir::ClosureId<ChalkIr>, TypeKind>;
24+
pub type TraitKinds = indexmap::IndexMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
25+
pub type AutoTraits = indexmap::IndexMap<chalk_ir::TraitId<ChalkIr>, bool>;
26+
pub type OpaqueTyVariableKinds = indexmap::IndexMap<chalk_ir::OpaqueTyId<ChalkIr>, TypeKind>;
27+
pub type GeneratorKinds = indexmap::IndexMap<chalk_ir::GeneratorId<ChalkIr>, TypeKind>;
28+
pub type AssociatedTyLookups =
29+
indexmap::IndexMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
2930
pub type AssociatedTyValueIds =
30-
BTreeMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
31-
pub type ForeignIds = BTreeMap<Ident, chalk_ir::ForeignDefId<ChalkIr>>;
31+
indexmap::IndexMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
32+
pub type ForeignIds = indexmap::IndexMap<Ident, chalk_ir::ForeignDefId<ChalkIr>>;
3233

33-
pub type ParameterMap = BTreeMap<Ident, chalk_ir::WithKind<ChalkIr, BoundVar>>;
34+
pub type ParameterMap = indexmap::IndexMap<Ident, chalk_ir::WithKind<ChalkIr, BoundVar>>;
3435

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

chalk-integration/src/lowering/program_lowerer.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chalk_solve::rust_ir::{
99
GeneratorWitnessDatum, GeneratorWitnessExistential, OpaqueTyDatum, OpaqueTyDatumBound,
1010
};
1111
use rust_ir::IntoWhereClauses;
12-
use std::collections::{BTreeMap, HashSet};
12+
use std::collections::HashSet;
1313
use std::sync::Arc;
1414
use string_cache::DefaultAtom as Atom;
1515

@@ -141,23 +141,23 @@ impl ProgramLowerer {
141141
}
142142

143143
pub fn lower(self, program: &Program, raw_ids: &Vec<RawId>) -> LowerResult<LoweredProgram> {
144-
let mut adt_data = BTreeMap::new();
145-
let mut adt_reprs = BTreeMap::new();
146-
let mut adt_variances = BTreeMap::new();
147-
let mut fn_def_data = BTreeMap::new();
148-
let mut fn_def_variances = BTreeMap::new();
149-
let mut closure_inputs_and_output = BTreeMap::new();
150-
let mut closure_closure_kind = BTreeMap::new();
151-
let mut closure_upvars = BTreeMap::new();
152-
let mut trait_data = BTreeMap::new();
153-
let mut well_known_traits = BTreeMap::new();
154-
let mut impl_data = BTreeMap::new();
155-
let mut associated_ty_data = BTreeMap::new();
156-
let mut associated_ty_values = BTreeMap::new();
157-
let mut opaque_ty_data = BTreeMap::new();
158-
let mut generator_data = BTreeMap::new();
159-
let mut generator_witness_data = BTreeMap::new();
160-
let mut hidden_opaque_types = BTreeMap::new();
144+
let mut adt_data = indexmap::IndexMap::new();
145+
let mut adt_reprs = indexmap::IndexMap::new();
146+
let mut adt_variances = indexmap::IndexMap::new();
147+
let mut fn_def_data = indexmap::IndexMap::new();
148+
let mut fn_def_variances = indexmap::IndexMap::new();
149+
let mut closure_inputs_and_output = indexmap::IndexMap::new();
150+
let mut closure_closure_kind = indexmap::IndexMap::new();
151+
let mut closure_upvars = indexmap::IndexMap::new();
152+
let mut trait_data = indexmap::IndexMap::new();
153+
let mut well_known_traits = indexmap::IndexMap::new();
154+
let mut impl_data = indexmap::IndexMap::new();
155+
let mut associated_ty_data = indexmap::IndexMap::new();
156+
let mut associated_ty_values = indexmap::IndexMap::new();
157+
let mut opaque_ty_data = indexmap::IndexMap::new();
158+
let mut generator_data = indexmap::IndexMap::new();
159+
let mut generator_witness_data = indexmap::IndexMap::new();
160+
let mut hidden_opaque_types = indexmap::IndexMap::new();
161161
let mut custom_clauses = Vec::new();
162162

163163
for (item, &raw_id) in program.items.iter().zip(raw_ids) {
@@ -175,7 +175,7 @@ impl ProgramLowerer {
175175
generator_ids: &self.generator_ids,
176176
generator_kinds: &self.generator_kinds,
177177
associated_ty_lookups: &self.associated_ty_lookups,
178-
parameter_map: BTreeMap::new(),
178+
parameter_map: indexmap::IndexMap::new(),
179179
auto_traits: &self.auto_traits,
180180
foreign_ty_ids: &self.foreign_ty_ids,
181181
};

chalk-integration/src/program.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,87 +15,89 @@ use chalk_solve::rust_ir::{
1515
};
1616
use chalk_solve::split::Split;
1717
use chalk_solve::RustIrDatabase;
18-
use std::collections::{BTreeMap, HashSet};
18+
use std::collections::HashSet;
1919
use std::fmt;
2020
use std::sync::Arc;
2121

2222
#[derive(Clone, Debug, PartialEq, Eq)]
2323
pub struct Program {
2424
/// From ADT name to item-id. Used during lowering only.
25-
pub adt_ids: BTreeMap<Identifier, AdtId<ChalkIr>>,
25+
pub adt_ids: indexmap::IndexMap<Identifier, AdtId<ChalkIr>>,
2626

2727
/// For each ADT:
28-
pub adt_kinds: BTreeMap<AdtId<ChalkIr>, TypeKind>,
28+
pub adt_kinds: indexmap::IndexMap<AdtId<ChalkIr>, TypeKind>,
2929

30-
pub adt_variances: BTreeMap<AdtId<ChalkIr>, Vec<Variance>>,
30+
pub adt_variances: indexmap::IndexMap<AdtId<ChalkIr>, Vec<Variance>>,
3131

32-
pub fn_def_ids: BTreeMap<Identifier, FnDefId<ChalkIr>>,
32+
pub fn_def_ids: indexmap::IndexMap<Identifier, FnDefId<ChalkIr>>,
3333

34-
pub fn_def_kinds: BTreeMap<FnDefId<ChalkIr>, TypeKind>,
34+
pub fn_def_kinds: indexmap::IndexMap<FnDefId<ChalkIr>, TypeKind>,
3535

36-
pub fn_def_variances: BTreeMap<FnDefId<ChalkIr>, Vec<Variance>>,
36+
pub fn_def_variances: indexmap::IndexMap<FnDefId<ChalkIr>, Vec<Variance>>,
3737

38-
pub closure_ids: BTreeMap<Identifier, ClosureId<ChalkIr>>,
38+
pub closure_ids: indexmap::IndexMap<Identifier, ClosureId<ChalkIr>>,
3939

40-
pub closure_upvars: BTreeMap<ClosureId<ChalkIr>, Binders<Ty<ChalkIr>>>,
40+
pub closure_upvars: indexmap::IndexMap<ClosureId<ChalkIr>, Binders<Ty<ChalkIr>>>,
4141

42-
pub closure_kinds: BTreeMap<ClosureId<ChalkIr>, TypeKind>,
42+
pub closure_kinds: indexmap::IndexMap<ClosureId<ChalkIr>, TypeKind>,
4343

4444
/// For each generator
45-
pub generator_ids: BTreeMap<Identifier, GeneratorId<ChalkIr>>,
45+
pub generator_ids: indexmap::IndexMap<Identifier, GeneratorId<ChalkIr>>,
4646

47-
pub generator_kinds: BTreeMap<GeneratorId<ChalkIr>, TypeKind>,
47+
pub generator_kinds: indexmap::IndexMap<GeneratorId<ChalkIr>, TypeKind>,
4848

49-
pub generator_data: BTreeMap<GeneratorId<ChalkIr>, Arc<GeneratorDatum<ChalkIr>>>,
49+
pub generator_data: indexmap::IndexMap<GeneratorId<ChalkIr>, Arc<GeneratorDatum<ChalkIr>>>,
5050

51-
pub generator_witness_data: BTreeMap<GeneratorId<ChalkIr>, Arc<GeneratorWitnessDatum<ChalkIr>>>,
51+
pub generator_witness_data:
52+
indexmap::IndexMap<GeneratorId<ChalkIr>, Arc<GeneratorWitnessDatum<ChalkIr>>>,
5253

5354
/// From trait name to item-id. Used during lowering only.
54-
pub trait_ids: BTreeMap<Identifier, TraitId<ChalkIr>>,
55+
pub trait_ids: indexmap::IndexMap<Identifier, TraitId<ChalkIr>>,
5556

5657
/// For each trait:
57-
pub trait_kinds: BTreeMap<TraitId<ChalkIr>, TypeKind>,
58+
pub trait_kinds: indexmap::IndexMap<TraitId<ChalkIr>, TypeKind>,
5859

5960
/// For each ADT:
60-
pub adt_data: BTreeMap<AdtId<ChalkIr>, Arc<AdtDatum<ChalkIr>>>,
61+
pub adt_data: indexmap::IndexMap<AdtId<ChalkIr>, Arc<AdtDatum<ChalkIr>>>,
6162

62-
pub adt_reprs: BTreeMap<AdtId<ChalkIr>, Arc<AdtRepr<ChalkIr>>>,
63+
pub adt_reprs: indexmap::IndexMap<AdtId<ChalkIr>, Arc<AdtRepr<ChalkIr>>>,
6364

64-
pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,
65+
pub fn_def_data: indexmap::IndexMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,
6566

6667
pub closure_inputs_and_output:
67-
BTreeMap<ClosureId<ChalkIr>, Binders<FnDefInputsAndOutputDatum<ChalkIr>>>,
68+
indexmap::IndexMap<ClosureId<ChalkIr>, Binders<FnDefInputsAndOutputDatum<ChalkIr>>>,
6869

6970
// Weird name, but otherwise would overlap with `closure_kinds` above.
70-
pub closure_closure_kind: BTreeMap<ClosureId<ChalkIr>, ClosureKind>,
71+
pub closure_closure_kind: indexmap::IndexMap<ClosureId<ChalkIr>, ClosureKind>,
7172

7273
/// For each impl:
73-
pub impl_data: BTreeMap<ImplId<ChalkIr>, Arc<ImplDatum<ChalkIr>>>,
74+
pub impl_data: indexmap::IndexMap<ImplId<ChalkIr>, Arc<ImplDatum<ChalkIr>>>,
7475

7576
/// For each associated ty value `type Foo = XXX` found in an impl:
7677
pub associated_ty_values:
77-
BTreeMap<AssociatedTyValueId<ChalkIr>, Arc<AssociatedTyValue<ChalkIr>>>,
78+
indexmap::IndexMap<AssociatedTyValueId<ChalkIr>, Arc<AssociatedTyValue<ChalkIr>>>,
7879

7980
// From opaque type name to item-id. Used during lowering only.
80-
pub opaque_ty_ids: BTreeMap<Identifier, OpaqueTyId<ChalkIr>>,
81+
pub opaque_ty_ids: indexmap::IndexMap<Identifier, OpaqueTyId<ChalkIr>>,
8182

8283
/// For each opaque type:
83-
pub opaque_ty_kinds: BTreeMap<OpaqueTyId<ChalkIr>, TypeKind>,
84+
pub opaque_ty_kinds: indexmap::IndexMap<OpaqueTyId<ChalkIr>, TypeKind>,
8485

8586
/// For each opaque type:
86-
pub opaque_ty_data: BTreeMap<OpaqueTyId<ChalkIr>, Arc<OpaqueTyDatum<ChalkIr>>>,
87+
pub opaque_ty_data: indexmap::IndexMap<OpaqueTyId<ChalkIr>, Arc<OpaqueTyDatum<ChalkIr>>>,
8788

8889
/// Stores the hidden types for opaque types
89-
pub hidden_opaque_types: BTreeMap<OpaqueTyId<ChalkIr>, Arc<Ty<ChalkIr>>>,
90+
pub hidden_opaque_types: indexmap::IndexMap<OpaqueTyId<ChalkIr>, Arc<Ty<ChalkIr>>>,
9091

9192
/// For each trait:
92-
pub trait_data: BTreeMap<TraitId<ChalkIr>, Arc<TraitDatum<ChalkIr>>>,
93+
pub trait_data: indexmap::IndexMap<TraitId<ChalkIr>, Arc<TraitDatum<ChalkIr>>>,
9394

9495
/// For each trait lang item
95-
pub well_known_traits: BTreeMap<WellKnownTrait, TraitId<ChalkIr>>,
96+
pub well_known_traits: indexmap::IndexMap<WellKnownTrait, TraitId<ChalkIr>>,
9697

9798
/// For each associated ty declaration `type Foo` found in a trait:
98-
pub associated_ty_data: BTreeMap<AssocTypeId<ChalkIr>, Arc<AssociatedTyDatum<ChalkIr>>>,
99+
pub associated_ty_data:
100+
indexmap::IndexMap<AssocTypeId<ChalkIr>, Arc<AssociatedTyDatum<ChalkIr>>>,
99101

100102
/// For each user-specified clause
101103
pub custom_clauses: Vec<ProgramClause<ChalkIr>>,
@@ -104,7 +106,7 @@ pub struct Program {
104106
pub object_safe_traits: HashSet<TraitId<ChalkIr>>,
105107

106108
/// For each foreign type `extern { type A; }`
107-
pub foreign_ty_ids: BTreeMap<Identifier, ForeignDefId<ChalkIr>>,
109+
pub foreign_ty_ids: indexmap::IndexMap<Identifier, ForeignDefId<ChalkIr>>,
108110
}
109111

110112
impl Program {

chalk-integration/src/query.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use chalk_solve::Solver;
1919
use salsa::Database;
2020
use std::clone::Clone;
2121
use std::cmp::{Eq, PartialEq};
22-
use std::collections::BTreeMap;
22+
// use std::collections::BTreeMap;
2323
use std::ops::{Deref, DerefMut};
2424
use std::sync::Arc;
2525
use std::sync::Mutex;
@@ -40,7 +40,10 @@ pub trait LoweringDatabase:
4040
/// one another (the "specialization priorities").
4141
fn coherence(
4242
&self,
43-
) -> Result<BTreeMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError>;
43+
) -> Result<
44+
indexmap::IndexMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>,
45+
ChalkError,
46+
>;
4447

4548
fn orphan_check(&self) -> Result<(), ChalkError>;
4649

@@ -142,12 +145,13 @@ fn orphan_check(db: &dyn LoweringDatabase) -> Result<(), ChalkError> {
142145

143146
fn coherence(
144147
db: &dyn LoweringDatabase,
145-
) -> Result<BTreeMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError> {
148+
) -> Result<indexmap::IndexMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError>
149+
{
146150
let program = db.program_ir()?;
147151
let solver_choice = db.solver_choice();
148152
let priorities_map = tls::set_current_program(&program, || -> Result<_, ChalkError> {
149153
let solver_builder = || solver_choice.into_solver();
150-
let priorities_map: Result<BTreeMap<_, _>, ChalkError> = program
154+
let priorities_map: Result<indexmap::IndexMap<_, _>, ChalkError> = program
151155
.trait_data
152156
.keys()
153157
.map(|&trait_id| {

chalk-solve/src/coherence.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ where
109109
fn build_specialization_forest(&self) -> Result<Graph<ImplId<I>, ()>, CoherenceError<I>> {
110110
// The forest is returned as a graph but built as a GraphMap; this is
111111
// so that we never add multiple nodes with the same ItemId.
112-
let mut forest = DiGraphMap::new();
112+
let mut forest = DiGraph::new();
113113

114114
// Find all specializations (implemented in coherence/solve)
115115
// Record them in the forest by adding an edge from the less special
116116
// to the more special.
117117
self.visit_specializations_of_trait(|less_special, more_special| {
118-
forest.add_edge(less_special, more_special, ());
118+
let l = forest.add_node(less_special);
119+
let m = forest.add_node(more_special);
120+
121+
forest.add_edge(l, m, ());
119122
})?;
120123

121-
Ok(forest.into_graph())
124+
Ok(forest)
122125
}
123126

124127
// Recursively set priorities for those node and all of its children.

0 commit comments

Comments
 (0)