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): cannot bring modules more than once #3605

Merged
merged 39 commits into from
Jul 27, 2023

Conversation

Chriscbr
Copy link
Contributor

@Chriscbr Chriscbr commented Jul 25, 2023

Refactors how the Wing compiler handles bring for local Wing files in order to support more complex bring use cases, like allowing multiple files to bring the same module.

The high level implementation approach is instead of generating a single, monolithic AST, we augment the parser so that after a Wing source file is parsed, an isolated AST and a list of dependent Wing files is produced. Using this, we can write a function that parses an entire Wing program given an entrypoint, and use the petgraph crate to produce topological ordering of the files for type checking, and each AST is independently type checked.

While this unblocks several use cases, there are still some limitations. For example, consider how it's possible for classes to recursively reference each other in TypeScript:

// foo.ts
import * as bar from "./bar";

export class Foo {
  constructor(public bar: bar.Bar | undefined) {}
}

new Foo(new bar.Bar(new Foo(undefined)));


// bar.ts
import * as foo from "./foo";

export class Bar {
  constructor(public foo: foo.Foo | undefined) {}
}

This doesn't work in Wing currently because each file is still type checked independently. To relax this constraint, we would need to perform symbol resolution on all files first (to identify all available types and members) before performing type checking. This kind of refactoring would be pretty large and is out of scope for this PR.

Fixes #3511
Fixes #3578
Fixes #3509

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.

@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 Jul 25, 2023
libs/wingc/src/files.rs Show resolved Hide resolved
libs/wingc/src/jsify.rs Show resolved Hide resolved
libs/wingc/src/jsify.rs Show resolved Hide resolved
libs/wingc/src/lsp/sync.rs Show resolved Hide resolved
Copy link
Contributor

@yoav-steinberg yoav-steinberg left a comment

Choose a reason for hiding this comment

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

Amazing!

libs/wingc/src/parser.rs Outdated Show resolved Hide resolved
libs/wingc/src/parser.rs Show resolved Hide resolved
libs/wingc/src/parser.rs Show resolved Hide resolved
libs/wingc/src/parser.rs Outdated Show resolved Hide resolved
libs/wingc/src/type_check.rs Outdated Show resolved Hide resolved
libs/wingc/src/type_check.rs Outdated Show resolved Hide resolved
libs/wingc/src/type_check.rs 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 Jul 26, 2023
@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 Jul 26, 2023
@Chriscbr
Copy link
Contributor Author

Chriscbr commented Jul 27, 2023

I was wondering if maybe entrypoint files should have the .main.w suffix (and also main.w).

Thoughts?

+1 from me. I think a convention based on the file name seems pretty good as far as options go. Since most source files in larger Wing projects will be non-entrypoint files, I think any extra rules or annotations required from the user should be added to the entrypoint files, not the non-entrypoint files - if that makes any sense.

@mergify
Copy link
Contributor

mergify bot commented Jul 27, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 1b24ec0 into main Jul 27, 2023
11 checks passed
@mergify mergify bot deleted the rybickic/bring-refactor branch July 27, 2023 19:11
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.24.53.

@skyrpex
Copy link
Contributor

skyrpex commented Jul 28, 2023

Hey, this PR caused an issue in the LSP. We had to revert Learn and Play to a previous version.

See the Slack convo https://winglang.slack.com/archives/C04QQD2SDM2/p1690534520811589.

We'll have to revert this PR or fix whatever problem there is.

@ShaiBer
Copy link
Contributor

ShaiBer commented Jul 28, 2023

Let's revert for now so newer releases will not break the playground again

@ShaiBer ShaiBer restored the rybickic/bring-refactor branch July 28, 2023 09:48
@skyrpex
Copy link
Contributor

skyrpex commented Jul 28, 2023

Here's the revert PR #3641

@skyrpex skyrpex deleted the rybickic/bring-refactor branch July 28, 2023 09:53
mergify bot pushed a commit that referenced this pull request Jul 28, 2023
The PR #3605 caused an issue with the LSP.
mergify bot pushed a commit that referenced this pull request Aug 7, 2023
Re-roll of the changes in #3605, but with more care taken to support the language server. The previous version of this PR  introduced some bugs where the LSP was using the text from disk instead of the text in the user's editor, resulting in invalid text completions and crashes. To fix this, I had to rethink the original multi-file parsing solution so that we store the latest file text from the LSP in memory.

----

Refactors how the Wing compiler handles `bring` for local Wing files in order to support more complex `bring` use cases, like allowing multiple files to `bring` the same module.

The high level implementation approach is instead of generating a single, monolithic AST, we augment the parser so that after a Wing source file is parsed, an isolated AST and a list of dependent Wing files is produced. Using this, we can write a function that parses an entire Wing program given an entrypoint, and use the `petgraph` crate to produce topological ordering of the files for type checking, and each AST is independently type checked.

Fixes #3511
Fixes #3578
Fixes #3509

## 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
7 participants