Skip to content

Commit 77f9a77

Browse files
committed
use closure.movability to judge coroutine
1 parent efa2fff commit 77f9a77

File tree

13 files changed

+35
-37
lines changed

13 files changed

+35
-37
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
@@ -2678,9 +2678,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26782678
let typeck_root_args = ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id);
26792679

26802680
let parent_args = match tcx.def_kind(def_id) {
2681-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => {
2682-
args.as_coroutine().parent_args()
2683-
}
2681+
DefKind::Closure if tcx.is_coroutine(def_id) => args.as_coroutine().parent_args(),
26842682
DefKind::Closure => args.as_closure().parent_args(),
26852683
DefKind::InlineConst => args.as_inline_const().parent_args(),
26862684
other => bug!("unexpected item {:?}", other),

compiler/rustc_hir_analysis/src/check/check.rs

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

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

compiler/rustc_interface/src/passes.rs

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

759759
tcx.hir().par_body_owners(|def_id| {
760-
if tcx.is_coroutine(def_id.to_def_id()) {
760+
if tcx.is_coroutine(def_id) {
761761
tcx.ensure().mir_coroutine_witnesses(def_id);
762762
tcx.ensure().check_coroutine_obligations(def_id);
763763
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ fn should_encode_mir(
10451045
| DefKind::Static(..)
10461046
| DefKind::Const => (true, false),
10471047
// Coroutines require optimized MIR to compute layout.
1048-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
1048+
DefKind::Closure if tcx.is_coroutine(def_id) => (false, true),
10491049
// Full-fledged functions + closures
10501050
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10511051
let generics = tcx.generics_of(def_id);
@@ -1345,7 +1345,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13451345
for local_id in tcx.iter_local_def_id() {
13461346
let def_id = local_id.to_def_id();
13471347
let def_kind = tcx.def_kind(local_id);
1348-
let is_coroutine = def_kind == DefKind::Closure && tcx.is_coroutine(def_id);
1348+
let is_coroutine = def_kind == DefKind::Closure && tcx.is_coroutine(local_id);
13491349
self.tables.def_kind.set_some(def_id.index, def_kind);
13501350
if should_encode_span(def_kind) {
13511351
let def_span = tcx.def_span(local_id);
@@ -1627,7 +1627,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16271627
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
16281628
<- tcx.closure_saved_names_of_captured_variables(def_id));
16291629

1630-
if self.tcx.is_coroutine(def_id.to_def_id())
1630+
if self.tcx.is_coroutine(def_id)
16311631
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16321632
{
16331633
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
@@ -1654,7 +1654,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16541654
}
16551655
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
16561656

1657-
if self.tcx.is_coroutine(def_id.to_def_id())
1657+
if self.tcx.is_coroutine(def_id)
16581658
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16591659
{
16601660
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
@@ -807,8 +807,18 @@ impl<'tcx> TyCtxt<'tcx> {
807807
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
808808
}
809809

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

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

compiler/rustc_middle/src/ty/mod.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -2464,16 +2464,10 @@ impl<'tcx> TyCtxt<'tcx> {
24642464

24652465
#[inline]
24662466
pub fn is_const_fn_raw(self, def_id: DefId) -> bool {
2467-
let def_kind = self.def_kind(def_id);
2468-
if def_kind == DefKind::Closure
2469-
&& !self.is_coroutine(def_id)
2470-
&& self.constness(def_id) == hir::Constness::Const
2471-
{
2472-
true
2473-
} else {
2474-
matches!(def_kind, DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..))
2475-
&& self.constness(def_id) == hir::Constness::Const
2476-
}
2467+
matches!(
2468+
self.def_kind(def_id),
2469+
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) | DefKind::Closure
2470+
) && self.constness(def_id) == hir::Constness::Const
24772471
}
24782472

24792473
#[inline]

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ fn construct_fn<'tcx>(
475475

476476
let mut abi = fn_sig.abi;
477477
if let DefKind::Closure = tcx.def_kind(fn_def)
478-
&& !tcx.is_coroutine(fn_def.to_def_id())
478+
&& !tcx.is_coroutine(fn_def)
479479
{
480480
// HACK(eddyb) Avoid having RustCall on closures,
481481
// as it adds unnecessary (and wrong) auto-tupling.

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-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +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.to_def_id()) => return false,
35+
DefKind::Closure if tcx.is_coroutine(def_id) => return false,
3636
DefKind::Ctor(..) | DefKind::Closure => return true,
3737
DefKind::Fn | DefKind::AssocFn => {}
3838
_ => return false,

compiler/rustc_mir_transform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +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.to_def_id()) => ConstQualifs::default(),
326+
DefKind::Closure if tcx.is_coroutine(def) => ConstQualifs::default(),
327327
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
328328
if tcx.constness(def) == hir::Constness::Const
329329
|| tcx.is_const_default_method(def.to_def_id()) =>
@@ -396,7 +396,7 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
396396
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
397397
/// end up missing the source MIR due to stealing happening.
398398
fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
399-
if tcx.is_coroutine(def.to_def_id()) {
399+
if tcx.is_coroutine(def) {
400400
tcx.ensure_with_value().mir_coroutine_witnesses(def);
401401
}
402402
let mir_borrowck = tcx.mir_borrowck(def);

0 commit comments

Comments
 (0)