Skip to content

Commit baacf12

Browse files
committed
[embedded] Diagnose usage of weak and unowned variables in embedded Swift
1 parent ce21af5 commit baacf12

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,9 @@ ERROR(attr_only_at_non_generic_scope, none,
20322032
ERROR(attr_only_on_static_properties, none,
20332033
"properties with attribute '_section' must be static", (StringRef))
20342034

2035+
ERROR(weak_unowned_in_embedded_swift, none,
2036+
"attribute %0 cannot be used in embedded Swift", (ReferenceOwnership))
2037+
20352038
ERROR(access_control_in_protocol,none,
20362039
"%0 modifier cannot be used in protocols", (DeclAttribute))
20372040
NOTE(access_control_in_protocol_detail,none,

lib/Parse/ParseDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,14 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
30553055
AttrRange = SourceRange(Loc);
30563056
}
30573057

3058+
// Embedded Swift prohibits weak/unowned but allows unowned(unsafe).
3059+
if (Context.LangOpts.hasFeature(Feature::Embedded)) {
3060+
if (Kind == ReferenceOwnership::Weak ||
3061+
Kind == ReferenceOwnership::Unowned) {
3062+
diagnose(Loc, diag::weak_unowned_in_embedded_swift, Kind);
3063+
}
3064+
}
3065+
30583066
if (!DiscardAttribute)
30593067
Attributes.add(
30603068
new (Context) ReferenceOwnershipAttr(AttrRange, Kind));

test/embedded/weak-unowned.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-emit-ir %s -wmo
2+
// RUN: %target-swift-emit-ir %s -enable-experimental-feature Embedded -verify
3+
4+
// REQUIRES: swift_in_compiler
5+
// REQUIRES: optimized_stdlib
6+
// REQUIRES: OS=macosx || OS=linux-gnu
7+
8+
public class MyClass { }
9+
10+
public struct MyStruct {
11+
var normalVar: MyClass
12+
weak var weakVar: MyClass? // expected-error {{attribute 'weak' cannot be used in embedded Swift}}
13+
unowned var unownedVar: MyClass // expected-error {{attribute 'unowned' cannot be used in embedded Swift}}
14+
unowned(unsafe) var unownedUnsafe: MyClass
15+
}

0 commit comments

Comments
 (0)