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

Allow trailing commas in type definitions #101

Merged
merged 2 commits into from
Jan 14, 2025
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
16 changes: 15 additions & 1 deletion src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
use proc_macro2::{Span as Span2, TokenStream as TokenStream2, TokenTree as TokenTree2};
use quote::{ToTokens, TokenStreamExt};
use syn::{
punctuated::Punctuated, spanned::Spanned, Attribute, Error, FnArg, GenericParam, Ident,
Expand Down Expand Up @@ -246,6 +246,20 @@ fn gen_header(
.skip(1) // the opening `<`
.collect::<Vec<_>>();
tts.pop(); // the closing `>`

// Pop a trailing comma if there is one
// We'll end up conditionally putting one back in shortly
if tts.last().and_then(|tt| {
if let TokenTree2::Punct(p) = tt {
Some(p.as_char())
} else {
None
}
}) == Some(',')
{
tts.pop();
}

params.append_all(&tts);

// Append proxy type parameter (if there aren't any parameters so far,
Expand Down
7 changes: 5 additions & 2 deletions tests/compile-fail/super_trait_not_implemented.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
error[E0277]: the trait bound `Box<Dog>: Supi` is not satisfied
error[E0277]: the trait bound `Box<Dog>: Foo` is not satisfied
--> tests/compile-fail/super_trait_not_implemented.rs:18:18
|
18 | requires_foo(Box::new(Dog)); // shouldn't, because `Box<Dog>: Supi` is not satisfied
| ------------ ^^^^^^^^^^^^^ the trait `Supi` is not implemented for `Box<Dog>`
| |
| required by a bound introduced by this call
|
= help: the trait `Supi` is implemented for `Dog`
note: required for `Box<Dog>` to implement `Foo`
--> tests/compile-fail/super_trait_not_implemented.rs:5:1
|
Expand All @@ -20,3 +19,7 @@ note: required by a bound in `requires_foo`
14 | fn requires_foo<T: Foo>(_: T) {}
| ^^^ required by this bound in `requires_foo`
= note: this error originates in the attribute macro `auto_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider dereferencing here
|
18 | requires_foo(*Box::new(Dog)); // shouldn't, because `Box<Dog>: Supi` is not satisfied
| +
6 changes: 6 additions & 0 deletions tests/compile-pass/trailing_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use auto_impl::auto_impl;

#[auto_impl(&)]
trait MyTrait<T,> {}

fn main() { }
Loading