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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_lower.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default [
}
},
function() {
return (_0) => __async(this, arguments, function* (bar) {
return (_0, ..._1) => __async(this, [_0, ..._1], function* (bar) {
yield bar;
return [this, arguments];
});
Expand Down
4 changes: 2 additions & 2 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

p.popScope()

if p.MangleSyntax && len(e.Body.Stmts) == 1 {
Expand Down Expand Up @@ -9133,7 +9133,7 @@ func (p *parser) visitFn(fn *js_ast.Fn, scopeLoc logger.Loc) {
p.pushScopeForVisitPass(js_ast.ScopeFunctionBody, fn.Body.Loc)
fn.Body.Stmts = p.visitStmtsAndPrependTempRefs(fn.Body.Stmts)
p.popScope()
p.lowerFunction(&fn.IsAsync, &fn.Args, fn.Body.Loc, &fn.Body.Stmts, nil, &fn.HasRestArg)
p.lowerFunction(&fn.IsAsync, &fn.Args, fn.Body.Loc, &fn.Body.Stmts, nil, &fn.HasRestArg, false)
p.popScope()

p.fnOrArrowDataVisit = oldFnOrArrowData
Expand Down
3 changes: 2 additions & 1 deletion internal/js_parser/js_parser_lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (p *parser) lowerFunction(
bodyStmts *[]js_ast.Stmt,
preferExpr *bool,
hasRestArg *bool,
isArrow bool,
) {
// Lower object rest binding patterns in function arguments
if p.UnsupportedFeatures.Has(compat.ObjectRestSpread) {
Expand Down Expand Up @@ -269,7 +270,7 @@ func (p *parser) lowerFunction(
}

// 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.

if !isArrow {
// Normal functions can just use "arguments" to forward everything
forwardedArgs = js_ast.Expr{Loc: bodyLoc, Data: &js_ast.EIdentifier{Ref: *p.fnOnlyDataVisit.argumentsRef}}
} else {
Expand Down
15 changes: 15 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,20 @@ func TestLowerLogicalAssign(t *testing.T) {
expectPrintedTarget(t, 2020, "a()[b()] ||= c", "var _a, _b;\n(_a = a())[_b = b()] || (_a[_b] = c);\n")
}

func TestLowerAsyncFunctions(t *testing.T) {
// Lowered non-arrow functions with argument evaluations should merely use
// "arguments" rather than allocating a new array when forwarding arguments
expectPrintedTarget(t, 2015, "async function foo(a, b = couldThrowErrors()) {console.log(a, b);}", `function foo(_0) {
return __async(this, arguments, function* (a, b = couldThrowErrors()) {
console.log(a, b);
});
}
import {
__async
} from "<runtime>";
`)
}

func TestLowerClassSideEffectOrder(t *testing.T) {
// The order of computed property side effects must not change
expectPrintedTarget(t, 2015, `class Foo {
Expand Down Expand Up @@ -2707,6 +2721,7 @@ func TestLowerClassInstance(t *testing.T) {
expectPrintedTarget(t, 2015, "class Foo extends Bar {}", `class Foo extends Bar {
}
`)

expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} constructor() { super() } }", `class Foo extends Bar {
bar() {
}
Expand Down
14 changes: 14 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,20 @@ in.js:24:30: warning: Writing to getter-only property "#getter" will throw
)
`,
}, { async: true }),
test(['in.js', '--outfile=node.js', '--target=es6'],
{
'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.)

if (x !== 123) {
throw 'fail';
}
console.log(paramWithDefault);
};
}
exports.async = () => nonArrowWrapper()(123);
`,
}, { async: true }),
test(['in.js', '--outfile=node.js', '--target=es6'], {
'in.js': `
exports.async = async () => {
Expand Down