Skip to content

Commit

Permalink
fix Issue 15778 - polysemous string type doesn't work in array operation
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Mar 9, 2016
1 parent 83be084 commit f384997
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/expression.d
Expand Up @@ -12428,7 +12428,18 @@ public:
e2x = e2x.castTo(sc, e1.type.constOf());
}
else
e2x = e2x.implicitCastTo(sc, e1.type);
{
/* Bugzilla 15778: A string literal has an array type of immutable
* elements by default, and normally it cannot be convertible to
* array type of mutable elements. But for element-wise assignment,
* elements need to be const at best. So we should give a chance
* to change code unit size for polysemous string literal.
*/
if (e2x.op == TOKstring)
e2x = e2x.implicitCastTo(sc, e1.type.constOf());
else
e2x = e2x.implicitCastTo(sc, e1.type);
}
}
else
{
Expand Down
25 changes: 25 additions & 0 deletions test/runnable/implicit.d
Expand Up @@ -436,6 +436,30 @@ struct S13640
}
}

/***********************************/
// 15778

void test15778()
{
char[] cs = new char[](3);
wchar[] ws = new wchar[](3);
dchar[] ds = new dchar[](3);

cs[] = "abc"; assert(cs == "abc");
cs[] = "def"c; assert(cs == "def");
ws[] = "abc"; assert(ws == "abc");
ws[] = "def"w; assert(ws == "def");
ds[] = "abc"; assert(ds == "abc");
ds[] = "def"d; assert(ds == "def");

static assert(!__traits(compiles, (cs[] = "a"w)));
static assert(!__traits(compiles, (cs[] = "a"d)));
static assert(!__traits(compiles, (ws[] = "a"c)));
static assert(!__traits(compiles, (ws[] = "a"d)));
static assert(!__traits(compiles, (ds[] = "a"c)));
static assert(!__traits(compiles, (ds[] = "a"w)));
}

/***********************************/

void main()
Expand All @@ -451,6 +475,7 @@ void main()
testDIP29_4();
testDIP29_5();
testDIP29_6();
test15778();

writefln("Success");
}

0 comments on commit f384997

Please sign in to comment.