-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
This is arguably quite confusing:
trait Trait {
fn abs(self) -> Self;
}
impl Trait for i64 {
fn abs(self) -> Self {
2 * self
}
}
fn main() {
let x = 42;
println!("{}", x.abs());
println!("{}", x.abs());
}
Ouput:
84
42
So the first x.abs()
call resolves to the trait method, which is then used to influence type inference to make x
an i64
. With x
being i64
, the second x.abs()
call calls the inherent method.
@rustbot label T-compiler, T-lang, A-traits, A-typesystem
dtolnaym-ou-se, drewtato, RiESAEX, Scripter17, OverHash and 3 more
Metadata
Metadata
Assignees
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
{integer}
” #72498eddyb commentedon Jul 18, 2022
One thing that fell through the tracks was one of my suggestions (to @workingjubilee) regarding a potential complete replacement for inherent impls on builtin types, that still has a lot of the same user-facing properties (i.e. those methods take precedence over methods defined by traits instead of being ambiguous with them).
The advantage is that if
iN::abs
came from a trait, we can represent<_ as core::num::IntMethods>::abs
before inferring which exact type it is, whereas inherentimpl
s require eagerly picking one.(original motivation was making
2.0.sqrt()
work, though42.abs()
is basically the same thing)workingjubilee commentedon Sep 30, 2022
Hello!
It didn't quite fall through the tracks so much as I wound up kinda busy at an inopportune time. I hope to take a closer look at this soon. 👀
ikey4u commentedon Feb 23, 2024
Dump the source into LLVM IR using command
rustc --emit=asm,llvm-bc,llvm-ir src/main.rs
, we got following codeAnd it calls
_ZN4main4main17hf586f8163becbb21E
which is:To be simple, it does two things:
Other observations:
Case 1:
This snippet outputs
i32
.Case 2:
This snippet does not compile:
Case 3:
This snippet outputs
2 1
.This behavior really surprises me, and is there any progress on this?
Auto merge of rust-lang#128013 - compiler-errors:inherent-numerical, …
{integer}
inference + method resolution #121453jieyouxu commentedon Aug 22, 2024
See also discussions in #121453.
unstable_name_collisions
#48919