Bug 21190 – generated strings should convert to immutable char *

Status
NEW
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-22T21:13:47Z
Last change time
2024-12-13T19:11:14Z
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: dmd#17984 →

Comments

Comment #0 by schveiguy — 2020-08-22T21:13:47Z
When a string is generated at compile time (and assigned to an enum), it should be equivalent to a string literal in all cases. This works today: enum x = "hello"; const char *p = x; enum x2 = "hello" ~ " there"; p = x2; static string foo() { return "hello" ~ " there"; } enum x3 = foo(); p = x3; This does not: static string foo2() { import std.string : join; return ["hello", "there"].join(" "); } enum x4 = foo2(); p = x4; // Error: cannot implicitly convert expression "hello there" of type string to const(char*) It's unclear why foo2 cannot produce a convertible string, whereas all the other cases can.
Comment #1 by razvan.nitu1305 — 2020-09-17T07:55:04Z
(In reply to Steven Schveighoffer from comment #0) > When a string is generated at compile time (and assigned to an enum), it > should be equivalent to a string literal in all cases. > > This works today: > > enum x = "hello"; > const char *p = x; > enum x2 = "hello" ~ " there"; > p = x2; > static string foo() > { > return "hello" ~ " there"; > } > > enum x3 = foo(); > p = x3; > > This does not: > > static string foo2() > { > import std.string : join; > return ["hello", "there"].join(" "); > } > > enum x4 = foo2(); > p = x4; // Error: cannot implicitly convert expression "hello there" of type > string to const(char*) > > It's unclear why foo2 cannot produce a convertible string, whereas all the > other cases can. Are you sure this is the correct code? I just tried running the following example: enum x = "hello"; const char *p = x; enum x2 = "hello" ~ " there"; static string foo() { return "hello" ~ " there"; } enum x3 = foo(); static string foo2() { import std.string : join; return ["hello", "there"].join(" "); } enum x4 = foo2(); void goo() { p = x2; p = x3; p = x4; } And I get errors to all three assignments of p.
Comment #2 by schveiguy — 2020-09-17T19:50:14Z
oof! Yeah, it's not valid, but the bug is still valid. Change p declaration to: const(char)* p = x; and you now see the single error for the assignment to x4.
Comment #3 by razvan.nitu1305 — 2020-09-18T02:41:23Z
(In reply to Steven Schveighoffer from comment #2) > oof! Yeah, it's not valid, but the bug is still valid. Change p declaration > to: > > const(char)* p = x; > > and you now see the single error for the assignment to x4. Yeah, it's definitely a bug. And a weird one also.
Comment #4 by robert.schadek — 2024-12-13T19:11:14Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17984 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB