Skip to content

Commit bc18637

Browse files
authored
Merge pull request swiftlang#72135 from kubamracek/embedded-weak-unowned
[embedded] Diagnose usage of weak and unowned variables in embedded Swift
2 parents d6d7f00 + d70059e commit bc18637

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-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/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,6 +4681,16 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
46814681
attr->setInvalid();
46824682
}
46834683

4684+
// Embedded Swift prohibits weak/unowned but allows unowned(unsafe).
4685+
if (var->getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
4686+
if (ownershipKind == ReferenceOwnership::Weak ||
4687+
ownershipKind == ReferenceOwnership::Unowned) {
4688+
Diags.diagnose(attr->getLocation(), diag::weak_unowned_in_embedded_swift,
4689+
ownershipKind);
4690+
attr->setInvalid();
4691+
}
4692+
}
4693+
46844694
if (attr->isInvalid())
46854695
return type;
46864696

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)