-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add E0790 as more specific variant of E0283 #98028
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
You need to specify a specific implementation of the trait in order to call the | ||
method. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0790 | ||
trait Generator { | ||
fn create() -> u32; | ||
} | ||
|
||
struct Impl; | ||
|
||
impl Generator for Impl { | ||
fn create() -> u32 { 1 } | ||
} | ||
|
||
struct AnotherImpl; | ||
|
||
impl Generator for AnotherImpl { | ||
fn create() -> u32 { 2 } | ||
} | ||
|
||
let cont: u32 = Generator::create(); | ||
// error, impossible to choose one of Generator trait implementation | ||
// Should it be Impl or AnotherImpl, maybe something else? | ||
``` | ||
|
||
This error can be solved by adding type annotations that provide the missing | ||
information to the compiler. In this case, the solution is to use a concrete | ||
type: | ||
|
||
``` | ||
trait Generator { | ||
fn create() -> u32; | ||
} | ||
|
||
struct AnotherImpl; | ||
|
||
impl Generator for AnotherImpl { | ||
fn create() -> u32 { 2 } | ||
} | ||
|
||
let gen1 = AnotherImpl::create(); | ||
|
||
// if there are multiple methods with same name (different traits) | ||
let gen2 = <AnotherImpl as Generator>::create(); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,21 @@ | ||
error[E0283]: type annotations needed | ||
error[E0790]: cannot refer to the associated constant on trait without specifying the corresponding `impl` type | ||
--> $DIR/issue-63496.rs:4:21 | ||
| | ||
LL | const C: usize; | ||
| --------------- `A::C` defined here | ||
LL | | ||
LL | fn f() -> ([u8; A::C], [u8; A::C]); | ||
| ^^^^ | ||
| | | ||
| cannot infer type | ||
| help: use the fully qualified path to an implementation: `<Type as A>::C` | ||
| | ||
= note: cannot satisfy `_: A` | ||
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl` | ||
| ^^^^ cannot refer to the associated constant of trait | ||
|
||
error[E0283]: type annotations needed | ||
error[E0790]: cannot refer to the associated constant on trait without specifying the corresponding `impl` type | ||
--> $DIR/issue-63496.rs:4:33 | ||
| | ||
LL | const C: usize; | ||
| --------------- `A::C` defined here | ||
LL | | ||
LL | fn f() -> ([u8; A::C], [u8; A::C]); | ||
| ^^^^ | ||
| | | ||
| cannot infer type | ||
| help: use the fully qualified path to an implementation: `<Type as A>::C` | ||
| | ||
= note: cannot satisfy `_: A` | ||
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl` | ||
| ^^^^ cannot refer to the associated constant of trait | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0283`. | ||
For more information about this error, try `rustc --explain E0790`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
error[E0283]: type annotations needed | ||
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type | ||
--> $DIR/associated-types-unconstrained.rs:14:20 | ||
| | ||
LL | fn bar() -> isize; | ||
| ------------------ `Foo::bar` defined here | ||
... | ||
LL | let x: isize = Foo::bar(); | ||
| ^^^^^^^^ cannot infer type | ||
| | ||
= note: cannot satisfy `_: Foo` | ||
| ^^^^^^^^ cannot call associated function of trait | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0283`. | ||
For more information about this error, try `rustc --explain E0790`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
error[E0283]: type annotations needed | ||
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type | ||
--> $DIR/E0283.rs:30:21 | ||
| | ||
LL | fn create() -> u32; | ||
| ------------------- `Generator::create` defined here | ||
... | ||
LL | let cont: u32 = Generator::create(); | ||
| ^^^^^^^^^^^^^^^^^ cannot infer type | ||
| ^^^^^^^^^^^^^^^^^ cannot call associated function of trait | ||
| | ||
help: use a fully-qualified path to a specific available implementation (2 found) | ||
| | ||
= note: cannot satisfy `_: Generator` | ||
LL | let cont: u32 = <::Impl as Generator>::create(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't recall exactly anymore, but if I remember correctly I tried that and got paths as a result that didn't make sense in the context where they were suggested. But I could have made a mistake then. If you'd like I could try it again when I find the time and make a follow-up PR with that or at least find out why it didn't work in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, either put up a follow up or I can also look into it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I was involved in the development of this PR) |
||
| ++++++++++ + | ||
|
||
error[E0283]: type annotations needed | ||
--> $DIR/E0283.rs:35:24 | ||
|
@@ -27,4 +33,5 @@ LL | let bar = <Impl as Into<T>>::into(foo_impl) * 1u32; | |
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0283`. | ||
Some errors have detailed explanations: E0283, E0790. | ||
For more information about an error, try `rustc --explain E0283`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could introduce a new
ObligationCauseCode
that stores the hir id of theExprKind::Path
that this obligation comes from, so we wouldn't need to use this span-based search method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a good idea.