Skip to content

Allow @usableFromInline and @inlinable to package decls #64408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2439,10 +2439,10 @@ WARNING(protocol_access_warn,none,
(bool, AccessLevel, bool, AccessLevel, bool, DescriptiveDeclKind))
ERROR(protocol_usable_from_inline,none,
"protocol %select{refined|used}0 by '@usableFromInline' protocol "
"must be '@usableForInline' or public", (bool))
"must be '@usableFromInline' or public", (bool))
WARNING(protocol_usable_from_inline_warn,none,
"protocol %select{refined|used}0 by '@usableFromInline' protocol "
"should be '@usableForInline' or public", (bool))
"should be '@usableFromInline' or public", (bool))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I wonder how long that typo was there.

ERROR(protocol_property_must_be_computed_var,none,
"protocols cannot require properties to be immutable; declare read-only "
"properties by using 'var' with a '{ get }' specifier", ())
Expand Down Expand Up @@ -6162,7 +6162,7 @@ ERROR(frozen_attr_on_internal_type,
(DeclName, AccessLevel))

ERROR(usable_from_inline_attr_with_explicit_access,
none, "'@usableFromInline' attribute can only be applied to internal "
none, "'@usableFromInline' attribute can only be applied to internal or package "
"declarations, but %0 is %select{private|fileprivate|%error|package|public|open}1",
(DeclName, AccessLevel))

