about summary refs log tree commit diff
path: root/tests/ui/dst
diff options
context:
space:
mode:
authorBen Reeves <benwolverine2019@gmail.com>2021-11-28 06:33:06 -0600
committerJack Huey <31162821+jackh726@users.noreply.github.com>2023-03-07 00:39:34 -0500
commit8ac7d0eef5621338d3650ec6884c58bc28a5c5f6 (patch)
tree95d55ed449133f41ecf84785521cf45c0a5b5d78 /tests/ui/dst
parent81be7b86d348a5b7cad08386003c43d44d1e9f94 (diff)
downloadrust-8ac7d0eef5621338d3650ec6884c58bc28a5c5f6.tar.gz
rust-8ac7d0eef5621338d3650ec6884c58bc28a5c5f6.zip
Add suggestion to diagnostic when user has array but trait wants slice.
For #90528.
Diffstat (limited to 'tests/ui/dst')
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-1.rs20
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-1.stderr56
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-2.rs28
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-2.stderr94
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-3.rs22
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-3.stderr75
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-4.rs26
-rw-r--r--tests/ui/dst/issue-90528-unsizing-suggestion-4.stderr79
8 files changed, 400 insertions, 0 deletions
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-1.rs b/tests/ui/dst/issue-90528-unsizing-suggestion-1.rs
new file mode 100644
index 00000000000..52863e22bb6
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-1.rs
@@ -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
+}
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-1.stderr b/tests/ui/dst/issue-90528-unsizing-suggestion-1.stderr
new file mode 100644
index 00000000000..27ef3fe97a5
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-1.stderr
@@ -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`.
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-2.rs b/tests/ui/dst/issue-90528-unsizing-suggestion-2.rs
new file mode 100644
index 00000000000..f2762ad421b
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-2.rs
@@ -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[..]);
+}
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-2.stderr b/tests/ui/dst/issue-90528-unsizing-suggestion-2.stderr
new file mode 100644
index 00000000000..ae0c4ca506a
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-2.stderr
@@ -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`.
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-3.rs b/tests/ui/dst/issue-90528-unsizing-suggestion-3.rs
new file mode 100644
index 00000000000..218843d0500
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-3.rs
@@ -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
+}
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-3.stderr b/tests/ui/dst/issue-90528-unsizing-suggestion-3.stderr
new file mode 100644
index 00000000000..774d5ba3c89
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-3.stderr
@@ -0,0 +1,75 @@
+error[E0277]: the trait bound `[u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:13:17
+   |
+LL |     wants_write([0u8]);
+   |     ----------- ^^^^^ the trait `Write` is not implemented for `[u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut [0u8][..]);
+   |                 ++++      ++++
+
+error[E0277]: the trait bound `&mut [u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:15:17
+   |
+LL |     wants_write(&mut [0u8]);
+   |     ----------- ^^^^^^^^^^ the trait `Write` is not implemented for `&mut [u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut [0u8][..]);
+   |                           ++++
+
+error[E0277]: the trait bound `&[u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:18:17
+   |
+LL |     wants_write(&[0u8]);
+   |     ----------- ^^^^^^ the trait `Write` is not implemented for `&[u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+
+error[E0277]: the trait bound `&[u8]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:20:17
+   |
+LL |     wants_write(&[0u8][..]);
+   |     ----------- ^^^^^^^^^^ the trait `Write` is not implemented for `&[u8]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-3.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: consider changing this borrow's mutability
+   |
+LL |     wants_write(&mut [0u8][..]);
+   |                 ~~~~
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-4.rs b/tests/ui/dst/issue-90528-unsizing-suggestion-4.rs
new file mode 100644
index 00000000000..eae953c61ff
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-4.rs
@@ -0,0 +1,26 @@
+// 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 mutable slices.
+
+trait Write {}
+
+impl Write for &mut [u8] {}
+
+fn wants_write(_: impl Write) {}
+
+fn main() {
+    let mut x = [0u8];
+    wants_write(x);
+    //~^ ERROR the trait bound `[u8; 1]: Write` is not satisfied
+    wants_write(&mut x);
+    //~^ ERROR the trait bound `&mut [u8; 1]: Write` is not satisfied
+    wants_write(&mut x[..]);
+
+    let x = &mut [0u8];
+    wants_write(x);
+    //~^ ERROR the trait bound `&mut [u8; 1]: Write` is not satisfied
+    wants_write(*x);
+    //~^ ERROR the trait bound `[u8; 1]: Write` is not satisfied
+    wants_write(&mut x[..]);
+}
diff --git a/tests/ui/dst/issue-90528-unsizing-suggestion-4.stderr b/tests/ui/dst/issue-90528-unsizing-suggestion-4.stderr
new file mode 100644
index 00000000000..a4020ee0708
--- /dev/null
+++ b/tests/ui/dst/issue-90528-unsizing-suggestion-4.stderr
@@ -0,0 +1,79 @@
+error[E0277]: the trait bound `[u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:14:17
+   |
+LL |     wants_write(x);
+   |     ----------- ^ the trait `Write` is not implemented for `[u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut x[..]);
+   |                 ++++  ++++
+
+error[E0277]: the trait bound `&mut [u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:16:17
+   |
+LL |     wants_write(&mut x);
+   |     ----------- ^^^^^^ the trait `Write` is not implemented for `&mut [u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut x[..]);
+   |                       ++++
+
+error[E0277]: the trait bound `&mut [u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:21:17
+   |
+LL |     wants_write(x);
+   |     ----------- ^ the trait `Write` is not implemented for `&mut [u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut x[..]);
+   |                 ++++  ++++
+
+error[E0277]: the trait bound `[u8; 1]: Write` is not satisfied
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:23:17
+   |
+LL |     wants_write(*x);
+   |     ----------- ^^ the trait `Write` is not implemented for `[u8; 1]`
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: the trait `Write` is implemented for `&mut [u8]`
+note: required by a bound in `wants_write`
+  --> $DIR/issue-90528-unsizing-suggestion-4.rs:10:24
+   |
+LL | fn wants_write(_: impl Write) {}
+   |                        ^^^^^ required by this bound in `wants_write`
+help: convert the array to a `&mut [u8]` slice instead
+   |
+LL |     wants_write(&mut *x[..]);
+   |                 ++++   ++++
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0277`.