Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ba64c93

Browse files
committed
Lower generator expression to HIR
1 parent 6dfd8ae commit ba64c93

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

crates/hir-def/src/body/lower.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ use crate::{
2929
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
3030
db::DefDatabase,
3131
expr::{
32-
dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId,
33-
Literal, MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
32+
dummy_expr_id, Array, BindingAnnotation, ClosureKind, Expr, ExprId, FloatTypeWrapper,
33+
Label, LabelId, Literal, MatchArm, Movability, Pat, PatId, RecordFieldPat, RecordLitField,
34+
Statement,
3435
},
3536
intern::Interned,
3637
item_scope::BuiltinShadowMode,
@@ -97,6 +98,7 @@ pub(super) fn lower(
9798
name_to_pat_grouping: Default::default(),
9899
is_lowering_inside_or_pat: false,
99100
is_lowering_assignee_expr: false,
101+
is_lowering_generator: false,
100102
}
101103
.collect(params, body)
102104
}
@@ -111,6 +113,7 @@ struct ExprCollector<'a> {
111113
name_to_pat_grouping: FxHashMap<Name, Vec<PatId>>,
112114
is_lowering_inside_or_pat: bool,
113115
is_lowering_assignee_expr: bool,
116+
is_lowering_generator: bool,
114117
}
115118

116119
impl ExprCollector<'_> {
@@ -358,6 +361,7 @@ impl ExprCollector<'_> {
358361
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
359362
}
360363
ast::Expr::YieldExpr(e) => {
364+
self.is_lowering_generator = true;
361365
let expr = e.expr().map(|e| self.collect_expr(e));
362366
self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
363367
}
@@ -459,13 +463,31 @@ impl ExprCollector<'_> {
459463
.ret_type()
460464
.and_then(|r| r.ty())
461465
.map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
466+
467+
let prev_is_lowering_generator = self.is_lowering_generator;
468+
self.is_lowering_generator = false;
469+
462470
let body = self.collect_expr_opt(e.body());
471+
472+
let closure_kind = if self.is_lowering_generator {
473+
let movability = if e.static_token().is_some() {
474+
Movability::Static
475+
} else {
476+
Movability::Movable
477+
};
478+
ClosureKind::Generator(movability)
479+
} else {
480+
ClosureKind::Closure
481+
};
482+
self.is_lowering_generator = prev_is_lowering_generator;
483+
463484
self.alloc_expr(
464485
Expr::Closure {
465486
args: args.into(),
466487
arg_types: arg_types.into(),
467488
ret_type,
468489
body,
490+
closure_kind,
469491
},
470492
syntax_ptr,
471493
)

crates/hir-def/src/body/pretty.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt::{self, Write};
44

55
use crate::{
6-
expr::{Array, BindingAnnotation, Literal, Statement},
6+
expr::{Array, BindingAnnotation, ClosureKind, Literal, Movability, Statement},
77
pretty::{print_generic_args, print_path, print_type_ref},
88
type_ref::TypeRef,
99
};
@@ -350,7 +350,10 @@ impl<'a> Printer<'a> {
350350
self.print_expr(*index);
351351
w!(self, "]");
352352
}
353-
Expr::Closure { args, arg_types, ret_type, body } => {
353+
Expr::Closure { args, arg_types, ret_type, body, closure_kind } => {
354+
if let ClosureKind::Generator(Movability::Static) = closure_kind {
355+
w!(self, "static ");
356+
}
354357
w!(self, "|");
355358
for (i, (pat, ty)) in args.iter().zip(arg_types.iter()).enumerate() {
356359
if i != 0 {

crates/hir-def/src/expr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ pub enum Expr {
198198
arg_types: Box<[Option<Interned<TypeRef>>]>,
199199
ret_type: Option<Interned<TypeRef>>,
200200
body: ExprId,
201+
closure_kind: ClosureKind,
201202
},
202203
Tuple {
203204
exprs: Box<[ExprId]>,
@@ -211,6 +212,18 @@ pub enum Expr {
211212
Underscore,
212213
}
213214

215+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216+
pub enum ClosureKind {
217+
Closure,
218+
Generator(Movability),
219+
}
220+
221+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
222+
pub enum Movability {
223+
Static,
224+
Movable,
225+
}
226+
214227
#[derive(Debug, Clone, Eq, PartialEq)]
215228
pub enum Array {
216229
ElementList { elements: Box<[ExprId]>, is_assignee_expr: bool },

crates/hir-ty/src/infer/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'a> InferenceContext<'a> {
216216
self.diverges = Diverges::Maybe;
217217
TyBuilder::unit()
218218
}
219-
Expr::Closure { body, args, ret_type, arg_types } => {
219+
Expr::Closure { body, args, ret_type, arg_types, closure_kind: _ } => {
220220
assert_eq!(args.len(), arg_types.len());
221221

222222
let mut sig_tys = Vec::new();

0 commit comments

Comments
 (0)