about summary refs log tree commit diff
path: root/src/test/ui/error-codes
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-01-17 11:16:37 +0530
committerGitHub <noreply@github.com>2020-01-17 11:16:37 +0530
commitde01a29fbee586a13f10ee928df7c15c5a170587 (patch)
tree5df2f8765a42cd59c9d6244b56d7d18b56d8d77f /src/test/ui/error-codes
parentecf42a3d624b859bcfeffc1c454ebd964eac0422 (diff)
parent029a9c625371e756d93024efd3deb7636a90f8f8 (diff)
downloadrust-de01a29fbee586a13f10ee928df7c15c5a170587.tar.gz
rust-de01a29fbee586a13f10ee928df7c15c5a170587.zip
Rollup merge of #68195 - estebank:impl-trait-2000, r=Centril
Account for common `impl Trait`/`dyn Trait` return type errors

- When all return paths have the same type, suggest `impl Trait`.
- When all return paths implement the expected `trait`, suggest `Box<dyn Trait>` and mention using an `enum`.
- When multiple different types are returned and `impl Trait` is expected, extend the explanation.
- When return type is `impl Trait` and the return paths do not implement `Trait`, point at the returned values.
- Split `src/librustc/traits/error_reporting.rs` into multiple files to keep size under control.

Fix #68110, cc #66523.
Diffstat (limited to 'src/test/ui/error-codes')
-rw-r--r--src/test/ui/error-codes/E0746.fixed18
-rw-r--r--src/test/ui/error-codes/E0746.rs18
-rw-r--r--src/test/ui/error-codes/E0746.stderr27
3 files changed, 63 insertions, 0 deletions
diff --git a/src/test/ui/error-codes/E0746.fixed b/src/test/ui/error-codes/E0746.fixed
new file mode 100644
index 00000000000..ca8319aa020
--- /dev/null
+++ b/src/test/ui/error-codes/E0746.fixed
@@ -0,0 +1,18 @@
+// run-rustfix
+#![allow(dead_code)]
+struct Struct;
+trait Trait {}
+impl Trait for Struct {}
+impl Trait for u32 {}
+
+fn foo() -> impl Trait { Struct }
+//~^ ERROR E0746
+
+fn bar() -> impl Trait { //~ ERROR E0746
+    if true {
+        return 0;
+    }
+    42
+}
+
+fn main() {}
diff --git a/src/test/ui/error-codes/E0746.rs b/src/test/ui/error-codes/E0746.rs
new file mode 100644
index 00000000000..bf5ba8fff56
--- /dev/null
+++ b/src/test/ui/error-codes/E0746.rs
@@ -0,0 +1,18 @@
+// run-rustfix
+#![allow(dead_code)]
+struct Struct;
+trait Trait {}
+impl Trait for Struct {}
+impl Trait for u32 {}
+
+fn foo() -> dyn Trait { Struct }
+//~^ ERROR E0746
+
+fn bar() -> dyn Trait { //~ ERROR E0746
+    if true {
+        return 0;
+    }
+    42
+}
+
+fn main() {}
diff --git a/src/test/ui/error-codes/E0746.stderr b/src/test/ui/error-codes/E0746.stderr
new file mode 100644
index 00000000000..e7a8fd304ca
--- /dev/null
+++ b/src/test/ui/error-codes/E0746.stderr
@@ -0,0 +1,27 @@
+error[E0746]: return type cannot have an unboxed trait object
+  --> $DIR/E0746.rs:8:13
+   |
+LL | fn foo() -> dyn Trait { Struct }
+   |             ^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
+help: return `impl Trait` instead, as all return paths are of type `Struct`, which implements `Trait`
+   |
+LL | fn foo() -> impl Trait { Struct }
+   |             ^^^^^^^^^^
+
+error[E0746]: return type cannot have an unboxed trait object
+  --> $DIR/E0746.rs:11:13
+   |
+LL | fn bar() -> dyn Trait {
+   |             ^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
+help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
+   |
+LL | fn bar() -> impl Trait {
+   |             ^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0746`.