Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token
);
}

// Function traits cannot support generics in their arguments
// These would require HRTB for types instead of just lifetimes
for type_param in sig.generics.type_params() {
emit_error!(
type_param.span(),
"the trait '{}' cannot be implemented for Fn-traits: generic arguments are not allowed",
trait_def.ident,
);
}

for const_param in sig.generics.const_params() {
emit_error!(
const_param.span(),
"the trait '{}' cannot be implemented for Fn-traits: constant arguments are not allowed",
trait_def.ident,
);
}

// =======================================================================
// Check if the trait can be implemented for the given proxy type
let self_type = SelfType::from_sig(sig);
Expand Down
10 changes: 10 additions & 0 deletions tests/compile-fail/fn_const_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use auto_impl::auto_impl;


#[auto_impl(Fn)]
trait Greeter {
fn greet<const N: usize>(&self, id: usize);
}


fn main() {}
5 changes: 5 additions & 0 deletions tests/compile-fail/fn_const_generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: the trait 'Greeter' cannot be implemented for Fn-traits: constant arguments are not allowed
--> tests/compile-fail/fn_const_generics.rs:6:14
|
6 | fn greet<const N: usize>(&self, id: usize);
| ^^^^^
11 changes: 11 additions & 0 deletions tests/compile-fail/fn_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::fmt::Display;
use auto_impl::auto_impl;


#[auto_impl(Fn)]
trait Greeter {
fn greet<T: Display>(&self, name: T);
}


fn main() {}
5 changes: 5 additions & 0 deletions tests/compile-fail/fn_generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: the trait 'Greeter' cannot be implemented for Fn-traits: generic arguments are not allowed
--> tests/compile-fail/fn_generics.rs:7:14
|
7 | fn greet<T: Display>(&self, name: T);
| ^