diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 106519813a..f4109f96b1 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -893,8 +893,10 @@ void DtoResolveVariable(VarDeclaration *vd) { // If a const/immutable value has a proper initializer (not "= void"), // it cannot be assigned again in a static constructor. Thus, we can // emit it as read-only data. - const bool isLLConst = (vd->isConst() || vd->isImmutable()) && vd->_init && - !vd->_init->isVoidInitializer(); + // We also do so for forward-declared (extern) globals, just like clang. + const bool isLLConst = (vd->isConst() || vd->isImmutable()) && + ((vd->_init && !vd->_init->isVoidInitializer()) || + (vd->storage_class & STCextern)); assert(!vd->ir->isInitialized()); if (gIR->dmodule) { diff --git a/tests/compilable/gh2849.d b/tests/compilable/gh2849.d new file mode 100644 index 0000000000..c440f80a4d --- /dev/null +++ b/tests/compilable/gh2849.d @@ -0,0 +1,8 @@ +// RUN: %ldc -c %s + +extern(C++): + +__gshared const int dblreg = 1; + +pragma(mangle, dblreg.mangleof) +extern __gshared const int bla;