-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Closed as not planned
Labels
C-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.
Description
I tried this code:
use std::marker::PhantomData;
struct DefaultU;
struct MockType<T, U = DefaultU> {
data: T,
_phantom: PhantomData<U>,
}
impl<T, U> MockType<T, U> {
fn new(value: T) -> Self {
Self {
data: value,
_phantom: PhantomData,
}
}
}
fn main() {
// Compiles
MockType::<()>::new(());
// Does not compile
MockType::new(());
}See on Compiler Explorer.
In main, I try to create a MockType and the compiler is acting inconsistently.
As I am not adding any information by specifiying ::<()>, I would expect it to either be able to infer that T = () in the second case or unable to default to DefaultU in the first.
Meta
rustc --version --verbose:
rustc 1.89.0-nightly (d97326eab 2025-05-15)
binary: rustc
commit-hash: d97326eabfc3b2c33abcb08d6bc117aefa697cb7
commit-date: 2025-05-15
host: x86_64-pc-windows-gnu
release: 1.89.0-nightly
LLVM version: 20.1.4
but it also happens on stable (1.87.0)
Backtrace
error[E0282]: type annotations needed
--> <source>:24:5
|
24 | MockType::new(());
| ^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the struct `MockType`
|
help: consider specifying the generic arguments
|
24 | MockType::<(), U>::new(());
| +++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
Compiler returned: 1
Metadata
Metadata
Assignees
Labels
C-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.