Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit 59fcd8a

Browse files
authored
Merge pull request #64 from dtolnay/kv
Work around conflict with extended_key_value_attributes
2 parents c613e7a + e5e87d3 commit 59fcd8a

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ use std::panic;
160160
#[proc_macro]
161161
pub fn paste(input: TokenStream) -> TokenStream {
162162
let mut contains_paste = false;
163-
match expand(input, &mut contains_paste) {
163+
let flatten_single_interpolation = true;
164+
match expand(input, &mut contains_paste, flatten_single_interpolation) {
164165
Ok(expanded) => expanded,
165166
Err(err) => err.to_compile_error(),
166167
}
@@ -178,7 +179,11 @@ pub fn expr(input: TokenStream) -> TokenStream {
178179
paste(input)
179180
}
180181

181-
fn expand(input: TokenStream, contains_paste: &mut bool) -> Result<TokenStream> {
182+
fn expand(
183+
input: TokenStream,
184+
contains_paste: &mut bool,
185+
flatten_single_interpolation: bool,
186+
) -> Result<TokenStream> {
182187
let mut expanded = TokenStream::new();
183188
let mut lookbehind = Lookbehind::Other;
184189
let mut prev_none_group = None::<Group>;
@@ -209,15 +214,22 @@ fn expand(input: TokenStream, contains_paste: &mut bool) -> Result<TokenStream>
209214
let tokens = pasted_to_tokens(pasted, span)?;
210215
expanded.extend(tokens);
211216
*contains_paste = true;
212-
} else if delimiter == Delimiter::None && is_flat_group(&content) {
217+
} else if flatten_single_interpolation
218+
&& delimiter == Delimiter::None
219+
&& is_single_interpolation_group(&content)
220+
{
213221
expanded.extend(content);
214222
*contains_paste = true;
215223
} else {
216224
let mut group_contains_paste = false;
217-
let mut nested = expand(content, &mut group_contains_paste)?;
218-
if delimiter == Delimiter::Bracket
219-
&& (lookbehind == Lookbehind::Pound || lookbehind == Lookbehind::PoundBang)
220-
{
225+
let is_attribute = delimiter == Delimiter::Bracket
226+
&& (lookbehind == Lookbehind::Pound || lookbehind == Lookbehind::PoundBang);
227+
let mut nested = expand(
228+
content,
229+
&mut group_contains_paste,
230+
flatten_single_interpolation && !is_attribute,
231+
)?;
232+
if is_attribute {
221233
nested = expand_attr(nested, span, &mut group_contains_paste)?
222234
}
223235
let group = if group_contains_paste {
@@ -268,7 +280,7 @@ enum Lookbehind {
268280
}
269281

270282
// https://github.com/dtolnay/paste/issues/26
271-
fn is_flat_group(input: &TokenStream) -> bool {
283+
fn is_single_interpolation_group(input: &TokenStream) -> bool {
272284
#[derive(PartialEq)]
273285
enum State {
274286
Init,

tests/test_doc.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,26 @@ fn test_case() {
5252
let expected = "HTTP GET!";
5353
assert_eq!(doc, expected);
5454
}
55+
56+
// https://github.com/dtolnay/paste/issues/63
57+
#[test]
58+
fn test_stringify() {
59+
macro_rules! create {
60+
($doc:expr) => {
61+
paste! {
62+
#[doc = $doc]
63+
pub struct Struct;
64+
}
65+
};
66+
}
67+
68+
macro_rules! forward {
69+
($name:ident) => {
70+
create!(stringify!($name));
71+
};
72+
}
73+
74+
forward!(documentation);
75+
76+
let _ = Struct;
77+
}

0 commit comments

Comments
 (0)