Skip to content

Commit 5b476df

Browse files
committed
Wire up fixups for precedence
1 parent 3dd7a23 commit 5b476df

13 files changed

+459
-273
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ verbatim = ["syn/parsing"]
1919

2020
[dependencies]
2121
proc-macro2 = { version = "1.0.80", default-features = false }
22-
syn = { version = "2.0.81", default-features = false, features = ["full"] }
22+
syn = { version = "2.0.95", default-features = false, features = ["full"] }
2323

2424
[dev-dependencies]
2525
indoc = "2"
2626
proc-macro2 = { version = "1.0.80", default-features = false }
2727
quote = { version = "1.0.35", default-features = false }
28-
syn = { version = "2.0.81", default-features = false, features = ["parsing"] }
28+
syn = { version = "2.0.95", default-features = false, features = ["parsing"] }
2929

3030
[lib]
3131
doc-scrape-examples = false

src/attr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::algorithm::Printer;
2+
use crate::fixup::FixupContext;
23
use crate::path::PathKind;
34
use crate::INDENT;
45
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
@@ -102,7 +103,7 @@ impl Printer {
102103
fn meta_name_value(&mut self, meta: &MetaNameValue) {
103104
self.path(&meta.path, PathKind::Simple);
104105
self.word(" = ");
105-
self.expr(&meta.value);
106+
self.expr(&meta.value, FixupContext::NONE);
106107
}
107108

108109
fn attr_tokens(&mut self, tokens: TokenStream) {

src/classify.rs

+54-45
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,54 @@ pub(crate) fn requires_semi_to_be_stmt(expr: &Expr) -> bool {
1010
}
1111
}
1212

13-
pub(crate) fn requires_comma_to_be_match_arm(expr: &Expr) -> bool {
14-
match expr {
15-
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
16-
Expr::If(_)
17-
| Expr::Match(_)
18-
| Expr::Block(_) | Expr::Unsafe(_) // both under ExprKind::Block in rustc
19-
| Expr::While(_)
20-
| Expr::Loop(_)
21-
| Expr::ForLoop(_)
22-
| Expr::TryBlock(_)
23-
| Expr::Const(_) => false,
13+
pub(crate) fn requires_comma_to_be_match_arm(mut expr: &Expr) -> bool {
14+
loop {
15+
match expr {
16+
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
17+
Expr::If(_)
18+
| Expr::Match(_)
19+
| Expr::Block(_) | Expr::Unsafe(_) // both under ExprKind::Block in rustc
20+
| Expr::While(_)
21+
| Expr::Loop(_)
22+
| Expr::ForLoop(_)
23+
| Expr::TryBlock(_)
24+
| Expr::Const(_) => return false,
25+
26+
Expr::Array(_)
27+
| Expr::Assign(_)
28+
| Expr::Async(_)
29+
| Expr::Await(_)
30+
| Expr::Binary(_)
31+
| Expr::Break(_)
32+
| Expr::Call(_)
33+
| Expr::Cast(_)
34+
| Expr::Closure(_)
35+
| Expr::Continue(_)
36+
| Expr::Field(_)
37+
| Expr::Index(_)
38+
| Expr::Infer(_)
39+
| Expr::Let(_)
40+
| Expr::Lit(_)
41+
| Expr::Macro(_)
42+
| Expr::MethodCall(_)
43+
| Expr::Paren(_)
44+
| Expr::Path(_)
45+
| Expr::Range(_)
46+
| Expr::RawAddr(_)
47+
| Expr::Reference(_)
48+
| Expr::Repeat(_)
49+
| Expr::Return(_)
50+
| Expr::Struct(_)
51+
| Expr::Try(_)
52+
| Expr::Tuple(_)
53+
| Expr::Unary(_)
54+
| Expr::Yield(_)
55+
| Expr::Verbatim(_) => return true,
2456

25-
Expr::Array(_)
26-
| Expr::Assign(_)
27-
| Expr::Async(_)
28-
| Expr::Await(_)
29-
| Expr::Binary(_)
30-
| Expr::Break(_)
31-
| Expr::Call(_)
32-
| Expr::Cast(_)
33-
| Expr::Closure(_)
34-
| Expr::Continue(_)
35-
| Expr::Field(_)
36-
| Expr::Group(_)
37-
| Expr::Index(_)
38-
| Expr::Infer(_)
39-
| Expr::Let(_)
40-
| Expr::Lit(_)
41-
| Expr::Macro(_)
42-
| Expr::MethodCall(_)
43-
| Expr::Paren(_)
44-
| Expr::Path(_)
45-
| Expr::Range(_)
46-
| Expr::RawAddr(_)
47-
| Expr::Reference(_)
48-
| Expr::Repeat(_)
49-
| Expr::Return(_)
50-
| Expr::Struct(_)
51-
| Expr::Try(_)
52-
| Expr::Tuple(_)
53-
| Expr::Unary(_)
54-
| Expr::Yield(_)
55-
| Expr::Verbatim(_) => true,
57+
Expr::Group(group) => expr = &group.expr,
5658

57-
_ => true,
59+
_ => return true,
60+
}
5861
}
5962
}
6063

@@ -150,7 +153,6 @@ pub(crate) fn expr_leading_label(mut expr: &Expr) -> bool {
150153
| Expr::Closure(_)
151154
| Expr::Const(_)
152155
| Expr::Continue(_)
153-
| Expr::Group(_)
154156
| Expr::If(_)
155157
| Expr::Infer(_)
156158
| Expr::Let(_)
@@ -171,6 +173,13 @@ pub(crate) fn expr_leading_label(mut expr: &Expr) -> bool {
171173
| Expr::Verbatim(_)
172174
| Expr::Yield(_) => return false,
173175

176+
Expr::Group(e) => {
177+
if !e.attrs.is_empty() {
178+
return false;
179+
}
180+
expr = &e.expr;
181+
}
182+
174183
_ => return false,
175184
}
176185
}
@@ -201,6 +210,7 @@ pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool {
201210
},
202211
Expr::Cast(e) => return type_trailing_brace(&e.ty),
203212
Expr::Closure(e) => expr = &e.body,
213+
Expr::Group(e) => expr = &e.expr,
204214
Expr::Let(e) => expr = &e.expr,
205215
Expr::Macro(e) => return matches!(e.mac.delimiter, MacroDelimiter::Brace(_)),
206216
Expr::Range(e) => match &e.end {
@@ -225,7 +235,6 @@ pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool {
225235
| Expr::Call(_)
226236
| Expr::Continue(_)
227237
| Expr::Field(_)
228-
| Expr::Group(_)
229238
| Expr::Index(_)
230239
| Expr::Infer(_)
231240
| Expr::Lit(_)

src/data.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::algorithm::Printer;
2+
use crate::fixup::FixupContext;
23
use crate::iter::IterDelimited;
34
use crate::path::PathKind;
45
use crate::INDENT;
@@ -31,7 +32,7 @@ impl Printer {
3132
}
3233
if let Some((_eq_token, discriminant)) = &variant.discriminant {
3334
self.word(" = ");
34-
self.expr(discriminant);
35+
self.expr(discriminant, FixupContext::NONE);
3536
}
3637
}
3738

0 commit comments

Comments
 (0)