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 broken edge case of async function lowering #390

Merged
merged 6 commits into from
Sep 18, 2020

Conversation

rtsao
Copy link
Contributor

@rtsao rtsao commented Sep 16, 2020

Fixes #388 and adds regression test.

Also adds an optimization to skip argument forwarding altogether when it is trivial to determine that all default parameter expressions cannot throw.

@@ -268,35 +268,26 @@ func (p *parser) lowerFunction(
*args = append(*args, js_ast.Arg{Binding: js_ast.Binding{Loc: arg.Binding.Loc, Data: &js_ast.BIdentifier{Ref: argRef}}})
}

// Forward all arguments from the outer function to the inner function
if p.fnOnlyDataVisit.argumentsRef != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem seems to be argumentsRef is non-nil if the async arrow function is nested inside a regular function. This code path only triggers when the parameters have a default value (and hence couldThrowErrors = true), so both conditions are required for this bug.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the summary.

@@ -8987,7 +8987,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
p.pushScopeForVisitPass(js_ast.ScopeFunctionBody, e.Body.Loc)
e.Body.Stmts = p.visitStmtsAndPrependTempRefs(e.Body.Stmts)
p.popScope()
p.lowerFunction(&e.IsAsync, &e.Args, e.Body.Loc, &e.Body.Stmts, &e.PreferExpr, &e.HasRestArg)
p.lowerFunction(&e.IsAsync, &e.Args, e.Body.Loc, &e.Body.Stmts, &e.PreferExpr, &e.HasRestArg, true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose adding something like &e.isArrow might be more readable. But I figured omitting such a property on Fn was intentional?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, that's correct. This change sounds good.

{
'in.js': `
function nonArrowWrapper() {
return async (x, paramWithDefault = {}) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some expressions can be statically determined to never throw. Of these, it might be worth handling the subset that are both fairly common and trivial to identify (e.g. false, true, etc.)

Copy link
Owner

@evanw evanw left a comment

Choose a reason for hiding this comment

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

This looks good. Thanks for fixing this.

@evanw evanw merged commit b4d808f into evanw:master Sep 18, 2020
evanw added a commit that referenced this pull request Sep 19, 2020
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.

Incorrect async transform
2 participants