summaryrefslogtreecommitdiff
path: root/libphobos/src/std/traits.d
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2022-02-13 20:17:53 +0100
committerIain Buclaw <ibuclaw@gdcproject.org>2022-02-16 11:15:02 +0100
commitd75691877c4a7521a995d2601021fcaf30f65d94 (patch)
tree36509d835d63b98ad1130ac9d4695b5033c10428 /libphobos/src/std/traits.d
parent023327643969d5469902a9ecfa6738a315f9e362 (diff)
d: Merge upstream dmd 52844d4b1, druntime dbd0c874, phobos 896b1d0e1.
D front-end changes: - Parsing and compiling C code is now possible using `import'. - `throw' statements can now be used as an expression. - Improvements to the D template emission strategy when compiling with `-funittest'. D Runtime changes: - New core.int128 module for implementing intrinsics to support 128-bit integer types. - C bindings for the kernel and C runtime have been better separated to allow compiling for hybrid targets, such as kFreeBSD. Phobos changes: - The std.experimental.checkedint module has been renamed to std.checkedint. gcc/d/ChangeLog: * d-builtins.cc (d_build_builtins_module): Set purity of DECL_PURE_P functions to PURE::const_. * d-gimplify.cc (bit_field_ref): New function. (d_gimplify_modify_expr): Handle implicit casting for assignments to bit-fields. (d_gimplify_unary_expr): New function. (d_gimplify_binary_expr): New function. (d_gimplify_expr): Handle UNARY_CLASS_P and BINARY_CLASS_P. * d-target.cc (Target::_init): Initialize bitFieldStyle. (TargetCPP::parameterType): Update signature. (Target::supportsLinkerDirective): New function. * dmd/MERGE: Merge upstream dmd 52844d4b1. * expr.cc (ExprVisitor::visit (ThrowExp *)): New function. * types.cc (d_build_bitfield_integer_type): New function. (insert_aggregate_bitfield): New function. (layout_aggregate_members): Handle inserting bit-fields into an aggregate type. libphobos/ChangeLog: * Makefile.in: Regenerate. * libdruntime/MERGE: Merge upstream druntime dbd0c874. * libdruntime/Makefile.am (DRUNTIME_CSOURCES): Add core/int128.d. (DRUNTIME_DISOURCES): Add __builtins.di. * libdruntime/Makefile.in: Regenerate. * src/MERGE: Merge upstream phobos 896b1d0e1. * src/Makefile.am (PHOBOS_DSOURCES): Add std/checkedint.d. * src/Makefile.in: Regenerate. * testsuite/testsuite_flags.in: Add -fall-instantiations to --gdcflags.
Diffstat (limited to 'libphobos/src/std/traits.d')
-rw-r--r--libphobos/src/std/traits.d101
1 files changed, 97 insertions, 4 deletions
diff --git a/libphobos/src/std/traits.d b/libphobos/src/std/traits.d
index c1d6bc97402..596c11cfb00 100644
--- a/libphobos/src/std/traits.d
+++ b/libphobos/src/std/traits.d
@@ -5195,16 +5195,54 @@ enum isAssignable(Lhs, Rhs = Lhs) = isRvalueAssignable!(Lhs, Rhs) && isLvalueAss
/**
Returns `true` iff an rvalue of type `Rhs` can be assigned to a variable of
-type `Lhs`
+type `Lhs`.
*/
enum isRvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, { lvalueOf!Lhs = rvalueOf!Rhs; });
+///
+@safe unittest
+{
+ struct S1
+ {
+ void opAssign(S1);
+ }
+
+ struct S2
+ {
+ void opAssign(ref S2);
+ }
+
+ static assert( isRvalueAssignable!(long, int));
+ static assert(!isRvalueAssignable!(int, long));
+ static assert( isRvalueAssignable!S1);
+ static assert(!isRvalueAssignable!S2);
+}
+
/**
Returns `true` iff an lvalue of type `Rhs` can be assigned to a variable of
-type `Lhs`
+type `Lhs`.
*/
enum isLvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, { lvalueOf!Lhs = lvalueOf!Rhs; });
+///
+@safe unittest
+{
+ struct S1
+ {
+ void opAssign(S1);
+ }
+
+ struct S2
+ {
+ void opAssign(ref S2);
+ }
+
+ static assert( isLvalueAssignable!(long, int));
+ static assert(!isLvalueAssignable!(int, long));
+ static assert( isLvalueAssignable!S1);
+ static assert( isLvalueAssignable!S2);
+}
+
@safe unittest
{
static assert(!isAssignable!(immutable int, int));
@@ -6095,7 +6133,7 @@ template BuiltinTypeOf(T)
alias X = OriginalType!T;
static if (__traits(isArithmetic, X) && !is(X == __vector) ||
__traits(isStaticArray, X) || is(X == E[], E) ||
- __traits(isAssociativeArray, X))
+ __traits(isAssociativeArray, X) || is(X == typeof(null)))
alias BuiltinTypeOf = X;
else
static assert(0);
@@ -6117,7 +6155,13 @@ enum bool isBoolean(T) = __traits(isUnsigned, T) && is(T : bool);
static assert( isBoolean!bool);
enum EB : bool { a = true }
static assert( isBoolean!EB);
- static assert(!isBoolean!(SubTypeOf!bool));
+
+ struct SubTypeOfBool
+ {
+ bool val;
+ alias val this;
+ }
+ static assert(!isBoolean!(SubTypeOfBool));
}
@safe unittest
@@ -6994,6 +7038,18 @@ enum bool isArray(T) = isStaticArray!T || isDynamicArray!T;
*/
enum bool isAssociativeArray(T) = __traits(isAssociativeArray, T);
+///
+@safe unittest
+{
+ struct S;
+
+ static assert( isAssociativeArray!(int[string]));
+ static assert( isAssociativeArray!(S[S]));
+ static assert(!isAssociativeArray!(string[]));
+ static assert(!isAssociativeArray!S);
+ static assert(!isAssociativeArray!(int[4]));
+}
+
@safe unittest
{
struct Foo
@@ -7037,6 +7093,7 @@ enum bool isBuiltinType(T) = is(BuiltinTypeOf!T) && !isAggregateType!T;
static assert( isBuiltinType!string);
static assert( isBuiltinType!(int[]));
static assert( isBuiltinType!(C[string]));
+ static assert( isBuiltinType!(typeof(null)));
static assert(!isBuiltinType!C);
static assert(!isBuiltinType!U);
static assert(!isBuiltinType!S);
@@ -7049,6 +7106,7 @@ enum bool isBuiltinType(T) = is(BuiltinTypeOf!T) && !isAggregateType!T;
*/
enum bool isSIMDVector(T) = is(T : __vector(V[N]), V, size_t N);
+///
@safe unittest
{
static if (is(__vector(float[4])))
@@ -7066,6 +7124,20 @@ enum bool isSIMDVector(T) = is(T : __vector(V[N]), V, size_t N);
*/
enum bool isPointer(T) = is(T == U*, U) && __traits(isScalar, T);
+///
+@safe unittest
+{
+ void fun();
+
+ static assert( isPointer!(int*));
+ static assert( isPointer!(int function()));
+ static assert(!isPointer!int);
+ static assert(!isPointer!string);
+ static assert(!isPointer!(typeof(null)));
+ static assert(!isPointer!(typeof(fun)));
+ static assert(!isPointer!(int delegate()));
+}
+
@safe unittest
{
static foreach (T; AliasSeq!(int*, void*, char[]*))
@@ -8835,6 +8907,27 @@ enum bool allSameType(Ts...) =
*/
enum ifTestable(T, alias pred = a => a) = __traits(compiles, { if (pred(T.init)) {} });
+///
+@safe unittest
+{
+ class C;
+ struct S1;
+ struct S2
+ {
+ T opCast(T)() const;
+ }
+
+ static assert( ifTestable!bool);
+ static assert( ifTestable!int);
+ static assert( ifTestable!(S1*));
+ static assert( ifTestable!(typeof(null)));
+ static assert( ifTestable!(int[]));
+ static assert( ifTestable!(int[string]));
+ static assert( ifTestable!S2);
+ static assert( ifTestable!C);
+ static assert(!ifTestable!S1);
+}
+
@safe unittest
{
import std.meta : AliasSeq, allSatisfy;