Skip to content

Commit ecfeb1d

Browse files
committed
Reject @_silgen_name on globals that are not statically initialized
1 parent c752b7b commit ecfeb1d

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
@@ -340,8 +340,8 @@ ERROR(performance_unknown_callees,none,
340340
"called function is not known at compile time and can have unpredictable performance", ())
341341
ERROR(performance_callee_unavailable,none,
342342
"called function is not available in this module and can have unpredictable performance", ())
343-
ERROR(section_attr_on_non_const_global,none,
344-
"global variable must be a compile-time constant to use @_section attribute", ())
343+
ERROR(bad_attr_on_non_const_global,none,
344+
"global variable must be a compile-time constant to use %0 attribute", (StringRef))
345345
NOTE(performance_called_from,none,
346346
"called from here", ())
347347

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
@@ -481,11 +481,22 @@ class PerformanceDiagnosticsPass : public SILModuleTransform {
481481

482482
PerformanceDiagnostics diagnoser(*module, getAnalysis<BasicCalleeAnalysis>());
483483

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

491502
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)