Skip to content

Commit c7ddea4

Browse files
committed
[TypeResolver] Fix a few corner cases in type resolution for explicit
existential types. Use the correct type resolver context when resolving SIL metatypes and function types, and diagnose an error if an explicit existential type appears in a protocol composition.
1 parent 2a973b1 commit c7ddea4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,13 @@ TypeResolver::resolveAttributedType(TypeAttributes &attrs, TypeRepr *repr,
22072207
Optional<MetatypeRepresentation> storedRepr;
22082208
// The instance type is not a SIL type.
22092209
auto instanceOptions = options;
2210-
instanceOptions.setContext(None);
2210+
TypeResolverContext context = TypeResolverContext::None;
2211+
if (isa<MetatypeTypeRepr>(repr)) {
2212+
context = TypeResolverContext::MetatypeBase;
2213+
} else if (isa<ProtocolTypeRepr>(repr)) {
2214+
context = TypeResolverContext::ProtocolMetatypeBase;
2215+
}
2216+
instanceOptions.setContext(context);
22112217
instanceOptions -= TypeResolutionFlags::SILType;
22122218

22132219
auto instanceTy = resolveType(base, instanceOptions);
@@ -3106,7 +3112,8 @@ NeverNullType TypeResolver::resolveSILFunctionType(
31063112

31073113
ProtocolConformanceRef witnessMethodConformance;
31083114
if (witnessMethodProtocol) {
3109-
auto resolved = resolveType(witnessMethodProtocol, options);
3115+
auto resolved = resolveType(witnessMethodProtocol,
3116+
options.withContext(TypeResolverContext::GenericRequirement));
31103117
if (resolved->hasError())
31113118
return resolved;
31123119

@@ -3383,9 +3390,7 @@ TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
33833390
}
33843391

33853392
// FIXME: Don't use ExistentialType for AnyObject for now.
3386-
bool isConstraintType = (result->is<ProtocolType>() &&
3387-
!result->isAnyObject());
3388-
if (isConstraintType &&
3393+
if (result->isConstraintType() && !result->isAnyObject() &&
33893394
getASTContext().LangOpts.EnableExplicitExistentialTypes &&
33903395
options.isConstraintImplicitExistential()) {
33913396
return ExistentialType::get(result);
@@ -3728,7 +3733,7 @@ TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
37283733
continue;
37293734
}
37303735

3731-
if (ty->isExistentialType()) {
3736+
if (ty->isConstraintType()) {
37323737
auto layout = ty->getExistentialLayout();
37333738
if (auto superclass = layout.explicitSuperclass)
37343739
if (checkSuperclass(tyR->getStartLoc(), superclass))

0 commit comments

Comments
 (0)