Skip to content

Commit 40db340

Browse files
committed
fix #18202 - string literals used as pointers aren't pooled/merged
1 parent f5a2e5c commit 40db340

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

‎compiler/src/dmd/glue/e2ir.d‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ package(dmd.glue)
216216
Symbol* toStringSymbol(const(char)* str, size_t len, size_t sz)
217217
{
218218
//printf("toStringSymbol() %s\n", str);
219-
auto sv = stringTab.update(str, len * sz);
219+
auto sv = stringTab[sz].update(str, len * sz);
220220
if (sv.value)
221221
return sv.value;
222222

@@ -479,15 +479,20 @@ package(dmd.glue)
479479
void clearStringTab()
480480
{
481481
//printf("clearStringTab()\n");
482-
if (stringTab)
483-
stringTab.reset(1000); // 1000 is arbitrary guess
484-
else
482+
foreach (sz; [1, 2, 4])
485483
{
486-
stringTab = new StringTable!(Symbol*)();
487-
stringTab._init(1000);
484+
if (stringTab[sz])
485+
stringTab[sz].reset(1000); // 1000 is arbitrary guess
486+
else
487+
{
488+
stringTab[sz] = new StringTable!(Symbol*)();
489+
stringTab[sz]._init(1000);
490+
}
488491
}
489492
}
490-
private __gshared StringTable!(Symbol*) *stringTab;
493+
// One pool per code unit size (1, 2, 4): the same bytes at a different width
494+
// need a terminator of a different width, so they can't share a pool.
495+
private __gshared StringTable!(Symbol*)*[5] stringTab;
491496

492497
/*********************************************
493498
* Figure out whether a data symbol should be dllimported
@@ -1221,24 +1226,8 @@ elem* toElem(Expression e, ref IRState irs)
12211226
}
12221227
else if (tb.ty == Tpointer)
12231228
{
1224-
if (config.objfmt == OBJ_MACH && target.isAArch64)
1225-
{
1226-
Symbol* si = toStringSymbol(se);
1227-
e = el_ptr(si);
1228-
}
1229-
else
1230-
{
1231-
e = el_calloc();
1232-
e.Eoper = OPstring;
1233-
// freed in el_free
1234-
string s;
1235-
const n = cast(int)se.numberOfCodeUnits(0, s);
1236-
const len = cast(size_t)((n + 1) * se.sz);
1237-
e.Vstring = cast(char *)mem_malloc2(cast(uint) len);
1238-
se.writeTo(e.Vstring, true);
1239-
e.Vstrlen = len;
1240-
e.Ety = TYnptr;
1241-
}
1229+
Symbol* si = toStringSymbol(se);
1230+
e = el_ptr(si);
12421231
}
12431232
else
12441233
{
@@ -3989,6 +3978,13 @@ elem* toElem(Expression e, ref IRState irs)
39893978

39903979
//printf("IndexExp.toElem() %s\n", ie.toChars());
39913980
Type t1 = ie.e1.type.toBasetype();
3981+
3982+
if (t1.isTypeNoreturn())
3983+
{
3984+
elem_setLoc(n1, ie.loc);
3985+
return n1;
3986+
}
3987+
39923988
if (auto taa = t1.isTypeAArray())
39933989
{
39943990
assert(false, "no index lowering for associative array literal");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://github.com/dlang/dmd/issues/18202
2+
3+
void main()
4+
{
5+
string s = "hello";
6+
immutable(char)* p = "hello".ptr;
7+
assert(s.ptr == p);
8+
9+
// pooled literals keep a terminator of their own code unit size
10+
immutable(char)* ec = "".ptr;
11+
immutable(wchar)* ew = ""w.ptr;
12+
immutable(dchar)* ed = ""d.ptr;
13+
assert(ec[0] == 0);
14+
assert(ew[0] == 0);
15+
assert(ed[0] == 0);
16+
}

0 commit comments

Comments
 (0)