Skip to content

Commit c431850

Browse files
committed
Avoid pointing at multiple places on return type error
1 parent 19255dc commit c431850

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/librustc_typeck/check/coercion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
12161216
"supposed to be part of a block tail expression, but the \
12171217
expression is empty");
12181218
});
1219-
fcx.suggest_mismatched_types_on_tail(
1219+
let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
12201220
&mut db,
12211221
expr,
12221222
expected,
@@ -1244,7 +1244,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
12441244
// as prior return coercions would not be relevant (#57664).
12451245
let parent_id = fcx.tcx.hir().get_parent_node(blk_id);
12461246
let parent = fcx.tcx.hir().get(fcx.tcx.hir().get_parent_node(parent_id));
1247-
if fcx.get_node_fn_decl(parent).is_some() {
1247+
if fcx.get_node_fn_decl(parent).is_some() && !pointing_at_return_type {
12481248
if let Some(sp) = fcx.ret_coercion_span.borrow().as_ref() {
12491249
db.span_label(*sp, reason_label);
12501250
}

src/librustc_typeck/check/mod.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,12 +5089,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50895089
found: Ty<'tcx>,
50905090
cause_span: Span,
50915091
blk_id: ast::NodeId,
5092-
) {
5092+
) -> bool {
50935093
self.suggest_missing_semicolon(err, expression, expected, cause_span);
5094+
let mut pointing_at_return_type = false;
50945095
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
5095-
self.suggest_missing_return_type(err, &fn_decl, expected, found, can_suggest);
5096+
pointing_at_return_type = self.suggest_missing_return_type(
5097+
err, &fn_decl, expected, found, can_suggest);
50965098
}
50975099
self.suggest_ref_or_into(err, expression, expected, found);
5100+
pointing_at_return_type
50985101
}
50995102

51005103
pub fn suggest_ref_or_into(
@@ -5193,12 +5196,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51935196
/// This routine checks if the return type is left as default, the method is not part of an
51945197
/// `impl` block and that it isn't the `main` method. If so, it suggests setting the return
51955198
/// type.
5196-
fn suggest_missing_return_type(&self,
5197-
err: &mut DiagnosticBuilder<'tcx>,
5198-
fn_decl: &hir::FnDecl,
5199-
expected: Ty<'tcx>,
5200-
found: Ty<'tcx>,
5201-
can_suggest: bool) {
5199+
fn suggest_missing_return_type(
5200+
&self,
5201+
err: &mut DiagnosticBuilder<'tcx>,
5202+
fn_decl: &hir::FnDecl,
5203+
expected: Ty<'tcx>,
5204+
found: Ty<'tcx>,
5205+
can_suggest: bool,
5206+
) -> bool {
52025207
// Only suggest changing the return type for methods that
52035208
// haven't set a return type at all (and aren't `fn main()` or an impl).
52045209
match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_unit()) {
@@ -5208,16 +5213,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52085213
"try adding a return type",
52095214
format!("-> {} ", self.resolve_type_vars_with_obligations(found)),
52105215
Applicability::MachineApplicable);
5216+
true
52115217
}
52125218
(&hir::FunctionRetTy::DefaultReturn(span), false, true, true) => {
52135219
err.span_label(span, "possibly return type missing here?");
5220+
true
52145221
}
52155222
(&hir::FunctionRetTy::DefaultReturn(span), _, false, true) => {
52165223
// `fn main()` must return `()`, do not suggest changing return type
52175224
err.span_label(span, "expected `()` because of default return type");
5225+
true
52185226
}
52195227
// expectation was caused by something else, not the default return
5220-
(&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => {}
5228+
(&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => false,
52215229
(&hir::FunctionRetTy::Return(ref ty), _, _, _) => {
52225230
// Only point to return type if the expected type is the return type, as if they
52235231
// are not, the expectation must have been caused by something else.
@@ -5229,7 +5237,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52295237
if ty.sty == expected.sty {
52305238
err.span_label(sp, format!("expected `{}` because of return type",
52315239
expected));
5240+
return true;
52325241
}
5242+
false
52335243
}
52345244
}
52355245
}

src/test/ui/diverging-tuple-parts-39485.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ error[E0308]: mismatched types
1515
LL | fn f() -> isize {
1616
| ----- expected `isize` because of return type
1717
LL | (return 1, return 2) //~ ERROR mismatched types
18-
| ^^^^^^^^^^^^^^^^^^-^
19-
| | |
20-
| | expected because of this statement
21-
| expected isize, found tuple
18+
| ^^^^^^^^^^^^^^^^^^^^ expected isize, found tuple
2219
|
2320
= note: expected type `isize`
2421
found type `(!, !)`

src/test/ui/issues/issue-10176.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ error[E0308]: mismatched types
44
LL | fn f() -> isize {
55
| ----- expected `isize` because of return type
66
LL | (return 1, return 2)
7-
| ^^^^^^^^^^^^^^^^^^-^
8-
| | |
9-
| | expected because of this statement
10-
| expected isize, found tuple
7+
| ^^^^^^^^^^^^^^^^^^^^ expected isize, found tuple
118
|
129
= note: expected type `isize`
1310
found type `(!, !)`

0 commit comments

Comments
 (0)