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): mutable lifted object may be modified inflight #6258

Merged
merged 14 commits into from
Apr 17, 2024

Conversation

yoav-steinberg
Copy link
Contributor

@yoav-steinberg yoav-steinberg commented Apr 17, 2024

fixes #3069
The type checker will now modify the type of preflight expressions that are being used inflight to a non-mutable version of the same type: MutArray -> Array, MutJson -> Json...

Note that since we change the type of the expression we don't get descriptive error telling us that we're trying to mutate a lifted mutable object, instead we just get the same error we'd expect if the lifted object was non-mutable to begin with. This isn't ideal but it makes the fix simpler. Let me know what you think...

Checklist

  • 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.

Copy link

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 Slack.

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 @chriscbr
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 @hasanaburayyan
Wing Playground @eladcon

@monadabot
Copy link
Contributor

monadabot commented Apr 17, 2024

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

Last Updated (UTC) 2024-04-17 20:38

@monadabot
Copy link
Contributor

monadabot commented Apr 17, 2024

Benchmarks

Comparison to Baseline ⬜⬜⬜⬜⬜⬜⬜⬜⬜🟥⬜⬜⬜
Benchmark Before After Change
version 58ms±0.52 59ms±0.82 +2ms (+2.81%)⬜
functions_1.test.w -t sim 400ms±13.56 395ms±7.22 -4ms (-1.12%)⬜
functions_1.test.w -t tf-aws 811ms±5.3 817ms±9.32 +5ms (+0.66%)⬜
jsii_big.test.w -t sim 2799ms±33.04 2796ms±29.02 -3ms (-0.11%)⬜
jsii_big.test.w -t tf-aws 3026ms±30.75 3021ms±20.83 -5ms (-0.16%)⬜
functions_10.test.w -t sim 487ms±9.47 482ms±6.42 -5ms (-0.99%)⬜
functions_10.test.w -t tf-aws 1992ms±12.49 1998ms±10.83 +6ms (+0.3%)⬜
jsii_small.test.w -t sim 381ms±10.38 363ms±4.74 -18ms (-4.64%)⬜
jsii_small.test.w -t tf-aws 600ms±8.42 600ms±4.72 0ms (-0.01%)⬜
empty.test.w -t sim 355ms±4.02 368ms±5.15 +13ms (+3.52%)🟥
empty.test.w -t tf-aws 594ms±5.13 593ms±5.22 -2ms (-0.29%)⬜
hello_world.test.w -t sim 393ms±4.64 395ms±2.8 +2ms (+0.47%)⬜
hello_world.test.w -t tf-aws 1505ms±11.3 1511ms±17.02 +6ms (+0.42%)⬜

⬜ 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 59ms 58ms 62ms 1ms 1ms
functions_1.test.w -t sim 395ms 385ms 416ms 7ms 10ms
functions_1.test.w -t tf-aws 817ms 802ms 845ms 9ms 13ms
jsii_big.test.w -t sim 2796ms 2757ms 2891ms 29ms 41ms
jsii_big.test.w -t tf-aws 3021ms 2982ms 3064ms 21ms 29ms
functions_10.test.w -t sim 482ms 462ms 491ms 6ms 9ms
functions_10.test.w -t tf-aws 1998ms 1973ms 2021ms 11ms 15ms
jsii_small.test.w -t sim 363ms 351ms 370ms 5ms 7ms
jsii_small.test.w -t tf-aws 600ms 587ms 610ms 5ms 7ms
empty.test.w -t sim 368ms 355ms 377ms 5ms 7ms
empty.test.w -t tf-aws 593ms 582ms 609ms 5ms 7ms
hello_world.test.w -t sim 395ms 387ms 400ms 3ms 4ms
hello_world.test.w -t tf-aws 1511ms 1490ms 1559ms 17ms 24ms
Last Updated (UTC) 2024-04-17 20:43

@yoav-steinberg yoav-steinberg marked this pull request as ready for review April 17, 2024 12:03
@yoav-steinberg yoav-steinberg requested a review from a team as a code owner April 17, 2024 12:03
@yoav-steinberg yoav-steinberg changed the title fix(compiler): mutable lifted object may be modifier inflight fix(compiler): mutable lifted object may be modified inflight Apr 17, 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 Apr 17, 2024
@yoav-steinberg yoav-steinberg added 🚧 pr/do-not-merge PRs with this label will not be automatically merged by mergify. and removed ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! labels Apr 17, 2024
@yoav-steinberg yoav-steinberg removed the 🚧 pr/do-not-merge PRs with this label will not be automatically merged by mergify. label Apr 17, 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 Apr 17, 2024
@yoav-steinberg yoav-steinberg removed the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Apr 17, 2024
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow that was easier than expected...

libs/wingc/src/type_check.rs Show resolved Hide resolved
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add to docs?

Copy link
Contributor

@Chriscbr Chriscbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slick 🚢

examples/tests/invalid/un_mut_lifted_objects.test.w Outdated Show resolved Hide resolved
@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 Apr 17, 2024
@yoav-steinberg yoav-steinberg removed the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Apr 17, 2024
Copy link
Contributor

mergify bot commented Apr 17, 2024

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

@mergify mergify bot merged commit d867af8 into main Apr 17, 2024
15 checks passed
@mergify mergify bot deleted the yoav/unmut_lifted_objects branch April 17, 2024 20:44
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.70.26.

mergify bot pushed a commit that referenced this pull request Apr 17, 2024
Reverts #6258

broke some winglibs due to strange behavior trying to call `copyMut` on immutable maps during inflight. See below

<img width="363" alt="image" src="https://github.com/winglang/wing/assets/45375125/89f09ba9-de35-4a01-b039-9330a2c3a586">
mergify bot pushed a commit that referenced this pull request Apr 24, 2024
…tempt (#6303)

Fixes #3069 (2nd attempt, instead of #6258)
The type checker will now modify the type of preflight expressions that are being used inflight to a non-mutable version of the same type: MutArray -> Array, MutJson -> Json...

Note that since we change the type of the expression we don't get descriptive error telling us that we're trying to mutate a lifted mutable object, instead we just get the same error we'd expect if the lifted object was non-mutable to begin with. This isn't ideal but it makes the fix simpler. Let me know what you think...

To get this working we needed to do the following changes to how we evaluate the phases of reference expressions:
* The phase of phase independent properties inflight will be inflight. This makes sure inflight calls like `preflight_array.copyMut()` will be we evaluated inflight so the side effect of turning the result to immutable won't happen. This is a major change to lifting because inflight calls like `preflight_array.at(0)` will also be executed inflight (no matter what args are passed to the call).
* There's a special exception to the above, struct fields: we need to keep them phase independent and not turn them to inflight when accessing them inflight. This is required so we can lift qualify methods called on struct field when those fields are preflight objects. This needs to be addressed in the future, because somehow struct fields being "phase independent" doesn't seem correct.
* Index access (`[i]`) phase calculation was wrong and didn't take into account the phase of the index. When the index phase is inflight the expression must be inflight too. When the index phase is independent and the instance is preflight we want to lift the entire expression and not only the instance so it can be correctly lift-qualified.
* All this means we don't need the (admittedly beautiful) logic of only turning phase independent method calls to inflight calls when they are passed some inflight state (instance or arg). Now we always treat them as inflight. This cleaned up some code.

## 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)
- [x] 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.

Prevent reassigning and mutating lifted objects
4 participants