Skip to content

Commit

Permalink
Add deprecation message for extern functions with default safety
Browse files Browse the repository at this point in the history
This message is needed to avoid silently introducing safety violations
to existing code when the default is changed from @System to @safe. It
should become an error when @safe is made the default.
  • Loading branch information
pbackus committed May 22, 2020
1 parent 93b1ca8 commit 4e3e27d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -4049,6 +4049,18 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
if (funcdecl.canInferAttributes(sc))
funcdecl.initInferAttributes();

/* Warn about external function declarations that may become incorrect
* when @safe is made the default.
*/
if (!funcdecl.fbody
&& funcdecl.storage_class == STC.extern_
&& funcdecl.type.toTypeFunction().trust == TRUST.default_)
{
deprecation(funcdecl.loc,
"`extern` function `%s` should be marked explicitly as `@safe`, `@system`, or `@trusted`",
funcdecl.toPrettyChars);
}

Module.dprogress++;
funcdecl.semanticRun = PASS.semanticdone;

Expand Down
2 changes: 1 addition & 1 deletion test/compilable/compile1.d
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ template test8163(T...)
}

enum N = 2; // N>=2 triggers the bug
extern Point[N] bar();
@system extern Point[N] bar();

void foo()
{
Expand Down
2 changes: 1 addition & 1 deletion test/compilable/test16031.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// REQUIRED_ARGS: -fPIC -lib
// PERMUTE_ARGS:
// DISABLED: win32 win64
extern void throwing();
@system extern void throwing();

void foo()
{
Expand Down
9 changes: 9 additions & 0 deletions test/fail_compilation/dep_extern_safety.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
REQUIRED_ARGS: -de
TEST_OUTPUT:
---
fail_compilation/dep_extern_safety.d(9): Deprecation: `extern` function `dep_extern_safety.fun` should be marked explicitly as `@safe`, `@system`, or `@trusted`
---
*/

extern extern(C) void fun();
2 changes: 1 addition & 1 deletion test/fail_compilation/fail20771.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fail_compilation/fail20771.d(19): Error: cannot pass types with postblits or cop
fail_compilation/fail20771.d(20): Error: cannot pass types with postblits or copy constructors as variadic arguments
---
*/
extern void variadic(...);
@system extern void variadic(...);

struct S20771
{
Expand Down
2 changes: 1 addition & 1 deletion test/fail_compilation/fail20772.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fail_compilation/fail20772.d(20): Error: cannot pass types with postblits or cop
fail_compilation/fail20772.d(21): Error: cannot pass types with postblits or copy constructors as variadic arguments
---
*/
extern void variadic(...);
@system extern void variadic(...);

struct S20772
{
Expand Down
2 changes: 1 addition & 1 deletion test/fail_compilation/fail20775.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fail_compilation/fail20775.d(19): Error: cannot pass types that need destruction
fail_compilation/fail20775.d(20): Error: cannot pass types that need destruction as variadic arguments
---
*/
extern void variadic(...);
@system extern void variadic(...);

struct S20775
{
Expand Down

0 comments on commit 4e3e27d

Please sign in to comment.