Skip to content

Commit c6d20d7

Browse files
Rollup merge of #118311 - bvanjoi:merge_coroutinue_into_closure, r=petrochenkov
merge `DefKind::Coroutine` into `Defkind::Closure` Related to #118188 We no longer need to be concerned about the precise type whether it's `DefKind::Closure` or `DefKind::Coroutine`. Furthermore, thanks for the great work done by `@petrochenkov` on investigating https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Why.20does.20it.20hang.20when.20querying.20.EF.BB.BF.60opt_def_kind.60.3F r? `@petrochenkov`
2 parents c67613b + f23befe commit c6d20d7

File tree

27 files changed

+83
-94
lines changed

27 files changed

+83
-94
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
8989
}
9090
}
9191

92-
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node: node });
92+
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node });
9393
}
9494

9595
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2072,8 +2072,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20722072
.map(|name| format!("function `{name}`"))
20732073
.unwrap_or_else(|| {
20742074
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+
{
2081+
"enclosing coroutine"
2082+
}
20752083
DefKind::Closure => "enclosing closure",
2076-
DefKind::Coroutine => "enclosing coroutine",
20772084
kind => bug!("expected closure or coroutine, found {:?}", kind),
20782085
}
20792086
.to_string()

compiler/rustc_borrowck/src/type_check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2678,8 +2678,10 @@ 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+
}
26812684
DefKind::Closure => args.as_closure().parent_args(),
2682-
DefKind::Coroutine => args.as_coroutine().parent_args(),
26832685
DefKind::InlineConst => args.as_inline_const().parent_args(),
26842686
other => bug!("unexpected item {:?}", other),
26852687
};

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,7 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
373373
// just "functions", like consts, statics, etc. Filter those out.
374374
// If `ignore_unused_generics` was specified, filter out any
375375
// generic functions from consideration as well.
376-
if !matches!(
377-
kind,
378-
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Coroutine
379-
) {
376+
if !matches!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Closure) {
380377
return None;
381378
}
382379
if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {

compiler/rustc_hir/src/def.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub enum DefKind {
114114
of_trait: bool,
115115
},
116116
Closure,
117-
Coroutine,
118117
}
119118

120119
impl DefKind {
@@ -157,7 +156,6 @@ impl DefKind {
157156
DefKind::Field => "field",
158157
DefKind::Impl { .. } => "implementation",
159158
DefKind::Closure => "closure",
160-
DefKind::Coroutine => "coroutine",
161159
DefKind::ExternCrate => "extern crate",
162160
DefKind::GlobalAsm => "global assembly block",
163161
}
@@ -216,7 +214,6 @@ impl DefKind {
216214
| DefKind::LifetimeParam
217215
| DefKind::ExternCrate
218216
| DefKind::Closure
219-
| DefKind::Coroutine
220217
| DefKind::Use
221218
| DefKind::ForeignMod
222219
| DefKind::GlobalAsm
@@ -226,7 +223,7 @@ impl DefKind {
226223

227224
#[inline]
228225
pub fn is_fn_like(self) -> bool {
229-
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Coroutine)
226+
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure)
230227
}
231228

232229
/// Whether `query get_codegen_attrs` should be used with this definition.
@@ -236,7 +233,6 @@ impl DefKind {
236233
| DefKind::AssocFn
237234
| DefKind::Ctor(..)
238235
| DefKind::Closure
239-
| DefKind::Coroutine
240236
| DefKind::Static(_) => true,
241237
DefKind::Mod
242238
| DefKind::Struct

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 let DefKind::Coroutine = tcx.def_kind(closure_def_id)
1452+
if tcx.is_coroutine(closure_def_id)
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!(matches!(tcx.def_kind(def_id), DefKind::Coroutine));
1473+
debug_assert!(tcx.is_coroutine(def_id.to_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
@@ -756,7 +756,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
756756
});
757757

758758
tcx.hir().par_body_owners(|def_id| {
759-
if let rustc_hir::def::DefKind::Coroutine = tcx.def_kind(def_id) {
759+
if tcx.is_coroutine(def_id.to_def_id()) {
760760
tcx.ensure().mir_coroutine_witnesses(def_id);
761761
tcx.ensure().check_coroutine_obligations(def_id);
762762
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
856856
| DefKind::OpaqueTy
857857
| DefKind::Field
858858
| DefKind::Impl { .. }
859-
| DefKind::Closure
860-
| DefKind::Coroutine => true,
859+
| DefKind::Closure => true,
861860
DefKind::ForeignMod | DefKind::GlobalAsm => false,
862861
}
863862
}
@@ -897,8 +896,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
897896
| DefKind::InlineConst
898897
| DefKind::OpaqueTy
899898
| DefKind::LifetimeParam
900-
| DefKind::GlobalAsm
901-
| DefKind::Coroutine => false,
899+
| DefKind::GlobalAsm => false,
902900
}
903901
}
904902

@@ -933,8 +931,7 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
933931
| DefKind::Field
934932
| DefKind::LifetimeParam
935933
| DefKind::GlobalAsm
936-
| DefKind::Closure
937-
| DefKind::Coroutine => false,
934+
| DefKind::Closure => false,
938935
}
939936
}
940937

