Skip to content

Commit 9d46af1

Browse files
Rollup merge of rust-lang#141584 - compiler-errors:typing-env-synthetic-body, r=lcnr
Support `opaque_types_defined_by` for `SyntheticCoroutineBody` We create a synthetic MIR body for the `AsyncFnOnce` impl for async closures. That body goes through all passes that a regular body does, including promotion. Promotion sometimes requires computing that the type of an rvalue is `Freeze`, which requires computing the typing env of a body. This requires calling `opaque_types_defined_by` on the body's def id, which leads to an ICE today since we don't expect that query to be called for synthetic bodies. While we could fix this by, for example, computing the typeck root of the body before calling a `TypingEnv` constructor, I think it's appropriate to do a more general fix here since I think it's reasonable that other passes might do analysis too. Fixes rust-lang#141466 r? ```@lcnr``` or ```@oli-obk```
2 parents fb4cc99 + 5e31cd3 commit 9d46af1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ fn opaque_types_defined_by<'tcx>(
321321
collector.collect_taits_declared_in_body();
322322
}
323323
// Closures and coroutines are type checked with their parent
324-
DefKind::Closure | DefKind::InlineConst => {
324+
// Note that we also support `SyntheticCoroutineBody` since we create
325+
// a MIR body for the def kind, and some MIR passes (like promotion)
326+
// may require doing analysis using its typing env.
327+
DefKind::Closure | DefKind::InlineConst | DefKind::SyntheticCoroutineBody => {
325328
collector.opaques.extend(tcx.opaque_types_defined_by(tcx.local_parent(item)));
326329
}
327330
DefKind::AssocTy | DefKind::TyAlias | DefKind::GlobalAsm => {}
@@ -343,8 +346,7 @@ fn opaque_types_defined_by<'tcx>(
343346
| DefKind::ForeignMod
344347
| DefKind::Field
345348
| DefKind::LifetimeParam
346-
| DefKind::Impl { .. }
347-
| DefKind::SyntheticCoroutineBody => {
349+
| DefKind::Impl { .. } => {
348350
span_bug!(
349351
tcx.def_span(item),
350352
"`opaque_types_defined_by` not defined for {} `{item:?}`",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ build-pass
2+
//@ compile-flags: --crate-type=lib
3+
//@ edition: 2024
4+
5+
union U {
6+
f: i32,
7+
}
8+
9+
fn foo() {
10+
async || {
11+
&U { f: 1 }
12+
};
13+
}

0 commit comments

Comments
 (0)