# HG changeset patch # User Christian Kamm # Date 1241342214 -7200 # Node ID ad41053c336eace028fd3a16355d1c15785eb6cb # Parent 875afb7a93b67286fd1dbefad6da0f59cfeb5a80 Make static int[] a = [1, 2]; a[0] = 4; not segfault by making the array data ptr a non-const global variable. diff -r 875afb7a93b6 -r ad41053c336e gen/arrays.cpp --- a/gen/arrays.cpp Sat May 02 20:42:58 2009 +0200 +++ b/gen/arrays.cpp Sun May 03 11:16:54 2009 +0200 @@ -317,7 +317,9 @@ return constarr; // for dynamic array we need to make a global with the data, so we have a pointer for the dynamic array - LLGlobalVariable* gvar = new LLGlobalVariable(constarr->getType(), true, LLGlobalValue::InternalLinkage, constarr, ".constarray", gIR->module); + // Important: don't make the gvar constant, since this const initializer might + // be used as an initializer for a static T[] - where modifying contents is allowed. + LLGlobalVariable* gvar = new LLGlobalVariable(constarr->getType(), false, LLGlobalValue::InternalLinkage, constarr, ".constarray", gIR->module); LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) }; LLConstant* gep = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); diff -r 875afb7a93b6 -r ad41053c336e gen/toir.cpp --- a/gen/toir.cpp Sat May 02 20:42:58 2009 +0200 +++ b/gen/toir.cpp Sun May 03 11:16:54 2009 +0200 @@ -2405,7 +2405,9 @@ return initval; // for dynamic arrays we need to put the initializer in a global, and build a constant dynamic array reference with the .ptr field pointing into this global - LLConstant* globalstore = new LLGlobalVariable(arrtype, true, LLGlobalValue::InternalLinkage, initval, ".dynarrayStorage", p->module); + // Important: don't make the global constant, since this const initializer might + // be used as an initializer for a static T[] - where modifying contents is allowed. + LLConstant* globalstore = new LLGlobalVariable(arrtype, false, LLGlobalValue::InternalLinkage, initval, ".dynarrayStorage", p->module); LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) }; LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, 2); diff -r 875afb7a93b6 -r ad41053c336e tests/mini/mutablearrayinit.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/mini/mutablearrayinit.d Sun May 03 11:16:54 2009 +0200 @@ -0,0 +1,9 @@ +void main() +{ + static int[] b = [1, 2]; + b[0] = 2; + + typedef int[] ia = [1,2]; + static ia a; + a[0] = 5; +} \ No newline at end of file