21
21
#include " swift/AST/DiagnosticsSIL.h"
22
22
#include " swift/AST/Expr.h"
23
23
#include " swift/AST/GenericEnvironment.h"
24
- #include " swift/AST/LazyResolver .h"
24
+ #include " swift/AST/LocalArchetypeRequirementCollector .h"
25
25
#include " swift/AST/Module.h"
26
26
#include " swift/AST/NameLookup.h"
27
27
#include " swift/AST/ParameterList.h"
@@ -3622,20 +3622,37 @@ const TypeLowering *TypeConverter::getTypeLoweringForExpansion(
3622
3622
return nullptr ;
3623
3623
}
3624
3624
3625
- static GenericSignature
3626
- getEffectiveGenericSignature (DeclContext *dc,
3627
- CaptureInfo captureInfo) {
3625
+ static GenericSignatureWithCapturedEnvironments
3626
+ getGenericSignatureWithCapturedEnvironments (DeclContext *dc,
3627
+ CaptureInfo captureInfo) {
3628
+ auto capturedEnvs = captureInfo.getGenericEnvironments ();
3629
+
3630
+ // A closure or local function does not need a generic signature if it
3631
+ // doesn't capture the primary generic environment or any pack element
3632
+ // environments from the outer scope.
3628
3633
if (dc->getParent ()->isLocalContext () &&
3634
+ capturedEnvs.empty () &&
3629
3635
!captureInfo.hasGenericParamCaptures ())
3630
- return nullptr ;
3636
+ return GenericSignatureWithCapturedEnvironments ();
3637
+
3638
+ auto genericSig = dc->getGenericSignatureOfContext ();
3639
+ if (capturedEnvs.empty ())
3640
+ return GenericSignatureWithCapturedEnvironments (genericSig);
3641
+
3642
+ // Build a new generic signature where all captured element archetypes
3643
+ // are represented by new generic parameters.
3644
+ auto newSig = buildGenericSignatureWithCapturedEnvironments (
3645
+ dc->getASTContext (), genericSig, capturedEnvs);
3631
3646
3632
- return dc->getGenericSignatureOfContext ();
3647
+ LLVM_DEBUG (llvm::dbgs () << " -- effective generic signature: " << newSig << " \n " );
3648
+ return GenericSignatureWithCapturedEnvironments (genericSig, newSig, capturedEnvs);
3633
3649
}
3634
3650
3635
- static GenericSignature
3636
- getEffectiveGenericSignature (AnyFunctionRef fn,
3637
- CaptureInfo captureInfo) {
3638
- return getEffectiveGenericSignature (fn.getAsDeclContext (), captureInfo);
3651
+ static GenericSignatureWithCapturedEnvironments
3652
+ getGenericSignatureWithCapturedEnvironments (AnyFunctionRef fn,
3653
+ CaptureInfo captureInfo) {
3654
+ return getGenericSignatureWithCapturedEnvironments (
3655
+ fn.getAsDeclContext (), captureInfo);
3639
3656
}
3640
3657
3641
3658
static CanGenericSignature
@@ -3692,7 +3709,7 @@ static CanAnyFunctionType getDefaultArgGeneratorInterfaceType(
3692
3709
canResultTy = removeNoEscape (canResultTy);
3693
3710
3694
3711
// Get the generic signature from the surrounding context.
3695
- auto sig = TC.getConstantGenericSignature (c);
3712
+ auto sig = TC.getGenericSignatureWithCapturedEnvironments (c). genericSig ;
3696
3713
3697
3714
// FIXME: Verify ExtInfo state is correct, not working by accident.
3698
3715
CanAnyFunctionType::ExtInfo info;
@@ -3745,7 +3762,8 @@ static CanAnyFunctionType getPropertyWrapperBackingInitializerInterfaceType(
3745
3762
inputType = interfaceType->getCanonicalType ();
3746
3763
}
3747
3764
3748
- GenericSignature sig = TC.getConstantGenericSignature (c);
3765
+ GenericSignature sig = TC.getGenericSignatureWithCapturedEnvironments (c)
3766
+ .genericSig ;
3749
3767
3750
3768
AnyFunctionType::Param param (
3751
3769
inputType, Identifier (),
@@ -3831,7 +3849,16 @@ getAnyFunctionRefInterfaceType(TypeConverter &TC,
3831
3849
3832
3850
// Capture generic parameters from the enclosing context if necessary.
3833
3851
auto closure = *constant.getAnyFunctionRef ();
3834
- auto genericSig = getEffectiveGenericSignature (closure, captureInfo);
3852
+ auto sig = getGenericSignatureWithCapturedEnvironments (closure, captureInfo);
3853
+
3854
+ if (funcType->hasArchetype ()) {
3855
+ assert (isa<FunctionType>(funcType));
3856
+ auto substType = Type (funcType).subst (
3857
+ MapLocalArchetypesOutOfContext (sig.baseGenericSig , sig.capturedEnvs ),
3858
+ MakeAbstractConformanceForGenericType (),
3859
+ SubstFlags::PreservePackExpansionLevel);
3860
+ funcType = cast<FunctionType>(substType->getCanonicalType ());
3861
+ }
3835
3862
3836
3863
auto innerExtInfo =
3837
3864
AnyFunctionType::ExtInfoBuilder (FunctionType::Representation::Thin,
@@ -3845,7 +3872,7 @@ getAnyFunctionRefInterfaceType(TypeConverter &TC,
3845
3872
.build ();
3846
3873
3847
3874
return CanAnyFunctionType::get (
3848
- getCanonicalSignatureOrNull (genericSig),
3875
+ getCanonicalSignatureOrNull (sig. genericSig ),
3849
3876
funcType.getParams (), funcType.getResult (),
3850
3877
innerExtInfo);
3851
3878
}
@@ -3931,12 +3958,9 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
3931
3958
case SILDeclRef::Kind::Func: {
3932
3959
CanAnyFunctionType funcTy;
3933
3960
if (auto *ACE = c.loc .dyn_cast <AbstractClosureExpr *>()) {
3934
- // FIXME: Closures could have an interface type computed by Sema.
3935
- funcTy = cast<AnyFunctionType>(
3936
- ACE->getType ()->mapTypeOutOfContext ()->getCanonicalType ());
3961
+ funcTy = cast<AnyFunctionType>(ACE->getType ()->getCanonicalType ());
3937
3962
} else {
3938
- funcTy = cast<AnyFunctionType>(
3939
- vd->getInterfaceType ()->getCanonicalType ());
3963
+ funcTy = cast<AnyFunctionType>(vd->getInterfaceType ()->getCanonicalType ());
3940
3964
}
3941
3965
return getAnyFunctionRefInterfaceType (*this , funcTy, c);
3942
3966
}
@@ -3999,10 +4023,10 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
3999
4023
llvm_unreachable (" Unhandled SILDeclRefKind in switch." );
4000
4024
}
4001
4025
4002
- GenericSignature
4003
- TypeConverter::getConstantGenericSignature (SILDeclRef c) {
4026
+ GenericSignatureWithCapturedEnvironments
4027
+ TypeConverter::getGenericSignatureWithCapturedEnvironments (SILDeclRef c) {
4004
4028
auto *vd = c.loc .dyn_cast <ValueDecl *>();
4005
-
4029
+
4006
4030
// / Get the function generic params, including outer params.
4007
4031
switch (c.kind ) {
4008
4032
case SILDeclRef::Kind::Func:
@@ -4011,16 +4035,17 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
4011
4035
case SILDeclRef::Kind::Destroyer:
4012
4036
case SILDeclRef::Kind::Deallocator: {
4013
4037
auto captureInfo = getLoweredLocalCaptures (c);
4014
- return getEffectiveGenericSignature (
4038
+ return :: getGenericSignatureWithCapturedEnvironments (
4015
4039
*c.getAnyFunctionRef (), captureInfo);
4016
4040
}
4017
4041
case SILDeclRef::Kind::IVarInitializer:
4018
4042
case SILDeclRef::Kind::IVarDestroyer:
4019
- return cast<ClassDecl>(vd)->getGenericSignature ();
4043
+ return GenericSignatureWithCapturedEnvironments (
4044
+ cast<ClassDecl>(vd)->getGenericSignature ());
4020
4045
case SILDeclRef::Kind::DefaultArgGenerator: {
4021
4046
// Use the generic environment of the original function.
4022
4047
auto captureInfo = getLoweredLocalCaptures (c);
4023
- return getEffectiveGenericSignature (
4048
+ return :: getGenericSignatureWithCapturedEnvironments (
4024
4049
vd->getInnermostDeclContext (), captureInfo);
4025
4050
}
4026
4051
case SILDeclRef::Kind::PropertyWrapperBackingInitializer:
@@ -4037,14 +4062,16 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
4037
4062
} else {
4038
4063
enclosingDecl = SILDeclRef (cast<AbstractFunctionDecl>(dc));
4039
4064
}
4040
- return getConstantGenericSignature (enclosingDecl);
4065
+ return getGenericSignatureWithCapturedEnvironments (enclosingDecl);
4041
4066
}
4042
- return dc->getGenericSignatureOfContext ();
4067
+ return GenericSignatureWithCapturedEnvironments (
4068
+ dc->getGenericSignatureOfContext ());
4043
4069
}
4044
4070
case SILDeclRef::Kind::EnumElement:
4045
4071
case SILDeclRef::Kind::GlobalAccessor:
4046
4072
case SILDeclRef::Kind::StoredPropertyInitializer:
4047
- return vd->getDeclContext ()->getGenericSignatureOfContext ();
4073
+ return GenericSignatureWithCapturedEnvironments (
4074
+ vd->getDeclContext ()->getGenericSignatureOfContext ());
4048
4075
case SILDeclRef::Kind::EntryPoint:
4049
4076
case SILDeclRef::Kind::AsyncEntryPoint:
4050
4077
llvm_unreachable (" Doesn't have generic signature" );
@@ -4055,7 +4082,8 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
4055
4082
4056
4083
GenericEnvironment *
4057
4084
TypeConverter::getConstantGenericEnvironment (SILDeclRef c) {
4058
- return getConstantGenericSignature (c).getGenericEnvironment ();
4085
+ return getGenericSignatureWithCapturedEnvironments (c)
4086
+ .genericSig .getGenericEnvironment ();
4059
4087
}
4060
4088
4061
4089
SILType TypeConverter::getSubstitutedStorageType (TypeExpansionContext context,
0 commit comments