Open
Description
As seen on StackOverflow.
Consider the following code:
use std::ops::Deref;
fn main() {
fn save(who: &str) {
println!("I'll save you, {}!", who);
}
struct Madoka;
impl Deref for Madoka {
type Target = str;
fn deref(&self) -> &Self::Target {
"Madoka"
}
}
save(&{ Madoka });
fn reset(how: &u32) {
println!("Reset {} times", how);
}
struct Homura;
impl Deref for Homura {
type Target = u32;
fn deref(&self) -> &Self::Target {
&42
}
}
// Uncomment this to get a type error
// reset(&{ Homura });
}
The following expressions do compile:
reset(&Homura)
reset(&&{ Homura })
reset({ &Homura })
But this doesn't:
reset(&{ Homura })
error[E0308]: mismatched types
--> src/main.rs:17:14
|
17 | reset(&{ Homura });
| ^^^^^^ expected u32, found struct `main::Homura`
|
= note: expected type `u32`
found type `main::Homura`
Is there are particular reason for this discrepancy?
I found two similar issues #26978 and #32122 but they don't seem to cover this particular case.