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

merge queue: embarking main (ed54e0c) and #5935 together #5990

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4e0b0f9
lift builtin, wip
yoav-steinberg Mar 11, 2024
4defa8b
wip: explicit lift builtin
yoav-steinberg Mar 13, 2024
f967066
flag explicit lifts
yoav-steinberg Mar 13, 2024
7e6bb13
Allow phase independent explicit qualifications.
yoav-steinberg Mar 13, 2024
05a042b
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 13, 2024
d078ae3
chore: self mutation (build.diff)
monadabot Mar 13, 2024
7a00323
chore: self mutation (e2e-1of2.diff)
monadabot Mar 13, 2024
599fab6
chore: self mutation (e2e-2of2.diff)
monadabot Mar 13, 2024
c6c79c3
Update libs/wingc/src/lifting.rs
yoav-steinberg Mar 13, 2024
49f4553
cr: cleanup jsification of liftmap ops, cleaner snapshots.
yoav-steinberg Mar 13, 2024
d136d5b
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 13, 2024
7cb7785
chore: self mutation (build.diff)
monadabot Mar 13, 2024
3c8551c
chore: self mutation (e2e-1of2.diff)
monadabot Mar 13, 2024
d0be4ce
chore: self mutation (e2e-2of2.diff)
monadabot Mar 13, 2024
776339e
typo
yoav-steinberg Mar 13, 2024
0568c5e
Merge branch 'yoav/explicit_lifting' of https://github.com/winglang/w…
yoav-steinberg Mar 13, 2024
65872d7
cleanups
yoav-steinberg Mar 14, 2024
1f36025
cr
yoav-steinberg Mar 14, 2024
67a6f2c
cr
yoav-steinberg Mar 14, 2024
d5127aa
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 14, 2024
f04a488
chore: self mutation (build.diff)
monadabot Mar 14, 2024
f556aad
chore: self mutation (e2e-2of2.diff)
monadabot Mar 14, 2024
f3e79c9
Update libs/wingc/src/jsify.rs
yoav-steinberg Mar 14, 2024
8b45961
cr
yoav-steinberg Mar 14, 2024
1d45cec
only allow explicit lifts on resource types
yoav-steinberg Mar 14, 2024
6097d4d
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 14, 2024
f777fe7
chore: self mutation (build.diff)
monadabot Mar 14, 2024
ecb6e1f
chore: self mutation (e2e-1of2.diff)
monadabot Mar 14, 2024
e500612
chore: self mutation (e2e-2of2.diff)
monadabot Mar 14, 2024
d6221a8
tests
yoav-steinberg Mar 14, 2024
917db54
Merge branch 'yoav/explicit_lifting' of https://github.com/winglang/w…
yoav-steinberg Mar 14, 2024
1319cec
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 14, 2024
1443164
chore: self mutation (e2e-2of2.diff)
monadabot Mar 14, 2024
e92ca4c
cleanup docs
yoav-steinberg Mar 14, 2024
65eb153
Merge branch 'yoav/explicit_lifting' of https://github.com/winglang/w…
yoav-steinberg Mar 14, 2024
3ed7f90
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 14, 2024
ab0ec45
chore: self mutation (build.diff)
monadabot Mar 14, 2024
18aad74
docs
yoav-steinberg Mar 14, 2024
cbd2f6d
Merge branch 'yoav/explicit_lifting' of https://github.com/winglang/w…
yoav-steinberg Mar 14, 2024
55aa06b
Only allow `lift()` calls at top level of inflight methods
yoav-steinberg Mar 17, 2024
44fc91e
Merge branch 'main' into yoav/explicit_lifting
monadabot Mar 17, 2024
b359acc
chore: self mutation (e2e-2of2.diff)
monadabot Mar 17, 2024
c51e276
Merge of #5935
mergify[bot] Mar 19, 2024
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
59 changes: 59 additions & 0 deletions docs/docs/02-concepts/01-preflight-and-inflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,65 @@ inflight () => {
};
```

### Lift qualification

Preflight objects referenced inflight are called "lifted" objects:

```js playground
let preflight_str = "hello from preflight";
inflight () => {
log(preflight_str); // `preflight_str` is "lifted" into inflight.
};
```

During the lifting process the compiler tries to figure out in what way the lifted objects are being used.
This is how Winglang generats least privilage permissions. Consider the case of lifting a [`cloud.Bucket`](../04-standard-library/cloud/bucket.md) object:

```js playground
bring cloud;
let bucket = new cloud.Bucket();
new cloud.Function(inflight () => {
bucket.put("key", "value"); // `bucket` is lifted and `put` is being used on it
});
```

In this example the compiler generates the correct _write_ access permissions for the [`cloud.Function`](../04-standard-library/cloud/function.md) on `bucket` based on the fact we're `put`ing into it. We say `bucket`'s lift is qualified with `put`.

#### Explicit lift qualification
In some cases the compiler can't figure out (yet) the lift qualifications, and therefore will report an error:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error: the compiler doesn't know the possible values for `b` and therefore can't qualify the lift.
});
```

