dtolnay/rust-quiz

Method resolution with numeric literals can be non-idempotent

Closed this issue · 1 comments

y21 commented

From https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/Method.20resolution.20non-idempotence/near/422734004

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.

y21 commented

Oh I'm late, this was already attempted in #81