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

await AST node for expressions #7368

Merged
merged 5 commits into from
Apr 4, 2025
Merged

await AST node for expressions #7368

merged 5 commits into from
Apr 4, 2025

Conversation

cristianoc
Copy link
Collaborator

No description provided.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: de28573 Previous: 3f06468 Ratio
Parse RedBlackTree.res - time/run 1.84985202 ms 1.3461563799999998 ms 1.37
Print RedBlackTree.res - time/run 3.0524644133333334 ms 1.8942094533333333 ms 1.61
Print RedBlackTreeNoComments.res - time/run 2.9472621 ms 1.7506641333333335 ms 1.68
Parse Napkinscript.res - time/run 65.18005879333333 ms 42.33869618 ms 1.54
Print Napkinscript.res - time/run 110.03848714 ms 57.47105978 ms 1.91
Parse HeroGraphic.res - time/run 8.063686846666666 ms 5.736426893333333 ms 1.41
Print HeroGraphic.res - time/run 14.140058206666666 ms 7.840572506666666 ms 1.80

This comment was automatically generated by workflow using github-action-benchmark.

@@ -29,11 +29,11 @@ user.data = await fetch()

<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>

let inBinaryExpression = await x->Js.Promise.resolve + 1
let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve
let inBinaryExpression = (await x->Js.Promise.resolve) + 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@nojaf before looking into why printing has changed here, any comments on what the ideal printer should look like, by analogy to the rest of the printer
cc @cknitt

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

here in this file in general

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I'm not entirely sure about this. I believe that no parentheses are needed if the top-level await expression is followed by a single expression (top-level from the expression point of view):

await x

However, if it is nested within another expression, it makes sense to use parentheses:

let x = foo((await bar), y, z)
let a = (await b) - (await c)

I think the changes in this file are reasonable so far. However, I'm no expert on this, so I would like to ask for input from others as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looks like the formatting for a simple binary operation is (await 1) + (await 2), so this example looks odd.
I think what's going on before this PR is that by adding await as an attribute, each sub-expressions is processed not as an await expression, but as a -> expression, as the checks go straight through the ast.
In general (await e1) + (await e2) should probably not look inside e1 and e2 for deciding how to parenthesise. So it seems the formatting before this PR might not have been intentional.

Copy link
Member

Choose a reason for hiding this comment

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

However, if it is nested within another expression, it makes sense to use parentheses:

Agreed, this makes things clearer. The formatting changes are fine with me.

@@ -88,7 +88,7 @@ let _ = await (assert(x))
let _ = await promises[0]
let _ = await promises["resolved"]
let _ = await promises["resolved"] = sideEffect()
let _ = @attr await (expr)
let _ = @outer await (@inner expr)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Update test to showcase the difference between attributes outside and inside the await expression.
This distinction was not possible before adding an ast node for await.

@cristianoc cristianoc changed the title wip: await AST await AST node for expressions Apr 3, 2025
@cristianoc cristianoc requested a review from cknitt April 3, 2025 09:57
@@ -9,7 +9,7 @@ done

for file in ppx/*.res; do
output="src/expected/$(basename $file).jsout"
../../cli/bsc -ppx "../../_build/install/default/bin/rescript-tools ppx" $file > $output
node ../../cli/bsc.js -ppx "../../_build/install/default/bin/rescript-tools ppx" $file > $output
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cometkim is this the way to call bsc now?

Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

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

Nice work!

@@ -29,11 +29,11 @@ user.data = await fetch()

<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>

let inBinaryExpression = await x->Js.Promise.resolve + 1
let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve
let inBinaryExpression = (await x->Js.Promise.resolve) + 1
Copy link
Member

Choose a reason for hiding this comment

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

However, if it is nested within another expression, it makes sense to use parentheses:

Agreed, this makes things clearer. The formatting changes are fine with me.

@cristianoc cristianoc enabled auto-merge (squash) April 4, 2025 06:51
@cristianoc cristianoc merged commit 712dec7 into master Apr 4, 2025
20 checks passed
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.

3 participants