-
-
Notifications
You must be signed in to change notification settings - Fork 610
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
Fix 22031 - Allow modification of constants in crt_constructor's
#13751
base: master
Are you sure you want to change the base?
Conversation
|
Thanks for your pull request and interest in making D better, @MoonlightSentinel! 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 references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#13751" |
|
|
Should taking the address be banned too? Direct function calls aren't the only way to corrupt using a constructor like this. |
e285978 to
180a5c1
Compare
crt_[con|de]structor'scrt_constructor's
Probably. As pointed out by @WebFreak001 on Discord:
I've added a second commit that restricts the error to
Good catch |
7bd83a4 to
e0f156b
Compare
Allow functions marked as `pragma(crt_constructor)` to modify `const` / `immutable` variables, which is currently restricted to module ctors. This enables the use of lazily initialized global constants for applications without druntime. Such function may not be called at runtime because they might violate the guarantees of `const` / `immutable`. Hence it is now an error to call / take the address of a function marked as `pragma(crt_constructor)`.
…` code Because there are valid reasons to explicitly call those functions, e.g. when they are not automatically called without a C runtime.
|
needs a spec PR at some point. |
|
I'm concerned about this. It has @safe code violating immutable guarantees. But currently even @System code is not allowed to violate immutability. This change allows any code, at any time, to change the state of those so-called immutable variables. This has adverse implications for compiler assumptions and optimizations. It sabotages any attempts to mechanically verify correctness of shared immutable code. I understand that D's rigid interpretation of immutability leaves some people frustrated at not being able to write "logical const" code, and other techniques of mutating immutable data. But it's worth it. If the programmer needs to mutate data, don't declare it immutable. Since the user can declare the data to be mutable, and then initialized, I oppose this change. BTW, the user can also do this today: |
Your proposed workaround disproves that statement -
That's why the initial implementation (see the first commit) explicitly disallows further calls to a |
Initialization is not mutation, we make this distinction. More importantly, pure function can access immutable globals, but it cannot access mutable ones, so making it mutable isn't the solution here. Also this works: void main() {
_sharedStaticCtor_L5_C1();
}
shared static this() {
} |
https://issues.dlang.org/show_bug.cgi?id=20607 I wish we just went with |
Allow functions marked as
pragma(crt_constructor)to modifyconst/immutablevariables, which is currently restricted to module ctors.This enables the use of lazily initialized global constants for
applications without druntime.
Such function may not be called at runtime because they might violate
the guarantees of
const/immutable. Hence it is now an error tocall / take the address of a function marked as
pragma(crt_constructor).