Skip to content

Commit

Permalink
Auto merge of rust-lang#118411 - bvanjoi:merge_coroutinue_into_closur…
Browse files Browse the repository at this point in the history
…e_2, r=<try>

aligns the behavior to that prior to rust-lang#118311

After rust-lang#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 rust-lang#118319 (comment)
  • Loading branch information
bors committed Nov 28, 2023
2 parents df0295f + 4307541 commit 3c773cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
13 changes: 7 additions & 6 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
}
}

fn should_encode_attrs(def_kind: DefKind) -> bool {
fn should_encode_attrs(def_kind: DefKind, is_coroutine: bool) -> bool {
match def_kind {
DefKind::Mod
| DefKind::Struct
Expand All @@ -886,7 +886,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
// closures from upstream crates, too. This is used by
// https://github.com/model-checking/kani and is not a performance
// or maintenance issue for us.
DefKind::Closure => true,
DefKind::Closure => !is_coroutine,
DefKind::TyParam
| DefKind::ConstParam
| DefKind::Ctor(..)
Expand Down Expand Up @@ -1228,11 +1228,11 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
}
}

fn should_encode_constness(def_kind: DefKind) -> bool {
fn should_encode_constness(def_kind: DefKind, is_coroutine: bool) -> bool {
match def_kind {
DefKind::Closure => !is_coroutine,
DefKind::Fn
| DefKind::AssocFn
| DefKind::Closure
| DefKind::Impl { of_trait: true }
| DefKind::Variant
| DefKind::Ctor(..) => true,
Expand Down Expand Up @@ -1345,12 +1345,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
for local_id in tcx.iter_local_def_id() {
let def_id = local_id.to_def_id();
let def_kind = tcx.def_kind(local_id);
let is_coroutine = def_kind == DefKind::Closure && tcx.is_coroutine(def_id);
self.tables.def_kind.set_some(def_id.index, def_kind);
if should_encode_span(def_kind) {
let def_span = tcx.def_span(local_id);
record!(self.tables.def_span[def_id] <- def_span);
}
if should_encode_attrs(def_kind) {
if should_encode_attrs(def_kind, is_coroutine) {
self.encode_attrs(local_id);
}
if should_encode_expn_that_defined(def_kind) {
Expand Down Expand Up @@ -1405,7 +1406,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if should_encode_type(tcx, local_id, def_kind) && !anon_const_without_hir {
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
}
if should_encode_constness(def_kind) {
if should_encode_constness(def_kind, is_coroutine) {
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
}
if let DefKind::Fn | DefKind::AssocFn = def_kind {
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,10 +2467,16 @@ impl<'tcx> TyCtxt<'tcx> {

#[inline]
pub fn is_const_fn_raw(self, def_id: DefId) -> bool {
matches!(
self.def_kind(def_id),
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) | DefKind::Closure
) && self.constness(def_id) == hir::Constness::Const
let def_kind = self.def_kind(def_id);
if def_kind == DefKind::Closure
&& !self.is_coroutine(def_id)
&& self.constness(def_id) == hir::Constness::Const
{
true
} else {
matches!(def_kind, DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..))
&& self.constness(def_id) == hir::Constness::Const
}
}

#[inline]
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ fn construct_fn<'tcx>(
};

let mut abi = fn_sig.abi;
if let DefKind::Closure = tcx.def_kind(fn_def) {
if let DefKind::Closure = tcx.def_kind(fn_def)
&& !tcx.is_coroutine(fn_def.to_def_id())
{
// HACK(eddyb) Avoid having RustCall on closures,
// as it adds unnecessary (and wrong) auto-tupling.
abi = Abi::Rust;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/cross_crate_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {

// This just reproduces the logic from Instance::requires_inline.
match tcx.def_kind(def_id) {
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => return false,
DefKind::Ctor(..) | DefKind::Closure => return true,
DefKind::Fn | DefKind::AssocFn => {}
_ => return false,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ fn mir_promoted(
// Also this means promotion can rely on all const checks having been done.

let const_qualifs = match tcx.def_kind(def) {
DefKind::Closure if tcx.is_coroutine(def.to_def_id()) => ConstQualifs::default(),
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
if tcx.constness(def) == hir::Constness::Const
|| tcx.is_const_default_method(def.to_def_id()) =>
Expand Down

0 comments on commit 3c773cd

Please sign in to comment.