Skip to content

Editorial: FunctionDeclarationInstantiation cannot return ReturnCompletion #3607

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

Merged
merged 1 commit into from
Jun 27, 2025

Conversation

bakkot
Copy link
Contributor

@bakkot bakkot commented May 15, 2025

This is because the only way to get a return completion in expression position is with a yield, and yield is statically forbidden in generator formal parameters.

I suspect esmeta will complain about this, because FunctionDeclarationInstantiation calls IteratorBindingInitialization to do the initialization, and that AO is also used for let [foo] = bar declarations, which can contain yield expressions and therefore can produce return completions. I didn't think it was worth adding an assert in FunctionDeclarationInstantiation which says that IteratorBindingInitialization cannot produce a return completion in this specific case, but I'm open to doing so if we can do it without making the algorithm too awkward.

This imprecision is exposed by #3606 failing esmeta's typechecks. It should pass if rebased on this (I think).

@michaelficarra
Copy link
Member

I didn't think it was worth adding an assert in FunctionDeclarationInstantiation which says that IteratorBindingInitialization cannot produce a return completion in this specific case, but I'm open to doing so if we can do it without making the algorithm too awkward.

I think an assert would be helpful here because it would explain why return completions can't be returned (the guaranteed absence of yield).

@bakkot
Copy link
Contributor Author

bakkot commented Jun 26, 2025

Suggest a wording? The main trouble is that we're using Perform ? ... and I don't want to switch that to a Let _completion_ be Completion(...) just so we can write the assert. I guess we could do "The previous step cannot return a ReturnCompletion" or something but that's kind of awkward. Also there's two calls, which would each need such an assert.

@michaelficarra
Copy link
Member

The invocations of IteratorBindingInitialization only differ in the second parameter, so you can deduplicate them by creating an alias for that parameter. Then, yeah, "the following step cannot return a ReturnCompletion because ..." (I prefer notes before a step to notes after a step).

@michaelficarra
Copy link
Member

Also, an xref to the early error (sec-generator-function-definitions-static-semantics-early-errors) would be great.

@bakkot
Copy link
Contributor Author

bakkot commented Jun 26, 2025

Done:

  1. Assert: The following step cannot return a ReturnCompletion because the only way such a completion can arise in expression position is by use of |YieldExpression|, which is forbidden in parameter lists by Early Error rules in <emu-xref href="#sec-generator-function-definitions-static-semantics-early-errors"></emu-xref> and <emu-xref href="#sec-async-generator-function-definitions-static-semantics-early-errors"></emu-xref>.

@bakkot
Copy link
Contributor Author

bakkot commented Jun 26, 2025

In EvalDeclarationInstantiation we have very similar steps, but as Note rather than Assert:

NOTE: The following invocation cannot return an abrupt completion because of the validation preceding step 14.

I guess either's technically fine, but Note is probably more appropriate since it's prose-y rather than a present-tense statement about some value. Switched it over.

@michaelficarra michaelficarra added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Jun 26, 2025
@ljharb ljharb force-pushed the fdi-not-return-completion branch from 512d881 to 48cab84 Compare June 27, 2025 06:19
@ljharb ljharb merged commit 48cab84 into main Jun 27, 2025
8 checks passed
@ljharb ljharb deleted the fdi-not-return-completion branch June 27, 2025 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants