@@ -1931,7 +1931,8 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
1931
1931
1932
1932
// Strip the "is function input" bits unless this is a type that knows about
1933
1933
// them.
1934
- if (!isa<SpecifierTypeRepr>(repr) && !isa<TupleTypeRepr>(repr) &&
1934
+ if (options.is (TypeResolverContext::FunctionInput) &&
1935
+ !isa<SpecifierTypeRepr>(repr) && !isa<TupleTypeRepr>(repr) &&
1935
1936
!isa<AttributedTypeRepr>(repr) && !isa<FunctionTypeRepr>(repr) &&
1936
1937
!isa<IdentTypeRepr>(repr) &&
1937
1938
!isa<ImplicitlyUnwrappedOptionalTypeRepr>(repr)) {
@@ -2049,7 +2050,7 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
2049
2050
}
2050
2051
2051
2052
auto *existential = cast<ExistentialTypeRepr>(repr);
2052
- return resolveType (existential-> getConstraint () , options);
2053
+ return resolveExistentialType (existential, options);
2053
2054
}
2054
2055
2055
2056
case TypeReprKind::NamedOpaqueReturn:
@@ -3363,6 +3364,14 @@ TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
3363
3364
return ErrorType::get (getASTContext ());
3364
3365
}
3365
3366
3367
+ bool isConstraintType = (result->is <ProtocolType>() ||
3368
+ result->is <ProtocolCompositionType>());
3369
+ if (isConstraintType &&
3370
+ getASTContext ().LangOpts .EnableExplicitExistentialTypes &&
3371
+ options.isConstraintImplicitExistential ()) {
3372
+ return ExistentialType::get (result);
3373
+ }
3374
+
3366
3375
// Hack to apply context-specific @escaping to a typealias with an underlying
3367
3376
// function type.
3368
3377
if (result->is <FunctionType>())
@@ -3542,6 +3551,9 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
3542
3551
case TypeResolverContext::TypeAliasDecl:
3543
3552
case TypeResolverContext::GenericTypeAliasDecl:
3544
3553
case TypeResolverContext::GenericRequirement:
3554
+ case TypeResolverContext::SameTypeRequirement:
3555
+ case TypeResolverContext::ProtocolMetatypeBase:
3556
+ case TypeResolverContext::MetatypeBase:
3545
3557
case TypeResolverContext::ImmediateOptionalTypeArgument:
3546
3558
case TypeResolverContext::InExpression:
3547
3559
case TypeResolverContext::EditorPlaceholderExpr:
@@ -3684,7 +3696,8 @@ TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
3684
3696
};
3685
3697
3686
3698
for (auto tyR : repr->getTypes ()) {
3687
- auto ty = resolveType (tyR, options.withoutContext ());
3699
+ auto ty = resolveType (tyR,
3700
+ options.withContext (TypeResolverContext::GenericRequirement));
3688
3701
if (ty->hasError ()) return ty;
3689
3702
3690
3703
auto nominalDecl = ty->getAnyNominal ();
@@ -3721,19 +3734,27 @@ TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
3721
3734
3722
3735
// In user-written types, AnyObject constraints always refer to the
3723
3736
// AnyObject type in the standard library.
3724
- return ProtocolCompositionType::get (getASTContext (), Members,
3725
- /* HasExplicitAnyObject=*/ false );
3737
+ auto composition =
3738
+ ProtocolCompositionType::get (getASTContext (), Members,
3739
+ /* HasExplicitAnyObject=*/ false );
3740
+ if (getASTContext ().LangOpts .EnableExplicitExistentialTypes &&
3741
+ options.isConstraintImplicitExistential ()) {
3742
+ composition = ExistentialType::get (composition);
3743
+ }
3744
+ return composition;
3726
3745
}
3727
3746
3728
3747
NeverNullType
3729
3748
TypeResolver::resolveExistentialType (ExistentialTypeRepr *repr,
3730
3749
TypeResolutionOptions options) {
3731
- auto constraintType = resolveType (repr->getConstraint (), options);
3750
+ auto constraintType = resolveType (repr->getConstraint (),
3751
+ options.withContext (TypeResolverContext::GenericRequirement));
3752
+ if (constraintType->is <ExistentialMetatypeType>())
3753
+ return constraintType;
3732
3754
3733
3755
auto anyStart = repr->getAnyLoc ();
3734
3756
auto anyEnd = Lexer::getLocForEndOfToken (getASTContext ().SourceMgr , anyStart);
3735
- if (!constraintType->isExistentialType () &&
3736
- !constraintType->is <ExistentialMetatypeType>()) {
3757
+ if (!constraintType->isExistentialType ()) {
3737
3758
diagnose (repr->getLoc (), diag::any_not_existential,
3738
3759
constraintType->isTypeParameter (),
3739
3760
constraintType)
@@ -3754,7 +3775,8 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
3754
3775
NeverNullType TypeResolver::resolveMetatypeType (MetatypeTypeRepr *repr,
3755
3776
TypeResolutionOptions options) {
3756
3777
// The instance type of a metatype is always abstract, not SIL-lowered.
3757
- auto ty = resolveType (repr->getBase (), options.withoutContext ());
3778
+ auto ty = resolveType (repr->getBase (),
3779
+ options.withContext (TypeResolverContext::MetatypeBase));
3758
3780
if (ty->hasError ()) {
3759
3781
return ErrorType::get (getASTContext ());
3760
3782
}
@@ -3775,7 +3797,8 @@ NeverNullType TypeResolver::resolveMetatypeType(MetatypeTypeRepr *repr,
3775
3797
NeverNullType
3776
3798
TypeResolver::buildMetatypeType (MetatypeTypeRepr *repr, Type instanceType,
3777
3799
Optional<MetatypeRepresentation> storedRepr) {
3778
- if (instanceType->isAnyExistentialType ()) {
3800
+ if (instanceType->isAnyExistentialType () &&
3801
+ !instanceType->is <ExistentialType>()) {
3779
3802
// TODO: diagnose invalid representations?
3780
3803
return ExistentialMetatypeType::get (instanceType, storedRepr);
3781
3804
} else {
@@ -3786,7 +3809,8 @@ TypeResolver::buildMetatypeType(MetatypeTypeRepr *repr, Type instanceType,
3786
3809
NeverNullType TypeResolver::resolveProtocolType (ProtocolTypeRepr *repr,
3787
3810
TypeResolutionOptions options) {
3788
3811
// The instance type of a metatype is always abstract, not SIL-lowered.
3789
- auto ty = resolveType (repr->getBase (), options.withoutContext ());
3812
+ auto ty = resolveType (repr->getBase (),
3813
+ options.withContext (TypeResolverContext::ProtocolMetatypeBase));
3790
3814
if (ty->hasError ()) {
3791
3815
return ErrorType::get (getASTContext ());
3792
3816
}
0 commit comments