Conversation
|
What ? |
|
Yes, via mutable indirections via input arguments. That's how D defines weak purity. Andrei has already approved this.
|
See also: #1683 |
|
See also: #1719 |
|
This is dependent upon dlang/dlang.org#1528, which makes |
it should already be! it accepts a parameter with mutable indirection, the very reason we have weak-pure. Note that the issue hasn't ever been that free itself would be omitted, it's that one typically casts away immutability to free something. So the wrapping function may accept an immutable indirected item, and then the wrapper function is never called. For example: pure void foo(immutable int *x) { .free(cast()x); }This now may be elided by the compiler, and the memory is never freed. Think of how this may apply e.g. to a reference counting struct that has an immutable item it's tracking. |
|
@schveiguy the cast makes the compiler wash its hands so I think we're fine - the programmer needs to take special measures to make sure the desired effect is attained |
|
hmmm I just realized destructors must be always called even if they seem pure... |
This isn't possible. You can't enforce this effect unless you can somehow mark the type itself as special to the compiler. |
|
|
|
oh, can actually return |
|
At some level, you will just have an Not to mention that if you throw in implied purity in templates, you now have to contend with the compiler both eliding your call and qualifying your function so it can do so. And I'm not 100% sure your workaround would work anyway -- you are ignoring obviously new memory as far as the compiler is concerned. It can safely elide the call as no side effects happen. What you need is a mutable parameter. |
It cannot by the rules proposed in dlang/dlang.org#1528, even if the result is not used. Also: @schveiguy you can always hoist the cast into the call: instead of defining |
|
The cast cannot happen inside pure code. But it will, someone will do it. Once it happens inside pure code, then that pure function could potentially be hiding this bug. I think making
That's not how I read it. I read that it cannot be memoized, but this eliding is not due to memoization, it's due to having no side effects. There's no explicit statement that says calls to weakly pure functions cannot be elided under any circumstances. |
Then I need to improve it! |
It doesn't matter. A strong pure function can call a weak pure one, and then that call can be elided. As long as the cast happens inside a strong-pure function at any level, the trap is set. |
|
@schveiguy I see your point. @nordlow fistfight it out with @schveiguy |
|
@WalterBright, do you oppose to @schveiguy's last objection? A code example proving you right would be in place, @schveiguy ;) |
|
@andralex @nordlow I know I'm late to the party, but issues with a pure
So, if we need to remove |
|
@andralex doesn't seem to be clear on
|
|
Not to worry. One thing we can do is write a pure wrapper for |
|
@andralex Then we need to mark the wrappers as pure, and not the C functions. |
|
correct |
|
I believe both Note that
This is one of those things where one-in-a-million times it will break. But it will break at some point. A good "real-world" example is hard to find, but I'm sure if we allow this PR to go through, we will encounter some. Then there will be the usual "you're just doing it wrong" pushback, until finally we have to painfully remove the The cautious and lower-risk path forward is to use escapes (as has been done in dlang/phobos#4832) for specific situations and see if we can make it work. If we can, then reexamining ways to add pure attribute for general use without issues might be in order. I still don't think adding |
|
OK so this is not required anymore because it has a local workaround. I'm not sure about @schveiguy's concerns but if there's no urgency we should move cautiously. OK to close for now? |
|
Ok, with me to go with local qualified imports for now. |
|
Closing this, as #1836 seems likely to get in. |
Enables dlang/phobos#4832 (comment)