Skip to content

AST: Fix ReplaceOpaqueTypesWithUnderlyingTypes to consider the current resilience expansion in non resilient mode #70770

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 1 commit into from
Jan 11, 2024
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
14 changes: 11 additions & 3 deletions lib/AST/TypeSubstitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ static Type substOpaqueTypesWithUnderlyingTypesRec(
/// opaque substitutions are or are not allowed.
static bool canSubstituteTypeInto(Type ty, const DeclContext *dc,
OpaqueSubstitutionKind kind,
ResilienceExpansion contextExpansion,
bool isContextWholeModule) {
TypeDecl *typeDecl = ty->getAnyNominal();
if (!typeDecl) {
Expand Down Expand Up @@ -1056,7 +1057,8 @@ static bool canSubstituteTypeInto(Type ty, const DeclContext *dc,

case OpaqueSubstitutionKind::SubstituteNonResilientModule:
// Can't access types that are not public from a different module.
if (dc->getParentModule() == typeDecl->getDeclContext()->getParentModule())
if (dc->getParentModule() == typeDecl->getDeclContext()->getParentModule() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this condition is the same as if (typeDecl->isResilient(contextExpansion)) no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't believe so:

We arrive at this if statement with the assumption SubstituteNonResilientModule (the naming decl of the opaque type is in a non-resilient module).

This means thetypeDecl->isResilient(dc->getParentModule(), contextExpansion) code would return false if dc->getParentModule() == typeDecl->getDeclContext()->getParentModule() and contextExpansion == ResilienceExpansion::Maximal.

bool NominalTypeDecl::isResilient() const {                                     
  if (!isFormallyResilient())                                                   
    return false;                                                               
                                                                                
  return getModuleContext()->isResilient(); // would also return false but we don't get here
} 
bool NominalTypeDecl::isResilient(ModuleDecl *M,                                
                                  ResilienceExpansion expansion) const {        
  switch (expansion) {                                                          
  case ResilienceExpansion::Minimal:                                            
    return isResilient();                                                        
  case ResilienceExpansion::Maximal:                                            
    // We can access declarations from the same module                          
    // non-resiliently in a maximal context.                                             
    if (M == getModuleContext()) {                                              
      return false;                    // <<< === will return false                                                  
    }
    ...                                                                
    // Otherwise, we have to access the declaration resiliently if it's         
    // resilient anywhere.                                                      
    return isResilient();                                                       
  } 

contextExpansion != ResilienceExpansion::Minimal)
return typeDecl->getEffectiveAccess() > AccessLevel::FilePrivate;

return typeDecl->getEffectiveAccess() > AccessLevel::Internal;
Expand Down Expand Up @@ -1100,10 +1102,13 @@ operator()(SubstitutableType *maybeOpaqueType) const {
// context.
auto inContext = this->getContext();
auto isContextWholeModule = this->isWholeModule();
auto contextExpansion = this->contextExpansion;
if (inContext &&
partialSubstTy.findIf(
[inContext, substitutionKind, isContextWholeModule](Type t) -> bool {
[inContext, substitutionKind, isContextWholeModule,
contextExpansion](Type t) -> bool {
if (!canSubstituteTypeInto(t, inContext, substitutionKind,
contextExpansion,
isContextWholeModule))
return true;
return false;
Expand Down Expand Up @@ -1211,9 +1216,12 @@ operator()(CanType maybeOpaqueType, Type replacementType,
// context.
auto inContext = this->getContext();
auto isContextWholeModule = this->isWholeModule();
auto contextExpansion = this->contextExpansion;
if (partialSubstTy.findIf(
[inContext, substitutionKind, isContextWholeModule](Type t) -> bool {
[inContext, substitutionKind, isContextWholeModule,
contextExpansion](Type t) -> bool {
if (!canSubstituteTypeInto(t, inContext, substitutionKind,
contextExpansion,
isContextWholeModule))
return true;
return false;
Expand Down
36 changes: 36 additions & 0 deletions test/IRGen/Inputs/opaque_result_type_internal_inlinable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public protocol P {}

public struct G<T, V> : P {

var t: T
var v: V

public init(_ t: T, _ v: V) {
self.t = t
self.v = v
}
}

struct I {
public init() {
}
}

public struct E : P {
public init() {}

@inlinable
public static var a : some P {
return G(E(), C().b())
}
}

public struct C {
public init() {}

@usableFromInline
internal func b() -> some P {
return G(self, I())
}
}

13 changes: 13 additions & 0 deletions test/IRGen/oapque_result_type_internal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -O -disable-availability-checking -emit-module -emit-module-path=%t/R.swiftmodule -module-name=R %S/Inputs/opaque_result_type_internal_inlinable.swift
// RUN: %target-swift-frontend -O -I %t -disable-availability-checking -c -primary-file %s

import R

// This used to crash because when we were importing E.a the serialized sil
// contained underlying types that this module does not have access to (internal type `I`).

public func testIt() {
var e = E.a
print(e)
}