Emit explanatory note for functions in trait and impl items as well
This commit is contained in:
parent
2586e962e0
commit
11fd8579e4
6 changed files with 83 additions and 10 deletions
|
@ -683,11 +683,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
|
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
|
||||||
|
|
||||||
// Somewhat confusingly, get_parent_item() does not necessarily return an
|
if self.tcx.hir().maybe_body_owned_by(encl_item_id).is_some() {
|
||||||
// item -- it can also return a Foreign-/Impl-/TraitItem or a Crate (see
|
if let Some(hir::Node::Item(hir::Item {
|
||||||
// issue #86721). If it does, we still report the same error.
|
kind: hir::ItemKind::Fn(..),
|
||||||
if let Some(hir::Node::Item(encl_item)) = self.tcx.hir().find(encl_item_id) {
|
span: encl_fn_span,
|
||||||
if let hir::ItemKind::Fn(..) = encl_item.kind {
|
..
|
||||||
|
}))
|
||||||
|
| Some(hir::Node::TraitItem(hir::TraitItem {
|
||||||
|
kind: hir::TraitItemKind::Fn(..),
|
||||||
|
span: encl_fn_span,
|
||||||
|
..
|
||||||
|
}))
|
||||||
|
| Some(hir::Node::ImplItem(hir::ImplItem {
|
||||||
|
kind: hir::ImplItemKind::Fn(..),
|
||||||
|
span: encl_fn_span,
|
||||||
|
..
|
||||||
|
})) = self.tcx.hir().find(encl_item_id)
|
||||||
|
{
|
||||||
// We are inside a function body, so reporting "return statement
|
// We are inside a function body, so reporting "return statement
|
||||||
// outside of function body" needs an explanation.
|
// outside of function body" needs an explanation.
|
||||||
|
|
||||||
|
@ -701,7 +713,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let encl_body = self.tcx.hir().body(encl_body_id);
|
let encl_body = self.tcx.hir().body(encl_body_id);
|
||||||
|
|
||||||
err.encl_body_span = Some(encl_body.value.span);
|
err.encl_body_span = Some(encl_body.value.span);
|
||||||
err.encl_fn_span = Some(encl_item.span);
|
err.encl_fn_span = Some(*encl_fn_span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,25 @@ const C: [(); 42] = {
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct S {}
|
||||||
|
trait Tr {
|
||||||
|
fn foo();
|
||||||
|
fn bar() {
|
||||||
|
//~^ NOTE: ...not the enclosing function body
|
||||||
|
[(); return];
|
||||||
|
//~^ ERROR: return statement outside of function body [E0572]
|
||||||
|
//~| NOTE: the return is part of this body...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Tr for S {
|
||||||
|
fn foo() {
|
||||||
|
//~^ NOTE: ...not the enclosing function body
|
||||||
|
[(); return];
|
||||||
|
//~^ ERROR: return statement outside of function body [E0572]
|
||||||
|
//~| NOTE: the return is part of this body...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
//~^ NOTE: ...not the enclosing function body
|
//~^ NOTE: ...not the enclosing function body
|
||||||
[(); return || {
|
[(); return || {
|
||||||
|
|
|
@ -9,7 +9,31 @@ LL | | }]
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
||||||
error[E0572]: return statement outside of function body
|
error[E0572]: return statement outside of function body
|
||||||
--> $DIR/issue-86188-return-not-in-fn-body.rs:17:10
|
--> $DIR/issue-86188-return-not-in-fn-body.rs:20:14
|
||||||
|
|
|
||||||
|
LL | / fn bar() {
|
||||||
|
LL | |
|
||||||
|
LL | | [(); return];
|
||||||
|
| | ^^^^^^ the return is part of this body...
|
||||||
|
LL | |
|
||||||
|
LL | |
|
||||||
|
LL | | }
|
||||||
|
| |_____- ...not the enclosing function body
|
||||||
|
|
||||||
|
error[E0572]: return statement outside of function body
|
||||||
|
--> $DIR/issue-86188-return-not-in-fn-body.rs:28:14
|
||||||
|
|
|
||||||
|
LL | / fn foo() {
|
||||||
|
LL | |
|
||||||
|
LL | | [(); return];
|
||||||
|
| | ^^^^^^ the return is part of this body...
|
||||||
|
LL | |
|
||||||
|
LL | |
|
||||||
|
LL | | }
|
||||||
|
| |_____- ...not the enclosing function body
|
||||||
|
|
||||||
|
error[E0572]: return statement outside of function body
|
||||||
|
--> $DIR/issue-86188-return-not-in-fn-body.rs:36:10
|
||||||
|
|
|
|
||||||
LL | / fn main() {
|
LL | / fn main() {
|
||||||
LL | |
|
LL | |
|
||||||
|
@ -23,6 +47,6 @@ LL | || }];
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_- ...not the enclosing function body
|
| |_- ...not the enclosing function body
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0572`.
|
For more information about this error, try `rustc --explain E0572`.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error[E0572]: return statement outside of function body
|
error[E0572]: return statement outside of function body
|
||||||
--> $DIR/issue-86721-return-expr-ice.rs:6:22
|
--> $DIR/issue-86721-return-expr-ice.rs:9:22
|
||||||
|
|
|
|
||||||
LL | const U: usize = return;
|
LL | const U: usize = return;
|
||||||
| ^^^^^^
|
| ^^^^^^
|
|
@ -0,0 +1,9 @@
|
||||||
|
error[E0572]: return statement outside of function body
|
||||||
|
--> $DIR/issue-86721-return-expr-ice.rs:15:20
|
||||||
|
|
|
||||||
|
LL | fn foo(a: [(); return]);
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0572`.
|
|
@ -1,8 +1,17 @@
|
||||||
// Regression test for the ICE described in #86721.
|
// Regression test for the ICE described in #86721.
|
||||||
|
|
||||||
|
// revisions: rev1 rev2
|
||||||
|
#![cfg_attr(any(), rev1, rev2)]
|
||||||
#![crate_type="lib"]
|
#![crate_type="lib"]
|
||||||
|
|
||||||
|
#[cfg(any(rev1))]
|
||||||
trait T {
|
trait T {
|
||||||
const U: usize = return;
|
const U: usize = return;
|
||||||
//~^ ERROR: return statement outside of function body [E0572]
|
//[rev1]~^ ERROR: return statement outside of function body [E0572]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(rev2))]
|
||||||
|
trait T2 {
|
||||||
|
fn foo(a: [(); return]);
|
||||||
|
//[rev2]~^ ERROR: return statement outside of function body [E0572]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue