Skip to content

Commit d7946b6

Browse files
committed
modify {:#?} to print the traditional debug
It is sometimes useful to see EXACTLY what is going on
1 parent 96f7077 commit d7946b6

File tree

5 files changed

+116
-44
lines changed

5 files changed

+116
-44
lines changed

crates/formality-macros/src/debug.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate proc_macro;
2+
23
use convert_case::{Case, Casing};
34
use proc_macro2::{Ident, Literal, TokenStream};
45
use quote::{quote, quote_spanned};
@@ -20,6 +21,7 @@ pub(crate) fn derive_debug_with_spec(
2021
.into_compile_error();
2122
}
2223

24+
let default_debug = default_debug_variant(&s);
2325
let debug_arms = s.each_variant(|v| debug_variant(v, external_spec));
2426

2527
s.gen_impl(quote! {
@@ -29,15 +31,50 @@ pub(crate) fn derive_debug_with_spec(
2931
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
3032
{
3133
#[allow(unused_assignments)]
32-
match self {
33-
#debug_arms
34+
if fmt.alternate() {
35+
#default_debug
36+
} else {
37+
match self {
38+
#debug_arms
39+
}
40+
Ok(())
3441
}
35-
Ok(())
3642
}
3743
}
3844
})
3945
}
4046

47+
fn default_debug_variant(s: &synstructure::Structure) -> TokenStream {
48+
let arms = s.each_variant(|v| {
49+
let fields: TokenStream = v.bindings().iter().map(|bi| {
50+
if let Some(name) = &bi.ast().ident {
51+
let name = as_literal(name);
52+
quote_spanned!(name.span() => .field(#name, #bi))
53+
} else {
54+
quote_spanned!(bi.span() => .field(#bi))
55+
}
56+
}).collect();
57+
let variant_name = as_literal(v.ast().ident);
58+
match v.ast().fields {
59+
syn::Fields::Named(_) => {
60+
quote_spanned!(variant_name.span() => fmt.debug_struct(#variant_name) #fields .finish())
61+
}
62+
syn::Fields::Unnamed(_) => {
63+
quote_spanned!(variant_name.span() => fmt.debug_tuple(#variant_name) #fields .finish())
64+
}
65+
syn::Fields::Unit => {
66+
quote_spanned!(variant_name.span() => fmt.debug_tuple(#variant_name) .finish())
67+
}
68+
}
69+
});
70+
71+
quote_spanned! { s.ast().span() =>
72+
match self {
73+
#arms
74+
}
75+
}
76+
}
77+
4178
fn debug_variant(
4279
variant: &synstructure::VariantInfo,
4380
external_spec: Option<&FormalitySpec>,

crates/formality-prove/src/prove/minimize/test.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,13 @@ fn minimize_a() {
1717
let (env, subst) = env.existential_substitution(&term);
1818
let term = term.instantiate_with(&subst).unwrap();
1919

20-
expect![[r#"
21-
(
22-
Env {
23-
variables: [
24-
?ty_1,
25-
?ty_2,
26-
?ty_3,
27-
],
28-
coherence_mode: false,
29-
},
30-
[
31-
?ty_1,
32-
?ty_3,
33-
],
34-
)
35-
"#]]
36-
.assert_debug_eq(&(&env, &term));
20+
expect!["(Env { variables: [?ty_1, ?ty_2, ?ty_3], coherence_mode: false }, [?ty_1, ?ty_3])"]
21+
.assert_eq(&format!("{:?}", (&env, &term)));
3722

3823
let (mut env_min, term_min, m) = minimize(env, term);
3924

40-
expect![[r#"
41-
(
42-
Env {
43-
variables: [
44-
?ty_0,
45-
?ty_1,
46-
],
47-
coherence_mode: false,
48-
},
49-
[
50-
?ty_0,
51-
?ty_1,
52-
],
53-
)
54-
"#]]
55-
.assert_debug_eq(&(&env_min, &term_min));
25+
expect!["(Env { variables: [?ty_0, ?ty_1], coherence_mode: false }, [?ty_0, ?ty_1])"]
26+
.assert_eq(&format!("{:?}", (&env_min, &term_min)));
5627

5728
let ty0 = term_min[0].as_variable().unwrap();
5829
let ty1 = term_min[1].as_variable().unwrap();

examples/formality-eg/grammar/test.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use super::{Expr, StructDecl, Ty};
88
fn test_struct_decl() {
99
let r: StructDecl = term("struct Point { x: integer, y: integer }");
1010
expect_test::expect![[r#"
11-
struct Point { x : integer, y : integer }
11+
StructDecl {
12+
id: Point,
13+
bound: { x : integer, y : integer },
14+
}
1215
"#]]
1316
.assert_debug_eq(&r);
1417
}
@@ -17,7 +20,12 @@ fn test_struct_decl() {
1720
fn test_struct_ty_empty_args() {
1821
let r: Ty = term("Point");
1922
expect_test::expect![[r#"
20-
Point
23+
StructTy(
24+
StructTy {
25+
id: Point,
26+
parameters: [],
27+
},
28+
)
2129
"#]]
2230
.assert_debug_eq(&r);
2331
}
@@ -26,7 +34,12 @@ fn test_struct_ty_empty_args() {
2634
fn test_struct_ty_no_args() {
2735
let r: Ty = term("Point");
2836
expect_test::expect![[r#"
29-
Point
37+
StructTy(
38+
StructTy {
39+
id: Point,
40+
parameters: [],
41+
},
42+
)
3043
"#]]
3144
.assert_debug_eq(&r);
3245
}
@@ -35,7 +48,16 @@ fn test_struct_ty_no_args() {
3548
fn test_vec_int_ty() {
3649
let r: Ty = term("Vec<integer>");
3750
expect_test::expect![[r#"
38-
Vec <integer>
51+
StructTy(
52+
StructTy {
53+
id: Vec,
54+
parameters: [
55+
Ty(
56+
Integer,
57+
),
58+
],
59+
},
60+
)
3961
"#]]
4062
.assert_debug_eq(&r);
4163
}
@@ -44,7 +66,19 @@ fn test_vec_int_ty() {
4466
fn test_expression() {
4567
let r: Expr = term("3 + 5 * 6");
4668
expect_test::expect![[r#"
47-
3 + 5 * 6
69+
Add(
70+
IntegerLiteral(
71+
3,
72+
),
73+
Mul(
74+
IntegerLiteral(
75+
5,
76+
),
77+
IntegerLiteral(
78+
6,
79+
),
80+
),
81+
)
4882
"#]]
4983
.assert_debug_eq(&r);
5084
}

tests/parser-torture-tests/ambiguity.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ fn reduce_reduce_ok() {
2727

2828
let term: Root = crate::ptt::term("my String");
2929
expect_test::expect![[r#"
30-
my String
30+
ClassTy(
31+
ClassTy {
32+
perm: My,
33+
class_id: String,
34+
},
35+
)
3136
"#]]
3237
.assert_debug_eq(&term);
3338
}

tests/parser-torture-tests/precedence.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,41 @@ formality_core::id!(Id);
2020
fn mul_is_higher_precedence() {
2121
let term: Expr = crate::ptt::term("a + b * c");
2222
expect_test::expect![[r#"
23-
a + b * c
23+
Add(
24+
Id(
25+
a,
26+
),
27+
Mul(
28+
Id(
29+
b,
30+
),
31+
Id(
32+
c,
33+
),
34+
),
35+
)
2436
"#]]
2537
.assert_debug_eq(&term);
2638
}
2739

2840
#[test]
41+
// FIXME #[should_panic(expected = "ambiguous parse")]
2942
fn equal_precedence_panics() {
3043
let term: Expr = crate::ptt::term("a + b * c");
3144
expect_test::expect![[r#"
32-
a + b * c
45+
Add(
46+
Id(
47+
a,
48+
),
49+
Mul(
50+
Id(
51+
b,
52+
),
53+
Id(
54+
c,
55+
),
56+
),
57+
)
3358
"#]]
3459
.assert_debug_eq(&term);
3560
}

0 commit comments

Comments
 (0)