-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 suggestion to diagnostic when user has array but trait wants slice. #91314
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
// Issue #90528: provide helpful suggestions when a trait bound is unsatisfied | ||
// due to a missed unsizing coercion. | ||
// | ||
// This test exercises array literals and a trait implemented on immutable slices. | ||
|
||
trait Read {} | ||
|
||
impl Read for &[u8] {} | ||
|
||
fn wants_read(_: impl Read) {} | ||
|
||
fn main() { | ||
wants_read([0u8]); | ||
//~^ ERROR the trait bound `[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &[0u8][..] | ||
wants_read(&[0u8]); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &[0u8][..] | ||
wants_read(&[0u8][..]); | ||
|
||
wants_read(&mut [0u8]); | ||
//~^ ERROR the trait bound `&mut [u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
} |
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,55 @@ | ||
error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:13:16 | ||
| | ||
LL | wants_read([0u8]); | ||
| ---------- ^^^^^ | ||
| | | | ||
| | the trait `Read` is not implemented for `[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&[0u8][..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:18:16 | ||
| | ||
LL | wants_read(&[0u8]); | ||
| ---------- ^^^^^^ | ||
| | | | ||
| | the trait `Read` is not implemented for `&[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&[0u8][..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `&mut [u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:25:16 | ||
| | ||
LL | wants_read(&mut [0u8]); | ||
| ---------- ^^^^^^^^^^ the trait `Read` is not implemented for `&mut [u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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,42 @@ | ||
// Issue #90528: provide helpful suggestions when a trait bound is unsatisfied | ||
// due to a missed unsizing coercion. | ||
// | ||
// This test exercises array variables and a trait implemented on immmutable slices. | ||
|
||
trait Read {} | ||
|
||
impl Read for &[u8] {} | ||
|
||
fn wants_read(_: impl Read) {} | ||
|
||
fn main() { | ||
let x = [0u8]; | ||
wants_read(x); | ||
//~^ ERROR the trait bound `[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &x[..] | ||
wants_read(&x); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &x[..] | ||
wants_read(&x[..]); | ||
|
||
let x = &[0u8]; | ||
wants_read(x); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &x[..] | ||
wants_read(&x); | ||
//~^ ERROR the trait bound `&&[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
wants_read(*x); | ||
//~^ ERROR the trait bound `[u8; 1]: Read` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &*x[..] | ||
// ^^^^^^^ bad suggestion | ||
wants_read(&x[..]); | ||
} |
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,91 @@ | ||
error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:14:16 | ||
| | ||
LL | wants_read(x); | ||
| ---------- ^ | ||
| | | | ||
| | the trait `Read` is not implemented for `[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&x[..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:19:16 | ||
| | ||
LL | wants_read(&x); | ||
| ---------- ^^ | ||
| | | | ||
| | the trait `Read` is not implemented for `&[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&x[..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:27:16 | ||
| | ||
LL | wants_read(x); | ||
| ---------- ^ | ||
| | | | ||
| | the trait `Read` is not implemented for `&[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&x[..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `&&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:32:16 | ||
| | ||
LL | wants_read(&x); | ||
| ---------- ^^ the trait `Read` is not implemented for `&&[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:35:16 | ||
| | ||
LL | wants_read(*x); | ||
| ---------- ^^ | ||
| | | | ||
| | the trait `Read` is not implemented for `[u8; 1]` | ||
| | help: convert the array to a `&[u8]` slice instead: `&*x[..]` | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following implementations were found: | ||
<&[u8] as Read> | ||
note: required by a bound in `wants_read` | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:10:23 | ||
| | ||
LL | fn wants_read(_: impl Read) {} | ||
| ^^^^ required by this bound in `wants_read` | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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,33 @@ | ||
// Issue #90528: provide helpful suggestions when a trait bound is unsatisfied | ||
// due to a missed unsizing coercion. | ||
// | ||
// This test exercises array literals and a trait implemented on mutable slices. | ||
|
||
trait Write {} | ||
|
||
impl Write for & mut [u8] {} | ||
|
||
fn wants_write(_: impl Write) {} | ||
|
||
fn main() { | ||
wants_write([0u8]); | ||
//~^ ERROR the trait bound `[u8; 1]: Write` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &mut [0u8][..] | ||
wants_write(&mut [0u8]); | ||
//~^ ERROR the trait bound `&mut [u8; 1]: Write` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP convert the array | ||
//~| SUGGESTION &mut [0u8][..] | ||
wants_write(&mut [0u8][..]); | ||
|
||
wants_write(&[0u8]); | ||
//~^ ERROR the trait bound `&[u8; 1]: Write` is not satisfied | ||
//~| HELP the following implementations were found | ||
|
||
wants_write(&[0u8][..]); | ||
//~^ ERROR the trait bound `&[u8]: Write` is not satisfied | ||
//~| HELP the following implementations were found | ||
//~| HELP consider changing this borrow's mutability | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: you could probably collapse these tests down into fewer files - perhaps cases where a suggestion is made and where a suggestion isn't made - and then you can add
// run-rustfix
to the test to confirm the suggestion works.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.
Sure. What should I do about the scenarios where the suggestion is wrong (see anywhere I put
bad suggestion
)?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.
We could split these out into another test, as long as the suggestion is
MaybeIncorrect
then it's fine that it's sometimes wrong, it'll just need to be in a different test if we want// run-rustfix
to work on the cases that do have correct suggestions.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'm still new to rustfix. Will it apply MaybeIncorrect changes if the test is annotated with
// run-rustfix
? As written, my code does not know when its suggestion is MachineApplicable and when it is MaybeIncorrect, so I just always give MaybeIncorrect.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 actually can't remember, if it isn't doing it when you're trying this locally then we can just approve this - apologies for the runaround.
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 think that in tests
MaybeIncorrect
ones do get applied. If they don't what we normally do is separate all the cases that can be fixed into its own file and another for the rest.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.
@estebank looks like you're right: https://rustc-dev-guide.rust-lang.org/tests/adding.html#other-header-commands