-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): lifting globals from inflight classes defined in prefl…
…ight fails (#5559) Fixes #2730 The wing compiler always generated dummy preflight classes for inflight classes defined preflight. This means such inflight classes have a lift map and therefore already include all the lift qualification information needed when their mehtods are used inflight. The problem was that our lift visitor ignored references to such classes because it looks for **preflight** expressions to lift, but references to inflight classes aren't preflight expressions. This PR finds such references and creates a special lift with correct qualifications using dummy singleton preflight object representing the relevant inflight class. To get this working the following changes where made: * Each inflight class **defined in preflight** gets a unique id * The generated JS code for each such class stores a map from these unique ids to the generated js preflight class of that inflight class. * Each generated preflight file (wing module) has this map, and when it's imported by another module its map is merged into the importer's map. * The root construct contains a global map from all the unique ids to all the inflight types defined preflight. To achieve this I had to make the imported js files local to the construct tree, so they are now imported in the root object's (entry point object) ctor and not globally. * The base `Resource` class has a satatic `_singleton()` method * The lift visitor, when encountering a reference to an inflight class instance (defined preflight) will qualify the lift in a similar manner to preflight class instances, but it'll use the `_singleton()` method to get a preflight expression representing that object. - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
1 parent
d6d8969
commit fb6404e
Showing
275 changed files
with
2,670 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 50 additions & 14 deletions
64
examples/tests/valid/inflight_class_capture_preflight_object.test.w
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,54 @@ | ||
// https://github.com/winglang/wing/issues/2730 | ||
// FAILING: | ||
bring cloud; | ||
bring "./subdir2" as subdir; | ||
|
||
// bring cloud; | ||
let b = new cloud.Bucket(); | ||
|
||
// let b = new cloud.Bucket(); | ||
// let myConst = "bang bang"; | ||
inflight class Foo { | ||
pub uploadToBucket(k: str, value: str) { | ||
b.put(k, value); | ||
assert(b.get(k) == value); | ||
} | ||
|
||
// inflight class Foo { | ||
// uploadToBucket(k: str, value: str) { | ||
// b.put(k, value); | ||
// } | ||
// } | ||
static pub fooStatic() { | ||
b.put("a", "b"); | ||
assert(b.list() == ["a"]); | ||
} | ||
} | ||
|
||
// test "inflight class captures preflight resource" { | ||
// let f = new Foo(); | ||
// f.uploadToBucket("hello.txt", "world"); | ||
// } | ||
test "inflight class captures preflight resource" { | ||
let f = new Foo(); | ||
f.uploadToBucket("hello.txt", "world"); | ||
} | ||
|
||
test "inflight class type captures preflight resource" { | ||
Foo.fooStatic(); | ||
} | ||
|
||
|
||
let getFoo = inflight () => { | ||
return new Foo(); | ||
}; | ||
|
||
test "inflight class qualified without explicit reference" { | ||
// Get instance of Foo without mentioning the type | ||
let foo = getFoo(); | ||
// Now Foo needs to be qualified correcly | ||
foo.uploadToBucket("greetings.txt", "universe"); | ||
} | ||
|
||
test "inflight class defined inflight captures preflight object" { | ||
class Foo2 { | ||
pub uploadToBucket() { | ||
b.put("x", "y"); | ||
assert(b.get("x") == "y"); | ||
} | ||
} | ||
|
||
let f = new Foo2(); | ||
f.uploadToBucket(); | ||
} | ||
|
||
test "bring inflight class from subdir" { | ||
let x = new subdir.InflightClass(); | ||
assert(x.method() == "What did you expect?"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub inflight class InflightClass { | ||
pub method(): str { | ||
return "What did you expect?"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.