@@ -969,7 +966,6 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
969966
| DefKind::GlobalAsm
970967
| DefKind::Impl { .. }
971968
| DefKind::Closure
972-
| DefKind::Coroutine
973969
| DefKind::ExternCrate => false,
974970
}
975971
}
@@ -1005,7 +1001,6 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
10051001
| DefKind::InlineConst
10061002
| DefKind::GlobalAsm
10071003
| DefKind::Closure
1008-
| DefKind::Coroutine
10091004
| DefKind::ExternCrate => false,
10101005
}
10111006
}
@@ -1048,6 +1043,8 @@ fn should_encode_mir(
10481043
| DefKind::AssocConst
10491044
| DefKind::Static(..)
10501045
| DefKind::Const => (true, false),
1046+
// Coroutines require optimized MIR to compute layout.
1047+
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
10511048
// Full-fledged functions + closures
10521049
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10531050
let generics = tcx.generics_of(def_id);
@@ -1061,8 +1058,6 @@ fn should_encode_mir(
10611058
|| tcx.is_const_default_method(def_id.to_def_id());
10621059
(is_const_fn, opt)
10631060
}
1064-
// Coroutines require optimized MIR to compute layout.
1065-
DefKind::Coroutine => (false, true),
10661061
// The others don't have MIR.
10671062
_ => (false, false),
10681063
}
@@ -1098,7 +1093,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
10981093
| DefKind::InlineConst
10991094
| DefKind::GlobalAsm
11001095
| DefKind::Closure
1101-
| DefKind::Coroutine
11021096
| DefKind::ExternCrate => false,
11031097
DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
11041098
}
@@ -1127,8 +1121,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
11271121
| DefKind::Impl { .. }
11281122
| DefKind::Field
11291123
| DefKind::TyParam
1130-
| DefKind::Closure
1131-
| DefKind::Coroutine => true,
1124+
| DefKind::Closure => true,
11321125
DefKind::Mod
11331126
| DefKind::ForeignMod
11341127
| DefKind::ConstParam
@@ -1157,7 +1150,6 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11571150
| DefKind::AssocFn
11581151
| DefKind::AssocConst
11591152
| DefKind::Closure
1160-
| DefKind::Coroutine
11611153
| DefKind::ConstParam
11621154
| DefKind::AnonConst
11631155
| DefKind::InlineConst => true,
@@ -1218,7 +1210,6 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
12181210
| DefKind::Impl { .. }
12191211
| DefKind::AssocConst
12201212
| DefKind::Closure
1221-
| DefKind::Coroutine
12221213
| DefKind::ConstParam
12231214
| DefKind::AnonConst
12241215
| DefKind::InlineConst
@@ -1257,7 +1248,6 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12571248
| DefKind::OpaqueTy
12581249
| DefKind::Impl { of_trait: false }
12591250
| DefKind::ForeignTy
1260-
| DefKind::Coroutine
12611251
| DefKind::ConstParam
12621252
| DefKind::InlineConst
12631253
| DefKind::AssocTy
@@ -1292,7 +1282,6 @@ fn should_encode_const(def_kind: DefKind) -> bool {
12921282
| DefKind::Impl { .. }
12931283
| DefKind::AssocFn
12941284
| DefKind::Closure
1295-
| DefKind::Coroutine
12961285
| DefKind::ConstParam
12971286
| DefKind::AssocTy
12981287
| DefKind::TyParam
@@ -1452,8 +1441,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14521441
self.encode_info_for_assoc_item(def_id);
14531442
}
14541443
}
1455-
if let DefKind::Coroutine = def_kind {
1456-
let data = self.tcx.coroutine_kind(def_id).unwrap();
1444+
if def_kind == DefKind::Closure
1445+
&& let Some(data) = self.tcx.coroutine_kind(def_id)
1446+
{
14571447
record!(self.tables.coroutine_kind[def_id] <- data);
14581448
}
14591449
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
@@ -1635,7 +1625,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16351625
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
16361626
<- tcx.closure_saved_names_of_captured_variables(def_id));
16371627

