Skip to content

Commit 422e8a6

Browse files
committed
track arity of associated items in their names
I could not cope with the bad printouts.
1 parent 941b0a8 commit 422e8a6

File tree

9 files changed

+52
-26
lines changed

9 files changed

+52
-26
lines changed

crates/formality-rust/src/prove.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl Crate {
204204
alias: AliasTy::associated_ty(
205205
&trait_id,
206206
item_id,
207+
assoc_vars.len(),
207208
seq![
208209
self_ty.to(),
209210
..trait_parameters.iter().cloned(),
@@ -255,6 +256,7 @@ impl Crate {
255256
let alias = AliasTy::associated_ty(
256257
trait_id,
257258
item_id,
259+
assoc_vars.len(),
258260
(&trait_vars, &assoc_vars),
259261
);
260262

crates/formality-types/src/grammar/ty.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ impl UpcastFrom<Ty> for TyData {
117117
}
118118
}
119119

120+
// ANCHOR: RigidTy_decl
120121
#[term((rigid $name $*parameters))]
121122
#[customize(parse, debug)]
122123
pub struct RigidTy {
123124
pub name: RigidName,
124125
pub parameters: Parameters,
125126
}
127+
// ANCHOR_END: RigidTy_decl
126128

127129
impl UpcastFrom<ScalarId> for RigidTy {
128130
fn upcast_from(s: ScalarId) -> Self {
@@ -198,15 +200,19 @@ impl AliasTy {
198200
pub fn associated_ty(
199201
trait_id: impl Upcast<TraitId>,
200202
item_id: impl Upcast<AssociatedItemId>,
203+
item_arity: usize,
201204
parameters: impl Upcast<Vec<Parameter>>,
202205
) -> Self {
206+
let parameters: Vec<Parameter> = parameters.upcast();
207+
assert!(item_arity <= parameters.len());
203208
AliasTy {
204209
name: AssociatedTyName {
205210
trait_id: trait_id.upcast(),
206211
item_id: item_id.upcast(),
212+
item_arity,
207213
}
208214
.upcast(),
209-
parameters: parameters.upcast(),
215+
parameters: parameters,
210216
}
211217
}
212218
}
@@ -217,10 +223,16 @@ pub enum AliasName {
217223
AssociatedTyId(AssociatedTyName),
218224
}
219225

220-
#[term(($trait_id :: $item_id))]
226+
#[term(($trait_id :: $item_id / $item_arity))]
221227
pub struct AssociatedTyName {
228+
/// The trait in which the associated type was declared.
222229
pub trait_id: TraitId,
230+
231+
/// The name of the associated type.
223232
pub item_id: AssociatedItemId,
233+
234+
/// The number of parameters on the associated type (often 0).
235+
pub item_arity: usize,
224236
}
225237

226238
#[term]

crates/formality-types/src/grammar/ty/debug_impls.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ impl Debug for RigidTy {
3333
}
3434
}
3535
_ => {
36-
write!(
37-
f,
38-
"{:?}{:?}",
39-
name,
40-
PrettyParameters::new("<", ">", parameters)
41-
)
36+
write!(f, "{:?}{:?}", name, PrettyParameters::angle(parameters))
4237
}
4338
}
4439
}
@@ -49,14 +44,23 @@ impl Debug for AliasTy {
4944
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5045
let AliasTy { name, parameters } = self;
5146
match name {
52-
AliasName::AssociatedTyId(AssociatedTyName { trait_id, item_id }) => {
47+
AliasName::AssociatedTyId(AssociatedTyName {
48+
trait_id,
49+
item_id,
50+
item_arity,
51+
}) => {
52+
let (trait_parameters, item_parameters) =
53+
parameters.split_at(parameters.len() - item_arity);
54+
let (self_parameter, other_parameters) = trait_parameters.split_at(1);
5355
// Grr, wish we would remember the number of parameters assigned to each position.
5456
write!(
5557
f,
56-
"({:?}::{:?}){:?}",
58+
"<{:?} as {:?}{:?}>::{:?}{:?}",
59+
self_parameter[0],
5760
trait_id,
61+
PrettyParameters::angle(other_parameters),
5862
item_id,
59-
PrettyParameters::new("<", ">", parameters),
63+
PrettyParameters::angle(item_parameters),
6064
)
6165
}
6266
}
@@ -72,6 +76,10 @@ impl<'a> PrettyParameters<'a> {
7276
fn new(open: &'a str, close: &'a str, p: &'a [Parameter]) -> Self {
7377
Self { open, close, p }
7478
}
79+
80+
fn angle(p: &'a [Parameter]) -> Self {
81+
Self::new("<", ">", p)
82+
}
7583
}
7684

7785
impl Debug for PrettyParameters<'_> {

crates/formality-types/src/grammar/ty/parse_impls.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ impl CoreParse<Rust> for AliasTy {
8888
p.expect_char(':')?;
8989
let item_id: AssociatedItemId = p.nonterminal()?;
9090
let item_parameters = parse_parameters(p)?;
91-
let name = AssociatedTyName { trait_id, item_id };
91+
let name = AssociatedTyName {
92+
trait_id,
93+
item_id,
94+
item_arity: item_parameters.len(),
95+
};
9296
let parameters: Vec<Parameter> = std::iter::once(ty0.upcast())
9397
.chain(trait_parameters1)
9498
.chain(item_parameters)

tests/associated_type_normalization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn test_mirror_normalizes_u32_to_u32() {
3939
},
4040
known_true: true,
4141
substitution: {
42-
?ty_1 => (Mirror::Assoc)<u32>,
42+
?ty_1 => <u32 as Mirror>::Assoc,
4343
},
4444
},
4545
},

tests/coherence_overlap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn test_overlap_normalize_alias_to_LocalType() {
5353

5454
expect_test::expect![[r#"
5555
Err(
56-
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <> LocalTrait < > for (Mirror::T)<LocalType> where [] { }",
56+
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <> LocalTrait < > for <LocalType as Mirror>::T where [] { }",
5757
)
5858
"#]]
5959
.assert_debug_eq(&test_program_ok(&gen_program(
@@ -114,7 +114,7 @@ fn test_overlap_alias_not_normalizable() {
114114

115115
expect_test::expect![[r#"
116116
Err(
117-
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <ty> LocalTrait < > for (Mirror::T)<^ty0_0> where [^ty0_0 : Mirror < >] { }",
117+
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <ty> LocalTrait < > for <^ty0_0 as Mirror>::T where [^ty0_0 : Mirror < >] { }",
118118
)
119119
"#]] // FIXME
120120
.assert_debug_eq(&test_program_ok(&gen_program(

tests/projection.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn normalize_basic() {
3131
},
3232
known_true: true,
3333
substitution: {
34-
?ty_2 => (Iterator::Item)<Vec<!ty_1>>,
34+
?ty_2 => <Vec<!ty_1> as Iterator>::Item,
3535
},
3636
},
3737
Constraints {
@@ -110,7 +110,7 @@ fn normalize_basic() {
110110
},
111111
known_true: true,
112112
substitution: {
113-
?ty_2 => (Iterator::Item)<!ty_1>,
113+
?ty_2 => <!ty_1 as Iterator>::Item,
114114
},
115115
},
116116
},
@@ -169,8 +169,8 @@ fn normalize_basic() {
169169
},
170170
known_true: true,
171171
substitution: {
172-
?ty_2 => Vec<(Iterator::Item)<!ty_1>>,
173-
?ty_3 => (Iterator::Item)<!ty_1>,
172+
?ty_2 => Vec<<!ty_1 as Iterator>::Item>,
173+
?ty_3 => <!ty_1 as Iterator>::Item,
174174
},
175175
},
176176
},
@@ -221,7 +221,7 @@ fn normalize_into_iterator() {
221221
},
222222
known_true: true,
223223
substitution: {
224-
?ty_2 => (IntoIterator::Item)<Vec<!ty_1>>,
224+
?ty_2 => <Vec<!ty_1> as IntoIterator>::Item,
225225
},
226226
},
227227
Constraints {
@@ -286,7 +286,7 @@ fn projection_equality() {
286286
},
287287
known_true: true,
288288
substitution: {
289-
?ty_1 => (Trait1::Type)<S>,
289+
?ty_1 => <S as Trait1>::Type,
290290
},
291291
},
292292
},
@@ -321,7 +321,7 @@ fn projection_equality() {
321321
},
322322
known_true: true,
323323
substitution: {
324-
?ty_1 => (Trait1::Type)<S>,
324+
?ty_1 => <S as Trait1>::Type,
325325
},
326326
},
327327
},
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Error: orphan_check(impl <> CoreTrait < > for (Unit::Assoc)<FooStruct> where [] { })
1+
Error: orphan_check(impl <> CoreTrait < > for <FooStruct as Unit>::Assoc where [] { })
22

33
Caused by:
4-
failed to prove {@ IsLocal(CoreTrait((Unit::Assoc)<FooStruct>))} given {}, got {}
4+
failed to prove {@ IsLocal(CoreTrait(<FooStruct as Unit>::Assoc))} given {}, got {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Error: orphan_check(impl <> CoreTrait < > for (Mirror::Assoc)<CoreStruct> where [] { })
1+
Error: orphan_check(impl <> CoreTrait < > for <CoreStruct as Mirror>::Assoc where [] { })
22

33
Caused by:
4-
failed to prove {@ IsLocal(CoreTrait((Mirror::Assoc)<CoreStruct>))} given {}, got {}
4+
failed to prove {@ IsLocal(CoreTrait(<CoreStruct as Mirror>::Assoc))} given {}, got {}

0 commit comments

Comments
 (0)