Point to where missing return type should go

This commit is contained in:
Michael Goulet 2023-09-30 19:29:41 +00:00
parent eea26141ec
commit 137b6d0b01
34 changed files with 86 additions and 71 deletions

View File

@ -1189,7 +1189,7 @@ fn report_trait_method_mismatch<'tcx>(
let ap = Applicability::MachineApplicable;
match sig.decl.output {
hir::FnRetTy::DefaultReturn(sp) => {
let sugg = format!("-> {} ", trait_sig.output());
let sugg = format!(" -> {}", trait_sig.output());
diag.span_suggestion_verbose(sp, msg, sugg, ap);
}
hir::FnRetTy::Return(hir_ty) => {

View File

@ -110,7 +110,7 @@ pub struct AddressOfTemporaryTaken {
pub enum AddReturnTypeSuggestion {
#[suggestion(
hir_typeck_add_return_type_add,
code = "-> {found} ",
code = " -> {found}",
applicability = "machine-applicable"
)]
Add {
@ -120,7 +120,7 @@ pub enum AddReturnTypeSuggestion {
},
#[suggestion(
hir_typeck_add_return_type_missing_here,
code = "-> _ ",
code = " -> _",
applicability = "has-placeholders"
)]
MissingHere {

View File

@ -782,8 +782,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
hir::FnRetTy::Return(hir_ty) => {
let span = hir_ty.span;
if let hir::TyKind::OpaqueDef(item_id, ..) = hir_ty.kind
&& let hir::Node::Item(hir::Item {
kind: hir::ItemKind::OpaqueTy(op_ty),
@ -799,28 +797,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!(?found);
if found.is_suggestable(self.tcx, false) {
if term.span.is_empty() {
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: found.to_string() });
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span: term.span, found: found.to_string() });
return true;
} else {
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected });
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span: term.span, expected });
}
}
}
// 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!("return type {:?}", hir_ty);
let ty = self.astconv().ast_ty_to_ty(hir_ty);
debug!("return type {:?}", ty);
debug!("expected type {:?}", expected);
let bound_vars = self.tcx.late_bound_vars(hir_ty.hir_id.owner.into());
let ty = Binder::bind_with_vars(ty, bound_vars);
let ty = self.normalize(span, ty);
let ty = self.tcx.erase_late_bound_regions(ty);
if self.can_coerce(expected, ty) {
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected });
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
return true;
} else {
// 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!("return type {:?}", hir_ty);
let ty = self.astconv().ast_ty_to_ty(hir_ty);
debug!("return type {:?}", ty);
debug!("expected type {:?}", expected);
let bound_vars = self.tcx.late_bound_vars(hir_ty.hir_id.owner.into());
let ty = Binder::bind_with_vars(ty, bound_vars);
let ty = self.normalize(hir_ty.span, ty);
let ty = self.tcx.erase_late_bound_regions(ty);
if self.can_coerce(expected, ty) {
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected });
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
return true;
}
}
}
_ => {}

View File

