Skip to content

Commit 7702fe6

Browse files
stepanchegfacebook-github-bot
authored andcommitted
StmtCompiledValue
Summary: Preserve some structure of function body after AST processing before closure creation. Used to implement type is inlining, this stack up to D30888372. Reviewed By: ndmitchell Differential Revision: D30888376 fbshipit-source-id: 0f60293b38cde29818377410891d63cc8c359669
1 parent 091fd60 commit 7702fe6

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

starlark/src/eval/fragment/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ macro_rules! stmt {
8585
f: impl for<'v> Fn(&mut Evaluator<'v, '_>) -> Result<(), EvalException<'v>>
8686
+ Send + Sync + 'static,
8787
) -> StmtsCompiled {
88-
StmtsCompiled::one(box move |eval| f(eval))
88+
StmtsCompiled::one(StmtCompiledValue::Compiled(box move |eval| f(eval)))
8989
}
9090
$self.maybe_wrap_before_stmt($span, [<ann_stmt_ $name>](move |$eval|
9191
$eval.ann($name, |$eval| {

starlark/src/eval/fragment/stmt.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ use thiserror::Error;
4848
pub(crate) type StmtCompiled =
4949
Box<dyn for<'v> Fn(&mut Evaluator<'v, '_>) -> Result<(), EvalException<'v>> + Send + Sync>;
5050

51+
pub(crate) enum StmtCompiledValue {
52+
Compiled(StmtCompiled),
53+
}
54+
55+
impl StmtCompiledValue {
56+
pub(crate) fn as_compiled(self) -> StmtCompiled {
57+
match self {
58+
StmtCompiledValue::Compiled(c) => c,
59+
}
60+
}
61+
}
62+
5163
enum SmallVec1<T> {
5264
Empty,
5365
One(T),
@@ -76,14 +88,14 @@ impl<T> SmallVec1<T> {
7688
}
7789
}
7890

79-
pub(crate) struct StmtsCompiled(SmallVec1<StmtCompiled>);
91+
pub(crate) struct StmtsCompiled(SmallVec1<StmtCompiledValue>);
8092

8193
impl StmtsCompiled {
8294
pub(crate) fn empty() -> StmtsCompiled {
8395
StmtsCompiled(SmallVec1::Empty)
8496
}
8597

86-
pub(crate) fn one(stmt: StmtCompiled) -> StmtsCompiled {
98+
pub(crate) fn one(stmt: StmtCompiledValue) -> StmtsCompiled {
8799
StmtsCompiled(SmallVec1::One(stmt))
88100
}
89101

@@ -113,9 +125,10 @@ impl StmtsCompiled {
113125
pub(crate) fn as_compiled(self) -> StmtCompiled {
114126
match self.0 {
115127
SmallVec1::Empty => box |_eval| Ok(()),
116-
SmallVec1::One(stmt) => stmt,
128+
SmallVec1::One(stmt) => stmt.as_compiled(),
117129
SmallVec1::Many(vec) => {
118130
debug_assert!(vec.len() > 1);
131+
let vec = vec.into_map(|s| s.as_compiled());
119132
box move |eval| {
120133
for stmt in &vec {
121134
stmt(eval)?;
@@ -413,10 +426,10 @@ impl Compiler<'_> {
413426
assert!(stmt.len() == 1);
414427
if self.has_before_stmt {
415428
let stmt = stmt.as_compiled();
416-
StmtsCompiled::one(box move |eval| {
429+
StmtsCompiled::one(StmtCompiledValue::Compiled(box move |eval| {
417430
before_stmt(span, eval);
418431
stmt(eval)
419-
})
432+
}))
420433
} else {
421434
stmt
422435
}
@@ -430,10 +443,10 @@ impl Compiler<'_> {
430443
// We could do this more efficiently by fusing the possible_gc
431444
// into the inner closure, but no real need - we insert allow_gc fairly rarely
432445
let res = res.as_compiled();
433-
StmtsCompiled::one(box move |eval| {
446+
StmtsCompiled::one(StmtCompiledValue::Compiled(box move |eval| {
434447
possible_gc(eval);
435448
res(eval)
436-
})
449+
}))
437450
} else {
438451
res
439452
}

0 commit comments

Comments
 (0)