Skip to content

Commit 46dc808

Browse files
committed
Auto merge of #119258 - compiler-errors:closure-kind, r=<try>
Make closures carry their own ClosureKind Right now, we use the "`movability`" field of `hir::Closure` to distinguish a closure and a coroutine. This is paired together with the `CoroutineKind`, which is located not in the `hir::Closure`, but the `hir::Body`. This is strange and redundant. This PR introduces `ClosureKind` with two variants -- `Closure` and `Coroutine`, which is put into `hir::Closure`. The `CoroutineKind` is thus removed from `hir::Body`, and `Option<Movability>` no longer needs to be a stand-in for "is this a closure or a coroutine". r? eholk
2 parents edcbcc7 + bd0eec7 commit 46dc808

File tree

22 files changed

+368
-378
lines changed

22 files changed

+368
-378
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
669669
};
670670
let params = arena_vec![self; param];
671671

672+
let coroutine_kind =
673+
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, async_coroutine_source);
672674
let body = self.lower_body(move |this| {
673-
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
674-
hir::CoroutineDesugaring::Async,
675-
async_coroutine_source,
676-
));
675+
this.coroutine_kind = Some(coroutine_kind);
677676

678677
let old_ctx = this.task_context;
679678
this.task_context = Some(task_context_hid);
@@ -692,7 +691,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
692691
body,
693692
fn_decl_span: self.lower_span(span),
694693
fn_arg_span: None,
695-
movability: Some(hir::Movability::Static),
694+
kind: hir::ClosureKind::Coroutine(coroutine_kind, Movability::Static),
696695
constness: hir::Constness::NotConst,
697696
}))
698697
}
@@ -726,11 +725,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
726725
lifetime_elision_allowed: false,
727726
});
728727

728+
let coroutine_kind =
729+
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, coroutine_source);
729730
let body = self.lower_body(move |this| {
730-
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
731-
hir::CoroutineDesugaring::Gen,
732-
coroutine_source,
733-
));
731+
this.coroutine_kind = Some(coroutine_kind);
734732

735733
let res = body(this);
736734
(&[], res)
@@ -746,7 +744,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
746744
body,
747745
fn_decl_span: self.lower_span(span),
748746
fn_arg_span: None,
749-
movability: Some(Movability::Movable),
747+
kind: hir::ClosureKind::Coroutine(coroutine_kind, Movability::Movable),
750748
constness: hir::Constness::NotConst,
751749
}))
752750
}
@@ -807,11 +805,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
807805
};
808806
let params = arena_vec![self; param];
809807

808+
let coroutine_kind = hir::CoroutineKind::Desugared(
809+
hir::CoroutineDesugaring::AsyncGen,
810+
async_coroutine_source,
811+
);
810812
let body = self.lower_body(move |this| {
811-
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
812-
hir::CoroutineDesugaring::AsyncGen,
813-
async_coroutine_source,
814-
));
813+
this.coroutine_kind = Some(coroutine_kind);
815814

816815
let old_ctx = this.task_context;
817816
this.task_context = Some(task_context_hid);
@@ -830,7 +829,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
830829
body,
831830
fn_decl_span: self.lower_span(span),
832831
fn_arg_span: None,
833-
movability: Some(hir::Movability::Static),
832+
kind: hir::ClosureKind::Coroutine(coroutine_kind, Movability::Static),
834833
constness: hir::Constness::NotConst,
835834
}))
836835
}
@@ -1087,15 +1086,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
10871086
) -> hir::ExprKind<'hir> {
10881087
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
10891088

1090-
let (body_id, coroutine_option) = self.with_new_scopes(fn_decl_span, move |this| {
1089+
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
10911090
let mut coroutine_kind = None;
10921091
let body_id = this.lower_fn_body(decl, |this| {
10931092
let e = this.lower_expr_mut(body);
10941093
coroutine_kind = this.coroutine_kind;
10951094
e
10961095
});
10971096
let coroutine_option =
1098-
this.coroutine_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
1097+
this.closure_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
10991098
(body_id, coroutine_option)
11001099
});
11011100

