Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions compiler/src/dmd/glue/e2ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ package(dmd.glue)
Symbol* toStringSymbol(const(char)* str, size_t len, size_t sz)
{
//printf("toStringSymbol() %s\n", str);
auto sv = stringTab.update(str, len * sz);
import dmd.common.outbuffer : OutBuffer;
OutBuffer key;
key.setsize(0);
key.writeByte(cast(ubyte)sz);
key.write(str[0 .. len * sz]);
auto sv = stringTab.update(cast(const(char)*)key[].ptr, key.length);
if (sv.value)
return sv.value;

Expand Down Expand Up @@ -1221,24 +1226,8 @@ elem* toElem(Expression e, ref IRState irs)
}
else if (tb.ty == Tpointer)
{
if (config.objfmt == OBJ_MACH && target.isAArch64)
{
Symbol* si = toStringSymbol(se);
e = el_ptr(si);
}
else
{
e = el_calloc();
e.Eoper = OPstring;
// freed in el_free
string s;
const n = cast(int)se.numberOfCodeUnits(0, s);
const len = cast(size_t)((n + 1) * se.sz);
e.Vstring = cast(char *)mem_malloc2(cast(uint) len);
se.writeTo(e.Vstring, true);
e.Vstrlen = len;
e.Ety = TYnptr;
}
Symbol* si = toStringSymbol(se);
e = el_ptr(si);
}
else
{
Expand Down
21 changes: 21 additions & 0 deletions compiler/test/runnable/issue18202.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://github.com/dlang/dmd/issues/18202

void main()
{
string s = "hello";
immutable(char)* p = "hello".ptr;
assert(s.ptr == p);

// pooled literals keep a terminator of their own code unit size
immutable(char)* ec = "".ptr;
immutable(wchar)* ew = ""w.ptr;
immutable(dchar)* ed = ""d.ptr;
assert(ec[0] == 0);
assert(ew[0] == 0);
assert(ed[0] == 0);

// a reinterpret cast gives the literal a code unit size wider than a
// dchar, so the pool must key on any size, not just 1/2/4
auto w8 = cast(long[1])"abcdefgh"c;
assert(w8[0] == 0x6867_6665_6463_6261);
Comment on lines +19 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this dependant on byte order?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is, thanks for catching, should I switch back to reading bytes back as char?

@zxan zxan Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace it with this?

    auto w8 = cast(long[1])"abcdefgh"c;
    auto w8b = cast(const(char)*)w8.ptr;
    foreach (i; 0 .. 8)
        assert(w8b[i] == 'a' + i);

}
Loading