mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-01-11 18:33:14 +01:00
Added checks for overlapping union initializers, as shown in bug #259 .
This commit is contained in:
@@ -256,9 +256,11 @@ LLConstant * IrStruct::createStructInitializer(StructInitializer * si)
|
||||
{
|
||||
VarDeclaration* vd = (VarDeclaration*)si->vars.data[i];
|
||||
Initializer* ini = (Initializer*)si->value.data[i];
|
||||
Loc loc = ini ? ini->loc : si->loc;
|
||||
|
||||
size_t idx = datamap[i];
|
||||
|
||||
// check for duplicate initialization
|
||||
if (data[idx].first != NULL)
|
||||
{
|
||||
Loc l = ini ? ini->loc : si->loc;
|
||||
@@ -266,6 +268,23 @@ LLConstant * IrStruct::createStructInitializer(StructInitializer * si)
|
||||
continue;
|
||||
}
|
||||
|
||||
// check for overlapping initialization
|
||||
for (size_t j = 0; j < i; j++)
|
||||
{
|
||||
size_t idx2 = datamap[j];
|
||||
assert(data[idx2].first);
|
||||
|
||||
VarDeclarationIter it(aggrdecl->fields, idx2);
|
||||
|
||||
unsigned f_begin = it->offset;
|
||||
unsigned f_end = f_begin + it->type->size();
|
||||
|
||||
if (vd->offset >= f_end || (vd->offset + vd->type->size()) <= f_begin)
|
||||
continue;
|
||||
|
||||
error(loc, "initializer for %s overlaps previous initialization of %s", vd->toChars(), it->toChars());
|
||||
}
|
||||
|
||||
IF_LOG Logger::println("Explicit initializer: %s @+%u", vd->toChars(), vd->offset);
|
||||
LOG_SCOPE;
|
||||
|
||||
|
||||
8
tests/mini/nocompile_initoverlap1.d
Normal file
8
tests/mini/nocompile_initoverlap1.d
Normal file
@@ -0,0 +1,8 @@
|
||||
struct Vector {
|
||||
union { float x; float y; }
|
||||
const static Vector zero = { x : 0, y : 0 };
|
||||
}
|
||||
|
||||
struct HBoxLayout {
|
||||
Vector padding = Vector.zero;
|
||||
}
|
||||
Reference in New Issue
Block a user