Skip to content

Commit 137b6d0

Browse files
Point to where missing return type should go
1 parent eea2614 commit 137b6d0

34 files changed

+86
-71
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ fn report_trait_method_mismatch<'tcx>(
11891189
let ap = Applicability::MachineApplicable;
11901190
match sig.decl.output {
11911191
hir::FnRetTy::DefaultReturn(sp) => {
1192-
let sugg = format!("-> {} ", trait_sig.output());
1192+
let sugg = format!(" -> {}", trait_sig.output());
11931193
diag.span_suggestion_verbose(sp, msg, sugg, ap);
11941194
}
11951195
hir::FnRetTy::Return(hir_ty) => {

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct AddressOfTemporaryTaken {
110110
pub enum AddReturnTypeSuggestion {
111111
#[suggestion(
112112
hir_typeck_add_return_type_add,
113-
code = "-> {found} ",
113+
code = " -> {found}",
114114
applicability = "machine-applicable"
115115
)]
116116
Add {
@@ -120,7 +120,7 @@ pub enum AddReturnTypeSuggestion {
120120
},
121121
#[suggestion(
122122
hir_typeck_add_return_type_missing_here,
123-
code = "-> _ ",
123+
code = " -> _",
124124
applicability = "has-placeholders"
125125
)]
126126
MissingHere {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
782782
}
783783
}
784784
hir::FnRetTy::Return(hir_ty) => {
785-
let span = hir_ty.span;
786-
787785
if let hir::TyKind::OpaqueDef(item_id, ..) = hir_ty.kind
788786
&& let hir::Node::Item(hir::Item {
789787
kind: hir::ItemKind::OpaqueTy(op_ty),
@@ -799,28 +797,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799797
debug!(?found);
800798
if found.is_suggestable(self.tcx, false) {
801799
if term.span.is_empty() {
802-
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: found.to_string() });
800+
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span: term.span, found: found.to_string() });
803801
return true;
804802
} else {
805-
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected });
803+
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span: term.span, expected });
806804
}
807805
}
808-
}
809-
810-
// Only point to return type if the expected type is the return type, as if they
811-
// are not, the expectation must have been caused by something else.
812-
debug!("return type {:?}", hir_ty);
813-
let ty = self.astconv().ast_ty_to_ty(hir_ty);
814-
debug!("return type {:?}", ty);
815-
debug!("expected type {:?}", expected);
816-
let bound_vars = self.tcx.late_bound_vars(hir_ty.hir_id.owner.into());
817-
let ty = Binder::bind_with_vars(ty, bound_vars);
818-
let ty = self.normalize(span, ty);
819-
let ty = self.tcx.erase_late_bound_regions(ty);
820-
if self.can_coerce(expected, ty) {
821-
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected });
822-
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
823-
return true;
806+
} else {
807+
// Only point to return type if the expected type is the return type, as if they
808+
// are not, the expectation must have been caused by something else.
809+
debug!("return type {:?}", hir_ty);
810+
let ty = self.astconv().ast_ty_to_ty(hir_ty);
811+
debug!("return type {:?}", ty);
812+
debug!("expected type {:?}", expected);
813+
let bound_vars = self.tcx.late_bound_vars(hir_ty.hir_id.owner.into());
814+
let ty = Binder::bind_with_vars(ty, bound_vars);
815+
let ty = self.normalize(hir_ty.span, ty);
816+
let ty = self.tcx.erase_late_bound_regions(ty);
817+
if self.can_coerce(expected, ty) {
818+
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected });
819+
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
820+
return true;
821+
}
824822
}
825823
}
826824
_ => {}

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ impl<'a> SourceKindMultiSuggestion<'a> {
194194
data: &'a FnRetTy<'a>,
195195
should_wrap_expr: Option<Span>,
196196
) -> Self {
197-
let (arrow, post) = match data {
198-
FnRetTy::DefaultReturn(_) => ("-> ", " "),
199-
_ => ("", ""),
197+
let arrow = match data {
198+
FnRetTy::DefaultReturn(_) => " -> ",
199+
_ => "",
200200
};
201201
let (start_span, start_span_code, end_span) = match should_wrap_expr {
202-
Some(end_span) => (data.span(), format!("{arrow}{ty_info}{post}{{ "), Some(end_span)),
203-
None => (data.span(), format!("{arrow}{ty_info}{post}"), None),
202+
Some(end_span) => (data.span(), format!("{arrow}{ty_info} {{"), Some(end_span)),
203+
None => (data.span(), format!("{arrow}{ty_info}"), None),
204204
};
205205
Self::ClosureReturn { start_span, start_span_code, end_span }
206206
}

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'a> Parser<'a> {
247247
)?;
248248
FnRetTy::Ty(ty)
249249
} else {
250-
FnRetTy::Default(self.token.span.shrink_to_lo())
250+
FnRetTy::Default(self.prev_token.span.shrink_to_hi())
251251
})
252252
}
253253

tests/ui/associated-type-bounds/issue-71443-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-71443-1.rs:6:5
33
|
44
LL | fn hello<F: for<'a> Iterator<Item: 'a>>() {
5-
| - help: try adding a return type: `-> Incorrect`
5+
| - help: try adding a return type: `-> Incorrect`
66
LL | Incorrect
77
| ^^^^^^^^^ expected `()`, found `Incorrect`
88

tests/ui/block-result/block-must-not-have-result-res.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/block-must-not-have-result-res.rs:5:9
33
|
44
LL | fn drop(&mut self) {
5-
| - expected `()` because of default return type
5+
| - expected `()` because of default return type
66
LL | true
77
| ^^^^ expected `()`, found `bool`
88

tests/ui/block-result/issue-20862.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-20862.rs:2:5
33
|
44
LL | fn foo(x: i32) {
5-
| - help: a return type might be missing here: `-> _`
5+
| - help: a return type might be missing here: `-> _`
66
LL | |y| x + y
77
| ^^^^^^^^^ expected `()`, found closure
88
|

tests/ui/block-result/issue-22645.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ error[E0308]: mismatched types
1717
--> $DIR/issue-22645.rs:15:3
1818
|
1919
LL | fn main() {
20-
| - expected `()` because of default return type
20+
| - expected `()` because of default return type
2121
LL | let b = Bob + 3.5;
2222
LL | b + 3
2323
| ^^^^^ expected `()`, found `Bob`

tests/ui/block-result/issue-5500.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-5500.rs:2:5
33
|
44
LL | fn main() {
5-
| - expected `()` because of default return type
5+
| - expected `()` because of default return type
66
LL | &panic!()
77
| ^^^^^^^^^ expected `()`, found `&_`
88
|

0 commit comments

Comments
 (0)