From d530864dcc6df62711ef4b74878d9f48d47b2374 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 6 Jan 2021 05:06:30 -0800 Subject: [PATCH 1/2] Parse empty supertrait list --- src/item.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/item.rs b/src/item.rs index 883ecc799f..c5083d0501 100644 --- a/src/item.rs +++ b/src/item.rs @@ -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()?); } } From d8c39a8247bc42be35bdc27f0c4fd1f6a0817fb7 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 6 Jan 2021 05:06:38 -0800 Subject: [PATCH 2/2] Add test of empty supertrait list --- tests/test_item.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/test_item.rs b/tests/test_item.rs index c28fc87fdc..7695f19906 100644 --- a/tests/test_item.rs +++ b/tests/test_item.rs @@ -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() { @@ -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, + }, + ], + }, + }), + ], + } + "###); +}