Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect type mismatch label pointing at return type #46720

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4625,21 +4625,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
can_suggest: bool) {
// Only suggest changing the return type for methods that
// haven't set a return type at all (and aren't `fn main()` or an impl).
match (&fn_decl.output, found.is_suggestable(), can_suggest) {
(&hir::FunctionRetTy::DefaultReturn(span), true, true) => {
match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_nil()) {
(&hir::FunctionRetTy::DefaultReturn(span), true, true, true) => {
err.span_suggestion(span,
"try adding a return type",
format!("-> {} ",
self.resolve_type_vars_with_obligations(found)));
}
(&hir::FunctionRetTy::DefaultReturn(span), false, true) => {
(&hir::FunctionRetTy::DefaultReturn(span), false, true, true) => {
err.span_label(span, "possibly return type missing here?");
}
(&hir::FunctionRetTy::DefaultReturn(span), _, _) => {
(&hir::FunctionRetTy::DefaultReturn(span), _, false, true) => {
// `fn main()` must return `()`, do not suggest changing return type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is not strictly true anymore -- main can return other types!

err.span_label(span, "expected `()` because of default return type");
}
(&hir::FunctionRetTy::Return(ref ty), _, _) => {
// expectation was caused by something else, not the default return
(&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => {}
(&hir::FunctionRetTy::Return(ref ty), _, _, _) => {
// Only point to return type if the expected type is the return type, as if they
// are not, the expectation must have been caused by something else.
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node);
Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/break-while-condition.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ LL | | };
error[E0308]: mismatched types
--> $DIR/break-while-condition.rs:26:13
|
LL | fn main() {
| - expected `()` because of default return type
...
LL | / while false { //~ ERROR mismatched types
LL | | break
LL | | }
Expand All @@ -27,9 +24,6 @@ LL | | }
error[E0308]: mismatched types
--> $DIR/break-while-condition.rs:34:13
|
LL | fn main() {
| - expected `()` because of default return type
...
LL | / while false { //~ ERROR mismatched types
LL | | return
LL | | }
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issue-50585.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
error[E0308]: mismatched types
--> $DIR/issue-50585.rs:12:18
|
LL | fn main() {
| - expected `()` because of default return type
LL | |y: Vec<[(); for x in 0..2 {}]>| {};
| ^^^^^^^^^^^^^^^^ expected usize, found ()
|
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/suggestions/issue-46302.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

fn foo() {
let s = "abc";
let u: &str = if true { s[..2] } else { s };
//~^ ERROR mismatched types
}

fn main() {
foo();
}
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/issue-46302.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/issue-46302.rs:13:27
|
LL | let u: &str = if true { s[..2] } else { s };
| ^^^^^^
| |
| expected &str, found str
| help: consider borrowing here: `&s[..2]`
|
= note: expected type `&str`
found type `str`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
3 changes: 0 additions & 3 deletions src/test/ui/suggestions/str-array-assignment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ LL | let t = if true { s[..2] } else { s };
error[E0308]: mismatched types
--> $DIR/str-array-assignment.rs:15:27
|
LL | fn main() {
| - expected `()` because of default return type
...
LL | let u: &str = if true { s[..2] } else { s };
| ^^^^^^
| |
Expand Down