Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse macro metavariable in leading component of path #832

Merged
merged 2 commits into from
May 26, 2020
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
4 changes: 2 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ pub(crate) mod parsing {
// <atom> ? ...
#[cfg(feature = "full")]
fn trailer_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
if input.peek(token::Group) {
if input.peek(token::Group) && !input.peek2(Token![::]) {
return input.call(expr_group).map(Expr::Group);
}

Expand Down Expand Up @@ -1695,7 +1695,7 @@ 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) && !input.peek2(Token![::]) {
input.call(expr_group).map(Expr::Group)
} else if input.peek(Lit) {
input.parse().map(Expr::Lit)
Expand Down
2 changes: 1 addition & 1 deletion src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub mod parsing {
}

fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> {
if input.peek(token::Group) {
if input.peek(token::Group) && !input.peek2(Token![::]) {
return input.parse().map(Type::Group);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/test_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;
use syn::{Expr, Type};

#[test]
fn parse_interpolated_leading_component() {
// mimics the token stream corresponding to `$mod::rest`
let tokens = TokenStream::from_iter(vec![
TokenTree::Group(Group::new(
Delimiter::None,
TokenStream::from_iter(vec![TokenTree::Ident(Ident::new(
"first",
Span::call_site(),
))]),
)),
TokenTree::Punct(Punct::new(':', Spacing::Joint)),
TokenTree::Punct(Punct::new(':', Spacing::Alone)),
TokenTree::Ident(Ident::new("rest", Span::call_site())),
]);

match syn::parse2::<Expr>(tokens.clone()) {
Ok(Expr::Path(_)) => {}
expr => panic!("incorrect expr: {:?}", expr),
}

match syn::parse2::<Type>(tokens) {
Ok(Type::Path(_)) => {}
expr => panic!("incorrect expr: {:?}", expr),
}
}