Bug 22595 – Error: integer constant expression expected instead of `"Hello!\x0a"`

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-12-13T20:59:19Z
Last change time
2024-12-13T19:19:49Z
Assigned to
No Owner
Creator
rempas
Moved to GitHub: dmd#20022 →

Comments

Comment #0 by rempas — 2021-12-13T20:59:19Z
I'm trying to compile the following code: ``` extern (C) void _start() { const char* hello = "Hello!\n"; ulong len = 7; asm { mov RAX, 1; mov RDI, 1; mov RSI, hello; mov RDX, len; syscall; }; asm { mov RAX, 60; mov RDI, 0; syscall; }; } ``` and I am using the following command: `dmd test.d -betterC -Xcc="-nostdlib" -O -of=testd` This will compile normally if I use ldc2 but with dmd, it will give me the error message in the title. If I change `const char*` to `char*` (of course I will also have to cast the string literal) then it will compile normally. Any ideas? It doesn't make sense for this to be the behavior right?
Comment #1 by dkorpel — 2022-04-13T11:48:48Z
The problem is that the compiler tries to constant fold when `hello` is `const` (hence available at compile time), but directly assigning a string literal to a register is not supported yet. The best workaround is to define `const(char)* hello = "Hello!\n";`, meaning the string data is const, but the pointer is mutable.
Comment #2 by robert.schadek — 2024-12-13T19:19:49Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20022 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB