Skip to content

Commit d3091c2

Browse files
kinkedlang-bot
authored andcommitted
Fix Issue 21464 - Purity check depending on semantic order
Accessing a mutable static but empty struct is pure. The check relied on `StructDeclaration.hasNoFields` but didn't make sure the struct size has already been determined.
1 parent 19658df commit d3091c2

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/dmd/expression.d

+7-2
Original file line numberDiff line numberDiff line change
@@ -1269,11 +1269,16 @@ extern (C++) abstract class Expression : ASTNode
12691269
if (v.storage_class & STC.manifest)
12701270
return false; // ...or manifest constants
12711271

1272+
// accessing empty structs is pure
12721273
if (v.type.ty == Tstruct)
12731274
{
12741275
StructDeclaration sd = (cast(TypeStruct)v.type).sym;
1275-
if (sd.hasNoFields)
1276-
return false;
1276+
if (sd.members) // not opaque
1277+
{
1278+
sd.determineSize(v.loc);
1279+
if (sd.hasNoFields)
1280+
return false;
1281+
}
12771282
}
12781283

12791284
bool err = false;

test/compilable/imports/test21464a.d

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Mallocator
2+
{
3+
static shared Mallocator instance;
4+
}

test/compilable/test21464.d

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=21464
2+
void foo() pure
3+
{
4+
import imports.test21464a : Mallocator;
5+
auto a = Mallocator.instance; // mutable static, but empty struct
6+
}

0 commit comments

Comments
 (0)