about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-11-02 08:30:03 +0000
committerbors <bors@rust-lang.org>2017-11-02 08:30:03 +0000
commita7d98c78377e5083d5add5d3ae8d26ffa938c005 (patch)
treeb476c024bb7cf49cf4cbd706564d58b4ddb63a1d /src/test
parent2379faa933923a97158a4939b9fc82dcbd45430f (diff)
parent87c951da0f36405ee9d7226f63a7208c58a16d07 (diff)
Auto merge of #45630 - joshleeb:iss35241, r=estebank
Improve display of error E0308

Ref. Forgetting to call a variant constructor causes a confusing error message #35241.

This PR modifies [`note_type_err`](https://github.com/rust-lang/rust/blob/b7041bfab3a83702a8026fb7a18d8ea7d54cc648/src/librustc/infer/error_reporting/mod.rs#L669-L674) to display a `help` message when a `TyFnPtr` or `TyFnDef` are found and the return type, of the function or function pointer, is the same as the type that is expected.

The output of compiling

```rust
struct Foo(u32);

fn test() -> Foo { Foo }

fn main() {}
```

is now

```bash
$ rustc src/test/ui/issue-35241.rs
error[E0308]: mismatched types
  --> src/test/ui/issue-35241.rs:13:20
   |
13 | fn test() -> Foo { Foo }
   |              ---   ^^^ expected struct `Foo`, found fn item
   |              |
   |              expected `Foo` because of return type
   |
   = help: did you mean `Foo { /* fields */ }`?
   = note: expected type `Foo`
              found type `fn(u32) -> Foo {Foo::{{constructor}}}`

error: aborting due to previous error
```
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/issue-35241.rs15
-rw-r--r--src/test/ui/issue-35241.stderr15
2 files changed, 30 insertions, 0 deletions
diff --git a/src/test/ui/issue-35241.rs b/src/test/ui/issue-35241.rs
new file mode 100644
index 00000000000..7ec3974854b
--- /dev/null
+++ b/src/test/ui/issue-35241.rs
@@ -0,0 +1,15 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Foo(u32);
+
+fn test() -> Foo { Foo }
+
+fn main() {}
diff --git a/src/test/ui/issue-35241.stderr b/src/test/ui/issue-35241.stderr
new file mode 100644
index 00000000000..bb1bba152bb
--- /dev/null
+++ b/src/test/ui/issue-35241.stderr
@@ -0,0 +1,15 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-35241.rs:13:20
+   |
+13 | fn test() -> Foo { Foo }
+   |              ---   ^^^
+   |              |     |
+   |              |     expected struct `Foo`, found fn item
+   |              |     did you mean `Foo(/* fields */)`?
+   |              expected `Foo` because of return type
+   |
+   = note: expected type `Foo`
+              found type `fn(u32) -> Foo {Foo::{{constructor}}}`
+
+error: aborting due to previous error
+