snazzy-d/sdc

String literals need to be able to decay to suitable pointer type.

maxhaton opened this issue · 3 comments

extern(C)
void puts(const char* x);
void main()
{
    puts("Hello World");
    const char* x = "Hello Again";
}

sdc complains, dmd does not.

Indeed. I have used .ptr explicitly but even that is not good. What's needed:

  • A CStringLiteral node in the IR.
  • The caster to be updated to be able to case StringLiterals to immutable(char)* (which then case case to const) generating a CStringLiteral node.
  • Properly materialize CStringLiterals node in the backend.

That would have been fairly challenging in the early days of SDC, but now that the caster got fairly fancy, it shouldn't be too hard.

A new node probably isn't the way to do this.

const char* xx = x ? "hello" : "world";
pragma(msg, typeof(x ? "e" : s));
//const char* x = x ? "e" : s;

These declarations are both initialized with strings but the later one is rejected by dmd. It's basically VRP on the null terminator.

There are a couple of problem with what DMD is doing, starting by the fact that there is absolutely no need for it. We probably still want a CStringLiteral node, and, if need be, do a deep rewrite of the expression.