Auto merge of #46720 - estebank:issue-46302, r=nikomatsakis
Fix incorrect type mismatch label pointing at return type CC #46302.
This commit is contained in:
commit
1029775ad5
6 changed files with 41 additions and 16 deletions
|
|
@ -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
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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 | | }
|
||||
|
|
@ -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 | | }
|
||||
|
|
|
|||
|
|
@ -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 ()
|
||||
|
|
||||
|
|
|
|||
19
src/test/ui/suggestions/issue-46302.rs
Normal file
19
src/test/ui/suggestions/issue-46302.rs
Normal file
|
|
@ -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
src/test/ui/suggestions/issue-46302.stderr
Normal file
15
src/test/ui/suggestions/issue-46302.stderr
Normal file
|
|
@ -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`.
|
||||
|
|
@ -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 };
|
||||
| ^^^^^^
|
||||
| |
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue