Skip to content

Commit 29e59cf

Browse files
committed
mlua_derive: Fix FromLua derive for where-clauses with a trailing comma
1 parent 85d707d commit 29e59cf

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

mlua_derive/src/from_lua.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::{DeriveInput, parse_macro_input};
3+
use syn::{DeriveInput, parse_macro_input, parse_quote};
44

55
pub fn from_lua(input: TokenStream) -> TokenStream {
6-
let DeriveInput { ident, generics, .. } = parse_macro_input!(input as DeriveInput);
6+
let DeriveInput {
7+
ident, mut generics, ..
8+
} = parse_macro_input!(input as DeriveInput);
79

810
let ident_str = ident.to_string();
9-
let (impl_generics, ty_generics, _) = generics.split_for_impl();
10-
let where_clause = match &generics.where_clause {
11-
Some(where_clause) => quote! { #where_clause, Self: 'static + Clone },
12-
None => quote! { where Self: 'static + Clone },
13-
};
11+
generics
12+
.make_where_clause()
13+
.predicates
14+
.push(parse_quote!(Self: 'static + Clone));
15+
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
1416

1517
quote! {
1618
impl #impl_generics ::mlua::FromLua for #ident #ty_generics #where_clause {

tests/userdata.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,11 @@ fn test_userdata_derive() -> Result<()> {
986986

987987
// More complex struct where generics and where clause
988988

989+
#[rustfmt::skip]
989990
#[derive(Clone, Copy, mlua::FromLua)]
990991
struct MyUserData2<'a, T: ?Sized>(&'a T)
991992
where
992-
T: Copy;
993+
T: Copy,; // trailing comma is needed for testing
993994

994995
lua.register_userdata_type::<MyUserData2<'static, i32>>(|reg| {
995996
reg.add_function("val", |_, this: MyUserData2<'static, i32>| Ok(*this.0));

0 commit comments

Comments
 (0)