Skip to content

Commit 03af946

Browse files
committed
ParseSIL: prevent _weakLinked on COFF targets
PE/COFF does not provide weak linking semantics. Ensure that we diagnose use of `_weakLinked` on those targets rather than consuming it and attempting to generate IR for that.
1 parent 1d10a6a commit 03af946

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,8 @@ WARNING(attr_renamed_warning, none,
13371337
"'@%0' has been renamed to '@%1'", (StringRef, StringRef))
13381338
ERROR(attr_name_close_match, none,
13391339
"no attribute named '@%0'; did you mean '@%1'?", (StringRef, StringRef))
1340+
ERROR(attr_unsupported_on_target, none,
1341+
"attribute '%0' is unsupported on target '%1'", (StringRef, StringRef))
13401342

13411343
// availability
13421344
ERROR(attr_availability_platform,none,

lib/Parse/ParseDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,15 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
906906
!isInSILMode()) {
907907
diagnose(Loc, diag::only_allowed_in_sil, AttrName);
908908
DiscardAttribute = true;
909-
}
909+
}
910+
911+
if (Context.LangOpts.Target.isOSBinFormatCOFF()) {
912+
if (DK == DAK_WeakLinked) {
913+
diagnose(Loc, diag::attr_unsupported_on_target, AttrName,
914+
Context.LangOpts.Target.str());
915+
DiscardAttribute = true;
916+
}
917+
}
910918

911919
// Filled in during parsing. If there is a duplicate
912920
// diagnostic this can be used for better error presentation.

lib/ParseSIL/ParseSIL.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,12 @@ static bool parseDeclSILOptional(bool *isTransparent,
10091009
else if (isGlobalInit && SP.P.Tok.getText() == "global_init")
10101010
*isGlobalInit = true;
10111011
else if (isWeakLinked && SP.P.Tok.getText() == "_weakLinked")
1012-
*isWeakLinked = true;
1012+
if (M.getASTContext().LangOpts.Target.isOSBinFormatCOFF())
1013+
SP.P.diagnose(SP.P.Tok, diag::attr_unsupported_on_target,
1014+
SP.P.Tok.getText(),
1015+
M.getASTContext().LangOpts.Target.str());
1016+
else
1017+
*isWeakLinked = true;
10131018
else if (inlineStrategy && SP.P.Tok.getText() == "noinline")
10141019
*inlineStrategy = NoInline;
10151020
else if (optimizationMode && SP.P.Tok.getText() == "Onone")

test/IRGen/weakLinked.sil

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %swift -target x86_64-unknown-windows-msvc -emit-ir -verify %s
2+
3+
sil_stage canonical
4+
5+
// expected-error@+1{{attribute '_weakLinked' is unsupported on target 'x86_64-unknown-windows-msvc'}}
6+
sil [_weakLinked] @f : $@convention(thin) () -> () {
7+
%unit = tuple()
8+
return %unit : $()
9+
}
10+

test/Parse/weakLinked.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %swift -target x86_64-unknown-windows-msvc -parse-stdlib -typecheck -verify %s
2+
3+
// expected-error@+1{{attribute '_weakLinked' is unsupported on target 'x86_64-unknown-windows-msvc'}}
4+
@_weakLinked func f() { }
5+

0 commit comments

Comments
 (0)