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
33 changes: 32 additions & 1 deletion impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu

fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStream> {
let mut tokens = Vec::new();
let mut turbofish = 0usize;
while !input.is_empty() {
if input.peek(token::Group) {
let group: TokenTree = input.parse()?;
Expand Down Expand Up @@ -247,6 +248,18 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
}
}

let closes_turbofish = if input.peek(Token![<]) {
if turbofish > 0 || follows_path_sep(&tokens) {
turbofish += 1;
}
false
} else if turbofish > 0 && input.peek(Token![>]) && !follows_hyphen(&tokens) {
turbofish -= 1;
true
} else {
false
};

begin_expr = input.peek(Token![break])
|| input.peek(Token![continue])
|| input.peek(Token![if])
Expand All @@ -262,7 +275,7 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
|| input.peek(Token![,])
|| input.peek(Token![/])
|| input.peek(Token![=])
|| input.peek(Token![>])
|| input.peek(Token![>]) && !closes_turbofish
|| input.peek(Token![<])
|| input.peek(Token![|])
|| input.peek(Token![%])
Expand Down Expand Up @@ -299,6 +312,24 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
Ok(TokenStream::from_iter(tokens))
}

fn follows_path_sep(tokens: &[TokenTree]) -> bool {
matches!(
tokens.last_chunk(),
Some([TokenTree::Punct(p), TokenTree::Punct(q)])
if p.spacing() == Spacing::Joint
&& p.as_char() == ':'
&& q.as_char() == ':',
)
}

fn follows_hyphen(tokens: &[TokenTree]) -> bool {
matches!(
tokens.last(),
Some(TokenTree::Punct(p))
if p.spacing() == Spacing::Joint && p.as_char() == '-',
)
}

impl ToTokens for Display<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if self.infinite_recursive {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,18 @@ fn test_assoc_type_equality_constraint() {
},
);
}

// Regression test for https://github.com/dtolnay/thiserror/issues/332
#[test]
fn test_turbofish() {
#[derive(Error, Debug)]
#[error(
"{} {} {}",
None::<i32>.is_some(),
Vec::<Vec<u8>>::new().len(),
(0..3).filter(|_| Option::<Box<dyn Fn(u8) -> u8>>::None.is_none()).count(),
)]
pub struct Error;

assert("false 0 3", Error);
}
Loading