Method resolution with numeric literals can be non-idempotent
Closed this issue · 1 comments
y21 commented
trait Trait {
fn abs(self) -> Self;
}
impl Trait for i64 {
fn abs(self) -> Self {
2 * self
}
}
fn main() {
let x = 42;
println!("{} {}", x.abs(), x.abs()); // 84 42
}
The zulip link has an explanation for what's going on there.
The first abs
call can't lookup inherent methods because no concrete integer type is known at that point, falling back to using the only abs
left in scope from Trait
, which constrains the type of 42
to i64
.
The second abs
call can then properly find and use the inherent method.