Skip to content

Commit

Permalink
refactor: recurse directly in syn::Fold impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Sep 13, 2024
1 parent 8373e7d commit 8babd84
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 62 deletions.
22 changes: 6 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct Bind {
#[derive(Default)]
struct MyFold {
binds: Vec<Bind>,
counter: i32,
counter: u32,
diagnostics: Vec<proc_macro_error2::Diagnostic>,
}
impl MyFold {
Expand Down Expand Up @@ -100,6 +100,7 @@ impl syn::fold::Fold for MyFold {
{
match syn::parse2::<PatSingle>(expr_macro.mac.tokens) {
Ok(PatSingle(subpat)) => {
let subpat = syn::fold::fold_pat(self, subpat);
let typ = typ.parse().unwrap();
let pat_ident = self.handle(subpat, typ, span);
syn::Pat::Ident(pat_ident)
Expand All @@ -121,7 +122,8 @@ impl syn::fold::Fold for MyFold {
if i.by_ref.is_some() || i.mutability.is_some() || i.ident != "Deref" {
syn::fold::fold_pat_ident(self, i)
} else if let Some((_at, subpat)) = i.subpat {
self.handle(*subpat, Type::Deref, i.ident.span())
let subpat = syn::fold::fold_pat(self, *subpat);
self.handle(subpat, Type::Deref, i.ident.span())
} else {
syn::fold::fold_pat_ident(self, i)
}
Expand All @@ -130,8 +132,7 @@ impl syn::fold::Fold for MyFold {

fn tower(binds: &[Bind], yes: syn::Expr, no: &syn::Expr, add_ref: bool) -> syn::Expr {
let mut out = yes;
for bind in binds.iter().rev()
{
for bind in binds {
let &Bind {
ref id,
ref pat,
Expand Down Expand Up @@ -163,18 +164,7 @@ fn matchbox_impl(mut m: syn::ExprMatch) -> syn::ExprMatch {
let span = arm.pat.span();
let mut my_fold = MyFold::default();
arm.pat = my_fold.fold_pat(arm.pat);
{
// recurse only after top layer is complete.
let mut i = 0;
while i < my_fold.binds.len() {
let a = std::mem::replace(
&mut my_fold.binds[i].pat,
syn::Pat::Verbatim(quote::quote_spanned!(span=> )), // Temp placeholder
);
my_fold.binds[i].pat = my_fold.fold_pat(a);
i += 1;
}
}

if !my_fold.binds.is_empty() {
let (yes, no) = if let Some((_if_token, src_guard)) = arm.guard {
(*src_guard, syn::parse_quote_spanned! {span=> false })
Expand Down
20 changes: 10 additions & 10 deletions src/snapshots/matchbox__test__basic-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ expression: "snapshot!\n{ match () { deref!((deref!(x),)) => {} Deref @ (Deref @
---
fn main() {
match () {
a0 if {
a1 if {
#[allow(unused_variables)]
if let (a1,) = &**a0 {
if let x = &**a1 { true } else { false }
if let (a0,) = &**a1 {
if let x = &**a0 { true } else { false }
} else {
false
}
} => {
if let (a1,) = &**a0 {
if let x = &**a1 {
if let (a0,) = &**a1 {
if let x = &**a0 {
{}
} else {
panic!(
Expand All @@ -26,16 +26,16 @@ fn main() {
)
}
}
a0 if {
a1 if {
#[allow(unused_variables)]
if let (a1,) = &**a0 {
if let x = &**a1 { true } else { false }
if let (a0,) = &**a1 {
if let x = &**a0 { true } else { false }
} else {
false
}
} => {
if let (a1,) = &**a0 {
if let x = &**a1 {
if let (a0,) = &**a1 {
if let x = &**a0 {
{}
} else {
panic!(
Expand Down
36 changes: 18 additions & 18 deletions src/snapshots/matchbox__test__basic-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: "snapshot!\n{\n match ()\n {\n deref!((deref!((deref!(x
---
fn main() {
match () {
a0 if {
a3 if {
#[allow(unused_variables)]
if let (a1, a2) = &**a0 {
if let (a3,) = &**a1 {
if let y = &**a2 {
if let x = &**a3 { true } else { false }
if let (a1, a2) = &**a3 {
if let y = &**a2 {
if let (a0,) = &**a1 {
if let x = &**a0 { true } else { false }
} else {
false
}
Expand All @@ -20,10 +20,10 @@ fn main() {
false
}
} => {
if let (a1, a2) = &**a0 {
if let (a3,) = &**a1 {
if let y = &**a2 {
if let x = &**a3 {
if let (a1, a2) = &**a3 {
if let y = &**a2 {
if let (a0,) = &**a1 {
if let x = &**a0 {
{}
} else {
panic!(
Expand All @@ -46,12 +46,12 @@ fn main() {
)
}
}
a0 if {
a3 if {
#[allow(unused_variables)]
if let (a1, a2) = &**a0 {
if let (a3,) = &**a1 {
if let y = &**a2 {
if let x = &**a3 { true } else { false }
if let (a1, a2) = &**a3 {
if let y = &**a2 {
if let (a0,) = &**a1 {
if let x = &**a0 { true } else { false }
} else {
false
}
Expand All @@ -62,10 +62,10 @@ fn main() {
false
}
} => {
if let (a1, a2) = &**a0 {
if let (a3,) = &**a1 {
if let y = &**a2 {
if let x = &**a3 {
if let (a1, a2) = &**a3 {
if let y = &**a2 {
if let (a0,) = &**a1 {
if let x = &**a0 {
{}
} else {
panic!(
Expand Down
10 changes: 5 additions & 5 deletions src/snapshots/matchbox__test__basic_owned-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ expression: "snapshot! { match () { owned!((owned!(x),)) => {} } }"
---
fn main() {
match () {
a0 if {
a1 if {
#[allow(unused_variables)]
if let (a1,) = &*a0 {
if let x = &*a1 { true } else { false }
if let (a0,) = &*a1 {
if let x = &*a0 { true } else { false }
} else {
false
}
} => {
if let (a1,) = *a0 {
if let x = *a1 {
if let (a0,) = *a1 {
if let x = *a0 {
{}
} else {
panic!(
Expand Down
18 changes: 9 additions & 9 deletions src/snapshots/matchbox__test__basic_owned-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: "snapshot! { match () { owned!((owned!((owned!(x),)), owned!(y))) =>
---
fn main() {
match () {
a0 if {
a3 if {
#[allow(unused_variables)]
if let (a1, a2) = &*a0 {
if let (a3,) = &*a1 {
if let y = &*a2 {
if let x = &*a3 { true } else { false }
if let (a1, a2) = &*a3 {
if let y = &*a2 {
if let (a0,) = &*a1 {
if let x = &*a0 { true } else { false }
} else {
false
}
Expand All @@ -20,10 +20,10 @@ fn main() {
false
}
} => {
if let (a1, a2) = *a0 {
if let (a3,) = *a1 {
if let y = *a2 {
if let x = *a3 {
if let (a1, a2) = *a3 {
if let y = *a2 {
if let (a0,) = *a1 {
if let x = *a0 {
{}
} else {
panic!(
Expand Down
8 changes: 4 additions & 4 deletions src/snapshots/matchbox__test__other-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: "snapshot! { match () { Deref @ Deref @ x => () } }"
---
fn main() {
match () {
a0 if {
a1 if {
#[allow(unused_variables)]
if let a1 = &**a0 { if let x = &**a1 { true } else { false } } else { false }
if let a0 = &**a1 { if let x = &**a0 { true } else { false } } else { false }
} => {
if let a1 = &**a0 {
if let x = &**a1 {
if let a0 = &**a1 {
if let x = &**a0 {
()
} else {
panic!(
Expand Down

0 comments on commit 8babd84

Please sign in to comment.