Skip to content

Commit 23350ac

Browse files
committed
Sema: Reject #_hasSymbol conditions in inlinable code.
1 parent 73b4b43 commit 23350ac

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5953,6 +5953,9 @@ ERROR(availability_macro_in_inlinable, none,
59535953
"availability macro cannot be used in " FRAGILE_FUNC_KIND "0",
59545954
(unsigned))
59555955

5956+
ERROR(has_symbol_condition_in_inlinable, none,
5957+
"'#_hasSymbol' cannot be used in " FRAGILE_FUNC_KIND "0", (unsigned))
5958+
59565959
#undef FRAGILE_FUNC_KIND
59575960

59585961
NOTE(resilience_decl_declared_here_public,
@@ -6619,7 +6622,7 @@ WARNING(has_symbol_decl_must_be_weak,none,
66196622
(DescriptiveDeclKind, DeclName))
66206623
ERROR(has_symbol_invalid_expr,none,
66216624
"'#_hasSymbol' condition must refer to a declaration", ())
6622-
ERROR(has_symbol_unsupported,none,
6625+
ERROR(has_symbol_unsupported_on_target,none,
66236626
"'#_hasSymbol' is unsupported on target '%0'", (StringRef))
66246627

66256628
//------------------------------------------------------------------------------

lib/Sema/MiscDiagnostics.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4410,11 +4410,29 @@ static bool diagnoseHasSymbolCondition(PoundHasSymbolInfo *info,
44104410
if (!ctx.LangOpts.Target.isOSDarwin()) {
44114411
// SILGen for #_hasSymbol is currently implemented assuming the target OS
44124412
// is a Darwin platform.
4413-
ctx.Diags.diagnose(info->getStartLoc(), diag::has_symbol_unsupported,
4413+
ctx.Diags.diagnose(info->getStartLoc(),
4414+
diag::has_symbol_unsupported_on_target,
44144415
ctx.LangOpts.Target.str());
44154416
return true;
44164417
}
44174418

4419+
if (DC->getAsDecl()) {
4420+
auto fragileKind = DC->getFragileFunctionKind();
4421+
if (fragileKind.kind != FragileFunctionKind::None) {
4422+
// #_hasSymbol cannot be used in inlinable code because of limitations of
4423+
// the current implementation strategy. It relies on recording the
4424+
// referenced ValueDecl, mangling a helper function name using that
4425+
// ValueDecl, and then passing the responsibility of generating the
4426+
// definition for that helper function to IRGen. In order to lift this
4427+
// restriction, we will need teach SIL to encode the ValueDecl, or take
4428+
// another approach entirely.
4429+
ctx.Diags.diagnose(info->getStartLoc(),
4430+
diag::has_symbol_condition_in_inlinable,
4431+
fragileKind.getSelector());
4432+
return true;
4433+
}
4434+
}
4435+
44184436
auto decl = info->getReferencedDecl().getDecl();
44194437
if (!decl) {
44204438
// Diagnose because we weren't able to interpret the expression as one

test/Sema/has_symbol.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ func testWhile() {
115115
while #_hasSymbol(localFunc) { break } // expected-warning {{global function 'localFunc()' is not a weakly linked declaration}}
116116
}
117117

118+
@inlinable
119+
func testInlinable() {
120+
if #_hasSymbol(noArgFunc) {} // expected-error {{'#_hasSymbol' cannot be used in an '@inlinable' function}}
121+
}
122+
123+
@_alwaysEmitIntoClient
124+
func testAEIC() {
125+
if #_hasSymbol(noArgFunc) {} // expected-error {{'#_hasSymbol' cannot be used in an '@_alwaysEmitIntoClient' function}}
126+
}
127+
118128
func doIt(_ closure: () -> ()) {
119129
closure()
120130
}

0 commit comments

Comments
 (0)