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

feat: trait aliases #6431

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

feat: trait aliases #6431

wants to merge 14 commits into from

Conversation

michaeljklein
Copy link
Contributor

@michaeljklein michaeljklein commented Nov 1, 2024

Description

Problem*

Resolves #6315

Summary*

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

michaeljklein and others added 6 commits November 1, 2024 14:54
…mpl's for trait aliases, wip implementing impl generator
…to-many case, modify parse_item_kind to return multiple ItemKind's, generate a trait impl from the trait parser (when the trait alias is parsed), test generated impl's, test several cases in unit tests
…issue with where clauses, add docs, cargo clippy/fmt
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 6, 2024
Copy link
Contributor

github-actions bot commented Nov 6, 2024

@michaeljklein michaeljklein marked this pull request as ready for review November 6, 2024 20:26
@michaeljklein michaeljklein requested a review from a team November 7, 2024 02:48
NoirTrait {
// Trait aliases:
// trait Foo<..> = A + B + E where ..;
let (bounds, where_clause, items, is_alias) = if self.eat_assign() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: instead of desugaring a trait alias in the parser itself, we could introduce a new AST node for it (maybe TraitAlias) and desugar it later on, maybe in the Elaborator or collector. That way:

  • parsing is a bit simpler
  • desugaring can be tested independently
  • LSP or tools working with parsed code (like the formatter) don't need to know about how trait aliases are expanded, knowing that they need to skip the synthetic impl, and can focus on better handling this new node

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, and it could be good to add a formatter test for this new syntax, to see if it works out of the box.

Copy link
Contributor Author

@michaeljklein michaeljklein Nov 7, 2024

Choose a reason for hiding this comment

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

I originally attempted to implement this by desugaring it in the Elaborator (based on the is_alias flag), but it was fairly complex to deal with post-parsing types.

  • Testing this approach seems a bit simpler since we can check ~equality up to spans between the parsed trait impl and the expected desugaring.

I suppose adding an is_synthetic: bool to NoirTraitImpl would be easy if that helps handling it in the LSP?

Sure, I'll add a formatter test for this 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asterite I've added is_synthetic: bool to NoirTraitImpl and formatter support for trait aliases, with tests for both.

@michaeljklein michaeljklein requested review from asterite and a team November 13, 2024 19:56
Copy link
Member

@TomAFrench TomAFrench left a comment

Choose a reason for hiding this comment

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

Looks good to me. We've got a number of desugarings happening in the parser atm so I don't think it's blocking to have this here. We can always refactor this to how @asterite mentioned in a later PR.

@@ -106,9 +109,9 @@ impl<'a> Parser<'a> {
/// | TypeAlias
/// | Function
/// )
fn parse_item_kind(&mut self) -> Option<ItemKind> {
fn parse_item_kind(&mut self) -> Vec<ItemKind> {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a quick bit of signposting here that we expect this to return a vec of length 1 in 90% of cases and it's specifically for the case of traits that we may end up with both the trait definition and an impl?

compiler/noirc_frontend/src/parser/parser/parse_many.rs Outdated Show resolved Hide resolved
Copy link
Contributor

FYI @noir-lang/developerrelations on Noir doc changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trait aliases
4 participants