forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#98028 - aticu:master, r=estebank
Add E0789 as more specific variant of E0283 Fixes rust-lang#81701 I think this should be good to go, there are only two things where I am somewhat unsure: - Is there a better way to get the fully-qualified path for the suggestion? I tried `self.tcx.def_path_str`, but that didn't seem to always give a correct path for the context. - Should all this be extracted into it's own method or is it fine where it is? r? `@estebank`
- Loading branch information
Showing
21 changed files
with
350 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,E0789 | ||
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,21 @@ | ||
error[E0283]: type annotations needed | ||
error[E0789]: 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[E0789]: 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 E0789`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
src/test/ui/associated-types/associated-types-unconstrained.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
error[E0283]: type annotations needed | ||
error[E0789]: 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 E0789`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.