From ec071370017911d5984675c03c28e9b7603c3b14 Mon Sep 17 00:00:00 2001 From: Razvan Nitu Date: Wed, 22 Mar 2023 23:37:15 +0800 Subject: [PATCH] Fix Issue 20908 - -preview=nosharedaccess requires zero-initializion for aggregates (#15023) --- compiler/src/dmd/expressionsem.d | 6 ++++ compiler/test/compilable/shared.d | 54 +++++++++++++++++++------------ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 1f4f57b1f3..45dcb9739f 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -13375,6 +13375,12 @@ bool checkSharedAccess(Expression e, Scope* sc, bool returnRef = false) bool visitVar(VarExp e) { + // https://issues.dlang.org/show_bug.cgi?id=20908 + // direct access to init symbols is ok as they + // cannot be modified. + if (e.var.isSymbolDeclaration()) + return false; + // https://issues.dlang.org/show_bug.cgi?id=22626 // Synchronized functions don't need to use core.atomic // when accessing `this`. diff --git a/compiler/test/compilable/shared.d b/compiler/test/compilable/shared.d index bd5c7ffe95..647910ecf1 100644 --- a/compiler/test/compilable/shared.d +++ b/compiler/test/compilable/shared.d @@ -11,34 +11,48 @@ ref shared(int) f(return shared ref int y) } // https://issues.dlang.org/show_bug.cgi?id=20908 +struct S +{ + int i = 2; +} + +union U +{ + int i = 1; + bool b; +} + void test20908() { - // shared locals (or struct members) should be able to be initialised: - shared int x; + // shared locals (or struct members) should be able to be initialised: + shared int x; - ref shared(int) fun() - { - static shared(int) val; + ref shared(int) fun() + { + static shared(int) val; - // return by reference - return val; - } + // return by reference + return val; + } - ref shared(int) fun2() - { - static shared(int)* val; + ref shared(int) fun2() + { + static shared(int)* val; - // transfer pointer to reference - return *val; - } + // transfer pointer to reference + return *val; + } - ref shared(int) fun3() - { - static shared(int)*** val; + ref shared(int) fun3() + { + static shared(int)*** val; - // Multiple indirections - return ***val; - } + // Multiple indirections + return ***val; + } + + shared S s; + shared U u; } // Simple tests for `DotVarExp`