| 1 |
/* REQUIRED_ARGS: -preview=dip1000 -preview=in -mcpu=native |
| 2 |
*/ |
| 3 |
|
| 4 |
import core.stdc.time; |
| 5 |
|
| 6 |
void fun(in int* inParam) @safe; |
| 7 |
static assert([__traits(getParameterStorageClasses, fun, 0)] == ["in"]); |
| 8 |
static assert (is(typeof(fun) P == __parameters) && is(P[0] == const int*)); |
| 9 |
|
| 10 |
struct Foo |
| 11 |
{ |
| 12 |
int a; |
| 13 |
double[100] b; |
| 14 |
} |
| 15 |
void fun2(in Foo inParam) @safe; |
| 16 |
static assert([__traits(getParameterStorageClasses, fun2, 0)] == ["in"]); |
| 17 |
static assert (is(typeof(fun2) P == __parameters) && is(P[0] == const Foo)); |
| 18 |
|
| 19 |
void test() |
| 20 |
{ |
| 21 |
withDefaultValue(42); |
| 22 |
withDefaultValue(); |
| 23 |
withDefaultRef(TimeRef.init); |
| 24 |
withDefaultRef(); |
| 25 |
|
| 26 |
withInitDefaultValue(); |
| 27 |
withInitDefaultRef(); |
| 28 |
} |
| 29 |
|
| 30 |
struct FooBar |
| 31 |
{ |
| 32 |
string toString() const |
| 33 |
{ |
| 34 |
string result; |
| 35 |
// Type is const |
| 36 |
this.toString((in char[] buf) { |
| 37 |
static assert(is(typeof(buf) == const(char[]))); |
| 38 |
result ~= buf; |
| 39 |
}); |
| 40 |
// Type inference works |
| 41 |
this.toString((in buf) { result ~= buf; }); |
| 42 |
return result; |
| 43 |
} |
| 44 |
|
| 45 |
void toString(scope void delegate(in char[]) sink) const |
| 46 |
{ |
| 47 |
sink("Hello world"); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// Ensure that default parameter works even if non CTFEable |
| 52 |
void withDefaultValue(in time_t currTime = time(null)) {} |
| 53 |
struct TimeRef { time_t now; ulong[4] bloat; } |
| 54 |
void withDefaultRef(in TimeRef currTime = TimeRef(time(null))) {} |
| 55 |
|
| 56 |
// Ensure that default parameters work with `.init` |
| 57 |
void withInitDefaultValue(in size_t defVal = size_t.init) {} |
| 58 |
void withInitDefaultRef(in TimeRef defVal = TimeRef.init) {} |
| 59 |
|
| 60 |
// Ensure that temporary aren't needlessly created |
| 61 |
// (if they are, it'll trigger the "goto skips declaration" error) |
| 62 |
void checkNotIdentity(in void* p1, in void* p2) { assert(p1 !is p2); } |
| 63 |
void checkTemporary() |
| 64 |
{ |
| 65 |
int* p = new int; |
| 66 |
if (p is null) |
| 67 |
goto LError; |
| 68 |
// Should not generate temporary, pass the pointers by value |
| 69 |
checkNotIdentity(/*lvalue*/ p, /*rvalue*/ null); |
| 70 |
checkNotIdentity(new int, null); |
| 71 |
LError: |
| 72 |
assert(0); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
// Some ABI-specific tests: |
| 77 |
|
| 78 |
version (Win64) |
| 79 |
{ |
| 80 |
void checkReal(in real p) |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
struct RGB { ubyte r, g, b; } |
| 85 |
void checkNonPowerOf2(in RGB p) |
| 86 |
{ |
| 87 |
} |
| 88 |
} |
| 89 |
else version (X86_64) // Posix x86_64 |
| 90 |
{ |
| 91 |
struct Empty {} // 1 dummy byte passed on the stack |
| 92 |
void checkEmptyStruct(in Empty p) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
static if (is(__vector(double[4]))) |
| 97 |
{ |
| 98 |
struct AvxVectorWrapper { __vector(double[4]) a; } // 256 bits |
| 99 |
void checkAvxVector(in AvxVectorWrapper p) |
| 100 |
{ |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
else version (AArch64) |
| 105 |
{ |
| 106 |
alias HVA = __vector(float[4])[4]; // can be passed in 4 vector registers |
| 107 |
void checkHVA(in HVA p) |
| 108 |
{ |
| 109 |
} |
| 110 |
} |