-
-
Notifications
You must be signed in to change notification settings - Fork 369
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
Ban undefined behavior at CTFE #4167
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request and interest in making D better, @Bolpat! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any specific examples of undefined behaviour that must not occur?
I think that it's in response to: dlang/dmd#20827 Mathias asked whether it was in the spec that UB is banned during CTFE, and I guess that he didn't find it there, so he's now adding it to the spec. I don't know if that's what we ultimately want or not. CTFE does already ban a number of useful things, but actually enforcing no UB during CTFE would likely lead to yet more things being banned. I don't know what currently can happen during UTFE that could be considered UB, but since you can obviously have UB during runtime, banning it during compile time means that there has to be stuff that you can do at runtime that you can't do at compile time, and if anything, any time that those restrictions pop up, it's annoying. We obviously can't ever do everything that we can do at runtime during CTFE, but in general, it would be nice if we could do more rather than less. So, maybe this is a good idea, and maybe it isn't. In essence, it's an argument for requiring that code be |
Exactly this. Why you would want to actually mutate a |
If the function pointer is used in a context where it follows the rules of CTFE is a somewhat different beast than runtime, and it already bans certain things that you can do just fine at runtime, and depending on the situation, we may want to ban more, but every time that anything is banned during CTFE that you can do at runtime, CTFE is then able to do less. And even if we all agree that casting away And personally, I'm perfectly fine with the code doing dumb stuff if you cast away |
Destructors ignore const. So this has (unfortunately) already been established that you need to be able to do so. |
They handle |
I’m truly wondering if I’m the only one in here who understands what UB really means or the only one who doesn’t. TL;DR: There are the following considerations w.r.t. UB:
At runtime, because most UB can’t be pin-pointed, tolerating UB is a necessity. At CTFE, tolerating UB is unacceptable, which means that if pin-pointing isn’t possible or, while possible, it’s decided not worth it, the right decision is overblocking.
That is incorrect if you’re talking about runtime. A simple example is
There’s a difference between not smart enough and provably unable to. You’re afraid of overblocking, i.e. banning more than needs to be banned. As you say, An example where the compiler does not have all the information is
It might happen accidentally. In D, I personally use C++ |
D should ban undefined behavior (UB) during compile-time function execution (CTFE) to ensure that the results are consistent and predictable. This restriction helps maintain the integrity of CTFE and guarantees that any constant expressions will yield the same result every time they are evaluated, give or take implementation-defined features. By avoiding undefined behavior, the language lives up the goal of being safe and reliable. If UB were allowed at CTFE, deterministic builds become impossible as a feature.
Considering what’s already banned during CTFE, it makes no sense whatsoever that UB is not generally ruled out.
This is not a breaking change because UB being what it is, allows the compiler to issue a diagnostic in the meaning of the old spec. The new spec would require a diagnostic and any remaining UB at CTFE that’s not caught constitutes a compiler bug.
C++ bans UB during CTFE (“constant evaluation” in C++ terminology) for the reasons mentioned in the first paragraph (possibly among others).