Skip to content

Commit

Permalink
Handle unused lifetime parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Mar 29, 2022
1 parent d179954 commit 71389d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
34 changes: 32 additions & 2 deletions examples/tests/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod receiver {

impl From<&X> for Y {
fn from(_: &X) -> Self {
Y
Self
}
}

Expand Down Expand Up @@ -66,6 +66,36 @@ mod receiver {
}
}

mod lifetime {
use serde::{Deserialize, Serialize};

#[derive(Clone)]
struct X<'a>(&'a bool);

#[derive(Clone, Deserialize, Serialize)]
struct Y(bool);

impl<'a> From<X<'a>> for Y {
fn from(x: X) -> Self {
Self(*x.0)
}
}

impl test_fuzz::Into<X<'static>> for Y {
fn into(self) -> X<'static> {
X(Box::leak(Box::new(self.0)))
}
}

#[test_fuzz::test_fuzz(convert = "X<'a>, Y")]
fn target<'a>(x: X<'a>) {}

#[test]
fn test() {
target(X(&false));
}
}

#[cfg(feature = "__inapplicable_conversion")]
mod inapplicable_conversion {
use serde::{Deserialize, Serialize};
Expand All @@ -81,7 +111,7 @@ mod inapplicable_conversion {

impl From<Y> for Z {
fn from(_: Y) -> Self {
Z
Self
}
}

Expand Down
16 changes: 9 additions & 7 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use subprocess::{Exec, Redirection};
use syn::{
parse::Parser, parse_macro_input, parse_quote, parse_str, punctuated::Punctuated, token,
Attribute, AttributeArgs, Block, Expr, FnArg, GenericArgument, GenericParam, Generics, Ident,
ImplItem, ImplItemMethod, ItemFn, ItemImpl, ItemMod, PatType, Path, PathArguments, PathSegment,
Receiver, ReturnType, Signature, Stmt, Type, TypeParam, TypePath, TypeReference, TypeSlice,
Visibility, WhereClause, WherePredicate,
ImplItem, ImplItemMethod, ItemFn, ItemImpl, ItemMod, LifetimeDef, PatType, Path, PathArguments,
PathSegment, Receiver, ReturnType, Signature, Stmt, Type, TypeParam, TypePath, TypeReference,
TypeSlice, Visibility, WhereClause, WherePredicate,
};
use toolchain_find::find_installed_component;
use unzip_n::unzip_n;
Expand Down Expand Up @@ -1008,12 +1008,14 @@ fn type_generic_phantom_types(generics: &Generics) -> Vec<Type> {
generics
.params
.iter()
.filter_map(|param| {
if let GenericParam::Type(TypeParam { ident, .. }) = param {
.filter_map(|param| match param {
GenericParam::Type(TypeParam { ident, .. }) => {
Some(parse_quote! { std::marker::PhantomData< #ident > })
} else {
None
}
GenericParam::Lifetime(LifetimeDef { lifetime, .. }) => {
Some(parse_quote! { std::marker::PhantomData< & #lifetime () > })
}
GenericParam::Const(_) => None,
})
.collect()
}
Expand Down

0 comments on commit 71389d8

Please sign in to comment.