Skip to content

Commit

Permalink
Add vec_wrapper macro
Browse files Browse the repository at this point in the history
Summary: We're going to need at least one more of these Vec-wrapping newtypes (for UserAttributes, to support D43119059), so move these forwarding impls into a macro. Add a few more impls and revert some churn caused by not implementing Deref/Extend/FromIterator for Block in D43102649 (3c06cb6).

Reviewed By: edwinsmith

Differential Revision: D43173230

fbshipit-source-id: 95c5f9a792a2a7afad87b37b63a4f28f91f67d9a
  • Loading branch information
rjbailey authored and facebook-github-bot committed Feb 10, 2023
1 parent 14183ab commit dff1d26
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 178 deletions.
17 changes: 5 additions & 12 deletions hphp/hack/src/hackc/compile/closure_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,7 @@ impl<'ast, 'a: 'b, 'b, 'arena: 'a> VisitorMut<'ast> for ClosureVisitor<'a, 'b, '
let cd = scope.as_class_summary().ok_or_else(|| {
Error::unrecoverable("unexpected scope shape - method is not inside the class")
})?;
let variables =
Self::compute_variables_from_fun(&md.params, md.body.fb_ast.as_slice(), None)?;
let variables = Self::compute_variables_from_fun(&md.params, &md.body.fb_ast, None)?;
let coeffects = Coeffects::from_ast(
self.alloc,
md.ctxs.as_ref(),
Expand Down Expand Up @@ -920,11 +919,8 @@ impl<'ast, 'a: 'b, 'b, 'arena: 'a> VisitorMut<'ast> for ClosureVisitor<'a, 'b, '
// need to handle it ourselvses, because visit_fun_ is
// called both for toplevel functions and lambdas
Def::Fun(fd) => {
let variables = Self::compute_variables_from_fun(
&fd.fun.params,
fd.fun.body.fb_ast.as_slice(),
None,
)?;
let variables =
Self::compute_variables_from_fun(&fd.fun.params, &fd.fun.body.fb_ast, None)?;
let coeffects = Coeffects::from_ast(
self.alloc,
fd.fun.ctxs.as_ref(),
Expand Down Expand Up @@ -1351,11 +1347,8 @@ impl<'a: 'b, 'b, 'arena: 'a + 'b> ClosureVisitor<'a, 'b, 'arena> {
.map(|Lid(_, (_, name))| name.to_string())
.collect()
});
let variables = Self::compute_variables_from_fun(
&fd.params,
fd.body.fb_ast.as_slice(),
explicit_capture,
)?;
let variables =
Self::compute_variables_from_fun(&fd.params, &fd.body.fb_ast, explicit_capture)?;
let coeffects =
Coeffects::from_ast(self.alloc, fd.ctxs.as_ref(), &fd.params, &fd.tparams, &[]);
let si = ScopeSummary::Lambda(LambdaSummary {
Expand Down
6 changes: 2 additions & 4 deletions hphp/hack/src/hackc/compile/rewrite_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn extract_debugger_main(
stmts
};
let p = Pos::make_none;
let mut unsets: Vec<_> = vars
let mut unsets: ast::Block = vars
.iter()
.map(|name| {
let name = local_id::make_unscoped(name);
Expand Down Expand Up @@ -176,9 +176,7 @@ fn extract_debugger_main(
params,
ctxs: None, // TODO(T70095684)
unsafe_ctxs: None, // TODO(T70095684)
body: FuncBody {
fb_ast: ast::Block(body),
},
body: FuncBody { fb_ast: body },
fun_kind: FunKind::FSync,
user_attributes: vec![UserAttribute {
name: Id(Pos::make_none(), "__DebuggerMain".into()),
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/src/hackc/emitter/emit_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn emit_function<'a, 'arena, 'decl>(
alloc,
e,
RcOc::clone(&fd.namespace),
ast_body.as_slice(),
ast_body,
instr::null(),
scope,
EmitBodyArgs {
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/src/hackc/emitter/emit_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub fn from_ast<'a, 'arena, 'decl>(
emitter.alloc,
emitter,
namespace,
ast_body_block.as_slice(),
ast_body_block,
instr::null(),
scope,
emit_body::Args {
Expand Down
33 changes: 14 additions & 19 deletions hphp/hack/src/hackc/emitter/emit_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ pub fn emit_stmt<'a, 'arena, 'decl>(
_ => emit_expr::emit_ignored_expr(e, env, pos, e_),
},
a::Stmt_::Return(r_opt) => emit_return_(e, env, (**r_opt).as_ref(), pos),
a::Stmt_::Block(b) => emit_block(env, e, &b.0),
a::Stmt_::If(f) => emit_if(e, env, pos, &f.0, &f.1.0, &f.2.0),
a::Stmt_::While(x) => emit_while(e, env, &x.0, &x.1.0),
a::Stmt_::Block(b) => emit_block(env, e, b),
a::Stmt_::If(f) => emit_if(e, env, pos, &f.0, &f.1, &f.2),
a::Stmt_::While(x) => emit_while(e, env, &x.0, &x.1),
a::Stmt_::Using(x) => emit_using(e, env, x),
a::Stmt_::Break => Ok(emit_break(e, env, pos)),
a::Stmt_::Continue => Ok(emit_continue(e, env, pos)),
a::Stmt_::Do(x) => emit_do(e, env, &x.0.0, &x.1),
a::Stmt_::For(x) => emit_for(e, env, &x.0, x.1.as_ref(), &x.2, &x.3.0),
a::Stmt_::Do(x) => emit_do(e, env, &x.0, &x.1),
a::Stmt_::For(x) => emit_for(e, env, &x.0, x.1.as_ref(), &x.2, &x.3),
a::Stmt_::Throw(x) => emit_throw(e, env, x, pos),
a::Stmt_::Try(x) => emit_try(e, env, x, pos),
a::Stmt_::Switch(x) => emit_switch(e, env, pos, &x.0, &x.1, &x.2),
a::Stmt_::Foreach(x) => emit_foreach(e, env, pos, &x.0, &x.1, &x.2.0),
a::Stmt_::Awaitall(x) => emit_awaitall(e, env, pos, &x.0, &x.1.0),
a::Stmt_::Foreach(x) => emit_foreach(e, env, pos, &x.0, &x.1, &x.2),
a::Stmt_::Awaitall(x) => emit_awaitall(e, env, pos, &x.0, &x.1),
a::Stmt_::Markup(x) => emit_markup(e, env, x, false),
a::Stmt_::Fallthrough | a::Stmt_::Noop => Ok(instr::empty()),
a::Stmt_::AssertEnv(_) => Ok(instr::empty()),
Expand Down Expand Up @@ -207,7 +207,6 @@ fn emit_try<'a, 'arena, 'decl>(
pos: &Pos,
) -> Result<InstrSeq<'arena>> {
let (try_block, catch_list, finally_block) = x;
let (try_block, finally_block) = (try_block.as_slice(), finally_block.as_slice());
if catch_list.is_empty() {
emit_try_finally(e, env, pos, try_block, finally_block)
} else if finally_block.is_empty() {
Expand Down Expand Up @@ -298,7 +297,7 @@ fn emit_case<'c, 'a, 'arena, 'decl>(
let ast::Case(case_expr, body) = case;

let label = e.label_gen_mut().next_regular();
let mut res = vec![instr::label(label), emit_block(env, e, body.as_slice())?];
let mut res = vec![instr::label(label), emit_block(env, e, body)?];
if addbreak {
res.push(emit_break(e, env, &Pos::make_none()));
}
Expand All @@ -314,10 +313,7 @@ fn emit_default_case<'a, 'arena, 'decl>(

let label = e.label_gen_mut().next_regular();
Ok((
InstrSeq::gather(vec![
instr::label(label),
emit_block(env, e, body.as_slice())?,
]),
InstrSeq::gather(vec![instr::label(label), emit_block(env, e, body)?]),
label,
))
}
Expand Down Expand Up @@ -457,7 +453,7 @@ fn emit_using<'a, 'arena, 'decl>(
using: &ast::UsingStmt,
) -> Result<InstrSeq<'arena>> {
let alloc = env.arena;
let block_pos = block_pos(using.block.as_slice())?;
let block_pos = block_pos(&using.block)?;
if using.exprs.1.len() > 1 {
emit_stmts(
e,
Expand Down Expand Up @@ -526,8 +522,7 @@ fn emit_using<'a, 'arena, 'decl>(
};
let finally_start = e.label_gen_mut().next_regular();
let finally_end = e.label_gen_mut().next_regular();
let body =
env.do_in_using_body(e, finally_start, using.block.as_slice(), emit_block)?;
let body = env.do_in_using_body(e, finally_start, &using.block, emit_block)?;
let jump_instrs = tfr::JumpInstructions::collect(&body, &mut env.jump_targets_gen);
let jump_instrs_is_empty = jump_instrs.is_empty();
let finally_epilogue =
Expand Down Expand Up @@ -590,7 +585,7 @@ fn emit_using<'a, 'arena, 'decl>(
])
};
let exn_local = e.local_gen_mut().get_unnamed();
let middle = if is_empty_block(using.block.as_slice()) {
let middle = if is_empty_block(&using.block) {
instr::empty()
} else {
let finally_instrs = emit_finally(e, local, using.has_await, using.is_block_scoped);
Expand Down Expand Up @@ -954,7 +949,7 @@ fn emit_catch<'a, 'arena, 'decl>(
instr::jmp_z(next_catch),
instr::set_l(e.named_local(local_id::get_name(catch_local_id).into())),
instr::pop_c(),
emit_stmts(e, env, catch_block.as_slice())?,
emit_stmts(e, env, catch_block)?,
emit_pos(pos),
instr::jmp(end_label),
instr::label(next_catch),
Expand Down Expand Up @@ -1509,7 +1504,7 @@ pub fn emit_final_stmt<'a, 'arena, 'decl>(
) -> Result<InstrSeq<'arena>> {
match &stmt.1 {
a::Stmt_::Throw(_) | a::Stmt_::Return(_) | a::Stmt_::YieldBreak => emit_stmt(e, env, stmt),
a::Stmt_::Block(stmts) => emit_final_stmts(env, e, stmts.as_slice()),
a::Stmt_::Block(stmts) => emit_final_stmts(env, e, stmts),
_ => {
let ret = emit_dropthrough_return(e, env)?;
Ok(InstrSeq::gather(vec![emit_stmt(e, env, stmt)?, ret]))
Expand Down
10 changes: 5 additions & 5 deletions hphp/hack/src/hackc/print_expr/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,9 +798,9 @@ fn print_block(
ctx: &Context<'_>,
w: &mut dyn Write,
env: &ExprEnv<'_, '_>,
block: &ast::Block,
block: &[ast::Stmt],
) -> Result<()> {
match block.as_slice() {
match block {
[] | [ast::Stmt(_, ast::Stmt_::Noop)] => Ok(()),
[ast::Stmt(_, ast::Stmt_::Block(b))] if b.len() == 1 => print_block_(ctx, w, env, b),
[_, _, ..] => print_block_(ctx, w, env, block),
Expand All @@ -812,10 +812,10 @@ fn print_block_(
ctx: &Context<'_>,
w: &mut dyn Write,
env: &ExprEnv<'_, '_>,
block: &ast::Block,
block: &[ast::Stmt],
) -> Result<()> {
write::wrap_by_(w, "{\n", "}\n", |w| {
write::concat(w, &block.0, |w, stmt| {
write::concat(w, block, |w, stmt| {
if !matches!(stmt.1, ast::Stmt_::Noop) {
w.write_all(b" ")?;
print_statement(ctx, w, env, stmt)?;
Expand Down Expand Up @@ -849,7 +849,7 @@ fn print_statement(
S_::While(x) => {
let (cond, block) = &**x;
write::wrap_by_(w, "while (", ") ", |w| print_expr(ctx, w, env, cond))?;
print_block(ctx, w, env, block)
print_block(ctx, w, env, block.as_ref())
}
S_::If(x) => {
let (cond, if_block, else_block) = &**x;
Expand Down
3 changes: 1 addition & 2 deletions hphp/hack/src/hackc/types/readonly_nonlocal_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,11 +697,10 @@ impl<'decl> Infer<'decl> {

fn infer_stmts(
&mut self,
stmts: impl AsRef<[ast::Stmt]>,
stmts: &[ast::Stmt],
mut ctx: Ctx,
where_: Where<'_>,
) -> (ast::Block, Ctx) {
let stmts = stmts.as_ref();
let mut out = Vec::with_capacity(stmts.len());
for stmt in stmts.iter() {
let (s, s_ctx) = self.infer_stmt(stmt, ctx, where_);
Expand Down
Loading

0 comments on commit dff1d26

Please sign in to comment.