Skip to content

Commit 6caa433

Browse files
committed
Revert "merge DefKind::Coroutine into DefKind::Closure"
This reverts commit f23befe.
1 parent 3ea2972 commit 6caa433

File tree

27 files changed

+94
-83
lines changed

27 files changed

+94
-83
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 });
92+
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, 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

+1-8
Original file line numberDiff line numberDiff line change
@@ -2072,15 +2072,8 @@ 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-
}
20832075
DefKind::Closure => "enclosing closure",
2076+
DefKind::Coroutine => "enclosing coroutine",
20842077
kind => bug!("expected closure or coroutine, found {:?}", kind),
20852078
}
20862079
.to_string()

compiler/rustc_borrowck/src/type_check/mod.rs

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

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ 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!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Closure) {
376+
if !matches!(
377+
kind,
378+
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Coroutine
379+
) {
377380
return None;
378381
}
379382
if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {

compiler/rustc_hir/src/def.rs

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

119120
impl DefKind {
@@ -156,6 +157,7 @@ impl DefKind {
156157
DefKind::Field => "field",
157158
DefKind::Impl { .. } => "implementation",
158159
DefKind::Closure => "closure",
160+
DefKind::Coroutine => "coroutine",
159161
DefKind::ExternCrate => "extern crate",
160162
DefKind::GlobalAsm => "global assembly block",
161163
}
@@ -214,6 +216,7 @@ impl DefKind {
214216
| DefKind::LifetimeParam
215217
| DefKind::ExternCrate
216218
| DefKind::Closure
219+
| DefKind::Coroutine
217220
| DefKind::Use
218221
| DefKind::ForeignMod
219222
| DefKind::GlobalAsm
@@ -223,7 +226,7 @@ impl DefKind {
223226

224227
#[inline]
225228
pub fn is_fn_like(self) -> bool {
226-
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure)
229+
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Coroutine)
227230
}
228231

229232
/// Whether `query get_codegen_attrs` should be used with this definition.
@@ -233,6 +236,7 @@ impl DefKind {
233236
| DefKind::AssocFn
234237
| DefKind::Ctor(..)
235238
| DefKind::Closure
239+
| DefKind::Coroutine
236240
| DefKind::Static(_) => true,
237241
DefKind::Mod
238242
| 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 tcx.is_coroutine(closure_def_id)
1452+
if let DefKind::Coroutine = tcx.def_kind(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!(tcx.is_coroutine(def_id.to_def_id()));
1473+
debug_assert!(matches!(tcx.def_kind(def_id), DefKind::Coroutine));
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 let rustc_hir::def::DefKind::Coroutine = tcx.def_kind(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

+21-11
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ fn should_encode_span(def_kind: DefKind) -> bool {
857857
| DefKind::OpaqueTy
858858
| DefKind::Field
859859
| DefKind::Impl { .. }
860-
| DefKind::Closure => true,
860+
| DefKind::Closure
861+
| DefKind::Coroutine => true,
861862
DefKind::ForeignMod | DefKind::GlobalAsm => false,
862863
}
863864
}
@@ -897,7 +898,8 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
897898
| DefKind::InlineConst
898899
| DefKind::OpaqueTy
899900
| DefKind::LifetimeParam
900-
| DefKind::GlobalAsm => false,
901+
| DefKind::GlobalAsm
902+
| DefKind::Coroutine => false,
901903
}
902904
}
903905

@@ -932,7 +934,8 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
932934
| DefKind::Field
933935
| DefKind::LifetimeParam
934936
| DefKind::GlobalAsm
935-
| DefKind::Closure => false,
937+
| DefKind::Closure
938+
| DefKind::Coroutine => false,
936939
}
937940
}
938941

@@ -967,6 +970,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
967970
| DefKind::GlobalAsm
968971
| DefKind::Impl { .. }
969972
| DefKind::Closure
973+
| DefKind::Coroutine
970974
| DefKind::ExternCrate => false,
971975
}
972976
}
@@ -1002,6 +1006,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
10021006
| DefKind::InlineConst
10031007
| DefKind::GlobalAsm
10041008
| DefKind::Closure
1009+
| DefKind::Coroutine
10051010
| DefKind::ExternCrate => false,
10061011
}
10071012
}
@@ -1044,8 +1049,6 @@ fn should_encode_mir(
10441049
| DefKind::AssocConst
10451050
| DefKind::Static(..)
10461051
| DefKind::Const => (true, false),
1047-
// Coroutines require optimized MIR to compute layout.
1048-
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
10491052
// Full-fledged functions + closures
10501053
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10511054
let generics = tcx.generics_of(def_id);
@@ -1059,6 +1062,8 @@ fn should_encode_mir(
10591062
|| tcx.is_const_default_method(def_id.to_def_id());
10601063
(is_const_fn, opt)
10611064
}
1065+
// Coroutines require optimized MIR to compute layout.
1066+
DefKind::Coroutine => (false, true),
10621067
// The others don't have MIR.
10631068
_ => (false, false),
10641069
}
@@ -1094,6 +1099,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
10941099
| DefKind::InlineConst
10951100
| DefKind::GlobalAsm
10961101
| DefKind::Closure
1102+
| DefKind::Coroutine
10971103
| DefKind::ExternCrate => false,
10981104
DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
10991105
}
@@ -1122,7 +1128,8 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
11221128
| DefKind::Impl { .. }
11231129
| DefKind::Field
11241130
| DefKind::TyParam
1125-
| DefKind::Closure => true,
1131+
| DefKind::Closure
1132+
| DefKind::Coroutine => true,
11261133
DefKind::Mod
11271134
| DefKind::ForeignMod
11281135
| DefKind::ConstParam
@@ -1151,6 +1158,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11511158
| DefKind::AssocFn
11521159
| DefKind::AssocConst
11531160
| DefKind::Closure
1161+
| DefKind::Coroutine
11541162
| DefKind::ConstParam
11551163
| DefKind::AnonConst
11561164
| DefKind::InlineConst => true,
@@ -1211,6 +1219,7 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
12111219
| DefKind::Impl { .. }
12121220
| DefKind::AssocConst
12131221
| DefKind::Closure
1222+
| DefKind::Coroutine
12141223
| DefKind::ConstParam
12151224
| DefKind::AnonConst
12161225
| DefKind::InlineConst
@@ -1249,6 +1258,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12491258
| DefKind::OpaqueTy
12501259
| DefKind::Impl { of_trait: false }
12511260
| DefKind::ForeignTy
1261+
| DefKind::Coroutine
12521262
| DefKind::ConstParam
12531263
| DefKind::InlineConst
12541264
| DefKind::AssocTy
@@ -1283,6 +1293,7 @@ fn should_encode_const(def_kind: DefKind) -> bool {
12831293
| DefKind::Impl { .. }
12841294
| DefKind::AssocFn
12851295
| DefKind::Closure
1296+
| DefKind::Coroutine
12861297
| DefKind::ConstParam
12871298
| DefKind::AssocTy
12881299
| DefKind::TyParam
@@ -1442,9 +1453,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14421453
self.encode_info_for_assoc_item(def_id);
14431454
}
14441455
}
1445-
if def_kind == DefKind::Closure
1446-
&& let Some(data) = self.tcx.coroutine_kind(def_id)
1447-
{
1456+
if let DefKind::Coroutine = def_kind {
1457+
let data = self.tcx.coroutine_kind(def_id).unwrap();
14481458
record!(self.tables.coroutine_kind[def_id] <- data);
14491459
}
14501460
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
@@ -1626,7 +1636,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16261636
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
16271637
<- tcx.closure_saved_names_of_captured_variables(def_id));
16281638

