Skip to content

Commit 205cae8

Browse files
committed
Auto merge of #118411 - bvanjoi:merge_coroutinue_into_closure_2, r=<try>
aligns the behavior to that prior to #118311 After #118311, it seems that due to an oversight some alignments were unintentionally omitted, possibly leading the code into different branches. This PR attempts to restore those alignments and aims to fix the regression reported at #118319 (comment)
2 parents 8a7b203 + 77f9a77 commit 205cae8

File tree

12 files changed

+39
-30
lines changed

12 files changed

+39
-30
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -2071,13 +2071,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20712071
.opt_item_name(self.mir_def_id().to_def_id())
20722072
.map(|name| format!("function `{name}`"))
20732073
.unwrap_or_else(|| {
2074-
match &self.infcx.tcx.def_kind(self.mir_def_id()) {
2075-
DefKind::Closure
2076-
if self
2077-
.infcx
2078-
.tcx
2079-
.is_coroutine(self.mir_def_id().to_def_id()) =>
2080-
{
2074+
let def_id = self.mir_def_id();
2075+
match &self.infcx.tcx.def_kind(def_id) {
2076+
DefKind::Closure if self.infcx.tcx.is_coroutine(def_id) => {
20812077
"enclosing coroutine"
20822078
}
20832079
DefKind::Closure => "enclosing closure",

compiler/rustc_borrowck/src/type_check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2664,9 +2664,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26642664
let typeck_root_args = ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id);
26652665

26662666
let parent_args = match tcx.def_kind(def_id) {
2667-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => {
2668-
args.as_coroutine().parent_args()
2669-
}
2667+
DefKind::Closure if tcx.is_coroutine(def_id) => args.as_coroutine().parent_args(),
26702668
DefKind::Closure => args.as_closure().parent_args(),
26712669
DefKind::InlineConst => args.as_inline_const().parent_args(),
26722670
other => bug!("unexpected item {:?}", other),

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ fn opaque_type_cycle_error(
14501450
label_match(capture.place.ty(), capture.get_path_span(tcx));
14511451
}
14521452
// Label any coroutine locals that capture the opaque
1453-
if tcx.is_coroutine(closure_def_id)
1453+
if tcx.is_coroutine(closure_local_did)
14541454
&& let Some(coroutine_layout) = tcx.mir_coroutine_witnesses(closure_def_id)
14551455
{
14561456
for interior_ty in &coroutine_layout.field_tys {
@@ -1471,7 +1471,7 @@ pub(super) fn check_coroutine_obligations(
14711471
tcx: TyCtxt<'_>,
14721472
def_id: LocalDefId,
14731473
) -> Result<(), ErrorGuaranteed> {
1474-
debug_assert!(tcx.is_coroutine(def_id.to_def_id()));
1474+
debug_assert!(tcx.is_coroutine(def_id));
14751475

14761476
let typeck = tcx.typeck(def_id);
14771477
let param_env = tcx.param_env(def_id);

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
755755
});
756756

757757
tcx.hir().par_body_owners(|def_id| {
758-
if tcx.is_coroutine(def_id.to_def_id()) {
758+
if tcx.is_coroutine(def_id) {
759759
tcx.ensure().mir_coroutine_witnesses(def_id);
760760
tcx.ensure().check_coroutine_obligations(def_id);
761761
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
861861
}
862862
}
863863

864-
fn should_encode_attrs(def_kind: DefKind) -> bool {
864+
fn should_encode_attrs(def_kind: DefKind, is_coroutine: bool) -> bool {
865865
match def_kind {
866866
DefKind::Mod
867867
| DefKind::Struct
@@ -885,7 +885,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
885885
// closures from upstream crates, too. This is used by
886886
// https://github.com/model-checking/kani and is not a performance
887887
// or maintenance issue for us.
888-
DefKind::Closure => true,
888+
DefKind::Closure => !is_coroutine,
889889
DefKind::TyParam
890890
| DefKind::ConstParam
891891
| DefKind::Ctor(..)
@@ -1044,7 +1044,7 @@ fn should_encode_mir(
10441044
| DefKind::Static(..)
10451045
| DefKind::Const => (true, false),
10461046
// Coroutines require optimized MIR to compute layout.
1047-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
1047+
DefKind::Closure if tcx.is_coroutine(def_id) => (false, true),
10481048
// Full-fledged functions + closures
10491049
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10501050
let generics = tcx.generics_of(def_id);
@@ -1227,11 +1227,11 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
12271227
}
12281228
}
12291229

1230-
fn should_encode_constness(def_kind: DefKind) -> bool {
1230+
fn should_encode_constness(def_kind: DefKind, is_coroutine: bool) -> bool {
12311231
match def_kind {
1232+
DefKind::Closure => !is_coroutine,
12321233
DefKind::Fn
12331234
| DefKind::AssocFn
1234-
| DefKind::Closure
12351235
| DefKind::Impl { of_trait: true }
12361236
| DefKind::Variant
12371237
| DefKind::Ctor(..) => true,
@@ -1344,12 +1344,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13441344
for local_id in tcx.iter_local_def_id() {
13451345
let def_id = local_id.to_def_id();
13461346
let def_kind = tcx.def_kind(local_id);
1347+
let is_coroutine = def_kind == DefKind::Closure && tcx.is_coroutine(local_id);
13471348
self.tables.def_kind.set_some(def_id.index, def_kind);
13481349
if should_encode_span(def_kind) {
13491350
let def_span = tcx.def_span(local_id);
13501351
record!(self.tables.def_span[def_id] <- def_span);
13511352
}
1352-
if should_encode_attrs(def_kind) {
1353+
if should_encode_attrs(def_kind, is_coroutine) {
13531354
self.encode_attrs(local_id);
13541355
}
13551356
if should_encode_expn_that_defined(def_kind) {
@@ -1404,7 +1405,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14041405
if should_encode_type(tcx, local_id, def_kind) && !anon_const_without_hir {
14051406
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
14061407
}
1407-
if should_encode_constness(def_kind) {
1408+
if should_encode_constness(def_kind, is_coroutine) {
14081409
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
14091410
}
14101411
if let DefKind::Fn | DefKind::AssocFn = def_kind {
@@ -1625,7 +1626,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16251626
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
16261627
<- tcx.closure_saved_names_of_captured_variables(def_id));
16271628

1628-
if self.tcx.is_coroutine(def_id.to_def_id())
1629+
if self.tcx.is_coroutine(def_id)
16291630
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16301631
{
16311632
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
@@ -1652,7 +1653,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16521653
}
16531654
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
16541655

1655-
if self.tcx.is_coroutine(def_id.to_def_id())
1656+
if self.tcx.is_coroutine(def_id)
16561657
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16571658
{
16581659
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);

compiler/rustc_middle/src/ty/context.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,18 @@ impl<'tcx> TyCtxt<'tcx> {
809809
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
810810
}
811811

812-
pub fn is_coroutine(self, def_id: DefId) -> bool {
813-
self.coroutine_kind(def_id).is_some()
812+
pub fn is_coroutine(self, def_id: LocalDefId) -> bool {
813+
let hir_id = self.local_def_id_to_hir_id(def_id);
814+
let Some(node) = self.hir().find(hir_id) else {
815+
return false;
816+
};
817+
if let hir::Node::Expr(expr) = node
818+
&& let hir::ExprKind::Closure(closure) = expr.kind
819+
{
820+
closure.movability.is_some()
821+
} else {
822+
false
823+
}
814824
}
815825

816826
/// Returns `true` if the node pointed to by `def_id` is a coroutine for an async construct.

compiler/rustc_mir_build/src/build/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ fn construct_fn<'tcx>(
474474
};
475475

476476
let mut abi = fn_sig.abi;
477-
if let DefKind::Closure = tcx.def_kind(fn_def) {
477+
if let DefKind::Closure = tcx.def_kind(fn_def)
478+
&& !tcx.is_coroutine(fn_def)
479+
{
478480
// HACK(eddyb) Avoid having RustCall on closures,
479481
// as it adds unnecessary (and wrong) auto-tupling.
480482
abi = Abi::Rust;

compiler/rustc_mir_build/src/thir/cx/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) fn thir_body(
3737

3838
// The resume argument may be missing, in that case we need to provide it here.
3939
// It will always be `()` in this case.
40-
if tcx.is_coroutine(owner_def.to_def_id()) && body.params.is_empty() {
40+
if tcx.is_coroutine(owner_def) && body.params.is_empty() {
4141
cx.thir.params.push(Param {
4242
ty: Ty::new_unit(tcx),
4343
pat: None,
@@ -119,7 +119,7 @@ impl<'tcx> Cx<'tcx> {
119119

120120
fn closure_env_param(&self, owner_def: LocalDefId, owner_id: HirId) -> Option<Param<'tcx>> {
121121
match self.tcx.def_kind(owner_def) {
122-
DefKind::Closure if self.tcx.is_coroutine(owner_def.to_def_id()) => {
122+
DefKind::Closure if self.tcx.is_coroutine(owner_def) => {
123123
let coroutine_ty = self.typeck_results.node_type(owner_id);
124124
let coroutine_param = Param {
125125
ty: coroutine_ty,

compiler/rustc_mir_transform/src/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
8484

8585
// FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
8686
// computing their layout.
87-
if tcx.is_coroutine(def_id.to_def_id()) {
87+
if def_kind == DefKind::Closure && tcx.is_coroutine(def_id) {
8888
trace!("ConstProp skipped for coroutine {:?}", def_id);
8989
return;
9090
}

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
6161

6262
// FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
6363
// computing their layout.
64-
if tcx.is_coroutine(def_id.to_def_id()) {
64+
if def_kind == DefKind::Closure && tcx.is_coroutine(def_id) {
6565
trace!("ConstPropLint skipped for coroutine {:?}", def_id);
6666
return;
6767
}

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3232

3333
// This just reproduces the logic from Instance::requires_inline.
3434
match tcx.def_kind(def_id) {
35+
DefKind::Closure if tcx.is_coroutine(def_id) => return false,
3536
DefKind::Ctor(..) | DefKind::Closure => return true,
3637
DefKind::Fn | DefKind::AssocFn => {}
3738
_ => return false,

compiler/rustc_mir_transform/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ fn mir_promoted(
323323
// Also this means promotion can rely on all const checks having been done.
324324

325325
let const_qualifs = match tcx.def_kind(def) {
326+
DefKind::Closure if tcx.is_coroutine(def) => ConstQualifs::default(),
326327
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
327328
if tcx.constness(def) == hir::Constness::Const
328329
|| tcx.is_const_default_method(def.to_def_id()) =>
@@ -395,7 +396,7 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
395396
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
396397
/// end up missing the source MIR due to stealing happening.
397398
fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
398-
if tcx.is_coroutine(def.to_def_id()) {
399+
if tcx.is_coroutine(def) {
399400
tcx.ensure_with_value().mir_coroutine_witnesses(def);
400401
}
401402
let mir_borrowck = tcx.mir_borrowck(def);

0 commit comments

Comments
 (0)