Skip to content

Commit 9abcdf6

Browse files
Scott Owensfacebook-github-bot
authored andcommitted
Make all expression tree virtual lambdas pure
Summary: Type checking currently turns off coeffect checks when checking the virtualized expression of an expression tree. This gets in the way of unifying the virtulaized repression and runtime expression, which I plan to do. Marking lambdas in the virtual expressions as pure will allow the expression trees to be used inside of pure functions. The only tricky bit is that the virtualized expression can contain assignments to properties where the expression tree expression does. We wrap those statements in lambdas with `write_prop` capabilities, and don't call the lambda. This keeps the type checking, and doesn't change runtime behaviour since the virtualized expression is never run. This does mean that property assignment in expression trees will no longer refine the type of the property. Reviewed By: viratyosin Differential Revision: D52842485 fbshipit-source-id: dffcff241cc79dded3600b80a9eb60acb4cc13d7
1 parent bcc0a88 commit 9abcdf6

File tree

9 files changed

+60
-11
lines changed

9 files changed

+60
-11
lines changed

hphp/hack/src/parser/lowerer/desugar_expression_tree.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bstr::BString;
22
use naming_special_names_rust::classes;
3+
use naming_special_names_rust::coeffects;
34
use naming_special_names_rust::expression_trees as et;
45
use naming_special_names_rust::pseudo_functions;
56
use naming_special_names_rust::special_idents;
@@ -14,8 +15,10 @@ use oxidized::aast_visitor::VisitorMut;
1415
use oxidized::ast;
1516
use oxidized::ast::ClassId;
1617
use oxidized::ast::ClassId_;
18+
use oxidized::ast::Contexts;
1719
use oxidized::ast::Expr;
1820
use oxidized::ast::Expr_;
21+
use oxidized::ast::Fun_;
1922
use oxidized::ast::Hint_;
2023
use oxidized::ast::Sid;
2124
use oxidized::ast::Stmt;
@@ -831,15 +834,60 @@ fn rewrite_expr(
831834
],
832835
&pos,
833836
);
834-
let virtual_expr = Expr(
837+
let is_property_lhs = matches!(
838+
rewritten_lhs.virtual_expr,
839+
Expr(_, _, Expr_::ObjGet(box (_, _, _, oxidized::aast::PropOrMethod::IsProp)))
840+
);
841+
let mut virtual_expr = Expr(
835842
(),
836-
pos,
843+
pos.clone(),
837844
Binop(Box::new(aast::Binop {
838845
bop,
839846
lhs: rewritten_lhs.virtual_expr,
840847
rhs: rewritten_rhs.virtual_expr,
841848
})),
842849
);
850+
if is_property_lhs {
851+
virtual_expr = Expr(
852+
(),
853+
pos.clone(),
854+
Lfun(Box::new((
855+
Fun_ {
856+
span: pos.clone(),
857+
readonly_this: None,
858+
annotation: (),
859+
readonly_ret: None,
860+
ret: aast::TypeHint((), None),
861+
params: vec![],
862+
ctxs: Some(Contexts(
863+
pos.clone(),
864+
vec![ast::Hint(
865+
pos.clone(),
866+
Box::new(Hint_::Happly(
867+
ast::Id(
868+
pos.clone(),
869+
coeffects::WRITE_PROPS.to_string(),
870+
),
871+
vec![],
872+
)),
873+
)],
874+
)),
875+
unsafe_ctxs: None,
876+
body: aast::FuncBody {
877+
fb_ast: aast::Block(vec![aast::Stmt(
878+
pos,
879+
aast::Stmt_::Expr(Box::new(virtual_expr)),
880+
)]),
881+
},
882+
fun_kind: FunKind::FSync,
883+
user_attributes: aast::UserAttributes(vec![]),
884+
external: false,
885+
doc_comment: None,
886+
},
887+
vec![],
888+
))),
889+
);
890+
}
843891
RewriteResult {
844892
virtual_expr,
845893
desugar_expr,
@@ -1274,7 +1322,7 @@ fn rewrite_expr(
12741322
}
12751323
}
12761324
// Source: MyDsl`($x) ==> { ... }`
1277-
// Virtualized: ($x) ==> { ...; return MyDsl::voidType(); }
1325+
// Virtualized: ($x)[] ==> { ...; return MyDsl::voidType(); }
12781326
// if no `return expr;` statements.
12791327
// Desugared: $0v->visitLambda(new ExprPos(...), vec['$x'], vec[...]).
12801328
Lfun(lf) => {
@@ -1351,6 +1399,7 @@ fn rewrite_expr(
13511399
&pos,
13521400
);
13531401
fun_.body.fb_ast = ast::Block(virtual_body_stmts);
1402+
fun_.ctxs = Some(Contexts(pos.clone(), vec![]));
13541403

13551404
let virtual_expr = _virtualize_lambda(
13561405
visitor_name,

hphp/hack/test/nast/expression_tree_assign.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
f_readonly_ret = None;
4040
f_ret = ((), None);
4141
f_params = [];
42-
f_ctxs = None;
42+
f_ctxs = (Some ([4:14-32], []));
4343
f_unsafe_ctxs = None;
4444
f_body =
4545
{ fb_ast =

hphp/hack/test/nast/expression_tree_for.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
f_readonly_ret = None;
4343
f_ret = ((), None);
4444
f_params = [];
45-
f_ctxs = None;
45+
f_ctxs = (Some ([4:14-8:4], []));
4646
f_unsafe_ctxs = None;
4747
f_body =
4848
{ fb_ast =

hphp/hack/test/nast/expression_tree_loop.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
f_readonly_ret = None;
4545
f_ret = ((), None);
4646
f_params = [];
47-
f_ctxs = None;
47+
f_ctxs = (Some ([4:14-47], []));
4848
f_unsafe_ctxs = None;
4949
f_body =
5050
{ fb_ast =

hphp/hack/test/nast/expression_tree_virtualize_functions/expression_tree_assign.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
f_readonly_ret = None;
4646
f_ret = ((), None);
4747
f_params = [];
48-
f_ctxs = None;
48+
f_ctxs = (Some ([4:14-32], []));
4949
f_unsafe_ctxs = None;
5050
f_body =
5151
{ fb_ast =

hphp/hack/test/nast/expression_tree_virtualize_functions/expression_tree_for.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
f_readonly_ret = None;
4949
f_ret = ((), None);
5050
f_params = [];
51-
f_ctxs = None;
51+
f_ctxs = (Some ([4:14-8:4], []));
5252
f_unsafe_ctxs = None;
5353
f_body =
5454
{ fb_ast =

hphp/hack/test/nast/expression_tree_virtualize_functions/expression_tree_loop.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
f_readonly_ret = None;
5151
f_ret = ((), None);
5252
f_params = [];
53-
f_ctxs = None;
53+
f_ctxs = (Some ([4:14-47], []));
5454
f_unsafe_ctxs = None;
5555
f_body =
5656
{ fb_ast =

hphp/hack/test/nast/expression_tree_virtualize_functions/hh_show.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
f_readonly_ret = None;
5454
f_ret = ((), None);
5555
f_params = [];
56-
f_ctxs = None;
56+
f_ctxs = (Some ([6:14-9:4], []));
5757
f_unsafe_ctxs = None;
5858
f_body =
5959
{ fb_ast =

hphp/hack/test/nast/hh_show.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
f_readonly_ret = None;
4848
f_ret = ((), None);
4949
f_params = [];
50-
f_ctxs = None;
50+
f_ctxs = (Some ([6:14-9:4], []));
5151
f_unsafe_ctxs = None;
5252
f_body =
5353
{ fb_ast =

0 commit comments

Comments
 (0)