From 60e9606fc6cdb4a72c2345dde3c76a6c0578892d Mon Sep 17 00:00:00 2001 From: RazvanN7 Date: Fri, 2 Dec 2022 12:53:58 +0200 Subject: [PATCH] Fix Issue 23534 - __traits(isZeroInit) is true for enums with explicit values --- compiler/src/dmd/traits.d | 7 ++++++- compiler/test/compilable/test23534.d | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 compiler/test/compilable/test23534.d diff --git a/compiler/src/dmd/traits.d b/compiler/src/dmd/traits.d index 2c637a1e7ffa..a6d2304c38ac 100644 --- a/compiler/src/dmd/traits.d +++ b/compiler/src/dmd/traits.d @@ -1943,7 +1943,12 @@ Expression semanticTraits(TraitsExp e, Scope* sc) return ErrorExp.get(); } - Type tb = t.baseElemOf(); + // https://issues.dlang.org/show_bug.cgi?id=23534 + // + // For enums, we need to get the enum initializer + // (the first enum member), not the initializer of the + // type of the enum members. + Type tb = t.isTypeEnum ? t : t.baseElemOf(); return tb.isZeroInit(e.loc) ? True() : False(); } if (e.ident == Id.getTargetInfo) diff --git a/compiler/test/compilable/test23534.d b/compiler/test/compilable/test23534.d new file mode 100644 index 000000000000..f76a692b8b5a --- /dev/null +++ b/compiler/test/compilable/test23534.d @@ -0,0 +1,6 @@ +// https://issues.dlang.org/show_bug.cgi?id=23534 + +enum E { a = 1, b = 2 } + +// `E.a` is not 0 +static assert(!__traits(isZeroInit, E));