Skip to content

Commit e3ce6f7

Browse files
authored
Merge pull request #30939 from artemcm/ImplOnlyPropertyWrappers
[Sema] Diagnose use of implementation-only property wrappers
2 parents 83183f4 + efdfcee commit e3ce6f7

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,14 +2655,14 @@ NOTE(construct_raw_representable_from_unwrapped_value,none,
26552655
"construct %0 from unwrapped %1 value", (Type, Type))
26562656

26572657
ERROR(decl_from_hidden_module,none,
2658-
"cannot use %0 %1 %select{here|"
2658+
"cannot use %0 %1 %select{here|as property wrapper here|"
26592659
"in an extension with public or '@usableFromInline' members|"
26602660
"in an extension with conditional conformances}2; "
26612661
"%select{%3 has been imported as implementation-only|"
26622662
"it is an SPI imported from %3}4",
26632663
(DescriptiveDeclKind, DeclName, unsigned, Identifier, unsigned))
26642664
ERROR(conformance_from_implementation_only_module,none,
2665-
"cannot use conformance of %0 to %1 %select{here|"
2665+
"cannot use conformance of %0 to %1 %select{here|as property wrapper here|"
26662666
"in an extension with public or '@usableFromInline' members|"
26672667
"in an extension with conditional conformances}2; %3 has been imported "
26682668
"as implementation-only",

lib/Sema/TypeCheckAccess.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
16391639
// diag::conformance_from_implementation_only_module.
16401640
enum class Reason : unsigned {
16411641
General,
1642+
PropertyWrapper,
16421643
ExtensionWithPublicMembers,
16431644
ExtensionWithConditionalConformances
16441645
};
@@ -1836,6 +1837,11 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18361837
return;
18371838

18381839
checkType(TP->getTypeLoc(), anyVar, getDiagnoser(anyVar));
1840+
1841+
// Check the property wrapper types.
1842+
for (auto attr : anyVar->getAttachedPropertyWrappers())
1843+
checkType(attr->getTypeLoc(), anyVar,
1844+
getDiagnoser(anyVar, Reason::PropertyWrapper));
18391845
}
18401846

18411847
void visitPatternBindingDecl(PatternBindingDecl *PBD) {

test/Sema/Inputs/implementation-only-import-in-decls-helper.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ public struct IntLike: ExpressibleByIntegerLiteral, Equatable {
1818
public init(integerLiteral: Int) {}
1919
}
2020

21+
@propertyWrapper
22+
public struct BadWrapper {
23+
public var wrappedValue: Int
24+
public init(wrappedValue: Int) {
25+
self.wrappedValue = wrappedValue
26+
}
27+
}
28+
2129
precedencegroup BadPrecedence {}

test/Sema/implementation-only-import-in-decls.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public struct TestInit {
4646
public init<T>(where _: T) where T: BadProto {} // expected-error {{cannot use protocol 'BadProto' here; 'BADLibrary' has been imported as implementation-only}}
4747
}
4848

49+
public struct TestPropertyWrapper {
50+
@BadWrapper public var BadProperty: Int // expected-error {{cannot use struct 'BadWrapper' as property wrapper here; 'BADLibrary' has been imported as implementation-only}}
51+
}
52+
4953
public protocol TestInherited: BadProto {} // expected-error {{cannot use protocol 'BadProto' here; 'BADLibrary' has been imported as implementation-only}}
5054

5155
public protocol TestConstraintBase {

test/Serialization/Recovery/implementation-only-missing.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public struct PublicStruct: LibProtocol {
7474

7575
public init() { }
7676

77-
@IoiPropertyWrapper("some text")
78-
public var wrappedVar: String
77+
public var nonWrappedVar: String = "some text"
7978
}
8079

8180
struct StructWithOverride: HiddenProtocolWithOverride {
@@ -87,6 +86,6 @@ struct StructWithOverride: HiddenProtocolWithOverride {
8786
import public_lib
8887

8988
var s = PublicStruct()
90-
print(s.wrappedVar)
89+
print(s.nonWrappedVar)
9190

9291
#endif

0 commit comments

Comments
 (0)