To explicitly qualify lifts in an inflight closure or inflight method and supress the above compiler error use the `lift()` utility function:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
lift(main_bucket, ["put"]); // Explicitly sate the "put" may be used on `main_bucket`
lift(secondary_bucket, ["put"]); // Explicitly sate the "put" may be used on `secondary_bucket`
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error is supressed and all possible values of `b` were explicitly qualified with "put"
});
```

## Phase-independent code

The global functions `log`, `assert`, and `throw` can all be used in both preflight and inflight code.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ log("UTC: {t1.utc.toIso())}"); // output: 2023-02-09T06:21:03.000Z
| `assert` | checks a condition and _throws_ if evaluated to false |
| `unsafeCast` | cast a value into a different type |
| `nodeof` | obtain the [tree node](./02-concepts/02-application-tree.md) of a preflight object |
| `lift` | explicitly qualify a [lift](./02-concepts/01-preflight-and-inflight.md#explicit-lift-qualification) of a preflight object |
> ```TS
> log("Hello {name}");
Expand Down
40 changes: 40 additions & 0 deletions examples/tests/invalid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
bring cloud;

let bucket = new cloud.Bucket();

let prelight_string = "hi";

class Foo {
pub inflight mehtod1() {
let b = bucket;
lift(b, ["put"]); // Explicit qualification with inflight object, lift call as non first statement
// ^ Expected a preflight object as first argument to `lift` builtin, found inflight expression instead
//^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

lift(prelight_string, ["contains"]); // Explicit qualification on preflight non-class
// ^^^^^^^^^^^^^^^ Expected type to be "Resource", but got "str" instead
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inflight_qualifier = "delete";
lift(bucket, [inflight_qualifier]); // Explicit qualification with inflight qualifiers, lift call as non first statement
// ^^^^^^^^^^^^^^^^^^^^ Qualification list must not contain any inflight elements
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inner_closure = () => {
lift(bucket, ["get"]); // lift() call in inner closure
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
};
class Bar {
pub inflight method() {
lift(bucket, ["get"]); // lift() call in inner class
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
}
}
}

pub inflight method2() {
let b = bucket;
b.put("k", "v"); // With no explicit qualification this should be an error
//^ Expression of type "Bucket" references an unknown preflight object
}
}
43 changes: 43 additions & 0 deletions examples/tests/valid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
bring cloud;

let bucket = new cloud.Bucket();
bucket.addObject("k", "value");

let put_and_list = ["put", "list"];

