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
Suppress the "read-modify-write" error if type is a struct or a class #10281
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Suppress the "read-modify-write" error if type is a struct or a class | ||
|
||
If a struct or class with shared type implements the opUnary or opOpAssign | ||
operator then it is possible to perform "read-modify-write" operations | ||
because the operator's implementation is supposed to perform atomic operations. | ||
|
||
The purpose of this modification is to allow the creation of wrappers | ||
(with structs or classes) to run atomic operations silently, for example: | ||
|
||
--- | ||
shared struct Atomic { | ||
int a; | ||
|
||
int opUnary(string s)() if (s == "++") | ||
{ | ||
import core.atomic : atomicOp; | ||
return atomicOp!"+="(a, 1); | ||
} | ||
} | ||
|
||
Atomic atomicvar; | ||
atomicvar++; // Safe! Atomic struct implements opUnary | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
REQUIRED_ARGS: | ||
TEST_OUTPUT: | ||
--- | ||
--- | ||
*/ | ||
shared struct S | ||
{ | ||
int x = 0; | ||
|
||
int opUnary(string s)() if (s == "++") | ||
{ | ||
import core.atomic : atomicOp; | ||
return atomicOp!"+="(x, 1); | ||
} | ||
} | ||
|
||
shared class C | ||
{ | ||
int x = 0; | ||
|
||
int opUnary(string s)() if (s == "++") | ||
{ | ||
import core.atomic : atomicOp; | ||
return atomicOp!"+="(x, 1); | ||
} | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
s++; | ||
shared(C) c = new C(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this need to be shared if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using shared in the class declaration does not seem to make the class type "implicitly" shared unlike what it does in a struct |
||
c++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't look right. It's blindly allowing
struct
/class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the class/struct does not implement the operator the compiler will give the error "is not a scalar"
dmd/src/dmd/expressionsem.d
Line 5574 in d9826c8
So I don't think further checks are necessary