Fix two classes of "unreachable code" warnings in Firefox.#1071
Merged
Conversation
When GopherJS flattens a loop into a while-switch-case construct, it
needs to generate an instruction to go back to the beginning of the loop
after the last statement of the loop body. However, when the last
statement is a control statement itself (return, continue, break, goto),
this instruction becomes unnecessary and unreachable. Example Go code:
```go
func foo() {
for {
// Do something
break
}
}
```
This change prevents such an instruction from being generated. This has
a marginal artifact size reduction (1632 bytes on todomvc), but also
eliminates a warning Firefox prints in the console, which may be
confusing to users (gopherjs#1069, gopherjs#575).
The improved check is able to detect cases when the return statement is
wrapped in another statement, but is effectively the last executed
statement in a list, for example:
```go
func withGoto() (resultType, error) {
// Do something.
return result, nil
Error:
return nil, err // This is inside an *ast.LabeledStmt!
}
func withBlock() resultType {
// Do something.
{
// Do more.
return result // This is inside a *ast.BlockStmt!
}
}
```
Better detection prevents generation of unnecessary return statements at
the end of a function (marginally smaller output size), as well as
prevents "unreachable code" warnings in Firefox (gopherjs#575, gopherjs#1069).
flimzy
approved these changes
Oct 4, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR prevents GopherJS compiler from emitting unnecessary control statements in two cases:
The change is mostly cosmetic and prevents Firefox from spamming "unreachable code" warnings, which can be confusing to GopherJS users. It also marginally reduces the size of the generated code, but the effect is so small that it doesn't really matter.
Fixed #575, #1069.