Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): can't pass inflight closure as parameter to super() call #6599

Merged
merged 8 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/tests/valid/inflight_closure_as_super_param.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Foo {}

class Base {
pub h: inflight (): str;
pub f_base: Foo;
new(handler: inflight (): str, f: Foo) {
this.h = handler;
this.f_base = f;
}
}

class Derived extends Base {
pub f: Foo;
new() {
super(inflight (): str => {
return "boom!";
}, new Foo() as "in_root");
yoav-steinberg marked this conversation as resolved.
Show resolved Hide resolved
this.f = new Foo() as "in_derived";
}
}

let c = new Derived() as "derived";
// Make sure that instances created in a ctor are scoped to the instance they were created in
// This is related to this test because the transformed inflight closure is also an instance
// created in a ctor, but it's special cased to to be scoped in the class because `this` isn't
// available during the closures instantiation.
assert(nodeof(c.f).path.endsWith("derived/in_derived"));
// Make sure the instance created in the super call is scoped to the parent (root)
assert(!nodeof(c.f_base).path.endsWith("derived/in_root"));
let appPath = nodeof(this).path;
assert(nodeof(c.f_base).path == "{appPath}/in_root");

test "boom!" {
assert(c.h() == "boom!");
}
11 changes: 9 additions & 2 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,14 @@ impl<'a> JSifier<'a> {
}) {
Some(SCOPE_PARAM.to_string())
} else {
Some("this".to_string())
// By default use `this` as the scope.
// If we're inside an argument to a `super()` call then `this` isn't avialble, in which case
// we can safely use the ctor's `$scope` arg.
if ctx.visit_ctx.current_stmt_is_super_call() {
Some(SCOPE_PARAM.to_string())
} else {
Some("this".to_string())
}
}
}
} else {
Expand Down Expand Up @@ -1146,7 +1153,7 @@ impl<'a> JSifier<'a> {
let mut code = CodeMaker::with_source(&statement.span);

CompilationContext::set(CompilationPhase::Jsifying, &statement.span);
ctx.visit_ctx.push_stmt(statement.idx);
ctx.visit_ctx.push_stmt(statement);
match &statement.kind {
StmtKind::Bring { source, identifier } => match source {
BringSource::BuiltinModule(name) => {
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/lifting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'a> Visit<'a> for LiftVisitor<'a> {
fn visit_stmt(&mut self, node: &'a Stmt) {
CompilationContext::set(CompilationPhase::Lifting, &node.span);

self.ctx.push_stmt(node.idx);
self.ctx.push_stmt(node);

// If this is an explicit lift statement then add the explicit lift
if let StmtKind::ExplicitLift(explicit_lift) = &node.kind {
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/lsp/symbol_locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<'a> Visit<'a> for SymbolLocator<'a> {
return;
}

self.ctx.push_stmt(node.idx);
self.ctx.push_stmt(node);

// Handle situations where symbols are actually defined in inner scopes
match &node.kind {
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4124,7 +4124,7 @@ new cloud.Function(@inflight("./handler.ts"), lifts: { bucket: ["put"] });
CompilationContext::set(CompilationPhase::TypeChecking, &stmt.span);

// Set the current statement index for symbol lookup checks.
self.with_stmt(stmt.idx, |tc| match &stmt.kind {
self.with_stmt(stmt, |tc| match &stmt.kind {
StmtKind::Let {
reassignable,
var_name,
Expand Down
25 changes: 19 additions & 6 deletions libs/wingc/src/visit_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::Itertools;

use crate::{
ast::{Class, ExprId, FunctionSignature, Phase, Symbol, UserDefinedType},
ast::{Class, ExprId, FunctionSignature, Phase, Stmt, StmtKind, Symbol, UserDefinedType},
type_check::symbol_env::SymbolEnvRef,
};

Expand All @@ -18,6 +18,12 @@ pub enum PropertyObject {
Instance(ExprId),
}

#[derive(Clone)]
pub struct StmtContext {
pub idx: usize,
pub super_call: bool,
}

#[derive(Clone)]
pub struct VisitContext {
phase: Vec<Phase>,
Expand All @@ -26,7 +32,7 @@ pub struct VisitContext {
property: Vec<(PropertyObject, Symbol)>,
function: Vec<FunctionContext>,
class: Vec<UserDefinedType>,
statement: Vec<usize>,
statement: Vec<StmtContext>,
in_json: Vec<bool>,
in_type_annotation: Vec<bool>,
expression: Vec<ExprId>,
Expand Down Expand Up @@ -64,16 +70,23 @@ impl VisitContext {

// --

pub fn push_stmt(&mut self, stmt: usize) {
self.statement.push(stmt);
pub fn push_stmt(&mut self, stmt: &Stmt) {
self.statement.push(StmtContext {
idx: stmt.idx,
super_call: matches!(stmt.kind, StmtKind::SuperConstructor { .. }),
});
}

pub fn pop_stmt(&mut self) {
self.statement.pop();
}

pub fn current_stmt_idx(&self) -> usize {
*self.statement.last().unwrap_or(&0)
self.statement.last().map_or(0, |s| s.idx)
}

pub fn current_stmt_is_super_call(&self) -> bool {
self.statement.last().map_or(false, |s| s.super_call)
}

// --
Expand Down Expand Up @@ -230,7 +243,7 @@ pub trait VisitorWithContext {
self.ctx().pop_expr();
}

fn with_stmt(&mut self, stmt: usize, f: impl FnOnce(&mut Self)) {
fn with_stmt(&mut self, stmt: &Stmt, f: impl FnOnce(&mut Self)) {
self.ctx().push_stmt(stmt);
f(self);
self.ctx().pop_stmt();
Expand Down
Loading
Loading