Skip to content

C++: Treat implicit end of body of non-void function as Unreached#3254

Merged
rdmarsh2 merged 4 commits into
github:masterfrom
dbartol:dbartol/ImplicitReturnValue2
Apr 14, 2020
Merged

C++: Treat implicit end of body of non-void function as Unreached#3254
rdmarsh2 merged 4 commits into
github:masterfrom
dbartol:dbartol/ImplicitReturnValue2

Conversation

@dbartol

@dbartol dbartol commented Apr 13, 2020

Copy link
Copy Markdown

When the extractor can't prove that control flow will never reach the end of a non-void-returning function without reaching an explicit return statement, it inserts an implicit return without an operand. If control actually reaches this point, the behavior is undefined.

We were previously generating invalid IR for these implicit return statements, because the lack of an operand meant that there was no definition of the return value variable along that path. Instead, I've changed the IR generation to emit an Unreached instruction for the implicit return. This ensures that we don't create a control flow edge from the end of the body to the function epilogue.

The change to the range analysis test avoids having that test depend on the previous bad IR behavior, while still preserving the original spirit of the test.

When the extractor can't prove that control flow will never reach the end of a non-`void`-returning function without reaching an explicit `return` statement, it inserts an implicit `return` without an operand. If control actually reaches this point, the behavior is undefined.

We were previously generating invalid IR for these implicit `return` statements, because the lack of an operand meant that there was no definition of the return value variable along that path. Instead, I've changed the IR generation to emit an `Unreached` instruction for the implicit `return`. This ensures that we don't create a control flow edge from the end of the body to the function epilogue.

The change to the range analysis test avoids having that test depend on the previous bad IR behavior, while still preserving the original spirit of the test.
@dbartol dbartol added the C++ label Apr 13, 2020
@dbartol dbartol requested review from MathiasVP and rdmarsh2 April 13, 2020 22:12
@dbartol dbartol requested a review from a team as a code owner April 13, 2020 22:12
class TranslatedReturnVoidStmt extends TranslatedReturnStmt {
TranslatedReturnVoidStmt() { not stmt.hasExpr() }
TranslatedReturnVoidStmt() {
not stmt.hasExpr() and not hasReturnValue(stmt.getEnclosingFunction())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A return statement in a function with void as its return type can have a void-typed expression - what happens then?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Great question. I’ll add a test case (and probably a fix).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fix and test case added. We didn't handle this correctly before, either. Good catch.

rdmarsh2
rdmarsh2 previously approved these changes Apr 14, 2020

@rdmarsh2 rdmarsh2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@rdmarsh2

Copy link
Copy Markdown
Contributor

oh, missed the failing tests - looks like some noise in block numbering in the syntax zoo consistency failures and a real change in the IR value numbering test

Dave Bartolomeo added 2 commits April 14, 2020 14:17
Mostly noise, but a couple of the missing operand errors are actual fixes.
@dbartol

dbartol commented Apr 14, 2020

Copy link
Copy Markdown
Author

A couple of the failures in syntax-zoo were actually fixed correctly by this change, but the rest of that directory was noise.

Also, whoever wrote the original value number tests had something against return statements:) I just made several functions return void, since they weren't returning a value anyway.

@rdmarsh2 rdmarsh2 merged commit 146bfca into github:master Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants