Skip to content

Commit 63d5dad

Browse files
authored
Merge pull request #208 from detrumi/splitting-ids
Split ItemId into several id types
2 parents 9ce7b6a + 9f592f9 commit 63d5dad

File tree

13 files changed

+268
-92
lines changed

13 files changed

+268
-92
lines changed

chalk-ir/src/debug.rs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@ use std::fmt::{Debug, Display, Error, Formatter};
22

33
use super::*;
44

5-
impl Debug for ItemId {
5+
impl Debug for RawId {
6+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
7+
fmt.debug_struct("RawId")
8+
.field("index", &self.index)
9+
.finish()
10+
}
11+
}
12+
13+
impl Debug for TypeKindId {
14+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
15+
match self {
16+
TypeKindId::TypeId(id) => write!(fmt, "{:?}", id),
17+
TypeKindId::TraitId(id) => write!(fmt, "{:?}", id),
18+
TypeKindId::StructId(id) => write!(fmt, "{:?}", id),
19+
}
20+
}
21+
}
22+
23+
impl Debug for TypeId {
24+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
25+
tls::with_current_program(|p| match p {
26+
Some(prog) => prog.debug_type_kind_id(TypeKindId::TypeId(*self), fmt),
27+
None => write!(fmt, "{:?}", self.0),
28+
})
29+
}
30+
}
31+
32+
impl Debug for TraitId {
33+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
34+
tls::with_current_program(|p| match p {
35+
Some(prog) => prog.debug_type_kind_id(TypeKindId::TraitId(*self), fmt),
36+
None => write!(fmt, "{:?}", self.0),
37+
})
38+
}
39+
}
40+
41+
impl Debug for StructId {
642
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
743
tls::with_current_program(|p| match p {
8-
Some(prog) => prog.debug_item_id(*self, fmt),
9-
None => fmt
10-
.debug_struct("ItemId")
11-
.field("index", &self.index)
12-
.finish(),
44+
Some(prog) => prog.debug_type_kind_id(TypeKindId::StructId(*self), fmt),
45+
None => write!(fmt, "{:?}", self.0),
1346
})
1447
}
1548
}
@@ -29,13 +62,25 @@ impl Debug for UniverseIndex {
2962
impl Debug for TypeName {
3063
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
3164
match self {
32-
TypeName::ItemId(id) => write!(fmt, "{:?}", id),
65+
TypeName::TypeKindId(id) => write!(fmt, "{:?}", id),
3366
TypeName::Placeholder(index) => write!(fmt, "{:?}", index),
3467
TypeName::AssociatedType(assoc_ty) => write!(fmt, "{:?}", assoc_ty),
3568
}
3669
}
3770
}
3871