1638-
if let DefKind::Coroutine = self.tcx.def_kind(def_id)
1628+
if self.tcx.is_coroutine(def_id.to_def_id())
16391629
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16401630
{
16411631
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
@@ -1662,7 +1652,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16621652
}
16631653
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
16641654

1665-
if let DefKind::Coroutine = self.tcx.def_kind(def_id)
1655+
if self.tcx.is_coroutine(def_id.to_def_id())
16661656
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16671657
{
16681658
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);

compiler/rustc_metadata/src/rmeta/table.rs

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ fixed_size_enum! {
167167
( Impl { of_trait: false } )
168168
( Impl { of_trait: true } )
169169
( Closure )
170-
( Coroutine )
171170
( Static(ast::Mutability::Not) )
172171
( Static(ast::Mutability::Mut) )
173172
( Ctor(CtorOf::Struct, CtorKind::Fn) )

compiler/rustc_middle/src/hir/map/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ impl<'hir> Map<'hir> {
231231
Node::ConstBlock(_) => DefKind::InlineConst,
232232
Node::Field(_) => DefKind::Field,
233233
Node::Expr(expr) => match expr.kind {
234-
ExprKind::Closure(Closure { movability: None, .. }) => DefKind::Closure,
235-
ExprKind::Closure(Closure { movability: Some(_), .. }) => DefKind::Coroutine,
234+
ExprKind::Closure(_) => DefKind::Closure,
236235
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
237236
},
238237
Node::GenericParam(param) => match param.kind {
@@ -436,7 +435,7 @@ impl<'hir> Map<'hir> {
436435
}
437436
DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
438437
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
439-
DefKind::Closure | DefKind::Coroutine => BodyOwnerKind::Closure,
438+
DefKind::Closure => BodyOwnerKind::Closure,
440439
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
441440
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
442441
}

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,10 @@ impl<'tcx> TyCtxt<'tcx> {
800800
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
801801
}
802802

803+
pub fn is_coroutine(self, def_id: DefId) -> bool {
804+
self.coroutine_kind(def_id).is_some()
805+
}
806+
803807
/// Returns `true` if the node pointed to by `def_id` is a coroutine for an async construct.
804808
pub fn coroutine_is_async(self, def_id: DefId) -> bool {
805809
matches!(self.coroutine_kind(def_id), Some(hir::CoroutineKind::Async(_)))

compiler/rustc_middle/src/ty/util.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -550,16 +550,13 @@ impl<'tcx> TyCtxt<'tcx> {
550550
/// those are not yet phased out). The parent of the closure's
551551
/// `DefId` will also be the context where it appears.
552552
pub fn is_closure(self, def_id: DefId) -> bool {
553-
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Coroutine)
553+
matches!(self.def_kind(def_id), DefKind::Closure)
554554
}
555555

556556
/// Returns `true` if `def_id` refers to a definition that does not have its own
557557
/// type-checking context, i.e. closure, coroutine or inline const.
558558
pub fn is_typeck_child(self, def_id: DefId) -> bool {
559-
matches!(
560-
self.def_kind(def_id),
561-
DefKind::Closure | DefKind::Coroutine | DefKind::InlineConst
562-
)
559+
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::InlineConst)
563560
}
564561

565562
/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
@@ -732,11 +729,13 @@ impl<'tcx> TyCtxt<'tcx> {
732729
pub fn def_kind_descr(self, def_kind: DefKind, def_id: DefId) -> &'static str {
733730
match def_kind {
734731
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
735-
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
736-
rustc_hir::CoroutineKind::Async(..) => "async closure",
737-
rustc_hir::CoroutineKind::Coroutine => "coroutine",
738-
rustc_hir::CoroutineKind::Gen(..) => "gen closure",
739-
},
732+
DefKind::Closure if let Some(coroutine_kind) = self.coroutine_kind(def_id) => {
733+
match coroutine_kind {
734+
rustc_hir::CoroutineKind::Async(..) => "async closure",
735+
rustc_hir::CoroutineKind::Coroutine => "coroutine",
736+
rustc_hir::CoroutineKind::Gen(..) => "gen closure",
737+
}
738+
}
740739
_ => def_kind.descr(def_id),
741740
}
742741
}
@@ -750,11 +749,13 @@ impl<'tcx> TyCtxt<'tcx> {
750749
pub fn def_kind_descr_article(self, def_kind: DefKind, def_id: DefId) -> &'static str {
751750
match def_kind {
752751
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
753-
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
754-
rustc_hir::CoroutineKind::Async(..) => "an",
755-
rustc_hir::CoroutineKind::Coroutine => "a",
756-
rustc_hir::CoroutineKind::Gen(..) => "a",
757-
},
752+
DefKind::Closure if let Some(coroutine_kind) = self.coroutine_kind(def_id) => {
753+
match coroutine_kind {
754+
rustc_hir::CoroutineKind::Async(..) => "an",
755+
rustc_hir::CoroutineKind::Coroutine => "a",
756+
rustc_hir::CoroutineKind::Gen(..) => "a",
757+
}
758+
}
758759
_ => def_kind.article(),
759760
}
760761
}

