Bug 17664 – Deprecate implicit casting between shared and unshared

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-19T09:04:00Z
Last change time
2017-07-20T14:56:48Z
Keywords
spec
Assigned to
nobody
Creator
dfj1esp02

Comments

Comment #0 by dfj1esp02 — 2017-07-19T09:04:45Z
--- struct A { int handle; } shared A b; void f(A a){ b=a; } --- This shouldn't compile. Value types include resource identifiers, which may have threading requirements, e.g. GUI widget handles and OpenGL handles, assuming they are thread-safe and making them implicitly shared is incorrect. union A { int handle; int* dummy; } This can't work as a workaround because it has different size than the handle.
Comment #1 by schveiguy — 2017-07-19T14:24:10Z
This is not the purpose of shared. The purpose of shared is to prevent sharing memory, not other resources. Since a copy of the struct does not share any of the original memory, this needs to be allowed. You will need to implement this a different way to prevent sharing, D provides many tools for this.
Comment #2 by dfj1esp02 — 2017-07-20T13:46:18Z
The idea that sharing is opt out contradicts the idea behind shared qualifier.
Comment #3 by schveiguy — 2017-07-20T14:56:48Z
Making a complete independent copy of a value is not sharing, it's copying. shared doesn't care about the semantic meaning of your integer. It only cares if multiple threads have access to the same memory in RAM. That's its only goal. Anything above that is on you to provide appropriate protections. In other words, don't use int handle, use some UnshareableResource struct that protects the handle internally from accidentally sharing.