Skip to content

Commit

Permalink
Check if enclosing body owner exists in get_fn_id_for_return_block
Browse files Browse the repository at this point in the history
  • Loading branch information
wafarm committed Aug 18, 2024
1 parent feeba19 commit 6721f91
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
18 changes: 14 additions & 4 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,22 @@ impl<'hir> Map<'hir> {
self.tcx.hir_node(hir_id).fn_sig()
}

#[track_caller]
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
pub fn opt_enclosing_body_owner(self, hir_id: HirId) -> Option<LocalDefId> {
for (_, node) in self.parent_iter(hir_id) {
if let Some((def_id, _)) = node.associated_body() {
return def_id;
return Some(def_id);
}
}

None
}

#[track_caller]
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
if let Some(hir_id) = self.opt_enclosing_body_owner(hir_id) {
return hir_id;
}

bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
}

Expand Down Expand Up @@ -554,7 +562,9 @@ impl<'hir> Map<'hir> {
/// }
/// ```
pub fn get_fn_id_for_return_block(self, id: HirId) -> Option<HirId> {
let enclosing_body_owner = self.tcx.local_def_id_to_hir_id(self.enclosing_body_owner(id));
// Id may not have a owner if it's a fn itself
let enclosing_body_owner =
self.tcx.local_def_id_to_hir_id(self.opt_enclosing_body_owner(id)?);

// Return `None` if the `id` expression is not the returned value of the enclosing body
let mut iter = [id].into_iter().chain(self.parent_id_iter(id)).peekable();
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/async-await/async-fn/recurse-ice-129215.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ edition: 2021

async fn a() {
//~^ ERROR `()` is not a future
//~| ERROR mismatched types
a() //~ ERROR `()` is not a future
}

fn main() {}
34 changes: 34 additions & 0 deletions tests/ui/async-await/async-fn/recurse-ice-129215.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
error[E0277]: `()` is not a future
--> $DIR/recurse-ice-129215.rs:6:5
|
LL | a()
| ^^^ `()` is not a future
|
= help: the trait `Future` is not implemented for `()`

error[E0277]: `()` is not a future
--> $DIR/recurse-ice-129215.rs:3:1
|
LL | async fn a() {
| ^^^^^^^^^^^^ `()` is not a future
|
= help: the trait `Future` is not implemented for `()`

error[E0308]: mismatched types
--> $DIR/recurse-ice-129215.rs:3:14
|
LL | async fn a() {
| ______________^
LL | |
LL | |
LL | | a()
LL | | }
| |_^ expected `()`, found `async` fn body
|
= note: expected unit type `()`
found `async` fn body `{async fn body of a()}`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.

0 comments on commit 6721f91

Please sign in to comment.