Skip to content

Commit dd5ff49

Browse files
committed
Use indexmap throughout
1 parent 1356f6d commit dd5ff49

File tree

11 files changed

+109
-134
lines changed

11 files changed

+109
-134
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use chalk_ir::{
88
};
99
use chalk_parse::ast::*;
1010
use chalk_solve::rust_ir::{self, IntoWhereClauses};
11+
use indexmap::IndexMap;
1112
use program_lowerer::ProgramLowerer;
12-
use std::collections::BTreeMap;
1313
use string_cache::DefaultAtom as Atom;
1414
use tracing::debug;
1515

@@ -564,8 +564,6 @@ impl LowerWithEnv for [QuantifiedInlineBound] {
564564
}
565565
}
566566

567-
auto_traits.sort_by_key(|b| b.1);
568-
569567
regular_traits
570568
.iter()
571569
.chain(auto_traits.iter())
@@ -1014,7 +1012,7 @@ impl LowerWithEnv for (&TraitDefn, chalk_ir::TraitId<ChalkIr>) {
10141012

10151013
pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult<chalk_ir::Goal<ChalkIr>> {
10161014
let interner = ChalkIr;
1017-
let associated_ty_lookups: BTreeMap<_, _> = program
1015+
let associated_ty_lookups: IndexMap<_, _> = program
10181016
.associated_ty_data
10191017
.iter()
10201018
.map(|(&associated_ty_id, datum)| {
@@ -1052,7 +1050,7 @@ pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult<chalk_ir
10521050
opaque_ty_kinds: &program.opaque_ty_kinds,
10531051
associated_ty_lookups: &associated_ty_lookups,
10541052
foreign_ty_ids: &program.foreign_ty_ids,
1055-
parameter_map: BTreeMap::new(),
1053+
parameter_map: IndexMap::new(),
10561054
auto_traits: &auto_traits,
10571055
};
10581056

chalk-integration/src/lowering/env.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ 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 indexmap::IndexMap;
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<Ident, chalk_ir::AdtId<ChalkIr>>;
16+
pub type FnDefIds = IndexMap<Ident, chalk_ir::FnDefId<ChalkIr>>;
17+
pub type ClosureIds = IndexMap<Ident, chalk_ir::ClosureId<ChalkIr>>;
18+
pub type TraitIds = IndexMap<Ident, chalk_ir::TraitId<ChalkIr>>;
19+
pub type GeneratorIds = IndexMap<Ident, chalk_ir::GeneratorId<ChalkIr>>;
20+
pub type OpaqueTyIds = IndexMap<Ident, chalk_ir::OpaqueTyId<ChalkIr>>;
21+
pub type AdtKinds = IndexMap<chalk_ir::AdtId<ChalkIr>, TypeKind>;
22+
pub type FnDefKinds = IndexMap<chalk_ir::FnDefId<ChalkIr>, TypeKind>;
23+
pub type ClosureKinds = IndexMap<chalk_ir::ClosureId<ChalkIr>, TypeKind>;
24+
pub type TraitKinds = IndexMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
25+
pub type AutoTraits = IndexMap<chalk_ir::TraitId<ChalkIr>, bool>;
26+
pub type OpaqueTyVariableKinds = IndexMap<chalk_ir::OpaqueTyId<ChalkIr>, TypeKind>;
27+
pub type GeneratorKinds = IndexMap<chalk_ir::GeneratorId<ChalkIr>, TypeKind>;
28+
pub type AssociatedTyLookups = IndexMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
2929
pub type AssociatedTyValueIds =
30-
BTreeMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
31-
pub type ForeignIds = BTreeMap<Ident, chalk_ir::ForeignDefId<ChalkIr>>;
30+
IndexMap<(chalk_ir::ImplId<ChalkIr>, Ident), AssociatedTyValueId<ChalkIr>>;
31+
pub type ForeignIds = IndexMap<Ident, chalk_ir::ForeignDefId<ChalkIr>>;
3232

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

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

chalk-integration/src/lowering/program_lowerer.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use chalk_solve::rust_ir::{
88
self, Anonymize, AssociatedTyValueId, GeneratorDatum, GeneratorInputOutputDatum,
99
GeneratorWitnessDatum, GeneratorWitnessExistential, OpaqueTyDatum, OpaqueTyDatumBound,
1010
};
11+
use indexmap::IndexMap;
1112
use rust_ir::IntoWhereClauses;
12-
use std::collections::{BTreeMap, HashSet};
13+
use std::collections::HashSet;
1314
use std::sync::Arc;
1415
use string_cache::DefaultAtom as Atom;
1516

@@ -141,23 +142,23 @@ impl ProgramLowerer {
141142
}
142143

143144
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();
145+
let mut adt_data = IndexMap::new();
146+
let mut adt_reprs = IndexMap::new();
147+
let mut adt_variances = IndexMap::new();
148+
let mut fn_def_data = IndexMap::new();
149+
let mut fn_def_variances = IndexMap::new();
150+
let mut closure_inputs_and_output = IndexMap::new();
151+
let mut closure_closure_kind = IndexMap::new();
152+
let mut closure_upvars = IndexMap::new();
153+
let mut trait_data = IndexMap::new();
154+
let mut well_known_traits = IndexMap::new();
155+
let mut impl_data = IndexMap::new();
156+
let mut associated_ty_data = IndexMap::new();
157+
let mut associated_ty_values = IndexMap::new();
158+
let mut opaque_ty_data = IndexMap::new();
159+
let mut generator_data = IndexMap::new();
160+
let mut generator_witness_data = IndexMap::new();
161+
let mut hidden_opaque_types = IndexMap::new();
161162
let mut custom_clauses = Vec::new();
162163

163164
for (item, &raw_id) in program.items.iter().zip(raw_ids) {
@@ -175,7 +176,7 @@ impl ProgramLowerer {
175176
generator_ids: &self.generator_ids,
176177
generator_kinds: &self.generator_kinds,
177178
associated_ty_lookups: &self.associated_ty_lookups,
178-
parameter_map: BTreeMap::new(),
179+
parameter_map: IndexMap::new(),
179180
auto_traits: &self.auto_traits,
180181
foreign_ty_ids: &self.foreign_ty_ids,
181182
};

chalk-integration/src/program.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,87 +15,88 @@ 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 indexmap::IndexMap;
19+
use std::collections::HashSet;
1920
use std::fmt;
2021
use std::sync::Arc;
2122

2223
#[derive(Clone, Debug, PartialEq, Eq)]
2324
pub struct Program {
2425
/// From ADT name to item-id. Used during lowering only.
25-
pub adt_ids: BTreeMap<Identifier, AdtId<ChalkIr>>,
26+
pub adt_ids: IndexMap<Identifier, AdtId<ChalkIr>>,
2627

2728
/// For each ADT:
28-
pub adt_kinds: BTreeMap<AdtId<ChalkIr>, TypeKind>,
29+
pub adt_kinds: IndexMap<AdtId<ChalkIr>, TypeKind>,
2930

30-
pub adt_variances: BTreeMap<AdtId<ChalkIr>, Vec<Variance>>,
31+
pub adt_variances: IndexMap<AdtId<ChalkIr>, Vec<Variance>>,
3132

32-
pub fn_def_ids: BTreeMap<Identifier, FnDefId<ChalkIr>>,
33+
pub fn_def_ids: IndexMap<Identifier, FnDefId<ChalkIr>>,
3334

34-
pub fn_def_kinds: BTreeMap<FnDefId<ChalkIr>, TypeKind>,
35+
pub fn_def_kinds: IndexMap<FnDefId<ChalkIr>, TypeKind>,
3536

36-
pub fn_def_variances: BTreeMap<FnDefId<ChalkIr>, Vec<Variance>>,
37+
pub fn_def_variances: IndexMap<FnDefId<ChalkIr>, Vec<Variance>>,
3738

38-
pub closure_ids: BTreeMap<Identifier, ClosureId<ChalkIr>>,
39+
pub closure_ids: IndexMap<Identifier, ClosureId<ChalkIr>>,
3940

40-
pub closure_upvars: BTreeMap<ClosureId<ChalkIr>, Binders<Ty<ChalkIr>>>,
41+
pub closure_upvars: IndexMap<ClosureId<ChalkIr>, Binders<Ty<ChalkIr>>>,
4142

42-
pub closure_kinds: BTreeMap<ClosureId<ChalkIr>, TypeKind>,
43+
pub closure_kinds: IndexMap<ClosureId<ChalkIr>, TypeKind>,
4344

4445
/// For each generator
45-
pub generator_ids: BTreeMap<Identifier, GeneratorId<ChalkIr>>,
46+
pub generator_ids: IndexMap<Identifier, GeneratorId<ChalkIr>>,
4647

47-
pub generator_kinds: BTreeMap<GeneratorId<ChalkIr>, TypeKind>,
48+
pub generator_kinds: IndexMap<GeneratorId<ChalkIr>, TypeKind>,
4849

49-
pub generator_data: BTreeMap<GeneratorId<ChalkIr>, Arc<GeneratorDatum<ChalkIr>>>,
50+
pub generator_data: IndexMap<GeneratorId<ChalkIr>, Arc<GeneratorDatum<ChalkIr>>>,
5051

51-
pub generator_witness_data: BTreeMap<GeneratorId<ChalkIr>, Arc<GeneratorWitnessDatum<ChalkIr>>>,
52+
pub generator_witness_data: 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<Identifier, TraitId<ChalkIr>>,
5556

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

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

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

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

6667
pub closure_inputs_and_output:
67-
BTreeMap<ClosureId<ChalkIr>, Binders<FnDefInputsAndOutputDatum<ChalkIr>>>,
68+
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<ClosureId<ChalkIr>, ClosureKind>,
7172

7273
/// For each impl:
73-
pub impl_data: BTreeMap<ImplId<ChalkIr>, Arc<ImplDatum<ChalkIr>>>,
74+
pub impl_data: 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<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<Identifier, OpaqueTyId<ChalkIr>>,
8182

8283
/// For each opaque type:
83-
pub opaque_ty_kinds: BTreeMap<OpaqueTyId<ChalkIr>, TypeKind>,
84+
pub opaque_ty_kinds: 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<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<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<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<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: IndexMap<AssocTypeId<ChalkIr>, Arc<AssociatedTyDatum<ChalkIr>>>,
99100

100101
/// For each user-specified clause
101102
pub custom_clauses: Vec<ProgramClause<ChalkIr>>,
@@ -104,7 +105,7 @@ pub struct Program {
104105
pub object_safe_traits: HashSet<TraitId<ChalkIr>>,
105106

106107
/// For each foreign type `extern { type A; }`
107-
pub foreign_ty_ids: BTreeMap<Identifier, ForeignDefId<ChalkIr>>,
108+
pub foreign_ty_ids: IndexMap<Identifier, ForeignDefId<ChalkIr>>,
108109
}
109110

110111
impl Program {

chalk-integration/src/query.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use chalk_solve::coherence::{CoherenceSolver, SpecializationPriorities};
1616
use chalk_solve::wf;
1717
use chalk_solve::RustIrDatabase;
1818
use chalk_solve::Solver;
19+
use indexmap::IndexMap;
1920
use salsa::Database;
2021
use std::clone::Clone;
2122
use std::cmp::{Eq, PartialEq};
22-
use std::collections::BTreeMap;
2323
use std::ops::{Deref, DerefMut};
2424
use std::sync::Arc;
2525
use std::sync::Mutex;
@@ -40,7 +40,7 @@ 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<IndexMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError>;
4444

4545
fn orphan_check(&self) -> Result<(), ChalkError>;
4646

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

143143
fn coherence(
144144
db: &dyn LoweringDatabase,
145-
) -> Result<BTreeMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError> {
145+
) -> Result<IndexMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError> {
146146
let program = db.program_ir()?;
147147
let solver_choice = db.solver_choice();
148148
let priorities_map = tls::set_current_program(&program, || -> Result<_, ChalkError> {
149149
let solver_builder = || solver_choice.into_solver();
150-
let priorities_map: Result<BTreeMap<_, _>, ChalkError> = program
150+
let priorities_map: Result<IndexMap<_, _>, ChalkError> = program
151151
.trait_data
152152
.keys()
153153
.map(|&trait_id| {

chalk-solve/src/coherence.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use petgraph::prelude::*;
22

3+
use indexmap::IndexMap;
4+
35
use crate::solve::Solver;
46
use crate::RustIrDatabase;
57
use chalk_ir::interner::Interner;
68
use chalk_ir::{self, ImplId, TraitId};
7-
use std::collections::BTreeMap;
89
use std::fmt;
910
use std::sync::Arc;
1011

@@ -42,13 +43,13 @@ impl<I: Interner> std::error::Error for CoherenceError<I> {}
4243
/// This basically encodes which impls specialize one another.
4344
#[derive(Clone, Debug, Default, PartialEq, Eq)]
4445
pub struct SpecializationPriorities<I: Interner> {
45-
map: BTreeMap<ImplId<I>, SpecializationPriority>,
46+
map: IndexMap<ImplId<I>, SpecializationPriority>,
4647
}
4748

4849
impl<I: Interner> SpecializationPriorities<I> {
4950
pub fn new() -> Self {
5051
Self {
51-
map: BTreeMap::new(),
52+
map: IndexMap::new(),
5253
}
5354
}
5455

@@ -108,16 +109,19 @@ where
108109
fn build_specialization_forest(&self) -> Result<Graph<ImplId<I>, ()>, CoherenceError<I>> {
109110
// The forest is returned as a graph but built as a GraphMap; this is
110111
// so that we never add multiple nodes with the same ItemId.
111-
let mut forest = DiGraphMap::new();
112+
let mut forest = DiGraph::new();
112113

113114
// Find all specializations (implemented in coherence/solve)
114115
// Record them in the forest by adding an edge from the less special
115116
// to the more special.
116117
self.visit_specializations_of_trait(|less_special, more_special| {
117-
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, ());
118122
})?;
119123

120-
Ok(forest.into_graph())
124+
Ok(forest)
121125
}
122126

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

0 commit comments

Comments
 (0)