Skip to content

Commit 6e4863b

Browse files
committed
Reject @_silgen_name on globals that are not statically initialized
1 parent f89792c commit 6e4863b

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ ERROR(performance_unknown_callees,none,
335335
"called function is not known at compile time and can have unpredictable performance", ())
336336
ERROR(performance_callee_unavailable,none,
337337
"called function is not available in this module and can have unpredictable performance", ())
338-
ERROR(section_attr_on_non_const_global,none,
339-
"global variable must be a compile-time constant to use @_section attribute", ())
338+
ERROR(bad_attr_on_non_const_global,none,
339+
"global variable must be a compile-time constant to use %0 attribute", (StringRef))
340340
NOTE(performance_called_from,none,
341341
"called from here", ())
342342

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ SILGlobalVariable *SILGenModule::getSILGlobalVariable(VarDecl *gDecl,
5050

5151
if (gDecl->getAttrs().hasAttribute<SILGenNameAttr>()) {
5252
silLinkage = SILLinkage::DefaultForDeclaration;
53+
if (! gDecl->hasInitialValue()) {
54+
forDef = NotForDefinition;
55+
}
5356
}
5457

5558
// Check if it is already created, and update linkage if necessary.

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,22 @@ class PerformanceDiagnosticsPass : public SILModuleTransform {
477477

478478
PerformanceDiagnostics diagnoser(*module, getAnalysis<BasicCalleeAnalysis>());
479479

480-
// Check that @_section is only on constant globals
480+
// Check that @_section, @_silgen_name is only on constant globals
481481
for (SILGlobalVariable &g : module->getSILGlobals()) {
482-
if (!g.getStaticInitializerValue() && g.getSectionAttr())
483-
module->getASTContext().Diags.diagnose(
484-
g.getDecl()->getLoc(), diag::section_attr_on_non_const_global);
482+
if (!g.getStaticInitializerValue()) {
483+
if (g.getSectionAttr()) {
484+
module->getASTContext().Diags.diagnose(
485+
g.getDecl()->getLoc(), diag::bad_attr_on_non_const_global,
486+
"@_section");
487+
}
488+
489+
auto *decl = g.getDecl();
490+
if (decl && g.isDefinition() && decl->getAttrs().hasAttribute<SILGenNameAttr>()) {
491+
module->getASTContext().Diags.diagnose(
492+
g.getDecl()->getLoc(), diag::bad_attr_on_non_const_global,
493+
"@_silgen_name");
494+
}
495+
}
485496
}
486497

487498
bool annotatedFunctionsFound = false;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -parse-as-library -emit-sil %s -o /dev/null -verify
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
@_silgen_name("g0") var g0: Int = 1
6+
@_silgen_name("g1") var g1: (Int, Int) = (1, 2)
7+
@_silgen_name("g2") var g2: [Int] = [1, 2, 3] // expected-error {{global variable must be a compile-time constant to use @_silgen_name attribute}}
8+
@_silgen_name("g3") var g3: [Int:Int] = [:] // expected-error {{global variable must be a compile-time constant to use @_silgen_name attribute}}
9+
@_silgen_name("g4") var g4: UInt = 42
10+
@_silgen_name("g5") var g5: String = "hello" // expected-error {{global variable must be a compile-time constant to use @_silgen_name attribute}}
11+
@_silgen_name("g6") var g6: Any = 1 // expected-error {{global variable must be a compile-time constant to use @_silgen_name attribute}}
12+
@_silgen_name("g7") var g7: UInt8 = 42
13+
@_silgen_name("g8") var g8: Int = 5 * 5
14+
15+
@_silgen_name("fwd1") var fwd1: Int

0 commit comments

Comments
 (0)