Skip to content

Commit a14b5fd

Browse files
authored
Merge pull request #61149 from simanerush/unboundGenericType-crash-fix
Sema: Fix compiler crash when resolving Extension declaration with UnboundGenericType
2 parents 67da6c6 + fb49d8a commit a14b5fd

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,12 +2855,7 @@ ExtendedTypeRequest::evaluate(Evaluator &eval, ExtensionDecl *ext) const {
28552855
if (ext->isInSpecializeExtensionContext())
28562856
options |= TypeResolutionFlags::AllowUsableFromInline;
28572857
const auto resolution = TypeResolution::forStructural(
2858-
ext->getDeclContext(), options,
2859-
[](auto unboundTy) {
2860-
// FIXME: Don't let unbound generic types escape type resolution.
2861-
// For now, just return the unbound generic type.
2862-
return unboundTy;
2863-
},
2858+
ext->getDeclContext(), options, nullptr,
28642859
// FIXME: Don't let placeholder types escape type resolution.
28652860
// For now, just return the placeholder type.
28662861
PlaceholderType::get);

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ static bool doesTypeAliasFullyConstrainAllOuterGenericParams(
350350

351351
TypeChecker::UnsupportedMemberTypeAccessKind
352352
TypeChecker::isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl,
353-
bool hasUnboundOpener) {
353+
bool hasUnboundOpener,
354+
bool isExtensionBinding) {
354355
// We don't allow lookups of a non-generic typealias of an unbound
355356
// generic type, because we have no way to model such a type in the
356357
// AST.
@@ -377,7 +378,7 @@ TypeChecker::isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl,
377378
return UnsupportedMemberTypeAccessKind::AssociatedTypeOfUnboundGeneric;
378379

379380
if (isa<NominalTypeDecl>(typeDecl))
380-
if (!hasUnboundOpener)
381+
if (!hasUnboundOpener && !isExtensionBinding)
381382
return UnsupportedMemberTypeAccessKind::NominalTypeOfUnboundGeneric;
382383
}
383384

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
678678
auto *generic = dyn_cast<GenericIdentTypeRepr>(comp);
679679
if (!generic) {
680680
if (auto *const unboundTy = type->getAs<UnboundGenericType>()) {
681-
if (!options.is(TypeResolverContext::TypeAliasDecl)) {
681+
if (!options.is(TypeResolverContext::TypeAliasDecl) &&
682+
!options.is(TypeResolverContext::ExtensionBinding)) {
682683
// If the resolution object carries an opener, attempt to open
683684
// the unbound generic type.
684685
// TODO: We should be able to just open the generic arguments as N
@@ -1699,6 +1700,7 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
16991700
auto DC = resolution.getDeclContext();
17001701
auto &ctx = DC->getASTContext();
17011702
auto &diags = ctx.Diags;
1703+
auto isExtenstionBinding = options.is(TypeResolverContext::ExtensionBinding);
17021704

17031705
auto maybeDiagnoseBadMemberType = [&](TypeDecl *member, Type memberType,
17041706
AssociatedTypeDecl *inferredAssocType) {
@@ -1712,14 +1714,14 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
17121714
}
17131715

17141716
if (options.contains(TypeResolutionFlags::SilenceErrors)) {
1715-
if (TypeChecker::isUnsupportedMemberTypeAccess(parentTy, member,
1716-
hasUnboundOpener)
1717-
!= TypeChecker::UnsupportedMemberTypeAccessKind::None)
1717+
if (TypeChecker::isUnsupportedMemberTypeAccess(
1718+
parentTy, member, hasUnboundOpener, isExtenstionBinding) !=
1719+
TypeChecker::UnsupportedMemberTypeAccessKind::None)
17181720
return ErrorType::get(ctx);
17191721
}
17201722

1721-
switch (TypeChecker::isUnsupportedMemberTypeAccess(parentTy, member,
1722-
hasUnboundOpener)) {
1723+
switch (TypeChecker::isUnsupportedMemberTypeAccess(
1724+
parentTy, member, hasUnboundOpener, isExtenstionBinding)) {
17231725
case TypeChecker::UnsupportedMemberTypeAccessKind::None:
17241726
break;
17251727

@@ -1870,7 +1872,8 @@ resolveIdentTypeComponent(TypeResolution resolution,
18701872
if (result->is<UnboundGenericType>() &&
18711873
!isa<GenericIdentTypeRepr>(lastComp) &&
18721874
!resolution.getUnboundTypeOpener() &&
1873-
!options.is(TypeResolverContext::TypeAliasDecl)) {
1875+
!options.is(TypeResolverContext::TypeAliasDecl) &&
1876+
!options.is(TypeResolverContext::ExtensionBinding)) {
18741877

18751878
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
18761879
// Tailored diagnostic for custom attributes.

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,8 @@ enum class UnsupportedMemberTypeAccessKind : uint8_t {
924924
/// member of the given base type.
925925
UnsupportedMemberTypeAccessKind
926926
isUnsupportedMemberTypeAccess(Type type, TypeDecl *typeDecl,
927-
bool hasUnboundOpener);
927+
bool hasUnboundOpener,
928+
bool isExtensionBinding = false);
928929

929930
/// @}
930931

test/Generics/unbound.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ do {
137137
assertExactType(of: x1, is: Generic<Int, Bool>.self)
138138
assertExactType(of: x2, is: Generic<Int, Bool>.self)
139139
}
140+
141+
// https://github.com/apple/swift/issues/60922
142+
143+
enum E<T> {}
144+
// expected-note@-1 2 {{generic type 'E' declared here}}
145+
146+
extension E? {}
147+
// expected-error@-1{{reference to generic type 'E' requires arguments in <...>}}
148+
extension Optional<E> {}
149+
// expected-error@-1{{reference to generic type 'E' requires arguments in <...>}}
150+
151+
struct G<T> {}
152+
// expected-note@-1{{generic type 'G' declared here}}
153+
154+
extension G? {}
155+
// expected-error@-1{{reference to generic type 'G' requires arguments in <...>}}

0 commit comments

Comments
 (0)