Skip to content

Commit 3fcdb8b

Browse files
committed
Only refer to return type when it matches
1 parent d96f9d4 commit 3fcdb8b

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<'hir> Map<'hir> {
664664
match *node {
665665
NodeExpr(ref expr) => {
666666
match expr.node {
667-
ExprWhile(..) | ExprLoop(..) | ExprIf(..) => true,
667+
ExprWhile(..) | ExprLoop(..) => true,
668668
_ => false,
669669
}
670670
}

src/librustc_typeck/check/mod.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use rustc::middle::region::CodeExtent;
9595
use rustc::ty::subst::{Kind, Subst, Substs};
9696
use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode};
9797
use rustc::ty::{ParamTy, LvaluePreference, NoPreference, PreferMutLvalue};
98-
use rustc::ty::{self, Ty, TyCtxt, Visibility};
98+
use rustc::ty::{self, Ty, TyCtxt, Visibility, TypeVariants};
9999
use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
100100
use rustc::ty::fold::{BottomUpFolder, TypeFoldable};
101101
use rustc::ty::maps::Providers;
@@ -4302,7 +4302,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43024302
expected: Ty<'tcx>,
43034303
found: Ty<'tcx>,
43044304
can_suggest: bool) {
4305-
43064305
// Only suggest changing the return type for methods that
43074306
// haven't set a return type at all (and aren't `fn main()` or an impl).
43084307
match (&fn_decl.output, found.is_suggestable(), can_suggest) {
@@ -4316,13 +4315,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43164315
}
43174316
(&hir::FunctionRetTy::DefaultReturn(span), _, _) => {
43184317
// `fn main()` must return `()`, do not suggest changing return type
4319-
err.span_label(span, "expected `()` because of default return type");
4318+
err.span_label(span, "expected () because of default return type");
43204319
}
43214320
(&hir::FunctionRetTy::Return(ref ty), _, _) => {
43224321
// Only point to return type if the expected type is the return type, as if they
43234322
// are not, the expectation must have been caused by something else.
4324-
err.span_label(ty.span,
4325-
format!("expected `{}` because of return type", expected));
4323+
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node);
4324+
let sp = ty.span;
4325+
let ty = AstConv::ast_ty_to_ty(self, ty);
4326+
debug!("suggest_missing_return_type: return type sty {:?}", ty.sty);
4327+
debug!("suggest_missing_return_type: expected type sty {:?}", ty.sty);
4328+
if ty.sty == expected.sty {
4329+
let quote = if let TypeVariants::TyTuple(ref slice, _) = expected.sty {
4330+
if slice.len() == 0 { // don't use backtics for ()
4331+
""
4332+
} else {
4333+
"`"
4334+
}
4335+
} else {
4336+
"`"
4337+
};
4338+
err.span_label(sp, format!("expected {}{}{} because of return type",
4339+
quote,
4340+
expected,
4341+
quote));
4342+
}
43264343
}
43274344
}
43284345
}

src/test/ui/block-result/block-must-not-have-result-res.stderr

+1-1
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:15:9
33
|
44
14 | fn drop(&mut self) {
5-
| - expected `()` because of default return type
5+
| - expected () because of default return type
66
15 | true //~ ERROR mismatched types
77
| ^^^^ expected (), found bool
88
|

src/test/ui/block-result/issue-13624.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-13624.rs:17:5
33
|
44
16 | pub fn get_enum_struct_variant() -> () {
5-
| -- expected `()` because of return type
5+
| -- expected () because of return type
66
17 | Enum::EnumStructVariant { x: 1, y: 2, z: 3 }
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `a::Enum`
88
|

src/test/ui/block-result/issue-22645.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error[E0308]: mismatched types
1212
--> $DIR/issue-22645.rs:25:3
1313
|
1414
23 | fn main() {
15-
| - expected `()` because of default return type
15+
| - expected () because of default return type
1616
24 | let b = Bob + 3.5;
1717
25 | b + 3 //~ ERROR E0277
1818
| ^^^^^ expected (), found struct `Bob`

src/test/ui/block-result/issue-5500.stderr

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

src/test/ui/impl-trait/equality.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/equality.rs:25:5
33
|
4-
21 | fn two(x: bool) -> impl Foo {
5-
| -------- expected `_` because of return type
6-
...
74
25 | 0_u32
85
| ^^^^^ expected i32, found u32
96
|

0 commit comments

Comments
 (0)