Skip to content

Commit

Permalink
Merge pull request #101 from KodrAus/fix/trailing-commas
Browse files Browse the repository at this point in the history
Allow trailing commas in type definitions
  • Loading branch information
KodrAus authored Jan 14, 2025
2 parents 15d8337 + e4a14fd commit 6675a84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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() { }

0 comments on commit 6675a84

Please sign in to comment.