Skip to content

Commit 8ee6d43

Browse files
authored
Merge pull request #65023 from tshortli/exclusivity-attribute-in-swiftinterface
Sema: Skip storage diagnostic for `@exclusivity` in swiftinterfaces
2 parents 3264428 + 7ba9601 commit 8ee6d43

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,22 +2972,27 @@ void AttributeChecker::visitOptimizeAttr(OptimizeAttr *attr) {
29722972

29732973
void AttributeChecker::visitExclusivityAttr(ExclusivityAttr *attr) {
29742974
if (auto *varDecl = dyn_cast<VarDecl>(D)) {
2975-
if (!varDecl->hasStorage()) {
2976-
diagnose(attr->getLocation(), diag::exclusivity_on_computed_property);
2977-
attr->setInvalid();
2978-
return;
2975+
auto *DC = D->getDeclContext();
2976+
auto *parentSF = DC->getParentSourceFile();
2977+
2978+
if (parentSF && parentSF->Kind != SourceFileKind::Interface) {
2979+
if (!varDecl->hasStorage()) {
2980+
diagnose(attr->getLocation(), diag::exclusivity_on_computed_property);
2981+
attr->setInvalid();
2982+
return;
2983+
}
29792984
}
2980-
2981-
if (isa<ClassDecl>(varDecl->getDeclContext()))
2985+
2986+
if (isa<ClassDecl>(DC))
29822987
return;
2983-
2984-
if (varDecl->getDeclContext()->isTypeContext() && !varDecl->isInstanceMember())
2988+
2989+
if (DC->isTypeContext() && !varDecl->isInstanceMember())
29852990
return;
2986-
2987-
if (varDecl->getDeclContext()->isModuleScopeContext())
2991+
2992+
if (DC->isModuleScopeContext())
29882993
return;
29892994
}
2990-
diagnose(attr->getLocation(), diag::exclusivity_on_wrong_decl);
2995+
diagnoseAndRemoveAttr(attr, diag::exclusivity_on_wrong_decl);
29912996
attr->setInvalid();
29922997
}
29932998

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name exclusivity
2+
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name exclusivity
3+
// RUN: %FileCheck %s < %t.swiftinterface
4+
5+
// CHECK: @exclusivity(checked) public var checkedGlobalVar: Swift.Int
6+
@exclusivity(checked)
7+
public var checkedGlobalVar = 1
8+
9+
// CHECK: @exclusivity(unchecked) public var uncheckedGlobalVar: Swift.Int
10+
@exclusivity(unchecked)
11+
public var uncheckedGlobalVar = 1
12+
13+
// CHECK-LABEL: public struct Struct
14+
public struct Struct {
15+
// CHECK: @exclusivity(unchecked) public static var uncheckedStaticVar: Swift.Int
16+
@exclusivity(unchecked)
17+
public static var uncheckedStaticVar: Int = 27
18+
19+
// CHECK: @exclusivity(checked) public static var checkedStaticVar: Swift.Int
20+
@exclusivity(checked)
21+
public static var checkedStaticVar: Int = 27
22+
}
23+
24+
// CHECK-LABEL: public class Class
25+
public class Class {
26+
// CHECK: @exclusivity(unchecked) public var uncheckedInstanceVar: Swift.Int
27+
@exclusivity(unchecked)
28+
public var uncheckedInstanceVar: Int = 27
29+
30+
// CHECK: @exclusivity(checked) public var checkedInstanceVar: Swift.Int
31+
@exclusivity(checked)
32+
public var checkedInstanceVar: Int = 27
33+
34+
// CHECK: @exclusivity(unchecked) public var uncheckedPrivateSetInstanceVar: Swift.Int {
35+
// CHECK-NEXT: get
36+
// CHECK-NEXT: }
37+
@exclusivity(unchecked)
38+
public private(set) var uncheckedPrivateSetInstanceVar: Int = 27
39+
40+
// CHECK: @exclusivity(unchecked) public static var uncheckedStaticVar: Swift.Int
41+
@exclusivity(unchecked)
42+
public static var uncheckedStaticVar: Int = 27
43+
}

test/SILGen/exclusivityattr.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public func getUncheckedVar() -> Int {
2323
return globalUncheckedVar
2424
}
2525

26-
public class ExclusivityAttrStruct {
26+
public struct ExclusivityAttrStruct {
2727

28-
// CHECK-LABEL: sil {{.*}}@$s4test21ExclusivityAttrStructC9staticVarSivsZ
28+
// CHECK-LABEL: sil {{.*}}@$s4test21ExclusivityAttrStructV9staticVarSivsZ
2929
// CHECK-ON: begin_access [modify] [unsafe]
30-
// CHECK: } // end sil function '$s4test21ExclusivityAttrStructC9staticVarSivsZ'
30+
// CHECK: } // end sil function '$s4test21ExclusivityAttrStructV9staticVarSivsZ'
3131
@exclusivity(unchecked)
3232
public static var staticVar: Int = 27
3333
}

0 commit comments

Comments
 (0)