From d7177c392f79ae80844f3430f2ee98eeba7f1da3 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Mon, 12 Aug 2024 18:26:23 -0700 Subject: [PATCH] Fix handling of supertraits, fix #21 --- src/lib.rs | 9 +++++---- tests/pass/14-supertrait.rs | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 tests/pass/14-supertrait.rs diff --git a/src/lib.rs b/src/lib.rs index 169ce7f..8fc771b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,13 +148,11 @@ pub fn sealed(args: TokenStream, input: TokenStream) -> TokenStream { fn parse_sealed_trait(mut item_trait: syn::ItemTrait, args: TraitArguments) -> TokenStream2 { let trait_ident = &item_trait.ident.unraw(); let trait_generics = &item_trait.generics; + let trait_supertraits = &item_trait.supertraits; let seal = seal_name(trait_ident); let vis = &args.visibility; let (_, ty_generics, where_clause) = trait_generics.split_for_impl(); - item_trait - .supertraits - .push(parse_quote!( #seal::Sealed #ty_generics )); let mod_code = if args.erased { let lifetimes = trait_generics.lifetimes(); @@ -174,10 +172,13 @@ fn parse_sealed_trait(mut item_trait: syn::ItemTrait, args: TraitArguments) -> T // to supertraits in the middle). So we output them separately. quote! { use super::*; - pub trait Sealed #trait_generics #where_clause {} + pub trait Sealed #trait_generics : #trait_supertraits #where_clause {} } }; + item_trait + .supertraits + .push(parse_quote!( #seal::Sealed #ty_generics )); quote! { #[automatically_derived] #vis mod #seal { diff --git a/tests/pass/14-supertrait.rs b/tests/pass/14-supertrait.rs new file mode 100644 index 0000000..c2e614e --- /dev/null +++ b/tests/pass/14-supertrait.rs @@ -0,0 +1,10 @@ +use sealed::sealed; + +#[sealed] +pub trait CopyIterator: Iterator +where + Self::Item: Copy, +{ +} + +fn main() {}