Skip to content

Commit be123bb

Browse files
stepanchegfacebook-github-bot
authored andcommitted
ExprCompiledValue::Type
Summary: A variant of compiled expression which is `type(expr)`. Used in the following diff D30867542. Reviewed By: ndmitchell Differential Revision: D30867363 fbshipit-source-id: b79a146545774b061993467d6b032706f77a6905
1 parent 3c1c0fc commit be123bb

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

starlark/src/eval/fragment/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ pub(crate) type ExprCompiled = Box<
4848
pub(crate) enum ExprCompiledValue {
4949
Value(FrozenValue),
5050
Compiled(ExprCompiled),
51+
/// `type(x)`
52+
Type(ExprCompiled),
5153
}
5254

5355
impl ExprCompiledValue {
5456
pub fn as_value(&self) -> Option<FrozenValue> {
5557
match self {
5658
Self::Value(x) => Some(*x),
57-
Self::Compiled(_) => None,
59+
_ => None,
5860
}
5961
}
6062

6163
pub fn as_compiled(self) -> ExprCompiled {
6264
match self {
6365
Self::Value(x) => box move |_| Ok(x.to_value()),
6466
Self::Compiled(x) => x,
67+
Self::Type(x) => expr!("type", |eval| {
68+
x(eval)?.get_ref().get_type_value().to_value()
69+
})
70+
.as_compiled(),
6571
}
6672
}
6773
}

starlark/src/eval/fragment/known.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ use crate::{
2222
eval::{
2323
compiler::{scope::CstExpr, Compiler},
2424
fragment::expr::{ExprCompiled, ExprCompiledValue},
25-
Evaluator,
2625
},
2726
syntax::ast::ExprP,
28-
values::{dict::Dict, list::List, Value},
27+
values::{dict::Dict, list::List},
2928
};
3029

3130
/// Convert a list into a tuple. In many cases (iteration, `in`) these types
@@ -72,11 +71,11 @@ impl Compiler<'_> {
7271
Conditional::False
7372
}
7473
}
75-
ExprCompiledValue::Compiled(v) => {
74+
v => {
7675
if expect {
77-
Conditional::Normal(v)
76+
Conditional::Normal(v.as_compiled())
7877
} else {
79-
Conditional::Negate(v)
78+
Conditional::Negate(v.as_compiled())
8079
}
8180
}
8281
}
@@ -99,11 +98,7 @@ impl Compiler<'_> {
9998
}
10099
match self.expr(expr) {
101100
ExprCompiledValue::Value(x) => ExprCompiledValue::Value(x.to_value().get_type_value()),
102-
ExprCompiledValue::Compiled(x) => {
103-
expr!("type", |eval| {
104-
x(eval)?.get_ref().get_type_value().to_value()
105-
})
106-
}
101+
x => ExprCompiledValue::Type(x.as_compiled()),
107102
}
108103
}
109104
}

starlark/src/eval/fragment/stmt.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,12 @@ impl Compiler<'_> {
435435
fn stmt_expr_compiled(&mut self, span: Span, expr: ExprCompiledValue) -> StmtsCompiled {
436436
match expr {
437437
ExprCompiledValue::Value(_) => StmtsCompiled::empty(),
438-
ExprCompiledValue::Compiled(e) => stmt!(self, "expr", span, |eval| {
439-
e(eval)?;
440-
}),
438+
e => {
439+
let e = e.as_compiled();
440+
stmt!(self, "expr", span, |eval| {
441+
e(eval)?;
442+
})
443+
}
441444
}
442445
}
443446

0 commit comments

Comments
 (0)