Skip to content

Commit 6925940

Browse files
committed
[Sema] Improve diagnostic when @usableFromInline is applied to a declaration without a name (i.e. accessor/init)
1 parent dbe151b commit 6925940

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6904,8 +6904,8 @@ ERROR(frozen_attr_on_internal_type,
69046904

69056905
ERROR(usable_from_inline_attr_with_explicit_access,
69066906
none, "'@usableFromInline' attribute can only be applied to internal or package "
6907-
"declarations, but %0 is %select{private|fileprivate|%error|package|public|open}1",
6908-
(DeclName, AccessLevel))
6907+
"declarations, but %kind0 is %select{private|fileprivate|%error|package|public|open}1",
6908+
(const ValueDecl *, AccessLevel))
69096909

69106910
WARNING(inlinable_implies_usable_from_inline,none,
69116911
"'@usableFromInline' attribute has no effect on '@inlinable' %kind0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3273,7 +3273,7 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {
32733273
VD->getFormalAccess() != AccessLevel::Package) {
32743274
diagnoseAndRemoveAttr(attr,
32753275
diag::usable_from_inline_attr_with_explicit_access,
3276-
VD->getName(), VD->getFormalAccess());
3276+
VD, VD->getFormalAccess());
32773277
return;
32783278
}
32793279

test/Compatibility/attr_usableFromInline_swift4.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %target-typecheck-verify-swift -enable-testing -swift-version 4 -disable-objc-attr-requires-foundation-module -enable-objc-interop
33

44
@usableFromInline private func privateVersioned() {}
5-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'privateVersioned()' is private}}
5+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'privateVersioned()' is private}}
66

77
@usableFromInline fileprivate func fileprivateVersioned() {}
8-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'fileprivateVersioned()' is fileprivate}}
8+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'fileprivateVersioned()' is fileprivate}}
99

1010
@usableFromInline internal func internalVersioned() {}
1111
// OK
@@ -14,11 +14,11 @@
1414
// OK
1515

1616
@usableFromInline public func publicVersioned() {}
17-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
17+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'publicVersioned()' is public}}
1818

1919
internal class InternalClass {
2020
@usableFromInline public func publicVersioned() {}
21-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
21+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
2222
}
2323

2424
fileprivate class filePrivateClass {

test/Compatibility/attr_usableFromInline_swift42.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %target-typecheck-verify-swift -enable-testing -swift-version 4.2 -disable-objc-attr-requires-foundation-module -enable-objc-interop
33

44
@usableFromInline private func privateVersioned() {}
5-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'privateVersioned()' is private}}
5+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'privateVersioned()' is private}}
66

77
@usableFromInline fileprivate func fileprivateVersioned() {}
8-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'fileprivateVersioned()' is fileprivate}}
8+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'fileprivateVersioned()' is fileprivate}}
99

1010
@usableFromInline internal func internalVersioned() {}
1111
// OK
@@ -14,12 +14,12 @@
1414
// OK
1515

1616
@usableFromInline public func publicVersioned() {}
17-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
17+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'publicVersioned()' is public}}
1818

1919
internal class InternalClass {
2020
// expected-note@-1 2{{type declared here}}
2121
@usableFromInline public func publicVersioned() {}
22-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
22+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
2323
}
2424

2525
fileprivate class filePrivateClass {

test/attr/attr_usableFromInline.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %target-typecheck-verify-swift -swift-version 5 -disable-objc-attr-requires-foundation-module -enable-objc-interop -enable-testing -package-name myPkg
33

44
@usableFromInline private func privateVersioned() {}
5-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'privateVersioned()' is private}}
5+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'privateVersioned()' is private}}
66

77
@usableFromInline fileprivate func fileprivateVersioned() {}
8-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'fileprivateVersioned()' is fileprivate}}
8+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'fileprivateVersioned()' is fileprivate}}
99

1010
@usableFromInline internal func internalVersioned() {}
1111
// OK
@@ -17,7 +17,7 @@
1717
// OK
1818

1919
@usableFromInline public func publicVersioned() {}
20-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
20+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'publicVersioned()' is public}}
2121

2222
// expected-note@+1 3{{global function 'internalFunc()' is not '@usableFromInline' or public}}
2323
internal func internalFunc() {}
@@ -55,13 +55,13 @@ package func packageInlinableFunc() {
5555
package class PackageClass {
5656
// expected-note@-1 *{{type declared here}}
5757
@usableFromInline public func publicVersioned() {}
58-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
58+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
5959
}
6060

6161
internal class InternalClass {
6262
// expected-note@-1 2{{type declared here}}
6363
@usableFromInline public func publicVersioned() {}
64-
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
64+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
6565
}
6666

6767
fileprivate class filePrivateClass {
@@ -334,3 +334,21 @@ public struct TestGenericSubscripts {
334334
@usableFromInline package func pkgNonGenericWhereClause() where T : PackageProtocol {}
335335
// expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' instance method must be '@usableFromInline' or public}}
336336
}
337+
338+
public struct IncorrectInitUse {
339+
public var x: Int {
340+
@usableFromInline
341+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but getter for property 'x' is public}}
342+
get { 0 }
343+
344+
@usableFromInline
345+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but setter for property 'x' is public}}
346+
set { }
347+
}
348+
349+
@usableFromInline
350+
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but initializer 'init(x:)' is public}}
351+
public init(x: Int) {
352+
self.x = x
353+
}
354+
}

0 commit comments

Comments
 (0)