Skip to content

Commit

Permalink
Merge pull request #952 from dtolnay/supertraits
Browse files Browse the repository at this point in the history
Parse empty supertrait list
  • Loading branch information
dtolnay authored Jan 6, 2021
2 parents 3414ed6 + d8c39a8 commit 43d89ae
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2032,14 +2032,14 @@ pub mod parsing {
let mut supertraits = Punctuated::new();
if colon_token.is_some() {
loop {
supertraits.push_value(input.parse()?);
if input.peek(Token![where]) || input.peek(token::Brace) {
break;
}
supertraits.push_punct(input.parse()?);
supertraits.push_value(input.parse()?);
if input.peek(Token![where]) || input.peek(token::Brace) {
break;
}
supertraits.push_punct(input.parse()?);
}
}

Expand Down
84 changes: 83 additions & 1 deletion tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod macros;
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
use quote::quote;
use std::iter::FromIterator;
use syn::Item;
use syn::{Item, ItemTrait};

#[test]
fn test_macro_variable_attr() {
Expand Down Expand Up @@ -159,3 +159,85 @@ fn test_macro_variable_impl() {
}
"###);
}

#[test]
fn test_supertraits() {
// Rustc parses all of the following.

#[rustfmt::skip]
let tokens = quote!(trait Trait where {});
snapshot!(tokens as ItemTrait, @r###"
ItemTrait {
vis: Inherited,
ident: "Trait",
generics: Generics {
where_clause: Some(WhereClause),
},
}
"###);

#[rustfmt::skip]
let tokens = quote!(trait Trait: where {});
snapshot!(tokens as ItemTrait, @r###"
ItemTrait {
vis: Inherited,
ident: "Trait",
generics: Generics {
where_clause: Some(WhereClause),
},
colon_token: Some,
}
"###);

#[rustfmt::skip]
let tokens = quote!(trait Trait: Sized where {});
snapshot!(tokens as ItemTrait, @r###"
ItemTrait {
vis: Inherited,
ident: "Trait",
generics: Generics {
where_clause: Some(WhereClause),
},
colon_token: Some,
supertraits: [
Trait(TraitBound {
modifier: None,
path: Path {
segments: [
PathSegment {
ident: "Sized",
arguments: None,
},
],
},
}),
],
}
"###);

#[rustfmt::skip]
let tokens = quote!(trait Trait: Sized + where {});
snapshot!(tokens as ItemTrait, @r###"
ItemTrait {
vis: Inherited,
ident: "Trait",
generics: Generics {
where_clause: Some(WhereClause),
},
colon_token: Some,
supertraits: [
Trait(TraitBound {
modifier: None,
path: Path {
segments: [
PathSegment {
ident: "Sized",
arguments: None,
},
],
},
}),
],
}
"###);
}

0 comments on commit 43d89ae

Please sign in to comment.