Skip to content

Commit b4919cf

Browse files
authored
Merge pull request #82 from dtolnay/rawaddr
Pretty print Expr::RawAddr
2 parents 75e1f4f + eb15c0c commit b4919cf

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
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.76", default-features = false, features = ["full"] }
22+
syn = { version = "2.0.80", 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.76", default-features = false, features = ["parsing"] }
28+
syn = { version = "2.0.80", default-features = false, features = ["parsing"] }
2929

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

src/expr.rs

+25-31
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use syn::{
1010
token, Arm, Attribute, BinOp, Block, Expr, ExprArray, ExprAssign, ExprAsync, ExprAwait,
1111
ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue,
1212
ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprInfer, ExprLet, ExprLit, ExprLoop,
13-
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference,
14-
ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe,
15-
ExprWhile, ExprYield, FieldValue, Index, Label, Member, RangeLimits, ReturnType, Stmt, Token,
16-
UnOp,
13+
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprRawAddr,
14+
ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary,
15+
ExprUnsafe, ExprWhile, ExprYield, FieldValue, Index, Label, Member, PointerMutability,
16+
RangeLimits, ReturnType, Stmt, Token, UnOp,
1717
};
1818

1919
impl Printer {
@@ -48,6 +48,7 @@ impl Printer {
4848
Expr::Paren(expr) => self.expr_paren(expr),
4949
Expr::Path(expr) => self.expr_path(expr),
5050
Expr::Range(expr) => self.expr_range(expr),
51+
Expr::RawAddr(expr) => self.expr_raw_addr(expr),
5152
Expr::Reference(expr) => self.expr_reference(expr),
5253
Expr::Repeat(expr) => self.expr_repeat(expr),
5354
Expr::Return(expr) => self.expr_return(expr),
@@ -564,6 +565,14 @@ impl Printer {
564565
}
565566
}
566567

568+
fn expr_raw_addr(&mut self, expr: &ExprRawAddr) {
569+
self.outer_attrs(&expr.attrs);
570+
self.word("&raw ");
571+
self.pointer_mutability(&expr.mutability);
572+
self.nbsp();
573+
self.expr(&expr.expr);
574+
}
575+
567576
fn expr_reference(&mut self, expr: &ExprReference) {
568577
self.outer_attrs(&expr.attrs);
569578
self.word("&");
@@ -683,7 +692,6 @@ impl Printer {
683692
Ellipsis,
684693
Become(Become),
685694
Builtin(Builtin),
686-
RawReference(RawReference),
687695
}
688696

689697
struct Become {
@@ -697,12 +705,6 @@ impl Printer {
697705
args: TokenStream,
698706
}
699707

700-
struct RawReference {
701-
attrs: Vec<Attribute>,
702-
mutable: bool,
703-
expr: Expr,
704-
}
705-
706708
mod kw {
707709
syn::custom_keyword!(builtin);
708710
syn::custom_keyword!(raw);
@@ -729,20 +731,6 @@ impl Printer {
729731
parenthesized!(args in input);
730732
let args: TokenStream = args.parse()?;
731733
Ok(ExprVerbatim::Builtin(Builtin { attrs, name, args }))
732-
} else if lookahead.peek(Token![&]) {
733-
input.advance_to(&ahead);
734-
input.parse::<Token![&]>()?;
735-
input.parse::<kw::raw>()?;
736-
let mutable = input.parse::<Option<Token![mut]>>()?.is_some();
737-
if !mutable {
738-
input.parse::<Token![const]>()?;
739-
}
740-
let expr: Expr = input.parse()?;
741-
Ok(ExprVerbatim::RawReference(RawReference {
742-
attrs,
743-
mutable,
744-
expr,
745-
}))
746734
} else if lookahead.peek(Token![...]) {
747735
input.parse::<Token![...]>()?;
748736
Ok(ExprVerbatim::Ellipsis)
@@ -785,12 +773,6 @@ impl Printer {
785773
}
786774
self.word(")");
787775
}
788-
ExprVerbatim::RawReference(expr) => {
789-
self.outer_attrs(&expr.attrs);
790-
self.word("&raw ");
791-
self.word(if expr.mutable { "mut " } else { "const " });
792-
self.expr(&expr.expr);
793-
}
794776
}
795777
}
796778

@@ -1013,6 +995,13 @@ impl Printer {
1013995
);
1014996
}
1015997

998+
fn pointer_mutability(&mut self, mutability: &PointerMutability) {
999+
match mutability {
1000+
PointerMutability::Const(_) => self.word("const"),
1001+
PointerMutability::Mut(_) => self.word("mut"),
1002+
}
1003+
}
1004+
10161005
fn zerobreak_unless_short_ident(&mut self, beginning_of_line: bool, expr: &Expr) {
10171006
if beginning_of_line && is_short_ident(expr) {
10181007
return;
@@ -1055,6 +1044,7 @@ fn requires_terminator(expr: &Expr) -> bool {
10551044
| Expr::Paren(_)
10561045
| Expr::Path(_)
10571046
| Expr::Range(_)
1047+
| Expr::RawAddr(_)
10581048
| Expr::Reference(_)
10591049
| Expr::Repeat(_)
10601050
| Expr::Return(_)
@@ -1090,6 +1080,7 @@ fn contains_exterior_struct_lit(expr: &Expr) -> bool {
10901080
| Expr::Group(ExprGroup { expr: e, .. })
10911081
| Expr::Index(ExprIndex { expr: e, .. })
10921082
| Expr::MethodCall(ExprMethodCall { receiver: e, .. })
1083+
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
10931084
| Expr::Reference(ExprReference { expr: e, .. })
10941085
| Expr::Unary(ExprUnary { expr: e, .. }) => {
10951086
// &X { y: 1 }, X { y: 1 }.y
@@ -1172,6 +1163,7 @@ fn needs_newline_if_wrap(expr: &Expr) -> bool {
11721163
| Expr::Let(ExprLet { expr: e, .. })
11731164
| Expr::Paren(ExprParen { expr: e, .. })
11741165
| Expr::Range(ExprRange { end: Some(e), .. })
1166+
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
11751167
| Expr::Reference(ExprReference { expr: e, .. })
11761168
| Expr::Return(ExprReturn { expr: Some(e), .. })
11771169
| Expr::Try(ExprTry { expr: e, .. })
@@ -1229,6 +1221,7 @@ fn is_blocklike(expr: &Expr) -> bool {
12291221
| Expr::Paren(_)
12301222
| Expr::Path(_)
12311223
| Expr::Range(_)
1224+
| Expr::RawAddr(_)
12321225
| Expr::Reference(_)
12331226
| Expr::Repeat(_)
12341227
| Expr::Return(_)
@@ -1266,6 +1259,7 @@ fn parseable_as_stmt(expr: &Expr) -> bool {
12661259
| Expr::Match(_)
12671260
| Expr::Paren(_)
12681261
| Expr::Path(_)
1262+
| Expr::RawAddr(_)
12691263
| Expr::Reference(_)
12701264
| Expr::Repeat(_)
12711265
| Expr::Return(_)

src/stmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub fn add_semi(expr: &Expr) -> bool {
145145
| Expr::Paren(_)
146146
| Expr::Path(_)
147147
| Expr::Range(_)
148+
| Expr::RawAddr(_)
148149
| Expr::Reference(_)
149150
| Expr::Repeat(_)
150151
| Expr::Struct(_)
@@ -203,6 +204,7 @@ fn remove_semi(expr: &Expr) -> bool {
203204
| Expr::Paren(_)
204205
| Expr::Path(_)
205206
| Expr::Range(_)
207+
| Expr::RawAddr(_)
206208
| Expr::Reference(_)
207209
| Expr::Repeat(_)
208210
| Expr::Return(_)

0 commit comments

Comments
 (0)