From 6439810ca19139674de75dae647fc415cfb5f5c4 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sat, 16 Jul 2022 11:14:19 +0200 Subject: [PATCH 1/4] fix Issue 11048 - Default arguments bypass most attributes check (pure, @safe, @nogc) --- compiler/src/dmd/chkformat.d | 4 ++-- compiler/src/dmd/expressionsem.d | 4 +++- compiler/test/fail_compilation/fail11048a.d | 22 +++++++++++++++++++++ druntime/src/core/atomic.d | 4 ++-- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 compiler/test/fail_compilation/fail11048a.d diff --git a/compiler/src/dmd/chkformat.d b/compiler/src/dmd/chkformat.d index 0788991d0b85..468019b806dd 100644 --- a/compiler/src/dmd/chkformat.d +++ b/compiler/src/dmd/chkformat.d @@ -526,7 +526,7 @@ private: * Format */ Format parseScanfFormatSpecifier(scope const char[] format, ref size_t idx, - out bool asterisk) nothrow pure @safe + out bool asterisk) nothrow @trusted { auto i = idx; assert(format[i] == '%'); @@ -634,7 +634,7 @@ Format parseScanfFormatSpecifier(scope const char[] format, ref size_t idx, * Format */ Format parsePrintfFormatSpecifier(scope const char[] format, ref size_t idx, - out bool widthStar, out bool precisionStar) nothrow pure @safe + out bool widthStar, out bool precisionStar) nothrow @trusted { auto i = idx; assert(format[i] == '%'); diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index b3633eba9d7d..194ae78c64fa 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -1755,9 +1755,11 @@ private bool functionParameters(const ref Loc loc, Scope* sc, return errorArgs(); } arg = p.defaultArg; - arg = inlineCopy(arg, sc); + arg = arg.syntaxCopy(); // __FILE__, __LINE__, __MODULE__, __FUNCTION__, and __PRETTY_FUNCTION__ arg = arg.resolveLoc(loc, sc); + arg = arg.expressionSemantic(sc); + arg = resolveProperties(sc, arg); arguments.push(arg); nargs++; } diff --git a/compiler/test/fail_compilation/fail11048a.d b/compiler/test/fail_compilation/fail11048a.d new file mode 100644 index 000000000000..e835ca8b4715 --- /dev/null +++ b/compiler/test/fail_compilation/fail11048a.d @@ -0,0 +1,22 @@ +// https://issues.dlang.org/show_bug.cgi?id=11048 +/* TEST_OUTPUT: +--- +fail_compilation/fail11048a.d(15): Error: `pure` function `fail11048a.foo` cannot access mutable static data `x` +fail_compilation/fail11048a.d(17): Error: `pure` function `fail11048a.foo` cannot access mutable static data `x` +fail_compilation/fail11048a.d(17): Error: function `fail11048a.baz(int a)` is not callable using argument types `(_error_)` +fail_compilation/fail11048a.d(17): cannot pass argument `__error` of type `_error_` to parameter `int a` +--- +*/ +int x = 7; + +void foo() pure +{ + // Does not detect use of mutable global and compiles when it shouldn't. + bar(); + // Correctly detects the use of a mutable global and gives an error + baz(x); +} + +void bar(int a = x) pure {} + +void baz(int a) pure {} diff --git a/druntime/src/core/atomic.d b/druntime/src/core/atomic.d index 4af3fdf2bd29..69efd140d3d2 100644 --- a/druntime/src/core/atomic.d +++ b/druntime/src/core/atomic.d @@ -929,8 +929,6 @@ version (CoreUnittest) @safe pure nothrow unittest { - testType!(shared int*)(); - static interface Inter {} static class KlassImpl : Inter {} testXCHG!(shared Inter)(new shared(KlassImpl)); @@ -1000,6 +998,8 @@ version (CoreUnittest) @betterC pure nothrow unittest { + testType!(shared int*)(); + static if (has128BitCAS) { struct DoubleValue From 68457fd61d6441f69a57bc32a6e8fb3fa4a4119a Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sat, 16 Jul 2022 12:38:06 +0200 Subject: [PATCH 2/4] Add test for Issue 2437 --- compiler/test/compilable/test2437.d | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 compiler/test/compilable/test2437.d diff --git a/compiler/test/compilable/test2437.d b/compiler/test/compilable/test2437.d new file mode 100644 index 000000000000..072f816ae80c --- /dev/null +++ b/compiler/test/compilable/test2437.d @@ -0,0 +1,22 @@ +// https://issues.dlang.org/show_bug.cgi?id=2437 + +struct S2437 +{ + int m; + + this(int a) + { + m = a; + } +} + +class C2437 +{ + void fun(S2437 a = S2437(44)) { } +} + +void main() +{ + C2437 a = new C2437(); + a.fun(); +} From 31c58cb7f4a97ad30dd4f5313a068b6cd7624382 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sat, 16 Jul 2022 12:38:32 +0200 Subject: [PATCH 3/4] Add test for Issue 2935 --- compiler/test/compilable/test2935.d | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 compiler/test/compilable/test2935.d diff --git a/compiler/test/compilable/test2935.d b/compiler/test/compilable/test2935.d new file mode 100644 index 000000000000..339e3122252b --- /dev/null +++ b/compiler/test/compilable/test2935.d @@ -0,0 +1,14 @@ +// https://issues.dlang.org/show_bug.cgi?id=2935 + +struct S2935 +{ + int z; + this(int a) { z = a; } +} + +void test2935(S2935 a = S2935(1)) { } + +void main() +{ + test2935(); +} From feaa8dd41398316afab5ea0e95ed7f2989e43583 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sat, 16 Jul 2022 13:12:59 +0200 Subject: [PATCH 4/4] Add test for Issue 13442 --- compiler/test/fail_compilation/fail13442.d | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 compiler/test/fail_compilation/fail13442.d diff --git a/compiler/test/fail_compilation/fail13442.d b/compiler/test/fail_compilation/fail13442.d new file mode 100644 index 000000000000..54d6c5d20ac6 --- /dev/null +++ b/compiler/test/fail_compilation/fail13442.d @@ -0,0 +1,16 @@ +// https://issues.dlang.org/show_bug.cgi?id=13442 +/* TEST_OUTPUT: +--- +fail_compilation/fail13442.d(15): Error: `@safe` function `main` cannot access `__gshared` data `var` +--- +*/ +__gshared int var; + +void f(int i = var) @safe +{ +} + +void main() @safe +{ + f(); +}