Skip to content

Commit 59f643f

Browse files
committed
Point to return span when writing return; on non-() fn
1 parent f4a421e commit 59f643f

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

src/librustc/hir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,15 @@ pub enum FunctionRetTy {
19791979
Return(P<Ty>),
19801980
}
19811981

1982+
impl fmt::Display for FunctionRetTy {
1983+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1984+
match self {
1985+
Return(ref ty) => print::to_string(print::NO_ANN, |s| s.print_type(ty)).fmt(f),
1986+
DefaultReturn(_) => "()".fmt(f),
1987+
}
1988+
}
1989+
}
1990+
19821991
impl FunctionRetTy {
19831992
pub fn span(&self) -> Span {
19841993
match *self {

src/librustc_typeck/check/coercion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
11431143
// `expression_ty` will be unit).
11441144
//
11451145
// Another example is `break` with no argument expression.
1146-
assert!(expression_ty.is_unit());
11471146
assert!(expression_ty.is_unit(), "if let hack without unit type");
11481147
fcx.at(cause, fcx.param_env)
11491148
.eq_exp(label_expression_as_expected, expression_ty, self.merged_ty())
@@ -1190,7 +1189,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
11901189
db = struct_span_err!(
11911190
fcx.tcx.sess, cause.span, E0069,
11921191
"`return;` in a function whose return type is not `()`");
1193-
db.span_label(cause.span, "return type is not ()");
1192+
db.span_label(cause.span, "return type is not `()`");
11941193
}
11951194
ObligationCauseCode::BlockTailExpression(blk_id) => {
11961195
db = fcx.report_mismatched_types(cause, expected, found, err);

src/librustc_typeck/check/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4103,7 +4103,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41034103
} else {
41044104
let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
41054105
let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
4106-
coercion.coerce_forced_unit(self, &cause, &mut |_| (), true);
4106+
if let Some((fn_decl, _)) = self.get_fn_decl(expr.id) {
4107+
coercion.coerce_forced_unit(
4108+
self,
4109+
&cause,
4110+
&mut |db| {
4111+
db.span_label(
4112+
fn_decl.output.span(),
4113+
format!(
4114+
"expected `{}` because of this return type",
4115+
fn_decl.output,
4116+
),
4117+
);
4118+
},
4119+
true,
4120+
);
4121+
} else {
4122+
coercion.coerce_forced_unit(self, &cause, &mut |_| (), true);
4123+
}
41074124
}
41084125
tcx.types.never
41094126
}

src/test/ui/error-codes/E0069.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0069]: `return;` in a function whose return type is not `()`
22
--> $DIR/E0069.rs:12:5
33
|
4+
LL | fn foo() -> u8 {
5+
| -- expected `u8` because of this return type
46
LL | return;
5-
| ^^^^^^ return type is not ()
7+
| ^^^^^^ return type is not `()`
68

79
error: aborting due to previous error
810

src/test/ui/ret-non-nil.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0069]: `return;` in a function whose return type is not `()`
22
--> $DIR/ret-non-nil.rs:15:19
33
|
44
LL | fn g() -> isize { return; }
5-
| ^^^^^^ return type is not ()
5+
| ----- ^^^^^^ return type is not `()`
6+
| |
7+
| expected `isize` because of this return type
68

79
error: aborting due to previous error
810

src/test/ui/return/return-unit-from-diverging.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0069]: `return;` in a function whose return type is not `()`
22
--> $DIR/return-unit-from-diverging.rs:15:5
33
|
4+
LL | fn fail() -> ! {
5+
| - expected `!` because of this return type
46
LL | return; //~ ERROR in a function whose return type is not
5-
| ^^^^^^ return type is not ()
7+
| ^^^^^^ return type is not `()`
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)