-
-
Notifications
You must be signed in to change notification settings - Fork 609
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 Issue 11714 - Improve error message for wrongly initialized thread-local class instances #2943
Conversation
…lized. The existing error message can give the impression that the thread-local class instance itself must be const or immutable, not its initial value. This revised version aims to make clearer what is wrong and how to solve it.
|
Should have a diagnostic test case, and in the future you should add a link to the bugzilla issue in the description. (I've added it to this one) |
|
Understood. I've attached a test case to the bug report, whose code is as follows: class A
{
int a;
int b;
}
A a1 = null; // works
A a2 = new A; // failsRun with |
|
The test case needs to go in the test suite, see |
|
Is that really appropriate for a patch that is simply changing an error message? Of course I'll do so if needed, but I don't see the logic in this case. There is not a bug that is being fixed here, just an error message whose meaning is being clarified. |
|
Yes. Ideally we'd have a test case for every error message, and since you're changing it, it would be a good time to add one. |
|
Ah, that makes things clear. Thanks for explanation; I'll get onto it. |
|
The change to the error message is not in fact correct (i.e. the check really disallows initializing static, mutable class variables with an instance, the issue IIRC being that a new one would have to be constructed for each thread on startup). Rewording the error message might still be a good idea, though. |
|
So the error message should read, |
|
@WebDrake: Well, if you make the variable either constant, or shared, then it's legal. |
|
... works? :-) |
|
@WebDrake: I'm not sure as I haven't looked at the related source, but you can never initialize a mutable reference with |
|
Maybe @IgorStepanov can comment on this. |
|
Can you guys think it over and confirm for me what's allowed and not allowed in terms of initialization, and then I'll follow up with a revised patch and a test case as requested? |
|
Conceptually, I think the error message should be something along the lines of "thread-local mutable class variable cannot be initialized with reference to compile-time constant". How to best word that - no idea... |
|
And maybe suggest a fix: "Use a static constructor instead." |
|
My suggestion: fail_compilation/diag11714.d /*
TEST_OUTPUT:
---
fail_compilation/diag11714.d(12): Error: thread-local mutable class reference diag11714.c is not allowed. Please use static this() instead.
fail_compilation/diag11714.d(13): Error: thread-local pointer to mutable struct diag11714.p is not allowed. Please use static this() instead.
---
*/
class C {}
struct S {}
C c = new C();
S* p = new S(); |
|
AFAICS the constraint is that the variable name must come right after How about,
Works for everyone? |
|
I still think "initialized with mutable struct" is a bit misleading, as it's not the initializer that is a problem, but the variable type. |
|
Any consensus on the diagnostic? The current one in the test is way too long IMO. |
|
Sorry all for the delay in any follow-up here. Moving country and job in the last month has been a distraction. ;-) I think from my point of view what still needs to be worked out here is an error message that really sheds light on what the problem is and how to solve it. References to How about, |
|
@WebDrake: To me, that makes it sound as if |
|
|
I apologize for the very long answer. In other words, l-value can be mutable now. This problem also applies to other reference types: I suggest the next rule: mutable literal of reference type (including pointer, array, AA, class) should be disallowed, except they used for initialize an automatic or shared/__gshared varibale. However, it is considerably more of this PR.
This message is wrong: value is known at compile time. However it shouldn't be used for initializing a variable, because we may get an implicit sharing of mutable data. |
|
Quite a bit of discussion here - any closure? |
|
Moving PR to #7508. |
The existing error message can give the impression that the thread-local class instance itself must be const or immutable, not its initial value. This revised version aims to make clearer what is wrong and how to solve it.
Background to this patch: http://forum.dlang.org/post/mailman.408.1386663198.3242.digitalmars-d-learn@puremagic.com
https://d.puremagic.com/issues/show_bug.cgi?id=11714