Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,13 +1293,12 @@ pub(crate) mod parsing {
#[cfg(feature = "full")]
use crate::generics::{self, BoundLifetimes};
use crate::ident::Ident;
#[cfg(feature = "full")]
use crate::lifetime::Lifetime;
use crate::lit::{Lit, LitFloat, LitInt};
use crate::mac::{self, Macro};
use crate::op::BinOp;
use crate::parse::discouraged::Speculative as _;
use crate::parse::{Parse, ParseStream};
use crate::parse::{End, Parse, ParseStream};
#[cfg(feature = "full")]
use crate::pat::{Pat, PatType};
use crate::path::{self, AngleBracketedGenericArguments, Path, QSelf};
Expand Down Expand Up @@ -1342,7 +1341,10 @@ pub(crate) mod parsing {
#[cfg(feature = "full")]
pub(super) fn parse_with_earlier_boundary_rule(input: ParseStream) -> Result<Expr> {
let mut attrs = input.call(expr_attrs)?;
let mut expr = if input.peek(token::Group) {
let mut expr = if input.peek(token::Group) && {
let group = crate::group::parse_group(&input.fork())?;
!(group.content.peek(Lifetime) && group.content.peek2(End))
} {
let allow_struct = AllowStruct(true);
let atom = expr_group(input, allow_struct)?;
if continue_parsing_early(&atom) {
Expand Down Expand Up @@ -1894,7 +1896,10 @@ pub(crate) mod parsing {
// interactions, as they are fully contained.
#[cfg(feature = "full")]
fn atom_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
if input.peek(token::Group) {
if input.peek(token::Group) && {
let group = crate::group::parse_group(&input.fork())?;
!(group.content.peek(Lifetime) && group.content.peek2(End))
} {
expr_group(input, allow_struct)
} else if input.peek(Lit) {
input.parse().map(Expr::Lit)
Expand Down Expand Up @@ -1994,7 +1999,10 @@ pub(crate) mod parsing {

#[cfg(not(feature = "full"))]
fn atom_expr(input: ParseStream) -> Result<Expr> {
if input.peek(token::Group) {
if input.peek(token::Group) && {
let group = crate::group::parse_group(&input.fork())?;
!(group.content.peek(Lifetime) && group.content.peek2(End))
} {
expr_group(input)
} else if input.peek(Lit) {
input.parse().map(Expr::Lit)
Expand Down
35 changes: 34 additions & 1 deletion tests/test_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod snapshot;

mod debug;

use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::{quote, ToTokens as _};
use syn::parse::Parser as _;
use syn::{Block, Stmt};
Expand Down Expand Up @@ -343,3 +343,36 @@ fn test_early_parse_loop() {
]
"#);
}

// Regression test for https://github.com/dtolnay/syn/issues/2081
#[test]
fn test_interpolated_lifetime_at_statement_start() {
// «∅ 'a ∅» : loop {}
let tokens = TokenStream::from_iter([
TokenTree::Group(Group::new(
Delimiter::None,
TokenStream::from_iter([
TokenTree::Punct(Punct::new('\'', Spacing::Joint)),
TokenTree::Ident(Ident::new("a", Span::call_site())),
]),
)),
TokenTree::Punct(Punct::new(':', Spacing::Joint)),
TokenTree::Ident(Ident::new("loop", Span::call_site())),
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
]);
snapshot!(tokens as Stmt, @r#"
Stmt::Expr(
Expr::Loop {
label: Some(Label {
name: Lifetime {
ident: "a",
},
}),
body: Block {
stmts: [],
},
},
None,
)
"#);
}