class Foo {
pub inflight mehtod() {
lift(bucket, put_and_list); // Qualify `bucket` with a preflight expression
lift(bucket, ["delete"]); // Qualify `bucket` with `delete` via literal
let b = bucket; // Assign `bucket` to an inflight variable

// `put` should work on `b` since we explicitly qualified `bucket` with `put`
// no error generated here because of use of `lift()` in this method
b.put("k2", "value2");

// validate `put` worked and that we can also `list`
assert(b.list() == ["k", "k2"]);

// Validate `delete` works
b.delete("k2");
assert(bucket.tryGet("k2") == nil);
}
}

let foo = new Foo();

test "explicit method lift qualification" {
foo.mehtod();
}

// Similar to the above test, but using a closure
let inflight_closure = inflight () => {
lift(bucket, ["put"]);
let b = bucket;
b.put("k3", "value3"); // Use inflight expression to access explicitly qualified `bucket`
assert(bucket.get("k3") == "value3");
};

test "explicit closure lift qualification" {
inflight_closure();
}
31 changes: 0 additions & 31 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,37 +304,6 @@ pub struct Stmt {
pub idx: usize,
}

#[derive(Debug)]
pub enum UtilityFunctions {
Log,
Assert,
UnsafeCast,
Nodeof,
}

impl UtilityFunctions {
/// Returns all utility functions.
pub fn all() -> Vec<UtilityFunctions> {
vec![
UtilityFunctions::Log,
UtilityFunctions::Assert,
UtilityFunctions::UnsafeCast,
UtilityFunctions::Nodeof,
]
}
}

impl Display for UtilityFunctions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UtilityFunctions::Log => write!(f, "log"),
UtilityFunctions::Assert => write!(f, "assert"),
UtilityFunctions::UnsafeCast => write!(f, "unsafeCast"),
UtilityFunctions::Nodeof => write!(f, "nodeof"),
}
}
}

#[derive(Debug)]
pub struct ElifBlock {
pub condition: Expr,
Expand Down
11 changes: 9 additions & 2 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,15 @@ impl<'a> JSifier<'a> {
for (method_name, method_qual) in lift_qualifications {
bind_method.open(format!("\"{method_name}\": [",));
for (code, method_lift_qual) in method_qual {
let ops_strings = method_lift_qual.ops.iter().map(|op| format!("\"{}\"", op)).join(", ");
bind_method.line(format!("[{code}, [{ops_strings}]],",));
let ops = method_lift_qual.ops.iter().join(", ");
// To keep the code concise treat no ops, single op and multiple ops differenly here, although the multiple ops is the generic case
if method_lift_qual.ops.len() == 0 {
bind_method.line(format!("[{code}, []],"));
} else if method_lift_qual.ops.len() == 1 {
bind_method.line(format!("[{code}, {ops}],"));
} else {
bind_method.line(format!("[{code}, [].concat({ops})],"));
}
}
bind_method.close("],");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"put": [
[this.b, ["list", "put"]],
[this.b, [].concat(["put"], ["list"])],
],
"$inflight_init": [
[this.b, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[b, ["list", "put"]],
[b, [].concat(["put"], ["list"])],
],
"$inflight_init": [
[b, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 7:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 7:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 6:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 6:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Bucket" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 6:6
Expression of type "Bucket" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 6:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 11:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 11:6
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/preflight_object.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[pf_obj, ["goodbye", "hello"]],
[pf_obj, [].concat(["hello"], ["goodbye"])],
],
"$inflight_init": [
[pf_obj, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[b, ["list", "put"]],
[b, [].concat(["list"], ["put"])],
],
"$inflight_init": [
[b, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source: libs/wingc/src/jsify/tests.rs
---
## Errors
Can't access preflight member "x" on inflight instance of type "A" 9:14
Expression of type "A" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 9:12
Expression of type "A" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 9:12
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class $Root extends $stdlib.std.Resource {
[this.s, ["length"]],
],
"bam": [
[this.b, ["get", "put"]],
[this.b, [].concat(["put"], ["get"])],
],
"$inflight_init": [
[this.b, []],
Expand Down
Loading
Loading