Skip to content

Commit 11fd857

Browse files
committed
Emit explanatory note for functions in trait and impl items as well
1 parent 2586e96 commit 11fd857

6 files changed

+83
-10
lines changed

compiler/rustc_typeck/src/check/expr.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
683683

684684
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
685685

686-
// Somewhat confusingly, get_parent_item() does not necessarily return an
687-
// item -- it can also return a Foreign-/Impl-/TraitItem or a Crate (see
688-
// issue #86721). If it does, we still report the same error.
689-
if let Some(hir::Node::Item(encl_item)) = self.tcx.hir().find(encl_item_id) {
690-
if let hir::ItemKind::Fn(..) = encl_item.kind {
686+
if self.tcx.hir().maybe_body_owned_by(encl_item_id).is_some() {
687+
if let Some(hir::Node::Item(hir::Item {
688+
kind: hir::ItemKind::Fn(..),
689+
span: encl_fn_span,
690+
..
691+
}))
692+
| Some(hir::Node::TraitItem(hir::TraitItem {
693+
kind: hir::TraitItemKind::Fn(..),
694+
span: encl_fn_span,
695+
..
696+
}))
697+
| Some(hir::Node::ImplItem(hir::ImplItem {
698+
kind: hir::ImplItemKind::Fn(..),
699+
span: encl_fn_span,
700+
..
701+
})) = self.tcx.hir().find(encl_item_id)
702+
{
691703
// We are inside a function body, so reporting "return statement
692704
// outside of function body" needs an explanation.
693705

@@ -701,7 +713,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
701713
let encl_body = self.tcx.hir().body(encl_body_id);
702714

703715
err.encl_body_span = Some(encl_body.value.span);
704-
err.encl_fn_span = Some(encl_item.span);
716+
err.encl_fn_span = Some(*encl_fn_span);
705717
}
706718
}
707719

src/test/ui/return/issue-86188-return-not-in-fn-body.rs

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ const C: [(); 42] = {
1212
}]
1313
};
1414

15+
struct S {}
16+
trait Tr {
17+
fn foo();
18+
fn bar() {
19+
//~^ NOTE: ...not the enclosing function body
20+
[(); return];
21+
//~^ ERROR: return statement outside of function body [E0572]
22+
//~| NOTE: the return is part of this body...
23+
}
24+
}
25+
impl Tr for S {
26+
fn foo() {
27+
//~^ NOTE: ...not the enclosing function body
28+
[(); return];
29+
//~^ ERROR: return statement outside of function body [E0572]
30+
//~| NOTE: the return is part of this body...
31+
}
32+
}
33+
1534
fn main() {
1635
//~^ NOTE: ...not the enclosing function body
1736
[(); return || {

src/test/ui/return/issue-86188-return-not-in-fn-body.stderr

+26-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,31 @@ LL | | }]
99
| |_____^
1010

1111
error[E0572]: return statement outside of function body
12-
--> $DIR/issue-86188-return-not-in-fn-body.rs:17:10
12+
--> $DIR/issue-86188-return-not-in-fn-body.rs:20:14
13+
|
14+
LL | / fn bar() {
15+
LL | |
16+
LL | | [(); return];
17+
| | ^^^^^^ the return is part of this body...
18+
LL | |
19+
LL | |
20+
LL | | }
21+
| |_____- ...not the enclosing function body
22+
23+
error[E0572]: return statement outside of function body
24+
--> $DIR/issue-86188-return-not-in-fn-body.rs:28:14
25+
|
26+
LL | / fn foo() {
27+
LL | |
28+
LL | | [(); return];
29+
| | ^^^^^^ the return is part of this body...
30+
LL | |
31+
LL | |
32+
LL | | }
33+
| |_____- ...not the enclosing function body
34+
35+
error[E0572]: return statement outside of function body
36+
--> $DIR/issue-86188-return-not-in-fn-body.rs:36:10
1337
|
1438
LL | / fn main() {
1539
LL | |
@@ -23,6 +47,6 @@ LL | || }];
2347
LL | | }
2448
| |_- ...not the enclosing function body
2549

26-
error: aborting due to 2 previous errors
50+
error: aborting due to 4 previous errors
2751

2852
For more information about this error, try `rustc --explain E0572`.

src/test/ui/typeck/issue-86721-return-expr-ice.stderr renamed to src/test/ui/typeck/issue-86721-return-expr-ice.rev1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0572]: return statement outside of function body
2-
--> $DIR/issue-86721-return-expr-ice.rs:6:22
2+
--> $DIR/issue-86721-return-expr-ice.rs:9:22
33
|
44
LL | const U: usize = return;
55
| ^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0572]: return statement outside of function body
2+
--> $DIR/issue-86721-return-expr-ice.rs:15:20
3+
|
4+
LL | fn foo(a: [(); return]);
5+
| ^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0572`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
// Regression test for the ICE described in #86721.
22

3+
// revisions: rev1 rev2
4+
#![cfg_attr(any(), rev1, rev2)]
35
#![crate_type="lib"]
46

7+
#[cfg(any(rev1))]
58
trait T {
69
const U: usize = return;
7-
//~^ ERROR: return statement outside of function body [E0572]
10+
//[rev1]~^ ERROR: return statement outside of function body [E0572]
11+
}
12+
13+
#[cfg(any(rev2))]
14+
trait T2 {
15+
fn foo(a: [(); return]);
16+
//[rev2]~^ ERROR: return statement outside of function body [E0572]
817
}

0 commit comments

Comments
 (0)