Skip to content

Commit a0892f9

Browse files
Merge pull request #24443 from aschwaighofer/fix_circular_dependency
Fix circular dependency between SIL and AST libraries
2 parents 986d5ca + 52dc2e6 commit a0892f9

File tree

5 files changed

+59
-41
lines changed

5 files changed

+59
-41
lines changed

include/swift/AST/Types.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4828,10 +4828,11 @@ END_CAN_TYPE_WRAPPER(OpaqueTypeArchetypeType, ArchetypeType)
48284828
/// to their underlying types.
48294829
class ReplaceOpaqueTypesWithUnderlyingTypes {
48304830
public:
4831-
SILFunction *context;
4832-
ReplaceOpaqueTypesWithUnderlyingTypes(
4833-
SILFunction *context)
4834-
: context(context) {}
4831+
ModuleDecl *contextModule;
4832+
ResilienceExpansion contextExpansion;
4833+
ReplaceOpaqueTypesWithUnderlyingTypes(ModuleDecl *contextModule,
4834+
ResilienceExpansion contextExpansion)
4835+
: contextModule(contextModule), contextExpansion(contextExpansion) {}
48354836

48364837
/// TypeSubstitutionFn
48374838
Type operator()(SubstitutableType *maybeOpaqueType) const;
@@ -4844,7 +4845,8 @@ class ReplaceOpaqueTypesWithUnderlyingTypes {
48444845
bool shouldPerformSubstitution(OpaqueTypeDecl *opaque) const;
48454846

48464847
static bool shouldPerformSubstitution(OpaqueTypeDecl *opaque,
4847-
SILFunction *context);
4848+
ModuleDecl *contextModule,
4849+
ResilienceExpansion contextExpansion);
48484850
};
48494851

48504852
/// An archetype that represents the dynamic type of an opened existential.

lib/AST/Type.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,12 +2474,36 @@ getArchetypeAndRootOpaqueArchetype(Type maybeOpaqueType) {
24742474

24752475
bool ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
24762476
OpaqueTypeDecl *opaque) const {
2477-
return shouldPerformSubstitution(opaque, context);
2477+
return shouldPerformSubstitution(opaque, contextModule, contextExpansion);
24782478
}
24792479

2480-
static Type substOpaqueTypesWithUnderlyingTypes(
2481-
Type ty, SILFunction *context) {
2482-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(context);
2480+
bool ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
2481+
OpaqueTypeDecl *opaque, ModuleDecl *contextModule,
2482+
ResilienceExpansion contextExpansion) {
2483+
auto namingDecl = opaque->getNamingDecl();
2484+
2485+
// Allow replacement of opaque result types of inlineable function regardless
2486+
// of resilience and in which context.
2487+
if (namingDecl->getAttrs().hasAttribute<InlinableAttr>()) {
2488+
return true;
2489+
}
2490+
// Allow replacement of opaque result types in the context of maximal
2491+
// resilient expansion if the context's and the opaque type's module are the
2492+
// same.
2493+
auto module = namingDecl->getModuleContext();
2494+
if (contextExpansion == ResilienceExpansion::Maximal &&
2495+
module == contextModule)
2496+
return true;
2497+
2498+
// Allow general replacement from non resilient modules. Otherwise, disallow.
2499+
return !module->isResilient();
2500+
}
2501+
2502+
static Type
2503+
substOpaqueTypesWithUnderlyingTypes(Type ty, ModuleDecl *contextModule,
2504+
ResilienceExpansion contextExpansion) {
2505+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(contextModule,
2506+
contextExpansion);
24832507
return ty.subst(replacer, replacer, SubstFlags::SubstituteOpaqueArchetypes);
24842508
}
24852509

@@ -2512,15 +2536,18 @@ operator()(SubstitutableType *maybeOpaqueType) const {
25122536

25132537
// If the type still contains opaque types, recur.
25142538
if (substTy->hasOpaqueArchetype()) {
2515-
return substOpaqueTypesWithUnderlyingTypes(substTy, context);
2539+
return substOpaqueTypesWithUnderlyingTypes(substTy, contextModule,
2540+
contextExpansion);
25162541
}
25172542
return substTy;
25182543
}
25192544

