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): lifting globals from inflight classes defined in preflight fails #5559

Merged
merged 102 commits into from
Jun 27, 2024

Conversation

yoav-steinberg
Copy link
Contributor

@yoav-steinberg yoav-steinberg commented Jan 28, 2024

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.
  • Title matches Winglang's style guide
  • Description explains motivation and solution
  • 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.

@yoav-steinberg yoav-steinberg changed the title fix(compiler): we fail to detect errors qualifying inflight closure lifts fix(compiler): ail to detect errors qualifying inflight closure lifts Jan 28, 2024
@monadabot
Copy link
Contributor

monadabot commented Jan 28, 2024

Thanks for opening this pull request! 🎉
Please consult the contributing guidelines for details on how to contribute to this project.
If you need any assistence, don't hesitate to ping the relevant owner over Discord.

Topic Owner
Wing SDK and utility APIs @chriscbr
Wing Console @ainvoner, @skyrpex, @polamoros
JSON, structs, primitives and collections @hasanaburayyan
Platforms and plugins @hasanaburayyan
Frontend resources (website, react, etc) @tsuf239
Language design @eladb
VSCode extension and language server @markmcculloh
Compiler architecture, inflights, lifting @yoav-steinberg
Wing Testing Framework @tsuf239
Wing CLI @markmcculloh
Build system, dev environment, releases @markmcculloh
Library Ecosystem @chriscbr
Documentation @hasanaburayyan
SDK test suite @tsuf239
Examples @skorfmann
Wing Playground @eladcon

@monadabot
Copy link
Contributor

monadabot commented Jan 28, 2024

Console preview environment is available at https://wing-console-pr-5559.fly.dev 🚀

Last Updated (UTC) 2024-06-27 11:59

@monadabot
Copy link
Contributor

monadabot commented Jan 28, 2024

Benchmarks

Comparison to Baseline ⬜🟥⬜⬜⬜🟥⬜⬜⬜⬜⬜⬜⬜
Benchmark Before After Change
version 57ms±0.74 56ms±0.56 0ms (-0.88%)⬜
jsii_small.test.w -t sim 373ms±5.68 393ms±17.51 +20ms (+5.27%)🟥
jsii_small.test.w -t tf-aws 609ms±5.87 612ms±5.05 +4ms (+0.58%)⬜
jsii_big.test.w -t sim 2972ms±19.2 2960ms±18.56 -12ms (-0.41%)⬜
jsii_big.test.w -t tf-aws 3171ms±18.96 3189ms±15.84 +18ms (+0.58%)⬜
functions_10.test.w -t sim 487ms±13.16 511ms±12.09 +24ms (+4.98%)🟥
functions_10.test.w -t tf-aws 2080ms±16.26 2136ms±33.78 +56ms (+2.71%)⬜
hello_world.test.w -t sim 404ms±3.92 404ms±4.5 0ms (-0.12%)⬜
hello_world.test.w -t tf-aws 1459ms±7.78 1463ms±9.24 +4ms (+0.28%)⬜
empty.test.w -t sim 360ms±5.39 368ms±6.14 +8ms (+2.34%)⬜
empty.test.w -t tf-aws 590ms±6.48 593ms±4.38 +3ms (+0.5%)⬜
functions_1.test.w -t sim 402ms±3.41 398ms±5.55 -3ms (-0.75%)⬜
functions_1.test.w -t tf-aws 829ms±3.96 837ms±7.98 +7ms (+0.9%)⬜

⬜ Within 1.5 standard deviations
🟩 Faster, Above 1.5 standard deviations
🟥 Slower, Above 1.5 standard deviations

Benchmarks may vary outside of normal expectations, especially when running in GitHub Actions CI.

Results
name mean min max moe sd
version 56ms 55ms 57ms 1ms 1ms
jsii_small.test.w -t sim 393ms 375ms 442ms 18ms 24ms
jsii_small.test.w -t tf-aws 612ms 597ms 622ms 5ms 7ms
jsii_big.test.w -t sim 2960ms 2926ms 3002ms 19ms 26ms
jsii_big.test.w -t tf-aws 3189ms 3161ms 3239ms 16ms 22ms
functions_10.test.w -t sim 511ms 474ms 529ms 12ms 17ms
functions_10.test.w -t tf-aws 2136ms 2060ms 2196ms 34ms 47ms
hello_world.test.w -t sim 404ms 393ms 411ms 5ms 6ms
hello_world.test.w -t tf-aws 1463ms 1447ms 1489ms 9ms 13ms
empty.test.w -t sim 368ms 355ms 385ms 6ms 9ms
empty.test.w -t tf-aws 593ms 586ms 602ms 4ms 6ms
functions_1.test.w -t sim 398ms 387ms 410ms 6ms 8ms
functions_1.test.w -t tf-aws 837ms 818ms 858ms 8ms 11ms
Last Updated (UTC) 2024-06-27 12:05

@monadabot monadabot added the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Jan 28, 2024
@yoav-steinberg yoav-steinberg changed the title fix(compiler): ail to detect errors qualifying inflight closure lifts fix(compiler): fail to detect errors qualifying inflight closure lifts Jan 28, 2024
@yoav-steinberg yoav-steinberg linked an issue Jan 28, 2024 that may be closed by this pull request
Because the type system can't distinguish between a closure class (which needs to be lifted) and a regular function type...
Also lift on reference and not all call, by assuming a ref to a closure will actually call it later on, we can correctly lift (and setup permissions) for closures stored in collections (for example). This is a special exception to our qualification rules, since closures only have a single method (`handle`) so we can implicitly qualify it.
Signed-off-by: monada-bot[bot] <monabot@monada.co>
Signed-off-by: monada-bot[bot] <monabot@monada.co>
Signed-off-by: monada-bot[bot] <monabot@monada.co>
libs/wingsdk/src/helpers.ts Outdated Show resolved Hide resolved
libs/wingsdk/src/helpers.ts Outdated Show resolved Hide resolved
tools/hangar/__snapshots__/invalid.ts.snap Show resolved Hide resolved
wt Outdated Show resolved Hide resolved
@Chriscbr Chriscbr removed the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Jun 26, 2024
@monadabot monadabot added the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Jun 27, 2024
@MarkMcCulloh MarkMcCulloh removed the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Jun 27, 2024
Copy link
Contributor

mergify bot commented Jun 27, 2024

Thanks for contributing, @yoav-steinberg! This PR will now be added to the merge queue, or immediately merged if yoav/qualify_err_closure_lift is up-to-date with main and the queue is empty.

@mergify mergify bot merged commit fb6404e into main Jun 27, 2024
15 checks passed
@mergify mergify bot deleted the yoav/qualify_err_closure_lift branch June 27, 2024 12:36
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.75.15.

mergify bot pushed a commit that referenced this pull request Jun 27, 2024
mergify bot pushed a commit that referenced this pull request Jun 29, 2024
…ight fails, take 2 (#6801)

This is a resubmit of #5559 with the following fix:
The `bringJs` helper function merges the brought module's type map into the bringing module's type map. During this merge there's a safety check to verify we don't overwrite one type over another if they have the same uids (should never happen). In reality there are cases where the same type in brought twice (two different files include the same .w file). I now detect these cases and ignore them in the safety check.
Also added a test for this.

## Checklist

- [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)*.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to capture preflight object from within an inflight class
4 participants