Skip to content

Commit 85b572c

Browse files
committed
Support impl Trait in return type
1 parent aff365f commit 85b572c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ proc-macro = true
1818
[dependencies]
1919
proc-macro2 = "1.0.74"
2020
quote = "1.0.35"
21-
syn = { version = "2.0.46", default-features = false, features = ["full", "parsing", "printing", "proc-macro", "visit-mut"] }
21+
syn = { version = "2.0.46", default-features = false, features = ["clone-impls", "full", "parsing", "printing", "proc-macro", "visit-mut"] }
2222

2323
[dev-dependencies]
2424
futures = "0.3.30"

src/expand.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syn::visit_mut::{self, VisitMut};
1212
use syn::{
1313
parse_quote, parse_quote_spanned, Attribute, Block, FnArg, GenericArgument, GenericParam,
1414
Generics, Ident, ImplItem, Lifetime, LifetimeParam, Pat, PatIdent, PathArguments, Receiver,
15-
ReturnType, Signature, Token, TraitItem, Type, TypePath, WhereClause,
15+
ReturnType, Signature, Token, TraitItem, Type, TypeInfer, TypePath, WhereClause,
1616
};
1717

1818
impl ToTokens for Item {
@@ -410,6 +410,8 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
410410
quote!(#(#decls)* { #(#stmts)* })
411411
}
412412
} else {
413+
let mut ret = ret.clone();
414+
replace_impl_trait_with_infer(&mut ret);
413415
quote! {
414416
if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> {
415417
#[allow(unreachable_code)]
@@ -475,3 +477,20 @@ fn where_clause_or_default(clause: &mut Option<WhereClause>) -> &mut WhereClause
475477
predicates: Punctuated::new(),
476478
})
477479
}
480+
481+
fn replace_impl_trait_with_infer(ty: &mut Type) {
482+
struct ReplaceImplTraitWithInfer;
483+
484+
impl VisitMut for ReplaceImplTraitWithInfer {
485+
fn visit_type_mut(&mut self, ty: &mut Type) {
486+
if let Type::ImplTrait(impl_trait) = ty {
487+
*ty = Type::Infer(TypeInfer {
488+
underscore_token: Token![_](impl_trait.impl_token.span),
489+
});
490+
}
491+
visit_mut::visit_type_mut(self, ty);
492+
}
493+
}
494+
495+
ReplaceImplTraitWithInfer.visit_type_mut(ty);
496+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy