-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 #123523 - estebank:issue-123414, r=BoxyUwU
Account for trait/impl difference when suggesting changing argument from ref to mut ref Do not ICE when encountering a lifetime error involving an argument with an immutable reference of a method that differs from the trait definition. Fix #123414.
- Loading branch information
Showing
7 changed files
with
158 additions
and
43 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,25 @@ | ||
// Issue https://github.com/rust-lang/rust/issues/123414 | ||
trait MemoryUnit { | ||
extern "C" fn read_word(&mut self) -> u8; | ||
extern "C" fn read_dword(Self::Assoc<'_>) -> u16; | ||
//~^ WARNING anonymous parameters are deprecated and will be removed in the next edition | ||
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! | ||
//~| ERROR associated type `Assoc` not found for `Self` | ||
} | ||
|
||
struct ROM {} | ||
|
||
impl MemoryUnit for ROM { | ||
//~^ ERROR not all trait items implemented, missing: `read_word` | ||
extern "C" fn read_dword(&'_ self) -> u16 { | ||
//~^ ERROR method `read_dword` has a `&self` declaration in the impl, but not in the trait | ||
let a16 = self.read_word() as u16; | ||
//~^ ERROR cannot borrow `*self` as mutable, as it is behind a `&` reference | ||
let b16 = self.read_word() as u16; | ||
//~^ ERROR cannot borrow `*self` as mutable, as it is behind a `&` reference | ||
|
||
(b16 << 8) | a16 | ||
} | ||
} | ||
|
||
pub fn main() {} |
60 changes: 60 additions & 0 deletions
60
tests/ui/borrowck/trait-impl-argument-difference-ice.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
warning: anonymous parameters are deprecated and will be removed in the next edition | ||
--> $DIR/trait-impl-argument-difference-ice.rs:4:30 | ||
| | ||
LL | extern "C" fn read_dword(Self::Assoc<'_>) -> u16; | ||
| ^^^^^^^^^^^^^^^ help: try naming the parameter or explicitly ignoring it: `_: Self::Assoc<'_>` | ||
| | ||
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! | ||
= note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686> | ||
= note: `#[warn(anonymous_parameters)]` on by default | ||
|
||
error[E0220]: associated type `Assoc` not found for `Self` | ||
--> $DIR/trait-impl-argument-difference-ice.rs:4:36 | ||
| | ||
LL | extern "C" fn read_dword(Self::Assoc<'_>) -> u16; | ||
| ^^^^^ associated type `Assoc` not found | ||
|
||
error[E0185]: method `read_dword` has a `&self` declaration in the impl, but not in the trait | ||
--> $DIR/trait-impl-argument-difference-ice.rs:14:5 | ||
| | ||
LL | extern "C" fn read_dword(Self::Assoc<'_>) -> u16; | ||
| ------------------------------------------------- trait method declared without `&self` | ||
... | ||
LL | extern "C" fn read_dword(&'_ self) -> u16 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&self` used in impl | ||
|
||
error[E0046]: not all trait items implemented, missing: `read_word` | ||
--> $DIR/trait-impl-argument-difference-ice.rs:12:1 | ||
| | ||
LL | extern "C" fn read_word(&mut self) -> u8; | ||
| ----------------------------------------- `read_word` from trait | ||
... | ||
LL | impl MemoryUnit for ROM { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ missing `read_word` in implementation | ||
|
||
error[E0596]: cannot borrow `*self` as mutable, as it is behind a `&` reference | ||
--> $DIR/trait-impl-argument-difference-ice.rs:16:19 | ||
| | ||
LL | let a16 = self.read_word() as u16; | ||
| ^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition | ||
| | ||
LL | extern "C" fn read_dword(&'_ mut self) -> u16 { | ||
| ~~~~~~~~~~~~ | ||
|
||
error[E0596]: cannot borrow `*self` as mutable, as it is behind a `&` reference | ||
--> $DIR/trait-impl-argument-difference-ice.rs:18:19 | ||
| | ||
LL | let b16 = self.read_word() as u16; | ||
| ^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | ||
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition | ||
| | ||
LL | extern "C" fn read_dword(&'_ mut self) -> u16 { | ||
| ~~~~~~~~~~~~ | ||
|
||
error: aborting due to 5 previous errors; 1 warning emitted | ||
|
||
Some errors have detailed explanations: E0046, E0185, E0220, E0596. | ||
For more information about an error, try `rustc --explain E0046`. |
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