Skip to content

Commit a6cb1a0

Browse files
committed
Add deprecation message for extern functions with default safety
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.
1 parent 93b1ca8 commit a6cb1a0

File tree

7 files changed

+28
-5
lines changed

7 files changed

+28
-5
lines changed

src/dmd/dsymbolsem.d

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4049,6 +4049,18 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
40494049
if (funcdecl.canInferAttributes(sc))
40504050
funcdecl.initInferAttributes();
40514051

4052+
/* Warn about external function declarations that may become incorrect
4053+
* when @safe is made the default.
4054+
*/
4055+
if (!funcdecl.fbody
4056+
&& funcdecl.storage_class == STC.extern_
4057+
&& funcdecl.type.toTypeFunction().trust == TRUST.default_)
4058+
{
4059+
deprecation(funcdecl.loc,
4060+
"`extern` function `%s` should be marked explicitly as `@safe`, `@system`, or `@trusted`",
4061+
funcdecl.toPrettyChars);
4062+
}
4063+
40524064
Module.dprogress++;
40534065
funcdecl.semanticRun = PASS.semanticdone;
40544066

test/compilable/compile1.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ template test8163(T...)
476476
}
477477

478478
enum N = 2; // N>=2 triggers the bug
479-
extern Point[N] bar();
479+
@system extern Point[N] bar();
480480

481481
void foo()
482482
{

test/compilable/test16031.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// REQUIRED_ARGS: -fPIC -lib
22
// PERMUTE_ARGS:
33
// DISABLED: win32 win64
4-
extern void throwing();
4+
@system extern void throwing();
55

66
void foo()
77
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
REQUIRED_ARGS: -de
3+
TEST_OUTPUT:
4+
---
5+
fail_compilation/dep_extern_safety.d(10): Deprecation: `extern` function `dep_extern_safety.cfun` should be marked explicitly as `@safe`, `@system`, or `@trusted`
6+
fail_compilation/dep_extern_safety.d(11): Deprecation: `extern` function `dep_extern_safety.dfun` should be marked explicitly as `@safe`, `@system`, or `@trusted`
7+
---
8+
*/
9+
10+
extern extern(C) void cfun();
11+
extern extern(D) void dfun();

test/fail_compilation/fail20771.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fail_compilation/fail20771.d(19): Error: cannot pass types with postblits or cop
55
fail_compilation/fail20771.d(20): Error: cannot pass types with postblits or copy constructors as variadic arguments
66
---
77
*/
8-
extern void variadic(...);
8+
@system extern void variadic(...);
99

1010
struct S20771
1111
{

test/fail_compilation/fail20772.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fail_compilation/fail20772.d(20): Error: cannot pass types with postblits or cop
55
fail_compilation/fail20772.d(21): Error: cannot pass types with postblits or copy constructors as variadic arguments
66
---
77
*/
8-
extern void variadic(...);
8+
@system extern void variadic(...);
99

1010
struct S20772
1111
{

test/fail_compilation/fail20775.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fail_compilation/fail20775.d(19): Error: cannot pass types that need destruction
55
fail_compilation/fail20775.d(20): Error: cannot pass types that need destruction as variadic arguments
66
---
77
*/
8-
extern void variadic(...);
8+
@system extern void variadic(...);
99

1010
struct S20775
1111
{

0 commit comments

Comments
 (0)