@ -194,13 +194,13 @@ impl<'a> SourceKindMultiSuggestion<'a> {
data: &'a FnRetTy<'a>,
should_wrap_expr: Option<Span>,
) -> Self {
let (arrow, post) = match data {
FnRetTy::DefaultReturn(_) => ("-> ", " "),
_ => ("", ""),
let arrow = match data {
FnRetTy::DefaultReturn(_) => " -> ",
_ => "",
};
let (start_span, start_span_code, end_span) = match should_wrap_expr {
Some(end_span) => (data.span(), format!("{arrow}{ty_info}{post}{{ "), Some(end_span)),
None => (data.span(), format!("{arrow}{ty_info}{post}"), None),
Some(end_span) => (data.span(), format!("{arrow}{ty_info} {{"), Some(end_span)),
None => (data.span(), format!("{arrow}{ty_info}"), None),
};
Self::ClosureReturn { start_span, start_span_code, end_span }
}

View File

@ -247,7 +247,7 @@ impl<'a> Parser<'a> {
)?;
FnRetTy::Ty(ty)
} else {
FnRetTy::Default(self.token.span.shrink_to_lo())
FnRetTy::Default(self.prev_token.span.shrink_to_hi())
})
}

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-71443-1.rs:6:5
|
LL | fn hello<F: for<'a> Iterator<Item: 'a>>() {
| - help: try adding a return type: `-> Incorrect`
| - help: try adding a return type: `-> Incorrect`
LL | Incorrect
| ^^^^^^^^^ expected `()`, found `Incorrect`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/block-must-not-have-result-res.rs:5:9
|
LL | fn drop(&mut self) {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | true
| ^^^^ expected `()`, found `bool`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-20862.rs:2:5
|
LL | fn foo(x: i32) {
| - help: a return type might be missing here: `-> _`
| - help: a return type might be missing here: `-> _`
LL | |y| x + y
| ^^^^^^^^^ expected `()`, found closure
|

View File

@ -17,7 +17,7 @@ error[E0308]: mismatched types
--> $DIR/issue-22645.rs:15:3
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | let b = Bob + 3.5;
LL | b + 3
| ^^^^^ expected `()`, found `Bob`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-5500.rs:2:5
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | &panic!()
| ^^^^^^^^^ expected `()`, found `&_`
|

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/add_semicolon_non_block_closure.rs:8:12
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | foo(|| bar())
| ^^^^^ expected `()`, found `i32`
|

View File

@ -1,8 +1,8 @@
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/implicit-return.rs:4:34
--> $DIR/implicit-return.rs:4:33
|
LL | let _f = for<'a> |_: &'a ()| {};
| ------- ^
| ------- ^
| |
| `for<...>` is here

View File

@ -41,10 +41,10 @@ LL | let _ = for<'a> |x: &'a ()| -> &() { x };
| ^ explicit lifetime name needed here
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/implicit-stuff.rs:5:22
--> $DIR/implicit-stuff.rs:5:21
|
LL | let _ = for<> || {};
| ----- ^
| ----- ^
| |
| `for<...>` is here

View File

@ -8,7 +8,7 @@ error[E0308]: mismatched types
--> $DIR/tab.rs:8:2
|
LL | fn foo() {
| - help: try adding a return type: `-> &'static str`
| - help: try adding a return type: `-> &'static str`
LL | "bar boo"
| ^^^^^^^^^^^^^^^^^^^^ expected `()`, found `&str`

View File

@ -28,10 +28,10 @@ LL | fn foo(self);
found signature `fn(Box<MyFuture>)`
error[E0053]: method `bar` has an incompatible type for trait
--> $DIR/bad-self-type.rs:24:18
--> $DIR/bad-self-type.rs:24:17
|
LL | fn bar(self) {}
| ^ expected `Option<()>`, found `()`
| ^ expected `Option<()>`, found `()`
|
note: type in trait
--> $DIR/bad-self-type.rs:18:21

View File

@ -30,8 +30,8 @@ LL | fn bar() {}
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
help: replace the return type so that it matches the trait
|
LL | fn bar() -> impl Sized {}
| +++++++++++++
LL | fn bar()-> impl Sized {}
| +++++++++++++
error: impl trait in impl method signature does not match trait method signature
--> $DIR/refine.rs:22:17

View File

@ -19,7 +19,7 @@ error[E0308]: mismatched types
--> $DIR/issue-66667-function-cmp-cycle.rs:2:5
|
LL | fn first() {
| - help: try adding a return type: `-> bool`
| - help: try adding a return type: `-> bool`
LL | second == 1
| ^^^^^^^^^^^ expected `()`, found `bool`
@ -44,7 +44,7 @@ error[E0308]: mismatched types
--> $DIR/issue-66667-function-cmp-cycle.rs:8:5
|
LL | fn second() {
| - help: try adding a return type: `-> bool`
| - help: try adding a return type: `-> bool`
LL | first == 1
| ^^^^^^^^^^ expected `()`, found `bool`
@ -69,7 +69,7 @@ error[E0308]: mismatched types
--> $DIR/issue-66667-function-cmp-cycle.rs:14:5
|
LL | fn bar() {
| - help: try adding a return type: `-> bool`
| - help: try adding a return type: `-> bool`
LL | bar == 1
| ^^^^^^^^ expected `()`, found `bool`

View File

@ -1,8 +1,8 @@
error[E0308]: lang item `start` function has wrong type
--> $DIR/start_lang_item_args.rs:29:84
--> $DIR/start_lang_item_args.rs:29:83
|
LL | fn start<T>(_main: fn() -> T, _argc: isize, _argv: *const *const u8, _sigpipe: u8) {}
| ^ expected `isize`, found `()`
| ^ expected `isize`, found `()`
|
= note: expected signature `fn(fn() -> _, _, _, _) -> isize`
found signature `fn(fn() -> _, _, _, _)`

View File

@ -319,7 +319,7 @@ error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:159:15
|
LL | fn main() {
| - expected `()` because of this return type
| - expected `()` because of this return type
...
LL | loop { // point at the return type
| ---- this loop is expected to be of type `()`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-19109.rs:4:5
|
LL | fn function(t: &mut dyn Trait) {
| - help: try adding a return type: `-> *mut dyn Trait`
| - help: try adding a return type: `-> *mut dyn Trait`
LL | t as *mut dyn Trait
| ^^^^^^^^^^^^^^^^^^^ expected `()`, found `*mut dyn Trait`
|

View File

@ -42,7 +42,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:19:5
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
...
LL | offset_of!(S, v)
| ^^^^^^^^^^^^^^^^ expected `()`, found `usize`

View File

@ -25,10 +25,10 @@ LL | for <Foo>::Bar in x {}
= help: consider removing `for<...>`
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/recover-quantified-closure.rs:2:25
--> $DIR/recover-quantified-closure.rs:2:24
|
LL | for<'a> |x: &'a u8| *x + 1;
| ------- ^
| ------- ^
| |
| `for<...>` is here

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-37788.rs:8:5
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | // Test that constructing the `visible_parent_map` (in `cstore_impl.rs`) does not ICE.
LL | std::cell::Cell::new(0)
| ^^^^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here: `;`

View File

@ -10,7 +10,7 @@ error[E0308]: mismatched types
--> $DIR/resolved-located-at.rs:7:27
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | resolve_located_at!(a b)
| ^ expected `()`, found `S`
|

View File

@ -38,7 +38,7 @@ error[E0308]: mismatched types
--> $DIR/span-preservation.rs:39:5
|
LL | extern "C" fn bar() {
| - help: try adding a return type: `-> i32`
| - help: try adding a return type: `-> i32`
LL | 0
| ^ expected `()`, found integer
@ -46,7 +46,7 @@ error[E0308]: mismatched types
--> $DIR/span-preservation.rs:44:5
|
LL | extern "C" fn baz() {
| - help: try adding a return type: `-> i32`
| - help: try adding a return type: `-> i32`
LL | 0
| ^ expected `()`, found integer
@ -54,7 +54,7 @@ error[E0308]: mismatched types
--> $DIR/span-preservation.rs:49:5
|
LL | extern "Rust" fn rust_abi() {
| - help: try adding a return type: `-> i32`
| - help: try adding a return type: `-> i32`
LL | 0
| ^ expected `()`, found integer
@ -62,7 +62,7 @@ error[E0308]: mismatched types
--> $DIR/span-preservation.rs:54:5
|
LL | extern "\x43" fn c_abi_escaped() {
| - help: try adding a return type: `-> i32`
| - help: try adding a return type: `-> i32`
LL | 0
| ^ expected `()`, found integer

View File

@ -17,7 +17,7 @@ error[E0308]: mismatched types
--> $DIR/return-struct.rs:15:5
|
LL | fn bar() {
| - help: try adding a return type: `-> Age`
| - help: try adding a return type: `-> Age`
LL | let mut age = 29;
LL | Age::Years(age, 55)
| ^^^^^^^^^^^^^^^^^^^ expected `()`, found `Age`
@ -26,7 +26,7 @@ error[E0308]: mismatched types
--> $DIR/return-struct.rs:20:5
|
LL | fn baz() {
| - help: try adding a return type: `-> S`
| - help: try adding a return type: `-> S`
LL | S
| ^ expected `()`, found `S`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-83892.rs:9:15
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | match () {
LL | () => func()
| ^^^^^^ expected `()`, found `u8`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/return-closures.rs:3:5
|
LL | fn foo() {
| - help: try adding a return type: `-> impl for<'a> Fn(&'a i32) -> i32`
| - help: try adding a return type: `-> impl for<'a> Fn(&'a i32) -> i32`
LL |
LL | |x: &i32| 1i32
| ^^^^^^^^^^^^^^ expected `()`, found closure
@ -14,7 +14,7 @@ error[E0308]: mismatched types
--> $DIR/return-closures.rs:9:5
|
LL | fn bar(i: impl Sized) {
| - help: a return type might be missing here: `-> _`
| - help: a return type might be missing here: `-> _`
LL |
LL | || i
| ^^^^ expected `()`, found closure

View File

@ -0,0 +1,14 @@
// edition: 2021
// run-rustfix
#![allow(unused)]
// Make sure we don't ICE when suggesting a return type
// for an async fn that has late-bound vars...
async fn ice(_: &i32) -> bool {
true
//~^ ERROR mismatched types
}
fn main() {}

View File

@ -1,4 +1,7 @@
// edition: 2021
// run-rustfix
#![allow(unused)]
// Make sure we don't ICE when suggesting a return type
// for an async fn that has late-bound vars...

View File

@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/suggest-ret-on-async-w-late.rs:7:5
--> $DIR/suggest-ret-on-async-w-late.rs:10:5
|
LL | async fn ice(_: &i32) {
| --------------------- help: try adding a return type: `-> bool`
| - help: try adding a return type: `-> bool`
LL | true
| ^^^^ expected `()`, found `bool`

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-57673-ice-on-deref-of-boxed-trait.rs:5:5
|
LL | fn ice(x: Box<dyn Iterator<Item=()>>) {
| - help: try adding a return type: `-> (dyn Iterator<Item = ()> + 'static)`
| - help: try adding a return type: `-> (dyn Iterator<Item = ()> + 'static)`
LL | *x
| ^^ expected `()`, found `dyn Iterator`
|

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-90027-async-fn-return-suggestion.rs:4:5
|
LL | async fn hello() {
| ---------------- help: try adding a return type: `-> i32`
| - help: try adding a return type: `-> i32`
LL | 0
| ^ expected `()`, found integer
@ -10,7 +10,7 @@ error[E0308]: mismatched types
--> $DIR/issue-90027-async-fn-return-suggestion.rs:9:5
|
LL | async fn world() -> () {
| ---------------------- expected `()` because of return type
| -- expected `()` because of return type
LL | 0
| ^ expected `()`, found integer

View File

@ -14,7 +14,7 @@ error[E0308]: mismatched types
--> $DIR/issue-91267.rs:4:5
|
LL | fn main() {
| - expected `()` because of default return type
| - expected `()` because of default return type
LL | type_ascribe!(0, u8<e<5>=e>)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u8`