Skip to content

Fix opaque type substitution in the package cmo resilience domain #73794

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
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
2 changes: 2 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6621,6 +6621,8 @@ enum class OpaqueSubstitutionKind {
// Can be done if the underlying type is accessible from the context we
// substitute into. Private types cannot be accessed from a different TU.
SubstituteSameModuleMaximalResilience,
// Same as previous but with package and above visibility.
SubstituteSamePackageMaximalResilience,
// Substitute in a different module from the opaque defining decl. Can only
// be done if the underlying type is public.
SubstituteNonResilientModule
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10078,6 +10078,11 @@ bool OpaqueTypeDecl::exportUnderlyingType() const {
if (mod->getResilienceStrategy() != ResilienceStrategy::Resilient)
return true;

// If we perform package CMO, in-package clients must have access to the
// underlying type.
if (mod->serializePackageEnabled())
return true;

ValueDecl *namingDecl = getNamingDecl();
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(namingDecl))
return AFD->getResilienceExpansion() == ResilienceExpansion::Minimal;
Expand Down
12 changes: 12 additions & 0 deletions lib/AST/TypeSubstitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,14 @@ ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
module == contextModule)
return OpaqueSubstitutionKind::SubstituteSameModuleMaximalResilience;

// Allow replacement of opaque result types in the context of maximal
// resilient expansion if the context's and the opaque type's module are in
// the same package.
if (contextExpansion == ResilienceExpansion::Maximal &&
module->isResilient() && module->serializePackageEnabled() &&
module->inSamePackage(contextModule))
return OpaqueSubstitutionKind::SubstituteSamePackageMaximalResilience;

// Allow general replacement from non resilient modules. Otherwise, disallow.
if (module->isResilient())
return OpaqueSubstitutionKind::DontSubstitute;
Expand Down Expand Up @@ -1047,6 +1055,10 @@ static bool canSubstituteTypeInto(Type ty, const DeclContext *dc,

return typeDecl->getEffectiveAccess() > AccessLevel::FilePrivate;

case OpaqueSubstitutionKind::SubstituteSamePackageMaximalResilience: {
return typeDecl->getEffectiveAccess() >= AccessLevel::Package;
}

case OpaqueSubstitutionKind::SubstituteNonResilientModule:
// Can't access types that are not public from a different module.
if (dc->getParentModule() == typeDecl->getDeclContext()->getParentModule() &&
Expand Down
54 changes: 54 additions & 0 deletions test/SILOptimizer/package-cmo-opaque-result.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-build-swift %t/Lib.swift \
// RUN: -module-name=Lib -package-name Pkg \
// RUN: -parse-as-library -emit-module -emit-module-path %t/Lib.swiftmodule -I%t \
// RUN: -Xfrontend -experimental-package-cmo -Xfrontend -experimental-allow-non-resilient-access \
// RUN: -O -wmo -enable-library-evolution -Xfrontend -disable-availability-checking
Copy link
Contributor

@elsh elsh May 24, 2024

Choose a reason for hiding this comment

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

Should we throw a proper diagnostics somewhere else so we don't need this -disable-availability-checking flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this flag is used out of convenience, without it availability annotations would have to be used (because of opaque result types).


// RUN: %target-build-swift -module-name=Main -package-name Pkg -I%t -emit-sil -O %t/main.swift -o %t/Main-res.sil
// RUN: %FileCheck %s --check-prefixes=CHECK-OPAQUE < %t/Main-res.sil

// REQUIRES: swift_in_compiler

//--- main.swift

import Lib

// CHECK-OPAQUE: sil @$s4Main023testPackageInSerializedC4FuncyyF : $@convention(thin) () -> ()
// CHECK-OPAQUE: struct $Thing
// CHECK-OPAQUE: struct $Thing1
// CHECK-OPAQUE: function_ref @$s3Lib13getSomeProto2QryF
// CHECK-OPAQUE: function_ref @$s3Lib13getSomeProto3QryF
// CHECK-OPAQUE: } // end sil function '$s4Main023testPackageInSerializedC4FuncyyF'

public func testPackageInSerializedPackageFunc() {
print(getSomeProto())
print(getSomeProto1())
print(getSomeProto2())
print(getSomeProto3())
}

//--- Lib.swift

public protocol SomeProto {}

public struct Thing : SomeProto {}
package struct Thing1 : SomeProto {}
internal struct Thing2 : SomeProto {}
private struct Thing3 : SomeProto {}

// Don't crash on this example.
public func getSomeProto() -> some SomeProto {
return Thing()
}
public func getSomeProto1() -> some SomeProto {
return Thing1()
}
public func getSomeProto2() -> some SomeProto {
return Thing2()
}
public func getSomeProto3() -> some SomeProto {
return Thing3()
}