Skip to content

Commit 04e744e

Browse files
authored
Rollup merge of #130199 - compiler-errors:by-move, r=cjgillot
Don't call closure_by_move_body_def_id on FnOnce async closures in MIR validation Refactors the check in #129847 to not unncessarily call the `closure_by_move_body_def_id` query for async closures that don't *need* a by-move body. Fixes #130167
2 parents 2bf0dd2 + 5cf117e commit 04e744e

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
788788
rustc_hir_analysis::check_crate(tcx);
789789
sess.time("MIR_coroutine_by_move_body", || {
790790
tcx.hir().par_body_owners(|def_id| {
791-
if tcx.needs_coroutine_by_move_body_def_id(def_id) {
791+
if tcx.needs_coroutine_by_move_body_def_id(def_id.to_def_id()) {
792792
tcx.ensure_with_value().coroutine_by_move_body_def_id(def_id);
793793
}
794794
});

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ impl<'tcx> TyCtxt<'tcx> {
31713171
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
31723172
}
31733173

3174-
pub fn needs_coroutine_by_move_body_def_id(self, def_id: LocalDefId) -> bool {
3174+
pub fn needs_coroutine_by_move_body_def_id(self, def_id: DefId) -> bool {
31753175
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure)) =
31763176
self.coroutine_kind(def_id)
31773177
&& let ty::Coroutine(_, args) = self.type_of(def_id).instantiate_identity().kind()

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn mir_promoted(
330330
tcx.ensure_with_value().has_ffi_unwind_calls(def);
331331

332332
// the `by_move_body` query uses the raw mir, so make sure it is run.
333-
if tcx.needs_coroutine_by_move_body_def_id(def) {
333+
if tcx.needs_coroutine_by_move_body_def_id(def.to_def_id()) {
334334
tcx.ensure_with_value().coroutine_by_move_body_def_id(def);
335335
}
336336

compiler/rustc_mir_transform/src/validate.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Validates the MIR to ensure that invariants are upheld.
22
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4-
use rustc_hir as hir;
54
use rustc_hir::LangItem;
65
use rustc_index::bit_set::BitSet;
76
use rustc_index::IndexVec;
@@ -717,10 +716,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
717716
// first place.
718717
let layout = if def_id == self.caller_body.source.def_id() {
719718
self.caller_body.coroutine_layout_raw()
720-
} else if let Some(hir::CoroutineKind::Desugared(
721-
_,
722-
hir::CoroutineSource::Closure,
723-
)) = self.tcx.coroutine_kind(def_id)
719+
} else if self.tcx.needs_coroutine_by_move_body_def_id(def_id)
724720
&& let ty::ClosureKind::FnOnce =
725721
args.as_coroutine().kind_ty().to_opt_closure_kind().unwrap()
726722
&& self.caller_body.source.def_id()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ aux-build:block-on.rs
2+
//@ edition:2021
3+
//@ build-pass
4+
5+
#![feature(async_closure)]
6+
7+
extern crate block_on;
8+
9+
// Make sure that we don't call `coroutine_by_move_body_def_id` query
10+
// on async closures that are `FnOnce`. See issue: #130167.
11+
12+
async fn empty() {}
13+
14+
pub async fn call_once<F: async FnOnce()>(f: F) {
15+
f().await;
16+
}
17+
18+
fn main() {
19+
block_on::block_on(call_once(async || empty().await));
20+
}

0 commit comments

Comments
 (0)