Skip to content

Commit 87780d3

Browse files
stepanchegfacebook-github-bot
authored andcommitted
StmtCompiledValue::Return
Summary: This is used in the following diff D30888373 to figure out if function does type is. Reviewed By: ndmitchell Differential Revision: D30888378 fbshipit-source-id: ecf91bc354b593741bdc23e057dcab1161180863
1 parent 7702fe6 commit 87780d3

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

starlark/src/eval/fragment/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Compiler<'_> {
170170

171171
let info = Arc::new(DefInfo {
172172
scope_names,
173-
body: body.as_compiled(),
173+
body: body.as_compiled(self),
174174
});
175175

176176
expr!("def", |eval| {

starlark/src/eval/fragment/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Compiler<'_> {
4242
Ok(())
4343
}
4444
_ => {
45-
let stmt = self.stmt(stmt, true).as_compiled();
45+
let stmt = self.stmt(stmt, true).as_compiled(self);
4646
stmt(evaluator)
4747
}
4848
}

starlark/src/eval/fragment/stmt.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,26 @@ pub(crate) type StmtCompiled =
5050

5151
pub(crate) enum StmtCompiledValue {
5252
Compiled(StmtCompiled),
53+
Return(Span, Option<ExprCompiledValue>),
5354
}
5455

5556
impl StmtCompiledValue {
56-
pub(crate) fn as_compiled(self) -> StmtCompiled {
57+
pub(crate) fn as_compiled(self, compiler: &mut Compiler) -> StmtCompiled {
5758
match self {
5859
StmtCompiledValue::Compiled(c) => c,
60+
StmtCompiledValue::Return(span, None) => {
61+
stmt!(compiler, "return_none", span, |_eval| {
62+
return Err(EvalException::Return(Value::new_none()));
63+
})
64+
.as_compiled(compiler)
65+
}
66+
StmtCompiledValue::Return(span, Some(e)) => {
67+
let e = e.as_compiled();
68+
stmt!(compiler, "return", span, |eval| {
69+
return Err(EvalException::Return(e(eval)?));
70+
})
71+
.as_compiled(compiler)
72+
}
5973
}
6074
}
6175
}
@@ -122,13 +136,13 @@ impl StmtsCompiled {
122136
self.0.extend(right.0);
123137
}
124138

125-
pub(crate) fn as_compiled(self) -> StmtCompiled {
139+
pub(crate) fn as_compiled(self, compiler: &mut Compiler) -> StmtCompiled {
126140
match self.0 {
127141
SmallVec1::Empty => box |_eval| Ok(()),
128-
SmallVec1::One(stmt) => stmt.as_compiled(),
142+
SmallVec1::One(stmt) => stmt.as_compiled(compiler),
129143
SmallVec1::Many(vec) => {
130144
debug_assert!(vec.len() > 1);
131-
let vec = vec.into_map(|s| s.as_compiled());
145+
let vec = vec.into_map(|s| s.as_compiled(compiler));
132146
box move |eval| {
133147
for stmt in &vec {
134148
stmt(eval)?;
@@ -425,7 +439,7 @@ impl Compiler<'_> {
425439
fn maybe_wrap_before_stmt(&mut self, span: Span, stmt: StmtsCompiled) -> StmtsCompiled {
426440
assert!(stmt.len() == 1);
427441
if self.has_before_stmt {
428-
let stmt = stmt.as_compiled();
442+
let stmt = stmt.as_compiled(self);
429443
StmtsCompiled::one(StmtCompiledValue::Compiled(box move |eval| {
430444
before_stmt(span, eval);
431445
stmt(eval)
@@ -442,7 +456,7 @@ impl Compiler<'_> {
442456
if allow_gc && !is_statements {
443457
// We could do this more efficiently by fusing the possible_gc
444458
// into the inner closure, but no real need - we insert allow_gc fairly rarely
445-
let res = res.as_compiled();
459+
let res = res.as_compiled(self);
446460
StmtsCompiled::one(StmtCompiledValue::Compiled(box move |eval| {
447461
possible_gc(eval);
448462
res(eval)
@@ -462,7 +476,7 @@ impl Compiler<'_> {
462476
if then_block.is_empty() {
463477
self.stmt_expr_compiled(span, ExprCompiledValue::Compiled(cond))
464478
} else {
465-
let then_block = then_block.as_compiled();
479+
let then_block = then_block.as_compiled(self);
466480
if cond_is_positive {
467481
stmt!(self, "if_then", span, |eval| if cond(eval)?.to_bool() {
468482
then_block(eval)?
@@ -512,8 +526,8 @@ impl Compiler<'_> {
512526
} else if t.is_empty() {
513527
self.stmt_if_compiled(span, cond, false, f)
514528
} else {
515-
let t = t.as_compiled();
516-
let f = f.as_compiled();
529+
let t = t.as_compiled(self);
530+
let f = f.as_compiled(self);
517531
stmt!(
518532
self,
519533
"if_then_else",
@@ -565,7 +579,7 @@ impl Compiler<'_> {
565579
let over_span = over.span;
566580
let var = self.assign(var);
567581
let over = self.expr(over).as_compiled();
568-
let st = self.stmt(body, false).as_compiled();
582+
let st = self.stmt(body, false).as_compiled(self);
569583
stmt!(self, "for", span, |eval| {
570584
let heap = eval.heap();
571585
let iterable = over(eval)?;
@@ -587,15 +601,9 @@ impl Compiler<'_> {
587601
)??;
588602
})
589603
}
590-
StmtP::Return(Some(e)) => {
591-
let e = self.expr(e).as_compiled();
592-
stmt!(self, "return_value", span, |eval| {
593-
return Err(EvalException::Return(e(eval)?));
594-
})
604+
StmtP::Return(e) => {
605+
StmtsCompiled::one(StmtCompiledValue::Return(span, e.map(|e| self.expr(e))))
595606
}
596-
StmtP::Return(None) => stmt!(self, "return", span, |_eval| {
597-
return Err(EvalException::Return(Value::new_none()));
598-
}),
599607
StmtP::If(cond, box then_block) => self.stmt_if(span, cond, then_block, allow_gc),
600608
StmtP::IfElse(cond, box (then_block, else_block)) => {
601609
self.stmt_if_else(span, cond, then_block, else_block, allow_gc)

0 commit comments

Comments
 (0)