72+
impl Debug for ItemId {
73+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
74+
match self {
75+
ItemId::StructId(id @ StructId(_)) => write!(fmt, "{:?}", TypeKindId::StructId(*id)),
76+
ItemId::TraitId(id @ TraitId(_)) => write!(fmt, "{:?}", TypeKindId::TraitId(*id)),
77+
ItemId::TypeId(id @ TypeId(_)) => write!(fmt, "{:?}", TypeKindId::TypeId(*id)),
78+
ItemId::ImplId(ImplId(id)) => write!(fmt, "{:?}", id),
79+
ItemId::ClauseId(ClauseId(id)) => write!(fmt, "{:?}", id),
80+
}
81+
}
82+
}
83+
3984
impl Debug for Ty {
4085
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
4186
match self {

chalk-ir/src/fold.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ pub fn super_fold_ty(folder: &mut dyn Folder, ty: &Ty, binders: usize) -> Fallib
358358
folder.fold_free_placeholder_ty(ui, binders)
359359
}
360360

361-
TypeName::ItemId(_) | TypeName::AssociatedType(_) => {
361+
TypeName::TypeKindId(_) | TypeName::AssociatedType(_) => {
362362
let parameters = parameters.fold_with(folder, binders)?;
363363
Ok(ApplicationTy { name, parameters }.cast())
364364
}
@@ -482,6 +482,10 @@ macro_rules! copy_fold {
482482
copy_fold!(Identifier);
483483
copy_fold!(UniverseIndex);
484484
copy_fold!(ItemId);
485+
copy_fold!(StructId);
486+
copy_fold!(TraitId);
487+
copy_fold!(TypeId);
488+
copy_fold!(TypeKindId);
485489
copy_fold!(usize);
486490
copy_fold!(QuantifierKind);
487491
copy_fold!(chalk_engine::TableIndex);

chalk-ir/src/lib.rs

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
macro_rules! impl_froms {
2+
($e:ident: $($v:ident), *) => {
3+
$(
4+
impl From<$v> for $e {
5+
fn from(it: $v) -> $e {
6+
$e::$v(it)
7+
}
8+
}
9+
)*
10+
}
11+
}
12+
13+
macro_rules! impl_debugs {
14+
($($id:ident), *) => {
15+
$(
16+
impl std::fmt::Debug for $id {
17+
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
18+
write!(fmt, "{:?}", self.0)
19+
}
20+
}
21+
)*
22+
};
23+
}
24+
125
use crate::cast::Cast;
226
use crate::fold::shift::Shift;
327
use crate::fold::{
@@ -33,7 +57,7 @@ pub type Identifier = InternedString;
3357
pub struct ProgramEnvironment {
3458
/// Indicates whether a given trait has coinductive semantics --
3559
/// at present, this is true only for auto traits.
36-
pub coinductive_traits: BTreeSet<ItemId>,
60+
pub coinductive_traits: BTreeSet<TraitId>,
3761

3862
/// Compiled forms of the above:
3963
pub program_clauses: Vec<ProgramClause>,
@@ -90,15 +114,15 @@ impl<G> InEnvironment<G> {
90114
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
91115
pub enum TypeName {
92116
/// a type like `Vec<T>`
93-
ItemId(ItemId),
117+
TypeKindId(TypeKindId),
94118

95119
/// instantiated form a universally quantified type, e.g., from
96120
/// `forall<T> { .. }`. Stands in as a representative of "some
97121
/// unknown type".
98122
Placeholder(PlaceholderIndex),
99123

100124
/// an associated type like `Iterator::Item`; see `AssociatedType` for details
101-
AssociatedType(ItemId),
125+
AssociatedType(TypeId),
102126
}
103127

104128
/// An universe index is how a universally quantified parameter is
@@ -132,8 +156,65 @@ impl UniverseIndex {
132156
}
133157

134158
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
135-
pub struct ItemId {
136-
pub index: usize,
159+
pub struct StructId(pub RawId);
160+
161+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
162+
pub struct TraitId(pub RawId);
163+
164+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
165+
pub struct ImplId(pub RawId);
166+
167+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
168+
pub struct ClauseId(pub RawId);
169+
170+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
171+
pub struct TypeId(pub RawId);
172+
173+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
174+
pub enum ItemId {
175+
StructId(StructId),
176+
TraitId(TraitId),
177+
ImplId(ImplId),
178+
ClauseId(ClauseId),
179+
TypeId(TypeId),
180+
}
181+
182+
impl_froms!(ItemId: StructId, TraitId, ImplId, ClauseId, TypeId);
183+
impl_debugs!(ImplId, ClauseId);
184+
185+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
186+
pub enum TypeKindId {
187+
TypeId(TypeId),
188+
TraitId(TraitId),
189+
StructId(StructId),
190+
}
191+
192+
impl TypeKindId {
193+
pub fn raw_id(&self) -> RawId {
194+
match self {
195+
TypeKindId::TypeId(id) => id.0,
196+
TypeKindId::TraitId(id) => id.0,
197+
TypeKindId::StructId(id) => id.0,
198+
}
199+
}
200+
}
201+
202+
impl_froms!(TypeKindId: TypeId, TraitId, StructId);
203+
204+
impl From<TypeKindId> for ItemId {
205+
fn from(type_kind_id: TypeKindId) -> ItemId {
206+
match type_kind_id {
207+
TypeKindId::TypeId(id) => ItemId::TypeId(id),
208+
TypeKindId::TraitId(id) => ItemId::TraitId(id),
209+
TypeKindId::StructId(id) => ItemId::StructId(id),
210+
}
211+
}
212+
}
213+
214+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
215+
#[allow(non_camel_case_types)]
216+
pub struct RawId {
217+
pub index: u32,
137218
}
138219

139220
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -411,7 +492,7 @@ impl Parameter {
411492

412493
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
413494
pub struct ProjectionTy {
414-
pub associated_ty_id: ItemId,
495+
pub associated_ty_id: TypeId,
415496
pub parameters: Vec<Parameter>,
416497
}
417498

@@ -431,7 +512,7 @@ pub type ProjectionTyRefEnum<'a> = ProjectionTyEnum<&'a ProjectionTy, &'a Unsele
431512

432513
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
433514
pub struct TraitRef {
434-
pub trait_id: ItemId,
515+
pub trait_id: TraitId,
435516
pub parameters: Vec<Parameter>,
436517
}
437518

@@ -525,7 +606,7 @@ pub enum DomainGoal {
525606
Normalize(Normalize),
526607
UnselectedNormalize(UnselectedNormalize),
527608

528-
InScope(ItemId),
609+
InScope(TypeKindId),
529610

530611
/// Whether a type can deref into another. Right now this is just:
531612
/// ```notrust

chalk-ir/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! ty {
1818

1919
(projection (item $n:tt) $($arg:tt)*) => {
2020
$crate::Ty::Projection(ProjectionTy {
21-
associated_ty_id: ItemId { index: $n },
21+
associated_ty_id: TypeId(RawId { index: $n }),
2222
parameters: vec![$(arg!($arg)),*],
2323
})
2424
};
@@ -77,7 +77,7 @@ macro_rules! lifetime {
7777
#[macro_export]
7878
macro_rules! ty_name {
7979
((item $n:expr)) => {
80-
$crate::TypeName::ItemId(ItemId { index: $n })
80+
$crate::TypeName::TypeKindId(TypeKindId::TypeId(TypeId(RawId { index: $n })))
8181
};
8282
((placeholder $n:expr)) => {
8383
$crate::TypeName::Placeholder(PlaceholderIndex {

chalk-ir/src/tls.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ItemId, ProjectionTy};
1+
use crate::{ProjectionTy, TypeKindId};
22
use std::cell::RefCell;
33
use std::fmt;
44
use std::sync::Arc;
@@ -8,7 +8,11 @@ thread_local! {
88
}
99

1010
pub trait DebugContext {
11-
fn debug_item_id(&self, item_id: ItemId, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error>;
11+
fn debug_type_kind_id(
12+
&self,
13+
id: TypeKindId,
14+
fmt: &mut fmt::Formatter,
15+
) -> Result<(), fmt::Error>;
1216

1317
fn debug_projection(
1418
&self,

chalk-ir/src/zip.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ macro_rules! eq_zip {
148148
}
149149

150150
eq_zip!(ItemId);
151+
eq_zip!(StructId);
152+
eq_zip!(TraitId);
153+
eq_zip!(TypeId);
154+
eq_zip!(TypeKindId);
151155
eq_zip!(TypeName);
152156
eq_zip!(Identifier);
153157
eq_zip!(QuantifierKind);

src/coherence.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use petgraph::prelude::*;
22

33
use crate::rust_ir::Program;
44
use chalk_ir::ProgramEnvironment;
5-
use chalk_ir::{self, Identifier, ItemId};
5+
use chalk_ir::{self, Identifier, ImplId};
66
use chalk_solve::solve::SolverChoice;
77
use failure::Fallible;
88
use std::sync::Arc;
@@ -43,7 +43,7 @@ impl Program {
4343
&self,
4444
env: Arc<ProgramEnvironment>,
4545
solver_choice: SolverChoice,
46-
) -> Fallible<Graph<ItemId, ()>> {
46+
) -> Fallible<Graph<ImplId, ()>> {
4747
// The forest is returned as a graph but built as a GraphMap; this is
4848
// so that we never add multiple nodes with the same ItemId.
4949
let mut forest = DiGraphMap::new();
@@ -59,7 +59,7 @@ impl Program {
5959
}
6060

6161
// Recursively set priorities for those node and all of its children.
62-
fn set_priorities(&mut self, idx: NodeIndex, forest: &Graph<ItemId, ()>, p: usize) {
62+
fn set_priorities(&mut self, idx: NodeIndex, forest: &Graph<ImplId, ()>, p: usize) {
6363
// Get the impl datum recorded at this node and reset its priority
6464
{
6565
let impl_id = forest

src/coherence/orphan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn perform_orphan_check(
2929
for impl_datum in local_impls {
3030
if !solver.orphan_check(impl_datum) {
3131
let trait_id = impl_datum.binders.value.trait_ref.trait_ref().trait_id;
32-
let trait_id = program.type_kinds.get(&trait_id).unwrap().name;
32+
let trait_id = program.type_kinds.get(&trait_id.into()).unwrap().name;
3333
Err(CoherenceError::FailedOrphanCheck(trait_id))?;
3434
}
3535
}

src/coherence/solve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Program {
2323
mut record_specialization: F,
2424
) -> Fallible<()>
2525
where
26-
F: FnMut(ItemId, ItemId),
26+
F: FnMut(ImplId, ImplId),
2727
{
2828
let mut solver = DisjointSolver { env, solver_choice };
2929

@@ -53,7 +53,7 @@ impl Program {
5353

5454
// Iterate over every pair of impls for the same trait.
5555
for (trait_id, impls) in &impl_groupings {
56-
let impls: Vec<(&ItemId, &ImplDatum)> = impls.collect();
56+
let impls: Vec<(&ImplId, &ImplDatum)> = impls.collect();
5757

5858
for ((&l_id, lhs), (&r_id, rhs)) in impls.into_iter().tuple_combinations() {
5959
// Two negative impls never overlap.
@@ -71,7 +71,7 @@ impl Program {
7171
(true, false) => record_specialization(l_id, r_id),
7272
(false, true) => record_specialization(r_id, l_id),
7373
(_, _) => {
74-
let trait_id = self.type_kinds.get(&trait_id).unwrap().name;
74+
let trait_id = self.type_kinds.get(&trait_id.into()).unwrap().name;
7575
Err(CoherenceError::OverlappingImpls(trait_id))?;
7676
}
7777
}

src/rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl AssociatedTyValue {
296296
}),
297297
conditions: vec![
298298
normalize_goal.cast(),
299-
DomainGoal::InScope(impl_trait_ref.trait_id).cast(),
299+
DomainGoal::InScope(impl_trait_ref.trait_id.into()).cast(),
300300
],
301301
},
302302
}

src/rules/wf.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,23 @@ fn solve_wf_requirements(
5555

5656
for (id, struct_datum) in &program.struct_data {
5757
if !solver.verify_struct_decl(struct_datum) {
58-
let name = program.type_kinds.get(id).unwrap().name;
58+
let name = program
59+
.type_kinds
60+
.get(&TypeKindId::StructId(*id))
61+
.unwrap()
62+
.name;
5963
Err(WfError::IllFormedTypeDecl(name))?;
6064
}
6165
}
6266

6367
for impl_datum in program.impl_data.values() {
6468
if !solver.verify_trait_impl(impl_datum) {
6569
let trait_ref = impl_datum.binders.value.trait_ref.trait_ref();
66-
let name = program.type_kinds.get(&trait_ref.trait_id).unwrap().name;
70+
let name = program
71+
.type_kinds
72+
.get(&trait_ref.trait_id.into())
73+
.unwrap()
74+
.name;
6775
Err(WfError::IllFormedTraitImpl(name))?;
6876
}
6977
}

0 commit comments

Comments
 (0)