Skip to content

Commit 932e523

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 af96549 commit 932e523

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/dmd/dsymbolsem.d

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4049,6 +4049,22 @@ 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+
* extern(D) virtual functions are ok because the compiler will check
4056+
* them when they are overridden.
4057+
*/
4058+
if (funcdecl.isExternal()
4059+
&& funcdecl.type.toTypeFunction().trust == TRUST.default_
4060+
&& !(funcdecl.isVirtual()
4061+
&& (funcdecl.linkage == LINK.d || funcdecl.linkage == LINK.default_)))
4062+
{
4063+
deprecation(funcdecl.loc,
4064+
"`extern` function `%s` should be marked explicitly as `@safe`, `@system`, or `@trusted`",
4065+
funcdecl.toPrettyChars);
4066+
}
4067+
40524068
Module.dprogress++;
40534069
funcdecl.semanticRun = PASS.semanticdone;
40544070

src/dmd/func.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,13 @@ extern (C++) class FuncDeclaration : Declaration
12601260
return false;
12611261
}
12621262

1263+
final bool isExternal()
1264+
{
1265+
if (fbody || storage_class & STC.disable)
1266+
return false;
1267+
return (storage_class & STC.extern_) || (linkage != LINK.default_);
1268+
}
1269+
12631270
/**********************************
12641271
* Decide if attributes for this function can be inferred from examining
12651272
* the function body.
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();

0 commit comments

Comments
 (0)