-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add suggestion to diagnostic when user has array but trait wants slice.
For #90528.
- Loading branch information
Showing
10 changed files
with
490 additions
and
7 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,20 @@ | ||
// 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 | ||
wants_read(&[0u8]); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
wants_read(&[0u8][..]); | ||
wants_read(&mut [0u8]); | ||
//~^ ERROR the trait bound `&mut [u8; 1]: Read` is not satisfied | ||
} |
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,56 @@ | ||
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]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&[0u8][..]); | ||
| + ++++ | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:15:16 | ||
| | ||
LL | wants_read(&[0u8]); | ||
| ---------- ^^^^^^ the trait `Read` is not implemented for `&[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&[0u8][..]); | ||
| ++++ | ||
|
||
error[E0277]: the trait bound `&mut [u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-1.rs:18: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 trait `Read` is implemented for `&[u8]` | ||
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,28 @@ | ||
// 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 | ||
wants_read(&x); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
wants_read(&x[..]); | ||
|
||
let x = &[0u8]; | ||
wants_read(x); | ||
//~^ ERROR the trait bound `&[u8; 1]: Read` is not satisfied | ||
wants_read(&x); | ||
//~^ ERROR the trait bound `&&[u8; 1]: Read` is not satisfied | ||
wants_read(*x); | ||
//~^ ERROR the trait bound `[u8; 1]: Read` is not satisfied | ||
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,94 @@ | ||
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]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&x[..]); | ||
| + ++++ | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:16:16 | ||
| | ||
LL | wants_read(&x); | ||
| ---------- ^^ the trait `Read` is not implemented for `&[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&x[..]); | ||
| ++++ | ||
|
||
error[E0277]: the trait bound `&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:21:16 | ||
| | ||
LL | wants_read(x); | ||
| ---------- ^ the trait `Read` is not implemented for `&[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&x[..]); | ||
| + ++++ | ||
|
||
error[E0277]: the trait bound `&&[u8; 1]: Read` is not satisfied | ||
--> $DIR/issue-90528-unsizing-suggestion-2.rs:23:16 | ||
| | ||
LL | wants_read(&x); | ||
| ---------- ^^ the trait `Read` is not implemented for `&&[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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:25:16 | ||
| | ||
LL | wants_read(*x); | ||
| ---------- ^^ the trait `Read` is not implemented for `[u8; 1]` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Read` is implemented for `&[u8]` | ||
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` | ||
help: convert the array to a `&[u8]` slice instead | ||
| | ||
LL | wants_read(&*x[..]); | ||
| + ++++ | ||
|
||
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,22 @@ | ||
// 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 | ||
wants_write(&mut [0u8]); | ||
//~^ ERROR the trait bound `&mut [u8; 1]: Write` is not satisfied | ||
wants_write(&mut [0u8][..]); | ||
wants_write(&[0u8]); | ||
//~^ ERROR the trait bound `&[u8; 1]: Write` is not satisfied | ||
wants_write(&[0u8][..]); | ||
//~^ ERROR the trait bound `&[u8]: Write` is not satisfied | ||
} |
Oops, something went wrong.