Fix Issue 23639 - Casting to shared not allowed with -preview=nosharedaccess#14836
Fix Issue 23639 - Casting to shared not allowed with -preview=nosharedaccess#14836RazvanN7 merged 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request and interest in making D better, @RazvanN7! 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#14836" |
|
No it doesn't. If you run that code it gives: "direct access to shared |
|
Oh, that's good |
|
Isn't this a special case patching over an issue with the nosharedaccess check? IIRC this same issue also breaks nosharedaccess atomics with ldc because the cast is badly analyzed and/or then "optimized" to strip out the shared (this is actually tested with a static assert somewhere in the test suite) |
thewilsonator
left a comment
There was a problem hiding this comment.
Couple of thing I'd like clarified before this is merged.
| if (e.type.isShared()) | ||
| // https://issues.dlang.org/show_bug.cgi?id=23639 | ||
| // Should be able to cast(shared) | ||
| if (!e.isCastExp() && e.type.isShared()) |
There was a problem hiding this comment.
what does this do to const shared?
There was a problem hiding this comment.
If you cast to const shared it just performs the cast.
There was a problem hiding this comment.
please augment the test case then.
There was a problem hiding this comment.
Why? Before this patch the cast would not have been allowed, but now it is allowed. Note that the code would fail because you cannot convert a const(shared) to a shared.
| { | ||
| void *p; | ||
| // assume p is allocated here | ||
| return cast(shared(T))p; |
There was a problem hiding this comment.
what is the @safety of this operation?
There was a problem hiding this comment.
and if typeof(p) == T?
There was a problem hiding this comment.
The modified code has nothing to do with the safety checks. Casts to differently qualified types remain unsafe, that part is not modified.
There was a problem hiding this comment.
-preview=nosharedaccess is all about safety. even if the end result is that the code must be system. Perhaps I was thinking things wrong, but it seems to me that the only reason why you would want to do this with -preview=nosharedaccess is because you have a unique reference to an object you then want to share. Its fine if that needs to be trusted, then please disregard this request for changes, but please do document this discussion in the test case.
There was a problem hiding this comment.
Yes, a prime example of this is memory allocation. You allocate heap memory which is naturally shared among threads and you want to express that. What other way of casting the mallocd memory to shared is there?
Well, You call this patch special casing. Fine, but I have no idea what is the general problem other than "casting to shared is not allowed".
Without a test case, I have no idea what you are referring to. |
|
@thewilsonator I have added a fail_compilation test. @maxhaton Ping. This is blocking progress on templating druntime hooks. |
Casting to shared should be allowed even if the result is returned, read or written because the variable has just been "promoted" to shared so it's not being actually shared with anyone. This is currently blocking the templatization of the __d_new_class runtime hook (cc @teodutu ).