Skip to content

Commit 6721f91

Browse files
committed
Check if enclosing body owner exists in get_fn_id_for_return_block
1 parent feeba19 commit 6721f91

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,22 @@ impl<'hir> Map<'hir> {
246246
self.tcx.hir_node(hir_id).fn_sig()
247247
}
248248

249-
#[track_caller]
250-
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
249+
pub fn opt_enclosing_body_owner(self, hir_id: HirId) -> Option<LocalDefId> {
251250
for (_, node) in self.parent_iter(hir_id) {
252251
if let Some((def_id, _)) = node.associated_body() {
253-
return def_id;
252+
return Some(def_id);
254253
}
255254
}
256255

256+
None
257+
}
258+
259+
#[track_caller]
260+
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
261+
if let Some(hir_id) = self.opt_enclosing_body_owner(hir_id) {
262+
return hir_id;
263+
}
264+
257265
bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
258266
}
259267

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

559569
// Return `None` if the `id` expression is not the returned value of the enclosing body
560570
let mut iter = [id].into_iter().chain(self.parent_id_iter(id)).peekable();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition: 2021
2+
3+
async fn a() {
4+
//~^ ERROR `()` is not a future
5+
//~| ERROR mismatched types
6+
a() //~ ERROR `()` is not a future
7+
}
8+
9+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0277]: `()` is not a future
2+
--> $DIR/recurse-ice-129215.rs:6:5
3+
|
4+
LL | a()
5+
| ^^^ `()` is not a future
6+
|
7+
= help: the trait `Future` is not implemented for `()`
8+
9+
error[E0277]: `()` is not a future
10+
--> $DIR/recurse-ice-129215.rs:3:1
11+
|
12+
LL | async fn a() {
13+
| ^^^^^^^^^^^^ `()` is not a future
14+
|
15+
= help: the trait `Future` is not implemented for `()`
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/recurse-ice-129215.rs:3:14
19+
|
20+
LL | async fn a() {
21+
| ______________^
22+
LL | |
23+
LL | |
24+
LL | | a()
25+
LL | | }
26+
| |_^ expected `()`, found `async` fn body
27+
|
28+
= note: expected unit type `()`
29+
found `async` fn body `{async fn body of a()}`
30+
31+
error: aborting due to 3 previous errors
32+
33+
Some errors have detailed explanations: E0277, E0308.
34+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)