Expand Down
7 changes: 4 additions & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3646,7 +3646,7 @@ SourceLoc ValueDecl::getAttributeInsertionLoc(bool forModifier) const {
/// Returns true if \p VD needs to be treated as publicly-accessible
/// at the SIL, LLVM, and machine levels due to being @usableFromInline.
bool ValueDecl::isUsableFromInline() const {
assert(getFormalAccess() <= AccessLevel::Internal);
assert(getFormalAccess() < AccessLevel::Public);

if (getAttrs().hasAttribute<UsableFromInlineAttr>() ||
getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>() ||
Expand Down Expand Up @@ -3767,7 +3767,7 @@ static AccessLevel getAdjustedFormalAccess(const ValueDecl *VD,
return getMaximallyOpenAccessFor(VD);

if (treatUsableFromInlineAsPublic &&
access <= AccessLevel::Internal &&
access < AccessLevel::Public &&
VD->isUsableFromInline()) {
return AccessLevel::Public;
}
Expand Down Expand Up @@ -4073,8 +4073,9 @@ static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
// marked 'public' if the protocol was '@_versioned' (now
// '@usableFromInline'). Which works at the ABI level, so let's keep
// supporting that here by explicitly checking for it.
auto protoAccess = proto->getFormalAccess();
if (access == AccessLevel::Public &&
proto->getFormalAccess() == AccessLevel::Internal &&
(protoAccess == AccessLevel::Internal || protoAccess == AccessLevel::Package) &&
proto->isUsableFromInline()) {
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
: AccessControlCheckerBase(/*checkUsableFromInline=*/true) {}

static bool shouldSkipChecking(const ValueDecl *VD) {
if (VD->getFormalAccess() != AccessLevel::Internal)
if (VD->getFormalAccess() != AccessLevel::Internal &&
VD->getFormalAccess() != AccessLevel::Package)
return true;
return !VD->isUsableFromInline();
};
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2886,8 +2886,9 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {
return;
}

// @usableFromInline can only be applied to internal declarations.
if (VD->getFormalAccess() != AccessLevel::Internal) {
// @usableFromInline can only be applied to internal or package declarations.
if (VD->getFormalAccess() != AccessLevel::Internal &&
VD->getFormalAccess() != AccessLevel::Package) {
diagnoseAndRemoveAttr(attr,
diag::usable_from_inline_attr_with_explicit_access,
VD->getName(), VD->getFormalAccess());
Expand Down
81 changes: 79 additions & 2 deletions test/AutoDiff/Sema/derivative_attr_type_checking.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-frontend-typecheck -verify -disable-availability-checking %s
// RUN: %target-swift-frontend-typecheck -enable-testing -verify -disable-availability-checking %s
// RUN: %target-swift-frontend-typecheck -verify -disable-availability-checking %s -package-name myPkg
// RUN: %target-swift-frontend-typecheck -enable-testing -verify -disable-availability-checking %s -package-name myPkg

// Swift.AdditiveArithmetic:3:17: note: cannot yet register derivative default implementation for protocol requirements

Expand Down Expand Up @@ -1001,6 +1001,12 @@ func _public_original_usablefrominline_derivative(_ x: Float) -> (value: Float,
fatalError()
}

package func package_original_package_derivative(_ x: Float) -> Float { x }
@derivative(of: package_original_package_derivative)
package func _package_original_package_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

func internal_original_internal_derivative(_ x: Float) -> Float { x }
@derivative(of: internal_original_internal_derivative)
func _internal_original_internal_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
Expand Down Expand Up @@ -1040,6 +1046,28 @@ func _internal_original_alwaysemitintoclient_derivative(_ x: Float) -> (value: F
fatalError()
}


package func package_original_usablefrominline_derivative(_ x: Float) -> Float { x }
@usableFromInline
@derivative(of: package_original_usablefrominline_derivative)
package func _package_original_usablefrominline_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

package func package_original_inlinable_derivative(_ x: Float) -> Float { x }
@inlinable
@derivative(of: package_original_inlinable_derivative)
package func _package_original_inlinable_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

package func package_original_alwaysemitintoclient_derivative(_ x: Float) -> Float { x }
@_alwaysEmitIntoClient
@derivative(of: package_original_alwaysemitintoclient_derivative)
package func _package_original_alwaysemitintoclient_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

// MARK: - Original function visibility < derivative function visibility

@usableFromInline
Expand All @@ -1051,6 +1079,23 @@ public func _usablefrominline_original_public_derivative(_ x: Float) -> (value:
fatalError()
}

@usableFromInline
package func package_usablefrominline_original_public_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_package__usablefrominline_original_public_derivative' is public, but original function 'package_usablefrominline_original_public_derivative' is package}}
@derivative(of: package_usablefrominline_original_public_derivative)
// expected-note @+1 {{mark the derivative function as 'package' to match the original function}} {{1-7=package}}
public func _package__usablefrominline_original_public_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

package func package_original_public_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_package_original_public_derivative' is public, but original function 'package_original_public_derivative' is package}}
@derivative(of: package_original_public_derivative)
// expected-note @+1 {{mark the derivative function as 'package' to match the original function}} {{1-7=package}}
public func _package_original_public_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

func internal_original_public_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_internal_original_public_derivative' is public, but original function 'internal_original_public_derivative' is internal}}
@derivative(of: internal_original_public_derivative)
Expand All @@ -1059,6 +1104,14 @@ public func _internal_original_public_derivative(_ x: Float) -> (value: Float, p
fatalError()
}

func internal_original_package_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_internal_original_package_derivative' is package, but original function 'internal_original_package_derivative' is internal}}
@derivative(of: internal_original_package_derivative)
// expected-note @+1 {{mark the derivative function as 'internal' to match the original function}} {{1-8=internal}}
package func _internal_original_package_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

private func private_original_usablefrominline_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_private_original_usablefrominline_derivative' is internal, but original function 'private_original_usablefrominline_derivative' is private}}
@derivative(of: private_original_usablefrominline_derivative)
Expand All @@ -1076,6 +1129,14 @@ public func _private_original_public_derivative(_ x: Float) -> (value: Float, pu
fatalError()
}

private func private_original_package_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_private_original_package_derivative' is package, but original function 'private_original_package_derivative' is private}}
@derivative(of: private_original_package_derivative)
// expected-note @+1 {{mark the derivative function as 'private' to match the original function}}
package func _private_original_package_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

private func private_original_internal_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_private_original_internal_derivative' is internal, but original function 'private_original_internal_derivative' is private}}
@derivative(of: private_original_internal_derivative)
Expand Down Expand Up @@ -1110,6 +1171,14 @@ fileprivate func _public_original_private_derivative(_ x: Float) -> (value: Floa
fatalError()
}

public func public_original_package_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_public_original_package_derivative' is package, but original function 'public_original_package_derivative' is public}}
@derivative(of: public_original_package_derivative)
package func _public_original_package_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
// expected-note @-1 {{mark the derivative function as '@usableFromInline' to match the original function}} {{-1:1-1=@usableFromInline }}
fatalError()
}

public func public_original_internal_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_public_original_internal_derivative' is internal, but original function 'public_original_internal_derivative' is public}}
@derivative(of: public_original_internal_derivative)
Expand All @@ -1118,6 +1187,14 @@ func _public_original_internal_derivative(_ x: Float) -> (value: Float, pullback
fatalError()
}

package func package_original_internal_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_package_original_internal_derivative' is internal, but original function 'package_original_internal_derivative' is package}}
@derivative(of: package_original_internal_derivative)
// expected-note @+1 {{mark the derivative function as 'package' to match the original function}} {{1-1=package }}
func _package_original_internal_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

func internal_original_fileprivate_derivative(_ x: Float) -> Float { x }
// expected-error @+1 {{derivative function must have same access level as original function; derivative function '_internal_original_fileprivate_derivative' is fileprivate, but original function 'internal_original_fileprivate_derivative' is internal}}
@derivative(of: internal_original_fileprivate_derivative)
Expand Down
8 changes: 4 additions & 4 deletions test/Compatibility/attr_usableFromInline_swift4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// RUN: %target-typecheck-verify-swift -enable-testing -swift-version 4 -disable-objc-attr-requires-foundation-module -enable-objc-interop

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

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

@usableFromInline internal func internalVersioned() {}
// OK
Expand All @@ -14,11 +14,11 @@
// OK

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

internal class InternalClass {
@usableFromInline public func publicVersioned() {}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal declarations, but 'publicVersioned()' is public}}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
}

fileprivate class filePrivateClass {
Expand Down
12 changes: 6 additions & 6 deletions test/Compatibility/attr_usableFromInline_swift42.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// RUN: %target-typecheck-verify-swift -enable-testing -swift-version 4.2 -disable-objc-attr-requires-foundation-module -enable-objc-interop

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

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

@usableFromInline internal func internalVersioned() {}
// OK
Expand All @@ -14,12 +14,12 @@
// OK

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

internal class InternalClass {
// expected-note@-1 2{{type declared here}}
@usableFromInline public func publicVersioned() {}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal declarations, but 'publicVersioned()' is public}}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but 'publicVersioned()' is public}}
}

fileprivate class filePrivateClass {
Expand Down Expand Up @@ -119,7 +119,7 @@ where T : InternalProtocol,

@usableFromInline
protocol BadProtocol : InternalProtocol {
// expected-warning@-1 {{protocol refined by '@usableFromInline' protocol should be '@usableForInline' or public}}
// expected-warning@-1 {{protocol refined by '@usableFromInline' protocol should be '@usableFromInline' or public}}
associatedtype X : InternalProtocol
// expected-warning@-1 {{type referenced from a requirement of an associated type in a '@usableFromInline' protocol should be '@usableFromInline' or public}}
associatedtype Y = InternalStruct
Expand All @@ -128,7 +128,7 @@ protocol BadProtocol : InternalProtocol {

@usableFromInline
protocol AnotherBadProtocol where Self.T : InternalProtocol {
// expected-warning@-1 {{protocol used by '@usableFromInline' protocol should be '@usableForInline' or public}}
// expected-warning@-1 {{protocol used by '@usableFromInline' protocol should be '@usableFromInline' or public}}
associatedtype T
}

Expand Down
8 changes: 6 additions & 2 deletions test/SPI/local_spi_decls.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Checks for SPI declarations and limited exportability.

// RUN: %empty-directory(%t)
// RUN: %target-typecheck-verify-swift -I %t -verify-ignore-unknown -enable-library-evolution -swift-version 5
// RUN: %target-typecheck-verify-swift -I %t -verify-ignore-unknown -enable-library-evolution -swift-version 5 -package-name myPkg

// Without -enable-library-evolution the exportability check looks at struct internal properties.
// RUN: %target-typecheck-verify-swift -I %t -verify-ignore-unknown -swift-version 5
// RUN: %target-typecheck-verify-swift -I %t -verify-ignore-unknown -swift-version 5 -package-name myPkg

// SPI declarations
@_spi(MySPI) public func spiFunc() {}
Expand Down Expand Up @@ -120,3 +120,7 @@ public func inlinableSPI() {
spiFunc()
let _ = SPIClass()
}

@_spi(S) func internalFunc() {} // expected-error {{internal global function cannot be declared '@_spi' because only public and open declarations can be '@_spi'}}

@_spi(S) package func packageFunc() {} // expected-error {{package global function cannot be declared '@_spi' because only public and open declarations can be '@_spi'}}
Loading