Closed
Description
struct A<'a>(&'a ());
impl<'a> A<'a> {
fn associated(a: &'a (), b: &()) {}
}
fn main() {
let works_not: for<'a> fn(&'a (), &()) = A::associated; // Error
let works_not: for<'a, 'b> fn(&'a (), &'b ()) = A::associated; // Error
let works: for<'a> fn(&'a (), &()) = |a, b| A::associated(a, b); // Ok
let works: for<'a, 'b> fn(&'a (), &'b ()) = |a, b| A::associated(a, b); // Ok
// Free standing functions work fine
fn fun<'a>(a: &'a (), b: &()) {}
let works: for<'a> fn(&'a (), &()) = fun; // Ok
}
I expected the associated function to be coercible into a function pointer directly or for a diagnostic to tell me to wrap the function in a closure to fix the error.
Instead, I got a confusing compiler error which lead me to think that the for<'_>
was wrong, which doesn't seem to be the case.
error[E0308]: mismatched types
--> src/main.rs:8:46
|
195 | let no_works: for<'a> fn(&'a (), &()) = A::associated; // Error
| ----------------------- ^^^^^^^^^^^^^ one type is more general than the other
| |
| expected due to this
|
= note: expected fn pointer `for<'a, 'b> fn(&'a (), &'b ())`
found fn item `for<'a> fn(&(), &'a ()) {A::<'_>::associated}`
Meta
rustc --version --verbose
:
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7