-
-
Notifications
You must be signed in to change notification settings - Fork 743
Issue 3882: Use cast(void) on calls to assumeSafeAppend #2025
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
Conversation
Wait, why is |
This function is a bit of an ugly duckling: Eventhough DMD (my Issue 3882 pull request for DMD at least warns about it being non-void returning strictly pure) infers it to be strictly pure it does modify memory allocations. However I don't understand how it can be inferred to be pure because it calls
Is this perhaps a bug in DMD? See line 641 in I'm not sure what to do with this. A question for Walter perhaps? Anyway it would to be nice to get this issue merged for both Phobos and DMD. |
It is pure one the grounds that it is a GC operation, that never actually manipulates elements. This is recent. It's PR: dlang/druntime#553
It returns it's argument so that you write things such as: int b = a[0 .. 5].assumeSafeAppend(); (for example). That said, it is a weekly pure function, so it is my understanding that the error should not be triggered. |
That would save the day. @nordlow ? |
Hmm. according to my tests cases only calls to non-void-returning strictly pure functions are warned about in my DMD pull. Is perhaps This code @safe pure nothrow void strictVoid(T)(T x) { }
@safe pure nothrow bool strictBool(T)(T x) { return false; }
@safe pure nothrow void nonstrictVoid(T)(ref T x) { x += 1; }
@safe pure nothrow bool nonstrictBool(T)(ref T x) { x += 1; return false; }
void main(string args[])
{
int x = 3;
strictVoid(x);
nonstrictVoid(x);
strictBool(x);
nonstrictBool(x);
} generates only this single line
when compiled using dlang/dmd#3342 |
I don't know how you test for strict purity, but Unless in these use cases, we are calling |
I added this Phobos pull request because my DMD pull errrors as https://d.puremagic.com/test-results/pull.ghtml?projectid=1&runid=931351 |
Yes, you can call assumeSafeAppend on a tail-immutable array. The result is un safe, but legal, as long as you don't violate immutability. For example, if you have a string, you can append to the string. If you want to re-use the buffer, and know there are no references to the buffer you are removing, you can legally do it. For example, this is legal: string s = "hello";
s ~= " world";
writeln(s);
s.length = 5;
s.assumeSafeAppend();
s ~= " joe"; Simply because there is never any other reference to Note that assumeSafeAppend in this case, probably should not be marked as pure, since it will be misinterpreted by the compiler as not having an effect on the parameter. I'm not sure how one could fix this, there is no specific "weak pure" attribute. BTW, I wasn't aware that your pull made it pure, I thought the pull just made it nothrow. I think we should back out the pure attribute. |
Agreed. |
I really don't see how it is any less pure than, say appending to an array. The fact that it is mis-interpreted as strongly pure rather than weakly pure, shouldn't mean we ditch its purity entirely. The medicine seems harsher than the illness here, IMO. |
What do you mean by backing out the pure attribute? Neither |
|
Ok. Who removes the pure keyword from Strange...I don't see any explicit
private
{
extern (C) void _d_arrayshrinkfit(TypeInfo ti, void[] arr);
} and private
{
extern (C) void _d_arrayshrinkfit(const TypeInfo ti, void[] arr);
} Have I missed something? |
Oops. I was using a too old copy of druntime. Sorry. Shall someone revert this or do we need time to reflect more on this matter? |
For example, adding a hidden dummy pointer should fix it? auto ref inout(T[]) assumeSafeAppend(T)(auto ref inout(T[]) arr, void* dummy = null); This fixes the issue, and we preserve the weak purity. |
@monarchdodra @nordlow I think we should back out the pure attribute for now. It was a very very recent change, so virtually no code depends on it. If we can re-introduce it in a way that makes it weak-pure for all types, then we can debate that. Note that assumeSafeAppend uses a cast to call |
Shall I remove the |
@nordlow that would be great. Thanks! |
I guess that means that this pull request is not needed right? I only need to fix druntime right? |
Yah. This work shouldn't go to waste though. Please also submit a bugzilla issue so we figure how we go about the larger problem. Thanks! |
Yeah, this pull request is definitely not needed.
Yes, please remove the pure attribute. I'll pull you myself. |
Based on last comment, should this PR be closed? |
This should make dlang/dmd#3342 compile Phobos without warnings.