Consdering the following example code (playground):
use std::marker::PhantomData;
struct Data<T>(PhantomData<T>);
pub trait Marker {}
struct Inner;
impl Marker for Inner {}
trait Id {
type Id;
}
impl<T> Id for T {
type Id = T;
}
fn create<T: Marker, U: Id<Id = T> + Marker>() -> Data<T> {
Data(PhantomData)
}
fn create2<T: Marker, U: Id<Id = T> + Marker>() -> Data<U> {
Data(PhantomData)
}
fn main() {
let mut data = create::<_, Inner>();
data = create();
data = create2();
}
It generates one error:
error[E0282]: type annotations needed
--> src/main.rs:24:12
|
24 | data = create();
| ^^^^^^ cannot infer type for `U`
I'm trying to understand two things:
- why the call to
create()
is erroneous, while in the call tocreate2()
all parameters are inferred successfully? - is this behaviour stable? This main point is that the error is desirable - I don't want this function to be callable without turbofish (since in real project it is a part of macro-generated code, not usable directly).