Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Issue 19180 - Expose dmd.mtype.Type.isZeroInit as __traits(isZero…
…Init, T)
- Loading branch information
Showing
6 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Expose `__traits(isZeroInit, T)` | ||
|
||
Takes one argument which must be a type. If the type's | ||
$(DDSUBLINK spec/property, init, default initializer) has no non-zero | ||
bits then `true` is returned, otherwise `false`. `isZeroInit` will | ||
always return `true` for a class `C` because `C.init` is a null | ||
reference. | ||
|
||
This property was already being computed internally by the compiler | ||
so exposing it via `__traits` is more efficient than re-implementing | ||
it in library code. | ||
|
||
--- | ||
struct S1 | ||
{ | ||
int x; | ||
} | ||
|
||
struct S2 | ||
{ | ||
int x = -1; | ||
} | ||
|
||
static assert(__traits(isZeroInit, S1)); | ||
static assert(!__traits(isZeroInit, S2)); | ||
|
||
void test() | ||
{ | ||
int x = 3; | ||
static assert(__traits(isZeroInit, typeof(x))); | ||
} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
alias AliasSeq(T...) = T; | ||
|
||
struct Holder(T, ubyte val) | ||
{ | ||
T x = val; | ||
} | ||
|
||
struct SArrayHolder(T, ubyte val) | ||
{ | ||
T[2] x = val; | ||
} | ||
|
||
static foreach (T; AliasSeq!(bool, byte, short, int, long, | ||
ubyte, ushort, uint, ulong, | ||
char, wchar, dchar, | ||
float, double, real)) | ||
{ | ||
static assert(__traits(isZeroInit, T) == (T.init is T(0))); | ||
static assert(__traits(isZeroInit, T[2]) == (T.init is T(0))); | ||
|
||
static assert(!__traits(isZeroInit, Holder!(T, 1))); | ||
static assert(__traits(isZeroInit, Holder!(T, 0))); | ||
|
||
static assert(__traits(isZeroInit, SArrayHolder!(T, 0))); | ||
static assert(!__traits(isZeroInit, SArrayHolder!(T, 1))); | ||
|
||
} | ||
|
||
static assert(__traits(isZeroInit, void)); // For initializing arrays of element type `void`. | ||
static assert(__traits(isZeroInit, void*)); | ||
static assert(__traits(isZeroInit, void[])); | ||
static assert(__traits(isZeroInit, float[])); | ||
static assert(__traits(isZeroInit, Object)); | ||
class C1 : Object | ||
{ | ||
int x = 1; | ||
} | ||
static assert(__traits(isZeroInit, C1)); // An Object's fields are irrelevant. | ||
|
||
struct S1 | ||
{ | ||
int[] a; | ||
int b; | ||
} | ||
static assert(__traits(isZeroInit, S1)); | ||
|
||
struct S2 | ||
{ | ||
alias H = Holder!(int, 1); | ||
H h; | ||
int a; | ||
} | ||
static assert(!__traits(isZeroInit, S2)); | ||
|
||
struct S3 | ||
{ | ||
S1 h; | ||
float f = 0; | ||
} | ||
static assert(__traits(isZeroInit, S3)); | ||
|
||
struct S4 | ||
{ | ||
S2 h = S2(S2.H(0), 0); | ||
int a; | ||
} | ||
static assert(__traits(isZeroInit, S4)); | ||
|
||
struct S5 | ||
{ | ||
Object o = null; | ||
} | ||
static assert(__traits(isZeroInit, S5)); | ||
|
||
version(D_SIMD): | ||
import core.simd : int4; | ||
static assert(__traits(isZeroInit, Holder!(int4, 0))); | ||
static assert(!__traits(isZeroInit, Holder!(int4, 1))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/fail_isZeroInit.d(11): Error: type expected as second argument of __traits `isZeroInit` instead of `a` | ||
--- | ||
*/ | ||
void test() | ||
{ | ||
int a = 3; | ||
// Providing a specific variable rather than a type isn't allowed. | ||
enum bool az = __traits(isZeroInit, a); | ||
} |