Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3336 from MoonlightSentinel/checkaction-enum
Browse files Browse the repository at this point in the history
Fix 21544 - -checkaction=context formats enum members as their base type
  • Loading branch information
RazvanN7 committed Jan 25, 2021
2 parents 13e358e + 02dd9d9 commit c1a3fe3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/core/internal/dassert.d
Expand Up @@ -152,6 +152,21 @@ private string miniFormat(V)(const scope ref V v)
return miniFormat(*cast(T*) &v);
}
}
// Format enum members using their name
else static if (is(V BaseType == enum))
{
// Always generate repeated if's instead of switch to skip the detection
// of non-integral enums. This method doesn't need to be fast.
static foreach (mem; __traits(allMembers, V))
{
if (v == __traits(getMember, V, mem))
return mem;
}

// Format invalid enum values as their base type
enum cast_ = "cast(" ~ V.stringof ~ ")";
return combine(cast_, "", miniFormat(*(cast(BaseType*) &v)));
}
else static if (is(V == bool))
{
return v ? "true" : "false";
Expand Down Expand Up @@ -383,3 +398,33 @@ private auto pureAlloc(size_t t)
}
return assumeFakeAttributes(&alloc)(t);
}

// https://issues.dlang.org/show_bug.cgi?id=21544
unittest
{
// Normal enum values
enum E { A, BCDE }
E e = E.A;
assert(miniFormat(e) == "A");
e = E.BCDE;
assert(miniFormat(e) == "BCDE");

// Invalid enum value is printed as their implicit base type (int)
e = cast(E) 3;
assert(miniFormat(e) == "cast(E) 3");

// Non-integral enums work as well
static struct S
{
int a;
string str;
}

enum E2 : S { a2 = S(1, "Hello") }
E2 es = E2.a2;
assert(miniFormat(es) == `a2`);

// Even invalid values
es = cast(E2) S(2, "World");
assert(miniFormat(es) == `cast(E2) S(2, "World")`);
}

0 comments on commit c1a3fe3

Please sign in to comment.