Skip to content

Commit

Permalink
customize debug impl for assoc ty
Browse files Browse the repository at this point in the history
could be better
  • Loading branch information
nikomatsakis committed Nov 4, 2023
1 parent 394ba0c commit c151b28
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/formality-types/src/grammar/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub enum ScalarId {
}

#[term((alias $name $*parameters))]
#[customize(parse)]
#[customize(parse, debug)]
pub struct AliasTy {
pub name: AliasName,
pub parameters: Parameters,
Expand Down
23 changes: 23 additions & 0 deletions crates/formality-types/src/grammar/ty/debug_impls.rs
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
use super::{AliasName, AliasTy, AssociatedTyName};
use std::fmt::Debug;

impl Debug for AliasTy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let AliasTy { name, parameters } = self;
match name {
AliasName::AssociatedTyId(AssociatedTyName { trait_id, item_id }) => {
// Grr, wish we would remember the number of parameters assigned to each position.
write!(
f,
"({:?}::{:?})<{}>",
trait_id,
item_id,
parameters
.iter()
.map(|p| format!("{p:?}"))
.collect::<Vec<String>>()
.join(","),
)
}
}
}
}
2 changes: 1 addition & 1 deletion tests/associated_type_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn test_mirror_normalizes_u32_to_u32() {
},
known_true: true,
substitution: {
?ty_1 => (alias (Mirror :: Assoc) (rigid (scalar u32))),
?ty_1 => (Mirror::Assoc)<(rigid (scalar u32))>,
},
},
},
Expand Down
18 changes: 10 additions & 8 deletions tests/coherence_overlap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ fn test_overlap_normalize_alias_to_LocalType() {
// ...but it's an error if LocalType implements Iterator (figuring *this* out also
// requires normalizing).

expect_test::expect![[r#"Err(
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <> LocalTrait < > for (alias (Mirror :: T) (rigid (adt LocalType))) where [] { }",
)
"#]]
expect_test::expect![[r#"
Err(
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <> LocalTrait < > for (Mirror::T)<(rigid (adt LocalType))> where [] { }",
)
"#]]
.assert_debug_eq(&test_program_ok(&gen_program(
"impl<> Iterator<> for LocalType<> where [] {}",
)));
Expand Down Expand Up @@ -111,10 +112,11 @@ fn test_overlap_alias_not_normalizable() {

// ...as long as there is at least one Iterator impl, however, we do flag an error.

expect_test::expect![[r#"Err(
"impls may overlap:\nimpl <ty> LocalTrait < > for ^ty0_0 where [^ty0_0 : Iterator < >] { }\nimpl <ty> LocalTrait < > for (alias (Mirror :: T) ^ty0_0) where [^ty0_0 : Mirror < >] { }",
)
"#]] // FIXME
expect_test::expect![[r#"
Err(
"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 < >] { }",
)
"#]] // FIXME
.assert_debug_eq(&test_program_ok(&gen_program(
"impl<> Iterator<> for u32 where[] {}",
)));
Expand Down
14 changes: 7 additions & 7 deletions tests/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn normalize_basic() {
},
known_true: true,
substitution: {
?ty_2 => (alias (Iterator :: Item) (rigid (adt Vec) !ty_1)),
?ty_2 => (Iterator::Item)<(rigid (adt Vec) !ty_1)>,
},
},
Constraints {
Expand Down Expand Up @@ -110,7 +110,7 @@ fn normalize_basic() {
},
known_true: true,
substitution: {
?ty_2 => (alias (Iterator :: Item) !ty_1),
?ty_2 => (Iterator::Item)<!ty_1>,
},
},
},
Expand Down Expand Up @@ -169,8 +169,8 @@ fn normalize_basic() {
},
known_true: true,
substitution: {
?ty_2 => (rigid (adt Vec) (alias (Iterator :: Item) !ty_1)),
?ty_3 => (alias (Iterator :: Item) !ty_1),
?ty_2 => (rigid (adt Vec) (Iterator::Item)<!ty_1>),
?ty_3 => (Iterator::Item)<!ty_1>,
},
},
},
Expand Down Expand Up @@ -221,7 +221,7 @@ fn normalize_into_iterator() {
},
known_true: true,
substitution: {
?ty_2 => (alias (IntoIterator :: Item) (rigid (adt Vec) !ty_1)),
?ty_2 => (IntoIterator::Item)<(rigid (adt Vec) !ty_1)>,
},
},
Constraints {
Expand Down Expand Up @@ -286,7 +286,7 @@ fn projection_equality() {
},
known_true: true,
substitution: {
?ty_1 => (alias (Trait1 :: Type) (rigid (adt S))),
?ty_1 => (Trait1::Type)<(rigid (adt S))>,
},
},
},
Expand Down Expand Up @@ -321,7 +321,7 @@ fn projection_equality() {
},
known_true: true,
substitution: {
?ty_1 => (alias (Trait1 :: Type) (rigid (adt S))),
?ty_1 => (Trait1::Type)<(rigid (adt S))>,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/coherence_orphan/alias_to_unit.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Error: orphan_check(impl <> CoreTrait < > for (alias (Unit :: Assoc) (rigid (adt FooStruct))) where [] { })
Error: orphan_check(impl <> CoreTrait < > for (Unit::Assoc)<(rigid (adt FooStruct))> where [] { })

Caused by:
failed to prove {@ IsLocal(CoreTrait((alias (Unit :: Assoc) (rigid (adt FooStruct)))))} given {}, got {}
failed to prove {@ IsLocal(CoreTrait((Unit::Assoc)<(rigid (adt FooStruct))>))} given {}, got {}
4 changes: 2 additions & 2 deletions tests/ui/coherence_orphan/mirror_CoreStruct.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Error: orphan_check(impl <> CoreTrait < > for (alias (Mirror :: Assoc) (rigid (adt CoreStruct))) where [] { })
Error: orphan_check(impl <> CoreTrait < > for (Mirror::Assoc)<(rigid (adt CoreStruct))> where [] { })

Caused by:
failed to prove {@ IsLocal(CoreTrait((alias (Mirror :: Assoc) (rigid (adt CoreStruct)))))} given {}, got {}
failed to prove {@ IsLocal(CoreTrait((Mirror::Assoc)<(rigid (adt CoreStruct))>))} given {}, got {}

0 comments on commit c151b28

Please sign in to comment.