Bug 23700 – ImportC: Missing examples of ImportC leveraging CTFE

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2023-02-12T15:33:21Z
Last change time
2023-02-21T09:35:40Z
Keywords
accepts-invalid, ImportC
Assigned to
No Owner
Creator
Iain Buclaw
Depends on
23689
See also
https://issues.dlang.org/show_bug.cgi?id=23689, https://issues.dlang.org/show_bug.cgi?id=23694

Comments

Comment #0 by ibuclaw — 2023-02-12T15:33:21Z
When compiling the test with `gcc -std=c11 -fsyntax-only` compilable/test22842.c:64:24: error: expression in static assertion is not constant 64 | _Static_assert(mint1() == -1, "1"); | ~~~~~~~~^~~~~ compilable/test22842.c: In function ‘mint3’: compilable/test22842.c:80:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 80 | unsigned x = (unsigned)(p) & 1; | ^ Test content: ``` // https://issues.dlang.org/show_bug.cgi?id=22876 int mint1() { int a = 0; // dmd gives 1, other compilers -1 // bug disappears if the parentheses around (a) are removed return - (a) - 1; } _Static_assert(mint1() == -1, "1"); ```
Comment #1 by ibuclaw — 2023-02-12T15:35:16Z
Another failing test: compilable/test23031.c:9:25: error: expression in static assertion is not constant 9 | _Static_assert("\x1"[0] == 1, "5"); | ~~~~~~~~~^~~~ compilable/test23031.c:10:26: error: expression in static assertion is not constant 10 | _Static_assert("\x20"[0] == 0x20, "6"); | ~~~~~~~~~~^~~~~~~ compilable/test23031.c:11:27: error: expression in static assertion is not constant 11 | _Static_assert("\x020"[0] == 0x20, "7"); | ~~~~~~~~~~~^~~~~~~ compilable/test23031.c:12:28: error: expression in static assertion is not constant 12 | _Static_assert("\x0020"[0] == 0x20, "8"); | ~~~~~~~~~~~~^~~~~~~ compilable/test23054.c:23:25: error: expression in static assertion is not constant 23 | _Static_assert(test1(1) == 2, "1"); | ~~~~~~~~~^~~~ compilable/test23054.c:24:25: error: expression in static assertion is not constant 24 | _Static_assert(test2(1) == 0, "2"); | ~~~~~~~~~^~~~ ``` /* https://issues.dlang.org/show_bug.cgi?id=23031 */ _Static_assert(sizeof("\x1") == 2, "1"); _Static_assert(sizeof("\x20") == 2, "2"); _Static_assert(sizeof("\x020") == 2, "3"); _Static_assert(sizeof("\x0020") == 2, "4"); _Static_assert("\x1"[0] == 1, "5"); _Static_assert("\x20"[0] == 0x20, "6"); _Static_assert("\x020"[0] == 0x20, "7"); _Static_assert("\x0020"[0] == 0x20, "8"); ```
Comment #2 by ibuclaw — 2023-02-15T16:26:17Z
Many more _Static_assert tests that fail with gcc, but pass with dmd. compilable/testcstuff1.c:113:25: error: expression in static assertion is not constant 113 | _Static_assert(f1(4, 2) == 7, "ok"); | ~~~~~~~~~^~~~ compilable/testcstuff1.c:131:22: error: expression in static assertion is not constant 131 | _Static_assert(s1(3) == 10, "s1(3) == 10"); | ~~~~~~^~~~~ compilable/testcstuff1.c:139:22: error: expression in static assertion is not constant 139 | _Static_assert(s2(3) == 10, "s2(3) == 10"); | ~~~~~~^~~~~ compilable/testcstuff1.c:150:22: error: expression in static assertion is not constant 150 | _Static_assert(s3(3) == 10, "s3(3) == 10"); | ~~~~~~^~~~~ compilable/testcstuff1.c:163:22: error: expression in static assertion is not constant 163 | _Static_assert(s4(1) == 1, "s4(1) == 1"); | ~~~~~~^~~~~ compilable/testcstuff1.c:164:22: error: expression in static assertion is not constant 164 | _Static_assert(s4(3) == 10, "s4(3) == 10"); | ~~~~~~^~~~~ compilable/testcstuff1.c:165:22: error: expression in static assertion is not constant 165 | _Static_assert(s4(5) == 11, "s4(5) == 11"); | ~~~~~~^~~~~ compilable/testcstuff1.c:175:22: error: expression in static assertion is not constant 175 | _Static_assert(s5(3) == 4, "s5(3) == 4"); | ~~~~~~^~~~ compilable/testcstuff1.c:176:22: error: expression in static assertion is not constant 176 | _Static_assert(s5(4) == 5, "s5(4) == 5"); | ~~~~~~^~~~
Comment #3 by ibuclaw — 2023-02-16T22:24:49Z
This is somewhat documented on https://dlang.org/spec/importc.html#ctfe Perhaps some examples of what ImportC can do vs. GCC.
Comment #4 by bugzilla — 2023-02-20T07:02:15Z
Indeed, CTFE is an ImportC extension. It's very useful for writing test cases like these.
Comment #5 by ibuclaw — 2023-02-20T11:33:49Z
(In reply to Walter Bright from comment #4) > Indeed, CTFE is an ImportC extension. It's very useful for writing test > cases like these. Updated title, the page really should give some quick examples.
Comment #6 by bugzilla — 2023-02-21T07:28:54Z
Comment #7 by ibuclaw — 2023-02-21T09:35:40Z
*** This issue has been marked as a duplicate of issue 23694 ***