Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
maxhaton opened this issue May 1, 2022 · 3 comments
Open

Comments

@maxhaton
Copy link
Collaborator

maxhaton commented May 1, 2022

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

sdc complains, dmd does not.

@deadalnix
Copy link
Contributor

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.

@maxhaton
Copy link
Collaborator Author

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.

@deadalnix
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants