Skip to content

Don't call closure_by_move_body_def_id on FnOnce async closures in MIR validation #130199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
rustc_hir_analysis::check_crate(tcx);
sess.time("MIR_coroutine_by_move_body", || {
tcx.hir().par_body_owners(|def_id| {
if tcx.needs_coroutine_by_move_body_def_id(def_id) {
if tcx.needs_coroutine_by_move_body_def_id(def_id.to_def_id()) {
tcx.ensure_with_value().coroutine_by_move_body_def_id(def_id);
}
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ impl<'tcx> TyCtxt<'tcx> {
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
}

pub fn needs_coroutine_by_move_body_def_id(self, def_id: LocalDefId) -> bool {
pub fn needs_coroutine_by_move_body_def_id(self, def_id: DefId) -> bool {
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure)) =
self.coroutine_kind(def_id)
&& let ty::Coroutine(_, args) = self.type_of(def_id).instantiate_identity().kind()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn mir_promoted(
tcx.ensure_with_value().has_ffi_unwind_calls(def);

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

Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_mir_transform/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Validates the MIR to ensure that invariants are upheld.

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::LangItem;
use rustc_index::bit_set::BitSet;
use rustc_index::IndexVec;
Expand Down Expand Up @@ -716,10 +715,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
// first place.
let layout = if def_id == self.caller_body.source.def_id() {
self.caller_body.coroutine_layout_raw()
} else if let Some(hir::CoroutineKind::Desugared(
_,
hir::CoroutineSource::Closure,
)) = self.tcx.coroutine_kind(def_id)
} else if self.tcx.needs_coroutine_by_move_body_def_id(def_id)
&& let ty::ClosureKind::FnOnce =
args.as_coroutine().kind_ty().to_opt_closure_kind().unwrap()
&& self.caller_body.source.def_id()
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/async-await/async-closures/body-check-on-non-fnmut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ aux-build:block-on.rs
//@ edition:2021
//@ build-pass

#![feature(async_closure)]

extern crate block_on;

// Make sure that we don't call `coroutine_by_move_body_def_id` query
// on async closures that are `FnOnce`. See issue: #130167.

async fn empty() {}

pub async fn call_once<F: async FnOnce()>(f: F) {
f().await;
}

fn main() {
block_on::block_on(call_once(async || empty().await));
}
Loading