Skip to content

Commit 7236802

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 e51e98d + 4fda2d8 commit 7236802

File tree

11 files changed

+34
-28
lines changed

11 files changed

+34
-28
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -2083,13 +2083,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20832083
.opt_item_name(self.mir_def_id().to_def_id())
20842084
.map(|name| format!("function `{name}`"))
20852085
.unwrap_or_else(|| {
2086-
match &self.infcx.tcx.def_kind(self.mir_def_id()) {
2087-
DefKind::Closure
2088-
if self
2089-
.infcx
2090-
.tcx
2091-
.is_coroutine(self.mir_def_id().to_def_id()) =>
2092-
{
2086+
let def_id = self.mir_def_id();
2087+
match &self.infcx.tcx.def_kind(def_id) {
2088+
DefKind::Closure if self.infcx.tcx.is_coroutine(def_id) => {
20932089
"enclosing coroutine"
20942090
}
20952091
DefKind::Closure => "enclosing closure",

compiler/rustc_borrowck/src/type_check/mod.rs

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

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

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ fn opaque_type_cycle_error(
14461446
label_match(capture.place.ty(), capture.get_path_span(tcx));
14471447
}
14481448
// Label any coroutine locals that capture the opaque
1449-
if tcx.is_coroutine(closure_def_id)
1449+
if tcx.is_coroutine(closure_local_did)
14501450
&& let Some(coroutine_layout) = tcx.mir_coroutine_witnesses(closure_def_id)
14511451
{
14521452
for interior_ty in &coroutine_layout.field_tys {
@@ -1467,7 +1467,7 @@ pub(super) fn check_coroutine_obligations(
14671467
tcx: TyCtxt<'_>,
14681468
def_id: LocalDefId,
14691469
) -> Result<(), ErrorGuaranteed> {
1470-
debug_assert!(tcx.is_coroutine(def_id.to_def_id()));
1470+
debug_assert!(tcx.is_coroutine(def_id));
14711471

14721472
let typeck = tcx.typeck(def_id);
14731473
let param_env = tcx.param_env(def_id);

compiler/rustc_interface/src/passes.rs

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

764764
tcx.hir().par_body_owners(|def_id| {
765-
if tcx.is_coroutine(def_id.to_def_id()) {
765+
if tcx.is_coroutine(def_id) {
766766
tcx.ensure().mir_coroutine_witnesses(def_id);
767767
tcx.ensure().check_coroutine_obligations(def_id);
768768
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
875875
}
876876
}
877877

878-
fn should_encode_attrs(def_kind: DefKind) -> bool {
878+
fn should_encode_attrs(def_kind: DefKind, is_coroutine: bool) -> bool {
879879
match def_kind {
880880
DefKind::Mod
881881
| DefKind::Struct
@@ -899,7 +899,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
899899
// closures from upstream crates, too. This is used by
900900
// https://github.com/model-checking/kani and is not a performance
901901
// or maintenance issue for us.
902-
DefKind::Closure => true,
902+
DefKind::Closure => !is_coroutine,
903903
DefKind::TyParam
904904
| DefKind::ConstParam
905905
| DefKind::Ctor(..)
@@ -1058,7 +1058,7 @@ fn should_encode_mir(
10581058
| DefKind::Static(..)
10591059
| DefKind::Const => (true, false),
10601060
// Coroutines require optimized MIR to compute layout.
1061-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
1061+
DefKind::Closure if tcx.is_coroutine(def_id) => (false, true),
10621062
// Full-fledged functions + closures
10631063
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10641064
let generics = tcx.generics_of(def_id);
@@ -1241,11 +1241,11 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
12411241
}
12421242
}
12431243

1244-
fn should_encode_constness(def_kind: DefKind) -> bool {
1244+
fn should_encode_constness(def_kind: DefKind, is_coroutine: bool) -> bool {
12451245
match def_kind {
1246+
DefKind::Closure => !is_coroutine,
12461247
DefKind::Fn
12471248
| DefKind::AssocFn
1248-
| DefKind::Closure
12491249
| DefKind::Impl { of_trait: true }
12501250
| DefKind::Variant
12511251
| DefKind::Ctor(..) => true,
@@ -1358,12 +1358,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13581358
for local_id in tcx.iter_local_def_id() {
13591359
let def_id = local_id.to_def_id();
13601360
let def_kind = tcx.def_kind(local_id);
1361+
let is_coroutine = def_kind == DefKind::Closure && tcx.is_coroutine(local_id);
13611362
self.tables.def_kind.set_some(def_id.index, def_kind);
13621363
if should_encode_span(def_kind) {
13631364
let def_span = tcx.def_span(local_id);
13641365
record!(self.tables.def_span[def_id] <- def_span);
13651366
}
1366-
if should_encode_attrs(def_kind) {
1367+
if should_encode_attrs(def_kind, is_coroutine) {
13671368
self.encode_attrs(local_id);
13681369
}
13691370
if should_encode_expn_that_defined(def_kind) {
@@ -1418,7 +1419,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14181419
if should_encode_type(tcx, local_id, def_kind) && !anon_const_without_hir {
14191420
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
14201421
}
1421-
if should_encode_constness(def_kind) {
1422+
if should_encode_constness(def_kind, is_coroutine) {
14221423
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
14231424
}
14241425
if let DefKind::Fn | DefKind::AssocFn = def_kind {
@@ -1639,7 +1640,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16391640
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
16401641
<- tcx.closure_saved_names_of_captured_variables(def_id));
16411642

1642-
if self.tcx.is_coroutine(def_id.to_def_id())
1643+
if self.tcx.is_coroutine(def_id)
16431644
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16441645
{
16451646
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
@@ -1666,7 +1667,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16661667
}
16671668
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
16681669

1669-
if self.tcx.is_coroutine(def_id.to_def_id())
1670+
if self.tcx.is_coroutine(def_id)
16701671
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16711672
{
16721673
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ impl<'tcx> TyCtxt<'tcx> {
843843
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
844844
}
845845

846-
pub fn is_coroutine(self, def_id: DefId) -> bool {
846+
pub fn is_coroutine(self, def_id: LocalDefId) -> bool {
847847
self.coroutine_kind(def_id).is_some()
848848
}
849849

compiler/rustc_mir_build/src/build/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ fn construct_fn<'tcx>(
480480
};
481481

482482
let mut abi = fn_sig.abi;
483-
if let DefKind::Closure = tcx.def_kind(fn_def) {
483+
if let DefKind::Closure = tcx.def_kind(fn_def)
484+
&& !tcx.is_coroutine(fn_def)
485+
{
484486
// HACK(eddyb) Avoid having RustCall on closures,
485487
// as it adds unnecessary (and wrong) auto-tupling.
486488
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_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

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
4242
_ => {}
4343
}
4444

45+
// This just reproduces the logic from Instance::requires_inline.
46+
match tcx.def_kind(def_id) {
47+
DefKind::Closure if tcx.is_coroutine(def_id) => return false,
48+
DefKind::Ctor(..) | DefKind::Closure => return true,
49+
DefKind::Fn | DefKind::AssocFn => {}
50+
_ => return false,
51+
}
52+
4553
// Don't do any inference when incremental compilation is enabled; the additional inlining that
4654
// inference permits also creates more work for small edits.
4755
if tcx.sess.opts.incremental.is_some() {

compiler/rustc_mir_transform/src/lib.rs

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

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

0 commit comments

Comments
 (0)