@@ -1112,26 +1111,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
11121111
body: body_id,
11131112
fn_decl_span: self.lower_span(fn_decl_span),
11141113
fn_arg_span: Some(self.lower_span(fn_arg_span)),
1115-
movability: coroutine_option,
1114+
kind: closure_kind,
11161115
constness: self.lower_constness(constness),
11171116
});
11181117

11191118
hir::ExprKind::Closure(c)
11201119
}
11211120

1122-
fn coroutine_movability_for_fn(
1121+
fn closure_movability_for_fn(
11231122
&mut self,
11241123
decl: &FnDecl,
11251124
fn_decl_span: Span,
11261125
coroutine_kind: Option<hir::CoroutineKind>,
11271126
movability: Movability,
1128-
) -> Option<hir::Movability> {
1127+
) -> hir::ClosureKind {
11291128
match coroutine_kind {
11301129
Some(hir::CoroutineKind::Coroutine) => {
11311130
if decl.inputs.len() > 1 {
11321131
self.tcx.sess.emit_err(CoroutineTooManyParameters { fn_decl_span });
11331132
}
1134-
Some(movability)
1133+
hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine, movability)
11351134
}
11361135
Some(
11371136
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
@@ -1144,7 +1143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11441143
if movability == Movability::Static {
11451144
self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
11461145
}
1147-
None
1146+
hir::ClosureKind::Closure
11481147
}
11491148
}
11501149
}
@@ -1236,7 +1235,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12361235
body,
12371236
fn_decl_span: self.lower_span(fn_decl_span),
12381237
fn_arg_span: Some(self.lower_span(fn_arg_span)),
1239-
movability: None,
1238+
kind: hir::ClosureKind::Closure,
12401239
constness: hir::Constness::NotConst,
12411240
});
12421241
hir::ExprKind::Closure(c)

compiler/rustc_ast_lowering/src/item.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
952952
params: &'hir [hir::Param<'hir>],
953953
value: hir::Expr<'hir>,
954954
) -> hir::BodyId {
955-
let body = hir::Body {
956-
coroutine_kind: self.coroutine_kind,
957-
params,
958-
value: self.arena.alloc(value),
959-
};
955+
let body = hir::Body { params, value: self.arena.alloc(value) };
960956
let id = body.id();
961957
debug_assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
962958
self.bodies.push((id.hir_id.local_id, self.arena.alloc(body)));

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
848848
move_spans.var_subdiag(None, &mut err, None, |kind, var_span| {
849849
use crate::session_diagnostics::CaptureVarCause::*;
850850
match kind {
851-
Some(_) => MoveUseInCoroutine { var_span },
852-
None => MoveUseInClosure { var_span },
851+
hir::ClosureKind::Coroutine(_, _) => MoveUseInCoroutine { var_span },
852+
hir::ClosureKind::Closure => MoveUseInClosure { var_span },
853853
}
854854
});
855855

@@ -893,10 +893,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
893893
let place = &borrow.borrowed_place;
894894
let desc_place = self.describe_any_place(place.as_ref());
895895
match kind {
896-
Some(_) => {
896+
hir::ClosureKind::Coroutine(_, _) => {
897897
BorrowUsePlaceCoroutine { place: desc_place, var_span, is_single_var: true }
898898
}
899-
None => BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: true },
899+
hir::ClosureKind::Closure => {
900+
BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: true }
901+
}
900902
}
901903
});
902904

