Skip to content

require CTFE to detect UB #60

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 3 commits into from
Nov 2, 2020
Merged

Conversation

RalfJung
Copy link
Member

This is what @oli-obk and me agreed on here, except I went one step further and now require that for non-detected UB, CTFE continues in a reasonable way, instead of producing an arbitrary result. Once we keep the unoptimized MIR around, that is cheap to do. For example, this means that when the code violates alignment, CTFE must either raise an error or continue as if the pointer was actually well-aligned (and perform the appropriate load from the given region of memory). The previous wording would have allowed CTFE to return Undef when loading from an unaligned pointer -- I cannot see a good reason why we would permit CTFE to do that.

@RalfJung RalfJung mentioned this pull request Oct 31, 2020
This can change from compiler version to compiler version: CTFE code that causes UB could build fine with one compiler and fail to build with another.
(This is in accordance with the general policy that unsound code is not subject to strict stability guarantees.)

[UB]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html

# Reference-level explanation
[reference-level-explanation]: #reference-level-explanation
Copy link
Member Author

Choose a reason for hiding this comment

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

As usual I have no idea how to split the RFC intention across "guide-level" and "reference-level" explanation.^^ That separation makes very little sense to me, for most of the RFCs that I end up writing...

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

non-detected UB, CTFE continues in a reasonable way, instead of producing an arbitrary result.

yes, we should not permit CTFE to explicitly create bogus values on UB when, in order to do that, it would need to detect the UB and choose to create a bogus value. If UB is detected, interpretation must be aborted. If UB is not detected, CTFE continues in a reasonable way.

Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
@oli-obk oli-obk merged commit 33053bb into rust-lang:master Nov 2, 2020
@RalfJung RalfJung deleted the const-ub-rfc branch November 2, 2020 13:50
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.

2 participants