compiler/rustc_mir_build/src/build/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
638638
);
639639
(sig.inputs().to_vec(), sig.output(), None)
640640
}
641+
DefKind::Closure if coroutine_kind.is_some() => {
642+
let coroutine_ty = tcx.type_of(def_id).instantiate_identity();
643+
let ty::Coroutine(_, args, _) = coroutine_ty.kind() else { bug!() };
644+
let args = args.as_coroutine();
645+
let yield_ty = args.yield_ty();
646+
let return_ty = args.return_ty();
647+
(vec![coroutine_ty, args.resume_ty()], return_ty, Some(yield_ty))
648+
}
641649
DefKind::Closure => {
642650
let closure_ty = tcx.type_of(def_id).instantiate_identity();
643651
let ty::Closure(_, args) = closure_ty.kind() else { bug!() };
@@ -650,14 +658,6 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
650658
};
651659
([self_ty].into_iter().chain(sig.inputs().to_vec()).collect(), sig.output(), None)
652660
}
653-
DefKind::Coroutine => {
654-
let coroutine_ty = tcx.type_of(def_id).instantiate_identity();
655-
let ty::Coroutine(_, args, _) = coroutine_ty.kind() else { bug!() };
656-
let args = args.as_coroutine();
657-
let yield_ty = args.yield_ty();
658-
let return_ty = args.return_ty();
659-
(vec![coroutine_ty, args.resume_ty()], return_ty, Some(yield_ty))
660-
}
661661
dk => bug!("{:?} is not a body: {:?}", def_id, dk),
662662
};
663663

0 commit comments

Comments
 (0)