From a699cf98033e3ba16820ca80e2219246eca4c75d Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Thu, 27 Jul 2023 05:59:29 -0700 Subject: [PATCH] make new safety checks warnings when using default feature setting (#15411) --- compiler/src/dmd/func.d | 8 +++++--- .../fail_compilation/dip1000_deprecation.d | 20 +++++++++---------- compiler/test/fail_compilation/fail_scope.d | 4 ++-- compiler/test/fail_compilation/test13536.d | 4 ++-- compiler/test/fail_compilation/test16365.d | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/compiler/src/dmd/func.d b/compiler/src/dmd/func.d index a1c88b2ca6..5cd3467b73 100644 --- a/compiler/src/dmd/func.d +++ b/compiler/src/dmd/func.d @@ -4606,6 +4606,7 @@ bool setUnsafe(Scope* sc, bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char)* msg, RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null) { + //printf("setUnsafePreview() fs:%d %s\n", fs, msg); with (FeatureState) final switch (fs) { case disabled: @@ -4620,9 +4621,10 @@ bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char) if (sc.func.isSafeBypassingInference()) { if (!gag) - previewErrorFunc(sc.isDeprecated(), fs)( - loc, msg, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "" - ); + { + if (!sc.isDeprecated() && global.params.obsolete) + warning(loc, msg, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : ""); + } } else if (!sc.func.safetyViolation) { diff --git a/compiler/test/fail_compilation/dip1000_deprecation.d b/compiler/test/fail_compilation/dip1000_deprecation.d index 61174395be..e591a1428a 100644 --- a/compiler/test/fail_compilation/dip1000_deprecation.d +++ b/compiler/test/fail_compilation/dip1000_deprecation.d @@ -1,5 +1,5 @@ /* -REQUIRED_ARGS: -de +REQUIRED_ARGS: -de -wo TEST_OUTPUT: --- fail_compilation/dip1000_deprecation.d(20): Deprecation: `@safe` function `main` calling `inferred` @@ -9,17 +9,17 @@ fail_compilation/dip1000_deprecation.d(22): Deprecation: `@safe` function `main` fail_compilation/dip1000_deprecation.d(39): which calls `dip1000_deprecation.inferred` fail_compilation/dip1000_deprecation.d(28): which wouldn't be `@safe` because of: fail_compilation/dip1000_deprecation.d(28): scope variable `x0` may not be returned -fail_compilation/dip1000_deprecation.d(54): Deprecation: escaping reference to stack allocated value returned by `S(null)` -fail_compilation/dip1000_deprecation.d(55): Deprecation: escaping reference to stack allocated value returned by `createS()` -fail_compilation/dip1000_deprecation.d(58): Deprecation: returning `s.incorrectReturnRef()` escapes a reference to local variable `s` +fail_compilation/dip1000_deprecation.d(54): Warning: escaping reference to stack allocated value returned by `S(null)` +fail_compilation/dip1000_deprecation.d(55): Warning: escaping reference to stack allocated value returned by `createS()` +fail_compilation/dip1000_deprecation.d(58): Warning: returning `s.incorrectReturnRef()` escapes a reference to local variable `s` --- */ void main() @safe { - inferred(); - inferredB(); // no deprecation, trusted - inferredC(); // nested deprecation + cast(void)inferred(); + cast(void)inferredB(); // no deprecation, trusted + cast(void)inferredC(); // nested deprecation } auto inferred() @@ -49,10 +49,10 @@ struct S S createS() { return S.init; } -int* escape() +int* escape(int i) { - return S().incorrectReturnRef(); - return createS().incorrectReturnRef(); + if (i) return S().incorrectReturnRef(); + if (i) return createS().incorrectReturnRef(); S s; return s.incorrectReturnRef(); diff --git a/compiler/test/fail_compilation/fail_scope.d b/compiler/test/fail_compilation/fail_scope.d index f209592196..8508b27d16 100644 --- a/compiler/test/fail_compilation/fail_scope.d +++ b/compiler/test/fail_compilation/fail_scope.d @@ -1,5 +1,5 @@ /* -REQUIRED_ARGS: +REQUIRED_ARGS: -wo TEST_OUTPUT: --- fail_compilation/fail_scope.d(30): Deprecation: scope parameter `da` may not be returned @@ -16,7 +16,7 @@ fail_compilation/fail_scope.d(82): Error: returning `& string` escapes a referen fail_compilation/fail_scope.d(92): Error: returning `cast(int[])a` escapes a reference to local variable `a` fail_compilation/fail_scope.d(100): Error: returning `cast(int[])a` escapes a reference to local variable `a` fail_compilation/fail_scope.d(108): Error: escaping reference to outer local variable `x` -fail_compilation/fail_scope.d(127): Deprecation: returning `s.bar()` escapes a reference to local variable `s` +fail_compilation/fail_scope.d(127): Warning: returning `s.bar()` escapes a reference to local variable `s` fail_compilation/fail_scope.d(137): Error: returning `foo16226(i)` escapes a reference to local variable `i` --- //fail_compilation/fail_scope.d(35): Error: scope variable `da` may not be returned diff --git a/compiler/test/fail_compilation/test13536.d b/compiler/test/fail_compilation/test13536.d index f4e2cac93c..4a4bb2672b 100644 --- a/compiler/test/fail_compilation/test13536.d +++ b/compiler/test/fail_compilation/test13536.d @@ -1,8 +1,8 @@ -/* +/* REQUIRED_ARGS: -wo TEST_OUTPUT: --- fail_compilation/test13536.d(23): Error: field `U.sysDg` cannot access pointers in `@safe` code that overlap other fields -fail_compilation/test13536.d(23): Deprecation: address of variable `s` assigned to `u` with longer lifetime +fail_compilation/test13536.d(23): Warning: address of variable `s` assigned to `u` with longer lifetime fail_compilation/test13536.d(24): Error: field `U.safeDg` cannot access pointers in `@safe` code that overlap other fields --- */ diff --git a/compiler/test/fail_compilation/test16365.d b/compiler/test/fail_compilation/test16365.d index c987969987..4d49365659 100644 --- a/compiler/test/fail_compilation/test16365.d +++ b/compiler/test/fail_compilation/test16365.d @@ -1,9 +1,9 @@ -/* +/* REQUIRED_ARGS: -wo TEST_OUTPUT: --- fail_compilation/test16365.d(21): Error: `this` reference necessary to take address of member `f1` in `@safe` function `main` fail_compilation/test16365.d(23): Error: cannot implicitly convert expression `&f2` of type `void delegate() pure nothrow @nogc @safe` to `void function() @safe` -fail_compilation/test16365.d(27): Deprecation: address of variable `s` assigned to `dg` with longer lifetime +fail_compilation/test16365.d(27): Warning: address of variable `s` assigned to `dg` with longer lifetime fail_compilation/test16365.d(28): Error: `dg.funcptr` cannot be used in `@safe` code --- */