@@ -1040,12 +1042,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10401042
|kind, var_span| {
10411043
use crate::session_diagnostics::CaptureVarCause::*;
10421044
match kind {
1043-
Some(_) => BorrowUsePlaceCoroutine {
1045+
hir::ClosureKind::Coroutine(_, _) => BorrowUsePlaceCoroutine {
10441046
place: desc_place,
10451047
var_span,
10461048
is_single_var: true,
10471049
},
1048-
None => BorrowUsePlaceClosure {
1050+
hir::ClosureKind::Closure => BorrowUsePlaceClosure {
10491051
place: desc_place,
10501052
var_span,
10511053
is_single_var: true,
@@ -1124,12 +1126,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11241126
borrow_spans.var_subdiag(None, &mut err, Some(gen_borrow_kind), |kind, var_span| {
11251127
use crate::session_diagnostics::CaptureVarCause::*;
11261128
match kind {
1127-
Some(_) => BorrowUsePlaceCoroutine {
1129+
hir::ClosureKind::Coroutine(_, _) => BorrowUsePlaceCoroutine {
11281130
place: desc_place,
11291131
var_span,
11301132
is_single_var: false,
11311133
},
1132-
None => {
1134+
hir::ClosureKind::Closure => {
11331135
BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: false }
11341136
}
11351137
}
@@ -1144,10 +1146,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11441146
let borrow_place = &issued_borrow.borrowed_place;
11451147
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
11461148
match kind {
1147-
Some(_) => {
1149+
hir::ClosureKind::Coroutine(_, _) => {
11481150
FirstBorrowUsePlaceCoroutine { place: borrow_place_desc, var_span }
11491151
}
1150-
None => FirstBorrowUsePlaceClosure { place: borrow_place_desc, var_span },
1152+
hir::ClosureKind::Closure => {
1153+
FirstBorrowUsePlaceClosure { place: borrow_place_desc, var_span }
1154+
}
11511155
}
11521156
},
11531157
);
@@ -1159,8 +1163,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11591163
|kind, var_span| {
11601164
use crate::session_diagnostics::CaptureVarCause::*;
11611165
match kind {
1162-
Some(_) => SecondBorrowUsePlaceCoroutine { place: desc_place, var_span },
1163-
None => SecondBorrowUsePlaceClosure { place: desc_place, var_span },
1166+
hir::ClosureKind::Coroutine(_, _) => {
1167+
SecondBorrowUsePlaceCoroutine { place: desc_place, var_span }
1168+
}
1169+
hir::ClosureKind::Closure => {
1170+
SecondBorrowUsePlaceClosure { place: desc_place, var_span }
1171+
}
11641172
}
11651173
},
11661174
);
@@ -1338,7 +1346,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13381346
// mut iter => {
13391347
// [opt_ident]: loop {
13401348
// match Iterator::next(&mut iter) {
1341-
// None => break,
1349+
// hir::ClosureKind::Closure => break,
13421350
// Some(<pat>) => <body>,
13431351
// };
13441352
// }
@@ -1651,7 +1659,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16511659
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
16521660
if e.span.contains(self.capture_span) {
16531661
if let hir::ExprKind::Closure(&hir::Closure {
1654-
movability: None,
1662+
kind: hir::ClosureKind::Closure,
16551663
body,
16561664
fn_arg_span,
16571665
fn_decl: hir::FnDecl { inputs, .. },
@@ -1686,7 +1694,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16861694
&& let Some(init) = local.init
16871695
{
16881696
if let hir::Expr {
1689-
kind: hir::ExprKind::Closure(&hir::Closure { movability: None, .. }),
1697+
kind:
1698+
hir::ExprKind::Closure(&hir::Closure {
1699+
kind: hir::ClosureKind::Closure,
1700+
..
1701+
}),
16901702
..
16911703
} = init
16921704
&& init.span.contains(self.capture_span)
@@ -2838,8 +2850,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
28382850
loan_spans.var_subdiag(None, &mut err, Some(loan.kind), |kind, var_span| {
28392851
use crate::session_diagnostics::CaptureVarCause::*;
28402852
match kind {
2841-
Some(_) => BorrowUseInCoroutine { var_span },
2842-
None => BorrowUseInClosure { var_span },
2853+
hir::ClosureKind::Coroutine(_, _) => BorrowUseInCoroutine { var_span },
2854+
hir::ClosureKind::Closure => BorrowUseInClosure { var_span },
28432855
}
28442856
});
28452857

@@ -2854,8 +2866,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
28542866
loan_spans.var_subdiag(None, &mut err, Some(loan.kind), |kind, var_span| {
28552867
use crate::session_diagnostics::CaptureVarCause::*;
28562868
match kind {
2857-
Some(_) => BorrowUseInCoroutine { var_span },
2858-
None => BorrowUseInClosure { var_span },
2869+
hir::ClosureKind::Coroutine(_, _) => BorrowUseInCoroutine { var_span },
2870+
hir::ClosureKind::Closure => BorrowUseInClosure { var_span },
28592871
}
28602872
});
28612873

0 commit comments

Comments
 (0)