22
22
#include " swift/AST/Expr.h"
23
23
#include " swift/AST/FileUnit.h"
24
24
#include " swift/AST/GenericEnvironment.h"
25
- #include " swift/AST/LazyResolver .h"
25
+ #include " swift/AST/LocalArchetypeRequirementCollector .h"
26
26
#include " swift/AST/Module.h"
27
27
#include " swift/AST/NameLookup.h"
28
28
#include " swift/AST/ParameterList.h"
@@ -3634,20 +3634,37 @@ const TypeLowering *TypeConverter::getTypeLoweringForExpansion(
3634
3634
return nullptr ;
3635
3635
}
3636
3636
3637
- static GenericSignature
3638
- getEffectiveGenericSignature (DeclContext *dc,
3639
- CaptureInfo captureInfo) {
3637
+ static GenericSignatureWithCapturedEnvironments
3638
+ getGenericSignatureWithCapturedEnvironments (DeclContext *dc,
3639
+ CaptureInfo captureInfo) {
3640
+ auto capturedEnvs = captureInfo.getGenericEnvironments ();
3641
+
3642
+ // A closure or local function does not need a generic signature if it
3643
+ // doesn't capture the primary generic environment or any pack element
3644
+ // environments from the outer scope.
3640
3645
if (dc->getParent ()->isLocalContext () &&
3646
+ capturedEnvs.empty () &&
3641
3647
!captureInfo.hasGenericParamCaptures ())
3642
- return nullptr ;
3648
+ return GenericSignatureWithCapturedEnvironments ();
3649
+
3650
+ auto genericSig = dc->getGenericSignatureOfContext ();
3651
+ if (capturedEnvs.empty ())
3652
+ return GenericSignatureWithCapturedEnvironments (genericSig);
3653
+
3654
+ // Build a new generic signature where all captured element archetypes
3655
+ // are represented by new generic parameters.
3656
+ auto newSig = buildGenericSignatureWithCapturedEnvironments (
3657
+ dc->getASTContext (), genericSig, capturedEnvs);
3643
3658
3644
- return dc->getGenericSignatureOfContext ();
3659
+ LLVM_DEBUG (llvm::dbgs () << " -- effective generic signature: " << newSig << " \n " );
3660
+ return GenericSignatureWithCapturedEnvironments (genericSig, newSig, capturedEnvs);
3645
3661
}
3646
3662
3647
- static GenericSignature
3648
- getEffectiveGenericSignature (AnyFunctionRef fn,
3649
- CaptureInfo captureInfo) {
3650
- return getEffectiveGenericSignature (fn.getAsDeclContext (), captureInfo);
3663
+ static GenericSignatureWithCapturedEnvironments
3664
+ getGenericSignatureWithCapturedEnvironments (AnyFunctionRef fn,
3665
+ CaptureInfo captureInfo) {
3666
+ return getGenericSignatureWithCapturedEnvironments (
3667
+ fn.getAsDeclContext (), captureInfo);
3651
3668
}
3652
3669
3653
3670
static CanGenericSignature
@@ -3704,7 +3721,7 @@ static CanAnyFunctionType getDefaultArgGeneratorInterfaceType(
3704
3721
canResultTy = removeNoEscape (canResultTy);
3705
3722
3706
3723
// Get the generic signature from the surrounding context.
3707
- auto sig = TC.getConstantGenericSignature (c);
3724
+ auto sig = TC.getGenericSignatureWithCapturedEnvironments (c). genericSig ;
3708
3725
3709
3726
// FIXME: Verify ExtInfo state is correct, not working by accident.
3710
3727
CanAnyFunctionType::ExtInfo info;
@@ -3757,7 +3774,8 @@ static CanAnyFunctionType getPropertyWrapperBackingInitializerInterfaceType(
3757
3774
inputType = interfaceType->getCanonicalType ();
3758
3775
}
3759
3776
3760
- GenericSignature sig = TC.getConstantGenericSignature (c);
3777
+ GenericSignature sig = TC.getGenericSignatureWithCapturedEnvironments (c)
3778
+ .genericSig ;
3761
3779
3762
3780
AnyFunctionType::Param param (
3763
3781
inputType, Identifier (),
@@ -3839,7 +3857,16 @@ getFunctionInterfaceTypeWithCaptures(TypeConverter &TC,
3839
3857
3840
3858
// Capture generic parameters from the enclosing context if necessary.
3841
3859
auto closure = *constant.getAnyFunctionRef ();
3842
- auto genericSig = getEffectiveGenericSignature (closure, captureInfo);
3860
+ auto sig = getGenericSignatureWithCapturedEnvironments (closure, captureInfo);
3861
+
3862
+ if (funcType->hasArchetype ()) {
3863
+ assert (isa<FunctionType>(funcType));
3864
+ auto substType = Type (funcType).subst (
3865
+ MapLocalArchetypesOutOfContext (sig.baseGenericSig , sig.capturedEnvs ),
3866
+ MakeAbstractConformanceForGenericType (),
3867
+ SubstFlags::PreservePackExpansionLevel);
3868
+ funcType = cast<FunctionType>(substType->getCanonicalType ());
3869
+ }
3843
3870
3844
3871
auto innerExtInfo =
3845
3872
AnyFunctionType::ExtInfoBuilder (FunctionType::Representation::Thin,
@@ -3853,7 +3880,7 @@ getFunctionInterfaceTypeWithCaptures(TypeConverter &TC,
3853
3880
.build ();
3854
3881
3855
3882
return CanAnyFunctionType::get (
3856
- getCanonicalSignatureOrNull (genericSig),
3883
+ getCanonicalSignatureOrNull (sig. genericSig ),
3857
3884
funcType.getParams (), funcType.getResult (),
3858
3885
innerExtInfo);
3859
3886
}
@@ -3939,12 +3966,9 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
3939
3966
case SILDeclRef::Kind::Func: {
3940
3967
CanAnyFunctionType funcTy;
3941
3968
if (auto *ACE = c.loc .dyn_cast <AbstractClosureExpr *>()) {
3942
- // FIXME: Closures could have an interface type computed by Sema.
3943
- funcTy = cast<AnyFunctionType>(
3944
- ACE->getType ()->mapTypeOutOfContext ()->getCanonicalType ());
3969
+ funcTy = cast<AnyFunctionType>(ACE->getType ()->getCanonicalType ());
3945
3970
} else {
3946
- funcTy = cast<AnyFunctionType>(
3947
- vd->getInterfaceType ()->getCanonicalType ());
3971
+ funcTy = cast<AnyFunctionType>(vd->getInterfaceType ()->getCanonicalType ());
3948
3972
}
3949
3973
return getFunctionInterfaceTypeWithCaptures (*this , funcTy, c);
3950
3974
}
@@ -4007,10 +4031,10 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
4007
4031
llvm_unreachable (" Unhandled SILDeclRefKind in switch." );
4008
4032
}
4009
4033
4010
- GenericSignature
4011
- TypeConverter::getConstantGenericSignature (SILDeclRef c) {
4034
+ GenericSignatureWithCapturedEnvironments
4035
+ TypeConverter::getGenericSignatureWithCapturedEnvironments (SILDeclRef c) {
4012
4036
auto *vd = c.loc .dyn_cast <ValueDecl *>();
4013
-
4037
+
4014
4038
// / Get the function generic params, including outer params.
4015
4039
switch (c.kind ) {
4016
4040
case SILDeclRef::Kind::Func:
@@ -4019,16 +4043,17 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
4019
4043
case SILDeclRef::Kind::Destroyer:
4020
4044
case SILDeclRef::Kind::Deallocator: {
4021
4045
auto captureInfo = getLoweredLocalCaptures (c);
4022
- return getEffectiveGenericSignature (
4046
+ return :: getGenericSignatureWithCapturedEnvironments (
4023
4047
*c.getAnyFunctionRef (), captureInfo);
4024
4048
}
4025
4049
case SILDeclRef::Kind::IVarInitializer:
4026
4050
case SILDeclRef::Kind::IVarDestroyer:
4027
- return cast<ClassDecl>(vd)->getGenericSignature ();
4051
+ return GenericSignatureWithCapturedEnvironments (
4052
+ cast<ClassDecl>(vd)->getGenericSignature ());
4028
4053
case SILDeclRef::Kind::DefaultArgGenerator: {
4029
4054
// Use the generic environment of the original function.
4030
4055
auto captureInfo = getLoweredLocalCaptures (c);
4031
- return getEffectiveGenericSignature (
4056
+ return :: getGenericSignatureWithCapturedEnvironments (
4032
4057
vd->getInnermostDeclContext (), captureInfo);
4033
4058
}
4034
4059
case SILDeclRef::Kind::PropertyWrapperBackingInitializer:
@@ -4045,25 +4070,28 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
4045
4070
} else {
4046
4071
enclosingDecl = SILDeclRef (cast<AbstractFunctionDecl>(dc));
4047
4072
}
4048
- return getConstantGenericSignature (enclosingDecl);
4073
+ return getGenericSignatureWithCapturedEnvironments (enclosingDecl);
4049
4074
}
4050
- return dc->getGenericSignatureOfContext ();
4075
+ return GenericSignatureWithCapturedEnvironments (
4076
+ dc->getGenericSignatureOfContext ());
4051
4077
}
4052
4078
case SILDeclRef::Kind::EnumElement:
4053
4079
case SILDeclRef::Kind::GlobalAccessor:
4054
4080
case SILDeclRef::Kind::StoredPropertyInitializer:
4055
- return vd->getDeclContext ()->getGenericSignatureOfContext ();
4081
+ return GenericSignatureWithCapturedEnvironments (
4082
+ vd->getDeclContext ()->getGenericSignatureOfContext ());
4056
4083
case SILDeclRef::Kind::EntryPoint:
4057
4084
case SILDeclRef::Kind::AsyncEntryPoint:
4058
- llvm_unreachable ( " Doesn't have generic signature " );
4085
+ return GenericSignatureWithCapturedEnvironments ( );
4059
4086
}
4060
4087
4061
4088
llvm_unreachable (" Unhandled SILDeclRefKind in switch." );
4062
4089
}
4063
4090
4064
4091
GenericEnvironment *
4065
4092
TypeConverter::getConstantGenericEnvironment (SILDeclRef c) {
4066
- return getConstantGenericSignature (c).getGenericEnvironment ();
4093
+ return getGenericSignatureWithCapturedEnvironments (c)
4094
+ .genericSig .getGenericEnvironment ();
4067
4095
}
4068
4096
4069
4097
SILType TypeConverter::getSubstitutedStorageType (TypeExpansionContext context,
0 commit comments