Skip to content

AST: Fix ReplaceOpaqueTypesWithUnderlyingTypes to consider the curren… #71120

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 25, 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 @@ -1009,6 +1009,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 @@ -1049,7 +1050,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() &&
contextExpansion != ResilienceExpansion::Minimal)
return typeDecl->getEffectiveAccess() > AccessLevel::FilePrivate;

return typeDecl->getEffectiveAccess() > AccessLevel::Internal;
Expand Down Expand Up @@ -1093,10 +1095,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 @@ -1204,9 +1209,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)
}