Skip to content

Commit

Permalink
Merge pull request #82 from dtolnay/rawaddr
Browse files Browse the repository at this point in the history
Pretty print Expr::RawAddr
  • Loading branch information
dtolnay authored Oct 19, 2024
2 parents 75e1f4f + eb15c0c commit b4919cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ verbatim = ["syn/parsing"]

[dependencies]
proc-macro2 = { version = "1.0.80", default-features = false }
syn = { version = "2.0.76", default-features = false, features = ["full"] }
syn = { version = "2.0.80", default-features = false, features = ["full"] }

[dev-dependencies]
indoc = "2"
proc-macro2 = { version = "1.0.80", default-features = false }
quote = { version = "1.0.35", default-features = false }
syn = { version = "2.0.76", default-features = false, features = ["parsing"] }
syn = { version = "2.0.80", default-features = false, features = ["parsing"] }

[lib]
doc-scrape-examples = false
Expand Down
56 changes: 25 additions & 31 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use syn::{
token, Arm, Attribute, BinOp, Block, Expr, ExprArray, ExprAssign, ExprAsync, ExprAwait,
ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue,
ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprInfer, ExprLet, ExprLit, ExprLoop,
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference,
ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe,
ExprWhile, ExprYield, FieldValue, Index, Label, Member, RangeLimits, ReturnType, Stmt, Token,
UnOp,
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprRawAddr,
ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary,
ExprUnsafe, ExprWhile, ExprYield, FieldValue, Index, Label, Member, PointerMutability,
RangeLimits, ReturnType, Stmt, Token, UnOp,
};

impl Printer {
Expand Down Expand Up @@ -48,6 +48,7 @@ impl Printer {
Expr::Paren(expr) => self.expr_paren(expr),
Expr::Path(expr) => self.expr_path(expr),
Expr::Range(expr) => self.expr_range(expr),
Expr::RawAddr(expr) => self.expr_raw_addr(expr),
Expr::Reference(expr) => self.expr_reference(expr),
Expr::Repeat(expr) => self.expr_repeat(expr),
Expr::Return(expr) => self.expr_return(expr),
Expand Down Expand Up @@ -564,6 +565,14 @@ impl Printer {
}
}

fn expr_raw_addr(&mut self, expr: &ExprRawAddr) {
self.outer_attrs(&expr.attrs);
self.word("&raw ");
self.pointer_mutability(&expr.mutability);
self.nbsp();
self.expr(&expr.expr);
}

fn expr_reference(&mut self, expr: &ExprReference) {
self.outer_attrs(&expr.attrs);
self.word("&");
Expand Down Expand Up @@ -683,7 +692,6 @@ impl Printer {
Ellipsis,
Become(Become),
Builtin(Builtin),
RawReference(RawReference),
}

struct Become {
Expand All @@ -697,12 +705,6 @@ impl Printer {
args: TokenStream,
}

struct RawReference {
attrs: Vec<Attribute>,
mutable: bool,
expr: Expr,
}

mod kw {
syn::custom_keyword!(builtin);
syn::custom_keyword!(raw);
Expand All @@ -729,20 +731,6 @@ impl Printer {
parenthesized!(args in input);
let args: TokenStream = args.parse()?;
Ok(ExprVerbatim::Builtin(Builtin { attrs, name, args }))
} else if lookahead.peek(Token![&]) {
input.advance_to(&ahead);
input.parse::<Token![&]>()?;
input.parse::<kw::raw>()?;
let mutable = input.parse::<Option<Token![mut]>>()?.is_some();
if !mutable {
input.parse::<Token![const]>()?;
}
let expr: Expr = input.parse()?;
Ok(ExprVerbatim::RawReference(RawReference {
attrs,
mutable,
expr,
}))
} else if lookahead.peek(Token![...]) {
input.parse::<Token![...]>()?;
Ok(ExprVerbatim::Ellipsis)
Expand Down Expand Up @@ -785,12 +773,6 @@ impl Printer {
}
self.word(")");
}
ExprVerbatim::RawReference(expr) => {
self.outer_attrs(&expr.attrs);
self.word("&raw ");
self.word(if expr.mutable { "mut " } else { "const " });
self.expr(&expr.expr);
}
}
}

Expand Down Expand Up @@ -1013,6 +995,13 @@ impl Printer {
);
}

fn pointer_mutability(&mut self, mutability: &PointerMutability) {
match mutability {
PointerMutability::Const(_) => self.word("const"),
PointerMutability::Mut(_) => self.word("mut"),
}
}

fn zerobreak_unless_short_ident(&mut self, beginning_of_line: bool, expr: &Expr) {
if beginning_of_line && is_short_ident(expr) {
return;
Expand Down Expand Up @@ -1055,6 +1044,7 @@ fn requires_terminator(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down Expand Up @@ -1090,6 +1080,7 @@ fn contains_exterior_struct_lit(expr: &Expr) -> bool {
| Expr::Group(ExprGroup { expr: e, .. })
| Expr::Index(ExprIndex { expr: e, .. })
| Expr::MethodCall(ExprMethodCall { receiver: e, .. })
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
| Expr::Reference(ExprReference { expr: e, .. })
| Expr::Unary(ExprUnary { expr: e, .. }) => {
// &X { y: 1 }, X { y: 1 }.y
Expand Down Expand Up @@ -1172,6 +1163,7 @@ fn needs_newline_if_wrap(expr: &Expr) -> bool {
| Expr::Let(ExprLet { expr: e, .. })
| Expr::Paren(ExprParen { expr: e, .. })
| Expr::Range(ExprRange { end: Some(e), .. })
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
| Expr::Reference(ExprReference { expr: e, .. })
| Expr::Return(ExprReturn { expr: Some(e), .. })
| Expr::Try(ExprTry { expr: e, .. })
Expand Down Expand Up @@ -1229,6 +1221,7 @@ fn is_blocklike(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down Expand Up @@ -1266,6 +1259,7 @@ fn parseable_as_stmt(expr: &Expr) -> bool {
| Expr::Match(_)
| Expr::Paren(_)
| Expr::Path(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down
2 changes: 2 additions & 0 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub fn add_semi(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Struct(_)
Expand Down Expand Up @@ -203,6 +204,7 @@ fn remove_semi(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down

0 comments on commit b4919cf

Please sign in to comment.