Skip to content

Rust Engine: first resugaring#1528

Closed
W95Psp wants to merge 17 commits intorengine-visitorsfrom
rengine-first-resugaring
Closed

Rust Engine: first resugaring#1528
W95Psp wants to merge 17 commits intorengine-visitorsfrom
rengine-first-resugaring

Conversation

@W95Psp
Copy link
Copy Markdown
Contributor

@W95Psp W95Psp commented Jun 25, 2025

Description

This PR introduces improvements and extensions to the AST representation and printer logic in the Rust engine, particularly focusing on resugaring types.

Key Changes:

  • AST Structure:

    • Modified Ty to publicly expose its inner box (pub Box<TyKind>).
    • Renamed ResugaredTyKind to ResugaredItemKind within ItemKind::Resugared to match the intended semantics.
    • Added a new variant Unit to ResugaredTyKind representing the unit type (()), differentiating it clearly from the empty tuple.
  • Resugaring Infrastructure:

    • Defined a generic ResugaredFragment trait and implemented From conversions between resugared types and their parent fragments.
    • Introduced a macro (derive_from!) to simplify repetitive trait implementations for AST fragment conversions.
  • Visitor Corrections:

    • Updated visitor methods (visit_resugared_ty_kindvisit_resugared_item_kind) to reflect the accurate naming for ItemKind::Resugared variants, resolving naming inconsistencies across visitor implementations.
  • Printer Enhancements:

    • Implemented Resugar and Printer traits for AST printing with resugaring phases.
    • Added a generic Print trait with automatic implementations via macro (derive_print!) to simplify printing logic for various AST fragments.
    • Introduced a common resugaring phase (UnitTyResugar) to convert zero-arity tuples into the dedicated unit type for clearer printing.
  • Allocator Improvements:

    • Enhanced the Allocator constructor with a new convenience method (new) for improved ergonomics.

Testing:

  • Added unit tests for the new UnitTyResugar phase to ensure correct resugaring behavior.

Fixes #1580.

@W95Psp W95Psp changed the base branch from main to rengine-visitors June 25, 2025 14:35
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch from c47fcbe to 8bff6c0 Compare June 25, 2025 15:10
@W95Psp W95Psp marked this pull request as ready for review June 25, 2025 15:12
@W95Psp W95Psp requested a review from a team as a code owner June 25, 2025 15:12
@W95Psp W95Psp requested review from a team, clementblaudeau, jschneider-bensch and maximebuyse and removed request for a team and maximebuyse June 25, 2025 15:12
@W95Psp W95Psp force-pushed the rengine-visitors branch from 3ebe0a7 to 14e243c Compare June 30, 2025 06:44
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch 2 times, most recently from cba9946 to bf655e9 Compare June 30, 2025 06:48
@W95Psp W95Psp force-pushed the rengine-visitors branch from 14e243c to bd0b1be Compare June 30, 2025 07:06
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch from 1f3d8b8 to fbbea41 Compare June 30, 2025 07:06
@W95Psp
Copy link
Copy Markdown
Contributor Author

W95Psp commented Jun 30, 2025

I just rebased on the base branch (this PR is part of a long PR chain)

Copy link
Copy Markdown
Contributor

@jschneider-bensch jschneider-bensch 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!
I just have some suggestions for documentation.

pub enum ResugaredTyKind {
/// The `unit` type.
///
/// Without resugaring: a tuple of arity zero.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it make sense to document here which backend is expected to use a specific resugared node?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not really part of this PR's scope, but I think it would be nice to reference codegen/README.md for details on how this module was generated in documentation at the top of this file.

@@ -1465,7 +1465,7 @@ pub enum ItemKind {
/// A resugared item.
/// This variant is introduced before printing only.
/// Phases must not produce this variant.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Phases must not produce this variant.
/// Backend phases must not produce this variant.

In the Printer documentation you also refer to "resugaring phases", so here it should be a bit more specific, no?

fn print(self, fragment: &mut T) -> Option<(String, SourceMap)>;
}

macro_rules! derive_print {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you document what this macro does and if there is anything to know about how it implements Print? (e.g. I don't know what doc.render(80, ...) means and whether you'd want to know that this number means when you use the macro.

Copy link
Copy Markdown
Contributor

@clementblaudeau clementblaudeau 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

#[derive_group_for_ast]
#[visitable]
pub enum ResugaredTyKind {}
pub enum ResugaredTyKind {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe also add the empty tuple as a unit expression ? It feels like only having the type will not be usable by backends

@W95Psp W95Psp force-pushed the rengine-visitors branch from bd0b1be to 97362d0 Compare July 3, 2025 11:27
@W95Psp W95Psp force-pushed the rengine-visitors branch 3 times, most recently from 7cb712b to 7217c07 Compare July 24, 2025 09:46
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch 4 times, most recently from ad80607 to d1a2f0f Compare July 24, 2025 10:08
@W95Psp W95Psp force-pushed the rengine-visitors branch from 69c0641 to 59da82a Compare July 24, 2025 12:11
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch from d1a2f0f to 2bc7064 Compare July 24, 2025 12:13
@W95Psp W95Psp force-pushed the rengine-visitors branch from 59da82a to 951050a Compare July 24, 2025 12:13
W95Psp and others added 6 commits July 24, 2025 14:13
Co-authored-by: Jonas Schneider-Bensch <124457079+jschneider-bensch@users.noreply.github.com>
Co-authored-by: Jonas Schneider-Bensch <124457079+jschneider-bensch@users.noreply.github.com>
@W95Psp W95Psp force-pushed the rengine-first-resugaring branch from 2bc7064 to aaa8388 Compare July 24, 2025 12:14
@W95Psp W95Psp force-pushed the rengine-visitors branch 2 times, most recently from 24d88b5 to 0acc923 Compare July 24, 2025 12:22
@W95Psp W95Psp linked an issue Jul 24, 2025 that may be closed by this pull request
@W95Psp
Copy link
Copy Markdown
Contributor Author

W95Psp commented Aug 11, 2025

Closing in favor of #1528

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.

Rust Engine: add first resugaring

3 participants