25202545
static ProtocolConformanceRef
25212546
substOpaqueTypesWithUnderlyingTypes(ProtocolConformanceRef ref, Type origType,
2522-
SILFunction *context) {
2523-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(context);
2547+
ModuleDecl *contextModule,
2548+
ResilienceExpansion contextExpansion) {
2549+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(contextModule,
2550+
contextExpansion);
25242551
return ref.subst(origType, replacer, replacer,
25252552
SubstFlags::SubstituteOpaqueArchetypes);
25262553
}
@@ -2562,7 +2589,8 @@ operator()(CanType maybeOpaqueType, Type replacementType,
25622589

25632590
// If the type still contains opaque types, recur.
25642591
if (substTy->hasOpaqueArchetype()) {
2565-
return substOpaqueTypesWithUnderlyingTypes(substRef, substTy, context);
2592+
return substOpaqueTypesWithUnderlyingTypes(substRef, substTy, contextModule,
2593+
contextExpansion);
25662594
}
25672595
return substRef;
25682596
}

lib/SIL/SILFunctionType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,9 @@ static bool areABICompatibleParamsOrReturns(SILType a, SILType b,
27622762
// Opaque types are compatible with their substitution.
27632763
if (inFunction) {
27642764
auto opaqueTypesSubsituted = aa;
2765-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(inFunction);
2765+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
2766+
inFunction->getModule().getSwiftModule(),
2767+
inFunction->getResilienceExpansion());
27662768
if (aa.getASTType()->hasOpaqueArchetype())
27672769
opaqueTypesSubsituted = aa.subst(inFunction->getModule(), replacer,
27682770
replacer, CanGenericSignature(), true);

lib/SIL/SILType.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -591,25 +591,3 @@ bool SILType::isLoweringOf(SILModule &Mod, CanType formalType) {
591591
// Other types are preserved through lowering.
592592
return loweredType.getASTType() == formalType;
593593
}
594-
595-
bool ReplaceOpaqueTypesWithUnderlyingTypes::shouldPerformSubstitution(
596-
OpaqueTypeDecl *opaque, SILFunction *context) {
597-
auto namingDecl = opaque->getNamingDecl();
598-
599-
// Allow replacement of opaque result types of inlineable function regardless
600-
// of resilience and in which context.
601-
if (namingDecl->getAttrs().hasAttribute<InlinableAttr>()) {
602-
return true;
603-
}
604-
// Allow replacement of opaque result types in the context of maximal
605-
// resilient expansion if the context's and the opaque type's module are the
606-
// same.
607-
auto expansion = context->getResilienceExpansion();
608-
auto module = namingDecl->getModuleContext();
609-
if (expansion == ResilienceExpansion::Maximal &&
610-
module == context->getModule().getSwiftModule())
611-
return true;
612-
613-
// Allow general replacement from non resilient modules. Otherwise, disallow.
614-
return !module->isResilient();
615-
}

lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ using namespace swift;
2828

2929
static Type substOpaqueTypesWithUnderlyingTypes(
3030
Type ty, SILFunction *context) {
31-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(context);
31+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
32+
context->getModule().getSwiftModule(), context->getResilienceExpansion());
3233
return ty.subst(replacer, replacer, SubstFlags::SubstituteOpaqueArchetypes);
3334
}
3435

3536
static SubstitutionMap
3637
substOpaqueTypesWithUnderlyingTypes(SubstitutionMap map, SILFunction *context) {
37-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(context);
38+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
39+
context->getModule().getSwiftModule(), context->getResilienceExpansion());
3840
return map.subst(replacer, replacer, SubstFlags::SubstituteOpaqueArchetypes);
3941
}
4042

@@ -292,7 +294,9 @@ class OpaqueSpecializerCloner
292294
return Sty;
293295

294296
// Apply the opaque types substitution.
295-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(&Original);
297+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
298+
Original.getModule().getSwiftModule(),
299+
Original.getResilienceExpansion());
296300
Sty = Ty.subst(Original.getModule(), replacer, replacer,
297301
CanGenericSignature(), true);
298302
return Sty;
@@ -307,7 +311,9 @@ class OpaqueSpecializerCloner
307311
ProtocolConformanceRef remapConformance(Type type,
308312
ProtocolConformanceRef conf) {
309313
// Apply the opaque types substitution.
310-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(&Original);
314+
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
315+
Original.getModule().getSwiftModule(),
316+
Original.getResilienceExpansion());
311317
return conf.subst(type, replacer, replacer,
312318
SubstFlags::SubstituteOpaqueArchetypes);
313319
}
@@ -434,7 +440,9 @@ class OpaqueArchetypeSpecializer : public SILFunctionTransform {
434440
if (auto opaqueTy = type->getAs<OpaqueTypeArchetypeType>()) {
435441
auto opaque = opaqueTy->getDecl();
436442
return ReplaceOpaqueTypesWithUnderlyingTypes::
437-
shouldPerformSubstitution(opaque, context);
443+
shouldPerformSubstitution(opaque,
444+
context->getModule().getSwiftModule(),
445+
context->getResilienceExpansion());
438446
}
439447
return false;
440448
});

0 commit comments

Comments
 (0)