Skip to content

Commit

Permalink
Only refer to return type when it matches
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Aug 9, 2017
1 parent d96f9d4 commit 3fcdb8b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Expand Up @@ -664,7 +664,7 @@ impl<'hir> Map<'hir> {
match *node {
NodeExpr(ref expr) => {
match expr.node {
ExprWhile(..) | ExprLoop(..) | ExprIf(..) => true,
ExprWhile(..) | ExprLoop(..) => true,
_ => false,
}
}
Expand Down
27 changes: 22 additions & 5 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -95,7 +95,7 @@ use rustc::middle::region::CodeExtent;
use rustc::ty::subst::{Kind, Subst, Substs};
use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode};
use rustc::ty::{ParamTy, LvaluePreference, NoPreference, PreferMutLvalue};
use rustc::ty::{self, Ty, TyCtxt, Visibility};
use rustc::ty::{self, Ty, TyCtxt, Visibility, TypeVariants};
use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc::ty::fold::{BottomUpFolder, TypeFoldable};
use rustc::ty::maps::Providers;
Expand Down Expand Up @@ -4302,7 +4302,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
expected: Ty<'tcx>,
found: Ty<'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) {
Expand All @@ -4316,13 +4315,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
(&hir::FunctionRetTy::DefaultReturn(span), _, _) => {
// `fn main()` must return `()`, do not suggest changing return type
err.span_label(span, "expected `()` because of default return type");
err.span_label(span, "expected () because of default return type");
}
(&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.
err.span_label(ty.span,
format!("expected `{}` because of return type", expected));
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node);
let sp = ty.span;
let ty = AstConv::ast_ty_to_ty(self, ty);
debug!("suggest_missing_return_type: return type sty {:?}", ty.sty);
debug!("suggest_missing_return_type: expected type sty {:?}", ty.sty);
if ty.sty == expected.sty {
let quote = if let TypeVariants::TyTuple(ref slice, _) = expected.sty {
if slice.len() == 0 { // don't use backtics for ()
""
} else {
"`"
}
} else {
"`"
};
err.span_label(sp, format!("expected {}{}{} because of return type",
quote,
expected,
quote));
}
}
}
}
Expand Down
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/block-must-not-have-result-res.rs:15:9
|
14 | fn drop(&mut self) {
| - expected `()` because of default return type
| - expected () because of default return type
15 | true //~ ERROR mismatched types
| ^^^^ expected (), found bool
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/block-result/issue-13624.stderr
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-13624.rs:17:5
|
16 | pub fn get_enum_struct_variant() -> () {
| -- expected `()` because of return type
| -- expected () because of return type
17 | Enum::EnumStructVariant { x: 1, y: 2, z: 3 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `a::Enum`
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/block-result/issue-22645.stderr
Expand Up @@ -12,7 +12,7 @@ error[E0308]: mismatched types
--> $DIR/issue-22645.rs:25:3
|
23 | fn main() {
| - expected `()` because of default return type
| - expected () because of default return type
24 | let b = Bob + 3.5;
25 | b + 3 //~ ERROR E0277
| ^^^^^ expected (), found struct `Bob`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/block-result/issue-5500.stderr
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-5500.rs:12:5
|
11 | fn main() {
| - expected `()` because of default return type
| - expected () because of default return type
12 | &panic!()
| ^^^^^^^^^ expected (), found reference
|
Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/impl-trait/equality.stderr
@@ -1,9 +1,6 @@
error[E0308]: mismatched types
--> $DIR/equality.rs:25:5
|
21 | fn two(x: bool) -> impl Foo {
| -------- expected `_` because of return type
...
25 | 0_u32
| ^^^^^ expected i32, found u32
|
Expand Down

0 comments on commit 3fcdb8b

Please sign in to comment.