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

feat: capture reassignable variable and mutable objects #3064

Merged
merged 16 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
16 changes: 0 additions & 16 deletions examples/tests/invalid/capture_mutables.w

This file was deleted.

8 changes: 0 additions & 8 deletions examples/tests/invalid/capture_reassignable.w

This file was deleted.

16 changes: 16 additions & 0 deletions examples/tests/valid/capture_mutables.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let a = MutArray<str>["hello"];
let s = MutSet<num>{12};
let m = MutMap<bool>{"hello": true};

let aCloned = (Array<str>["hello"]).copyMut();

let handler = inflight () => {
assert(a.length == 1);
assert(s.size == 1);
assert(m.size() == 1);
assert(aCloned.length == 1);
};

test "main" {
handler();
}
38 changes: 38 additions & 0 deletions examples/tests/valid/capture_reassigable_class_field.w
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
bring cloud;
class KeyValueStore {
bucket: cloud.Bucket;
var onUpdateCallback : inflight (str): void;
init(store: cloud.Bucket) {
this.bucket = store;
this.onUpdateCallback = inflight (k: str) => {};
}

onUpdate(fn: inflight (str):void){
this.onUpdateCallback = fn;
}

inflight get(key: str): Json {
return this.bucket.getJson(key);
}
inflight set(key: str, value: Json): void {
this.onUpdateCallback(key);
this.bucket.putJson(key, value);
}
}

let kv = new KeyValueStore(new cloud.Bucket());
let counter = new cloud.Counter();
kv.onUpdate(inflight (key: str): void => {
counter.inc(1, key);
});

test "main" {
kv.set("k", Json {
value: "v"
});
kv.get("k");
kv.get("k");
kv.get("k2");
assert(counter.peek("k") == 2);
assert(counter.peek("k2") == 1);
}
12 changes: 12 additions & 0 deletions examples/tests/valid/capture_reassignable.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;

let var x = 5;

let handler = inflight (): void => {
assert(x == 5);

};

test "main" {
handler();
}
13 changes: 1 addition & 12 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ impl<'a> JSifier<'a> {
.filter(|(_, kind, _)| {
let var = kind.as_variable().unwrap();
// We capture preflight non-reassignable fields
var.phase != Phase::Inflight && !var.reassignable && var.type_.is_capturable()
var.phase != Phase::Inflight && var.type_.is_capturable()
})
.map(|(name, ..)| name)
.collect_vec()
Expand Down Expand Up @@ -1541,19 +1541,8 @@ impl<'ast> Visit<'ast> for FieldReferenceVisitor<'ast> {
}

// now we need to verify that the component can be captured.
// (1) non-reassignable
// (2) capturable type (immutable/resource).

// if the variable is reassignable, bail out
if variable.reassignable {
report_diagnostic(Diagnostic {
message: format!("Cannot capture reassignable field '{curr}'"),
span: Some(curr.span.clone()),
});

return;
}

// if this type is not capturable, bail out
if !variable.type_.is_capturable() {
report_diagnostic(Diagnostic {
Expand Down
8 changes: 4 additions & 4 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,10 @@ impl TypeRef {
Type::Anything => false,
Type::Unresolved => false,
Type::Void => false,
Type::MutJson => false,
Type::MutArray(_) => false,
Type::MutMap(_) => false,
Type::MutSet(_) => false,
Type::MutJson => true,
Type::MutArray(v) => v.is_capturable(),
Type::MutMap(v) => v.is_capturable(),
Type::MutSet(v) => v.is_capturable(),
Type::Function(sig) => sig.phase == Phase::Inflight,

// only preflight classes can be captured
Expand Down
90 changes: 3 additions & 87 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,63 +116,6 @@ error: Cannot find module \\"foobar\\" in source directory: Unable to load \\"fo



Tests 1 failed (1)
Duration <DURATION>
"
`;

exports[`capture_mutables.w 1`] = `
"error: Cannot capture field 'a' with non-capturable type 'MutArray<str>'
--> ../../../examples/tests/invalid/capture_mutables.w:8:10
|
8 | assert(a.length == 1);
| ^ Cannot capture field 'a' with non-capturable type 'MutArray<str>'


error: Cannot capture field 's' with non-capturable type 'MutSet<num>'
--> ../../../examples/tests/invalid/capture_mutables.w:10:10
|
10 | assert(s.size == 1);
| ^ Cannot capture field 's' with non-capturable type 'MutSet<num>'


error: Cannot capture field 'm' with non-capturable type 'MutMap<bool>'
--> ../../../examples/tests/invalid/capture_mutables.w:12:10
|
12 | assert(m.size() == 1);
| ^ Cannot capture field 'm' with non-capturable type 'MutMap<bool>'


error: Cannot capture field 'aCloned' with non-capturable type 'MutArray<str>'
--> ../../../examples/tests/invalid/capture_mutables.w:14:10
|
14 | assert(aCloned.length == 1);
| ^^^^^^^ Cannot capture field 'aCloned' with non-capturable type 'MutArray<str>'







Tests 1 failed (1)
Duration <DURATION>
"
`;

exports[`capture_reassignable.w 1`] = `
"error: Cannot capture reassignable field 'x'
--> ../../../examples/tests/invalid/capture_reassignable.w:6:15
|
6 | log(\\"x: \${x}\\");
| ^ Cannot capture reassignable field 'x'







Tests 1 failed (1)
Duration <DURATION>
"
Expand Down Expand Up @@ -733,26 +676,13 @@ Duration <DURATION>
`;

exports[`inflight_class_capture_mutable.w 1`] = `
"error: Cannot capture field 'x' with non-capturable type 'MutArray<str>'
--> ../../../examples/tests/invalid/inflight_class_capture_mutable.w:5:9
|
5 | log(x.at(0));
| ^ Cannot capture field 'x' with non-capturable type 'MutArray<str>'


error: Cannot capture reassignable field 'foo'
--> ../../../examples/tests/invalid/inflight_class_capture_mutable.w:14:12
|
14 | return foo;
| ^^^ Cannot capture reassignable field 'foo'


"pass ─ inflight_class_capture_mutable.wsim (no tests)





Tests 1 failed (1)
Tests 1 passed (1)
Duration <DURATION>
"
`;
Expand Down Expand Up @@ -1503,21 +1433,7 @@ Duration <DURATION>
`;

exports[`resource_captures.w 1`] = `
"error: Cannot capture reassignable field 'reassignable'
--> ../../../examples/tests/invalid/resource_captures.w:18:17
|
18 | log(\\"\${this.reassignable}\\");
| ^^^^^^^^^^^^ Cannot capture reassignable field 'reassignable'


error: Cannot capture field 'mutArray' with non-capturable type 'MutArray<str>'
--> ../../../examples/tests/invalid/resource_captures.w:20:14
|
20 | log(this.mutArray.at(0));
| ^^^^^^^^ Cannot capture field 'mutArray' with non-capturable type 'MutArray<str>'


error: Unable to qualify which operations are performed on 'this.bucket' of type 'Bucket'. This is not supported yet.
"error: Unable to qualify which operations are performed on 'this.bucket' of type 'Bucket'. This is not supported yet.
--> ../../../examples/tests/invalid/resource_captures.w:23:13
|
23 | let b = this.bucket;
Expand Down
Loading
Loading