Skip to content

Commit 228f8db

Browse files
authored
Improve fix: dlang#20867 ICE on final switch forward referencing its enum. (dlang#21097)
* Mark C enums as semantic2done to prevent segfaults in final switch * Added corresponding test case
1 parent 21cfefc commit 228f8db

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/src/dmd/importc.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,8 @@ void cEnumSemantic(Scope* sc, EnumDeclaration ed)
751751
}
752752

753753
ed.memtype = commonType;
754-
ed.semanticRun = PASS.semanticdone;
754+
// Set semantic2done to mark C enums as fully processed
755+
// Prevents issues with final switch statements that reference C enums
756+
ed.semanticRun = PASS.semantic2done;
755757
return;
756758
}

compiler/test/compilable/imports/cstuff3.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@ int squared(int a)
44
{
55
return a * a;
66
}
7+
8+
/* test case for issue #21094 */
9+
typedef enum upng_error {
10+
UPNG_EOK = 0, /* success (no error) */
11+
UPNG_ENOMEM = 1, /* memory allocation failed */
12+
UPNG_ENOTFOUND = 2, /* resource not found (file missing) */
13+
UPNG_ENOTPNG = 3, /* image data does not have a PNG header */
14+
UPNG_EMALFORMED = 4, /* image data is not a valid PNG image */
15+
UPNG_EUNSUPPORTED = 5, /* critical PNG chunk type is not supported */
16+
UPNG_EUNINTERLACED = 6, /* image interlacing is not supported */
17+
UPNG_EUNFORMAT = 7, /* image color format is not supported */
18+
UPNG_EPARAM = 8 /* invalid parameter to method call */
19+
} upng_error;

compiler/test/compilable/testcstuff3.d

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@
22
import imports.cstuff3;
33

44
static assert(squared(4) == 16);
5+
6+
/* test case for issue #21094 */
7+
string enum_to_str(E)(E v) if (is(E == enum))
8+
{
9+
final switch (v) with(E)
10+
{
11+
static foreach (m; __traits(allMembers, E))
12+
{
13+
case mixin(m):
14+
return m;
15+
}
16+
}
17+
}
18+
19+
void testEnumSwitch()
20+
{
21+
auto str = enum_to_str(UPNG_EOK);
22+
assert(str == "UPNG_EOK");
23+
}

0 commit comments

Comments
 (0)