From 596ceaf8da67f86ec84ac2e1c769b550ddd93a1c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 24 Jun 2023 20:13:58 -0700 Subject: [PATCH] Preserve attributes on verbatim Expr --- src/expr.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 19405e6..525f09b 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -666,6 +666,7 @@ impl Printer { #[cfg(feature = "verbatim")] fn expr_verbatim(&mut self, tokens: &TokenStream) { + use syn::parse::discouraged::Speculative; use syn::parse::{Parse, ParseStream, Result}; use syn::{parenthesized, Ident}; @@ -677,11 +678,13 @@ impl Printer { } struct Builtin { + attrs: Vec, name: Ident, args: TokenStream, } struct RawReference { + attrs: Vec, mutable: bool, expr: Expr, } @@ -693,18 +696,22 @@ impl Printer { impl Parse for ExprVerbatim { fn parse(input: ParseStream) -> Result { - let lookahead = input.lookahead1(); + let ahead = input.fork(); + let attrs = ahead.call(Attribute::parse_outer)?; + let lookahead = ahead.lookahead1(); if input.is_empty() { Ok(ExprVerbatim::Empty) } else if lookahead.peek(kw::builtin) { + input.advance_to(&ahead); input.parse::()?; input.parse::()?; let name: Ident = input.parse()?; let args; parenthesized!(args in input); let args: TokenStream = args.parse()?; - Ok(ExprVerbatim::Builtin(Builtin { name, args })) + Ok(ExprVerbatim::Builtin(Builtin { attrs, name, args })) } else if lookahead.peek(Token![&]) { + input.advance_to(&ahead); input.parse::()?; input.parse::()?; let mutable = input.parse::>()?.is_some(); @@ -712,7 +719,11 @@ impl Printer { input.parse::()?; } let expr: Expr = input.parse()?; - Ok(ExprVerbatim::RawReference(RawReference { mutable, expr })) + Ok(ExprVerbatim::RawReference(RawReference { + attrs, + mutable, + expr, + })) } else if lookahead.peek(Token![...]) { input.parse::()?; Ok(ExprVerbatim::Ellipsis) @@ -733,6 +744,7 @@ impl Printer { self.word("..."); } ExprVerbatim::Builtin(expr) => { + self.outer_attrs(&expr.attrs); self.word("builtin # "); self.ident(&expr.name); self.word("("); @@ -749,6 +761,7 @@ 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);