Skip to content

Commit

Permalink
fix Issue 15126 - dmd crashes when analyzing array literal
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Sep 29, 2015
1 parent 5d2a7bc commit c4b80e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,20 @@ extern (C++) bool arrayExpressionToCommonType(Scope* sc, Expressions* exps, Type
* which works if the array literal is initialized top down with the ubyte[][]
* type, but fails with this function doing bottom up typing.
*/

//printf("arrayExpressionToCommonType()\n");
scope IntegerExp integerexp = new IntegerExp(0);
scope CondExp condexp = new CondExp(Loc(), integerexp, null, null);
Type t0 = null;
Expression e0 = null; // dead-store to prevent spurious warning
size_t j0 = ~0; // dead-store to prevent spurious warning
Expression e0 = null;
size_t j0 = ~0;

for (size_t i = 0; i < exps.dim; i++)
{
Expression e = (*exps)[i];
if (!e)
continue;

e = resolveProperties(sc, e);
if (!e.type)
{
Expand All @@ -1057,7 +1062,9 @@ extern (C++) bool arrayExpressionToCommonType(Scope* sc, Expressions* exps, Type
t0 = Type.terror;
continue;
}

e = e.isLvalue() ? callCpCtor(sc, e) : valueNoDtor(e);

if (t0 && !t0.equals(e.type))
{
/* This applies ?: to merge the types. It's backwards;
Expand Down Expand Up @@ -1089,6 +1096,9 @@ extern (C++) bool arrayExpressionToCommonType(Scope* sc, Expressions* exps, Type
for (size_t i = 0; i < exps.dim; i++)
{
Expression e = (*exps)[i];
if (!e)
continue;

e = e.implicitCastTo(sc, t0);
//assert(e->op != TOKerror);
if (e.op == TOKerror)
Expand Down Expand Up @@ -4690,17 +4700,27 @@ public:
}
if (type)
return this;

/* Perhaps an empty array literal [ ] should be rewritten as null?
*/
if (arrayExpressionSemantic(elements, sc)) // run semantic() on each element

if (basis)
basis = basis.semantic(sc);
if (arrayExpressionSemantic(elements, sc) || (basis && basis.op == TOKerror))
return new ErrorExp();
expandTuples(elements);

Type t0;
if (arrayExpressionToCommonType(sc, elements, &t0))
if (basis)
elements.push(basis);
bool err = arrayExpressionToCommonType(sc, elements, &t0);
if (basis)
basis = elements.pop();
if (err)
return new ErrorExp();
type = t0.arrayOf();
//type = new TypeSArray(t0, new IntegerExp(elements->dim));
type = type.semantic(loc, sc);

/* Disallow array literals of type void being used.
*/
if (elements.dim > 0 && t0.ty == Tvoid)
Expand Down
17 changes: 17 additions & 0 deletions test/runnable/xtest46.d
Original file line number Diff line number Diff line change
Expand Up @@ -7659,6 +7659,23 @@ void test15045()
test!(S3, false)();
}

/***************************************************/
// 15126

struct Json15126
{
ubyte[16] m_data;
int opDispatch(string prop)() const { return 0; }
int opDispatch(string prop)() { return 0; }
}

template isCustomSerializable15126(T)
{
enum isCustomSerializable15126 = T.init.toRepresentation();
}

alias bug15126 = isCustomSerializable15126!Json15126;

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

int main()
Expand Down

0 comments on commit c4b80e7

Please sign in to comment.