1629-
if self.tcx.is_coroutine(def_id.to_def_id())
1639+
if let DefKind::Coroutine = self.tcx.def_kind(def_id)
16301640
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16311641
{
16321642
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
@@ -1653,7 +1663,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16531663
}
16541664
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
16551665

1656-
if self.tcx.is_coroutine(def_id.to_def_id())
1666+
if let DefKind::Coroutine = self.tcx.def_kind(def_id)
16571667
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)
16581668
{
16591669
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,6 +167,7 @@ fixed_size_enum! {
167167
( Impl { of_trait: false } )
168168
( Impl { of_trait: true } )
169169
( Closure )
170+
( Coroutine )
170171
( Static(ast::Mutability::Not) )
171172
( Static(ast::Mutability::Mut) )
172173
( Ctor(CtorOf::Struct, CtorKind::Fn) )

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

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

compiler/rustc_middle/src/ty/context.rs

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

807-
pub fn is_coroutine(self, def_id: DefId) -> bool {
808-
self.coroutine_kind(def_id).is_some()
809-
}
810-
811807
/// Returns `true` if the node pointed to by `def_id` is a coroutine for an async construct.
812808
pub fn coroutine_is_async(self, def_id: DefId) -> bool {
813809
matches!(self.coroutine_kind(def_id), Some(hir::CoroutineKind::Async(_)))

compiler/rustc_middle/src/ty/util.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,16 @@ 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)
553+
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Coroutine)
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!(self.def_kind(def_id), DefKind::Closure | DefKind::InlineConst)
559+
matches!(
560+
self.def_kind(def_id),
561+
DefKind::Closure | DefKind::Coroutine | DefKind::InlineConst
562+
)
560563
}
561564

562565
/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
@@ -729,13 +732,11 @@ impl<'tcx> TyCtxt<'tcx> {
729732
pub fn def_kind_descr(self, def_kind: DefKind, def_id: DefId) -> &'static str {
730733
match def_kind {
731734
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
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-
}
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+
},
739740
_ => def_kind.descr(def_id),
740741
}
741742
}
@@ -749,13 +750,11 @@ impl<'tcx> TyCtxt<'tcx> {
749750
pub fn def_kind_descr_article(self, def_kind: DefKind, def_id: DefId) -> &'static str {
750751
match def_kind {
751752
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
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-
}
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+
},
759758
_ => def_kind.article(),
760759
}
761760
}

compiler/rustc_mir_build/src/build/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,6 @@ 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-
}
649641
DefKind::Closure => {
650642
let closure_ty = tcx.type_of(def_id).instantiate_identity();
651643
let ty::Closure(_, args) = closure_ty.kind() else { bug!() };
@@ -658,6 +650,14 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
658650
};
659651
([self_ty].into_iter().chain(sig.inputs().to_vec()).collect(), sig.output(), None)
660652
}
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)