Skip to content

Commit 56a7608

Browse files
authored
Update to syn 2.0 (#93)
1 parent 9bc2982 commit 56a7608

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

async-stream-impl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ proc-macro = true
1313

1414
[dependencies]
1515
proc-macro2 = "1"
16-
syn = { version = "1", features = ["full", "visit-mut"] }
16+
syn = { version = "2.0.2", features = ["full", "visit-mut"] }
1717
quote = "1"
1818

1919
[dev-dependencies]

async-stream-impl/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl VisitMut for Scrub<'_> {
147147
syn::Expr::ForLoop(expr) => {
148148
syn::visit_mut::visit_expr_for_loop_mut(self, expr);
149149
// TODO: Should we allow other attributes?
150-
if expr.attrs.len() != 1 || !expr.attrs[0].path.is_ident("await") {
150+
if expr.attrs.len() != 1 || !expr.attrs[0].meta.path().is_ident(AWAIT_ATTR_NAME) {
151151
return;
152152
}
153153
let syn::ExprForLoop {
@@ -159,11 +159,7 @@ impl VisitMut for Scrub<'_> {
159159
..
160160
} = expr;
161161

162-
let attr = attrs.pop().unwrap();
163-
if let Err(e) = syn::parse2::<syn::parse::Nothing>(attr.tokens) {
164-
*i = syn::parse2(e.to_compile_error()).unwrap();
165-
return;
166-
}
162+
attrs.pop().unwrap();
167163

168164
let crate_path = self.crate_path;
169165
*i = syn::parse_quote! {{
@@ -270,6 +266,10 @@ pub fn try_stream_inner(input: TokenStream) -> TokenStream {
270266
.into()
271267
}
272268

269+
// syn 2.0 wont parse `#[await] for x in xs {}`
270+
// because `await` is a keyword, use `await_` instead
271+
const AWAIT_ATTR_NAME: &str = "await_";
272+
273273
/// Replace `for await` with `#[await] for`, which will be later transformed into a `next` loop.
274274
fn replace_for_await(input: impl IntoIterator<Item = TokenTree>) -> TokenStream2 {
275275
let mut input = input.into_iter().peekable();
@@ -280,6 +280,8 @@ fn replace_for_await(input: impl IntoIterator<Item = TokenTree>) -> TokenStream2
280280
TokenTree::Ident(ident) => {
281281
match input.peek() {
282282
Some(TokenTree::Ident(next)) if ident == "for" && next == "await" => {
283+
let next_span = next.span();
284+
let next = syn::Ident::new(AWAIT_ATTR_NAME, next_span);
283285
tokens.extend(quote!(#[#next]));
284286
let _ = input.next();
285287
}

0 commit comments

Comments
 (0)