Skip to content

Commit 2636b31

Browse files
committedJun 29, 2018
Fix incorrect type mismatch label pointing at return type
1 parent 5fdcd3a commit 2636b31

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed
 

‎src/librustc_typeck/check/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -4625,21 +4625,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
46254625
can_suggest: bool) {
46264626
// Only suggest changing the return type for methods that
46274627
// haven't set a return type at all (and aren't `fn main()` or an impl).
4628-
match (&fn_decl.output, found.is_suggestable(), can_suggest) {
4629-
(&hir::FunctionRetTy::DefaultReturn(span), true, true) => {
4628+
match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_nil()) {
4629+
(&hir::FunctionRetTy::DefaultReturn(span), true, true, true) => {
46304630
err.span_suggestion(span,
46314631
"try adding a return type",
46324632
format!("-> {} ",
46334633
self.resolve_type_vars_with_obligations(found)));
46344634
}
4635-
(&hir::FunctionRetTy::DefaultReturn(span), false, true) => {
4635+
(&hir::FunctionRetTy::DefaultReturn(span), false, true, true) => {
46364636
err.span_label(span, "possibly return type missing here?");
46374637
}
4638-
(&hir::FunctionRetTy::DefaultReturn(span), _, _) => {
4638+
(&hir::FunctionRetTy::DefaultReturn(span), _, false, true) => {
46394639
// `fn main()` must return `()`, do not suggest changing return type
46404640
err.span_label(span, "expected `()` because of default return type");
46414641
}
4642-
(&hir::FunctionRetTy::Return(ref ty), _, _) => {
4642+
// expectation was caused by something else, not the default return
4643+
(&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => {}
4644+
(&hir::FunctionRetTy::Return(ref ty), _, _, _) => {
46434645
// Only point to return type if the expected type is the return type, as if they
46444646
// are not, the expectation must have been caused by something else.
46454647
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node);

‎src/test/ui/break-while-condition.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ LL | | };
1313
error[E0308]: mismatched types
1414
--> $DIR/break-while-condition.rs:26:13
1515
|
16-
LL | fn main() {
17-
| - expected `()` because of default return type
18-
...
1916
LL | / while false { //~ ERROR mismatched types
2017
LL | | break
2118
LL | | }
@@ -27,9 +24,6 @@ LL | | }
2724
error[E0308]: mismatched types
2825
--> $DIR/break-while-condition.rs:34:13
2926
|
30-
LL | fn main() {
31-
| - expected `()` because of default return type
32-
...
3327
LL | / while false { //~ ERROR mismatched types
3428
LL | | return
3529
LL | | }

‎src/test/ui/issue-50585.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/issue-50585.rs:12:18
33
|
4-
LL | fn main() {
5-
| - expected `()` because of default return type
64
LL | |y: Vec<[(); for x in 0..2 {}]>| {};
75
| ^^^^^^^^^^^^^^^^ expected usize, found ()
86
|
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() {
12+
let s = "abc";
13+
let u: &str = if true { s[..2] } else { s };
14+
//~^ ERROR mismatched types
15+
}
16+
17+
fn main() {
18+
foo();
19+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-46302.rs:13:27
3+
|
4+
LL | let u: &str = if true { s[..2] } else { s };
5+
| ^^^^^^
6+
| |
7+
| expected &str, found str
8+
| help: consider borrowing here: `&s[..2]`
9+
|
10+
= note: expected type `&str`
11+
found type `str`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

‎src/test/ui/suggestions/str-array-assignment.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ LL | let t = if true { s[..2] } else { s };
1010
error[E0308]: mismatched types
1111
--> $DIR/str-array-assignment.rs:15:27
1212
|
13-
LL | fn main() {
14-
| - expected `()` because of default return type
15-
...
1613
LL | let u: &str = if true { s[..2] } else { s };
1714
| ^^^^^^
1815
| |

0 commit comments

Comments
 (0)
Please sign in to comment.