Skip to content

Commit d976d46

Browse files
authored
Merge pull request #10661 from DougGregor/gsb-get-nested-type-without-partial
2 parents ae61020 + c1cb8eb commit d976d46

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ class GenericSignatureBuilder::PotentialArchetype {
15961596

15971597
/// \brief Retrieve (or create) a nested type with the given name.
15981598
PotentialArchetype *getNestedType(Identifier Name,
1599+
ArchetypeResolutionKind kind,
15991600
GenericSignatureBuilder &builder);
16001601

16011602
/// \brief Retrieve (or create) a nested type with a known associated type.

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,7 @@ static void concretizeNestedTypeFromConcreteParent(
17751775

17761776
PotentialArchetype *PotentialArchetype::getNestedType(
17771777
Identifier nestedName,
1778+
ArchetypeResolutionKind kind,
17781779
GenericSignatureBuilder &builder) {
17791780
// If we already have a nested type with this name, return it.
17801781
auto known = NestedTypes.find(nestedName);
@@ -1783,8 +1784,7 @@ PotentialArchetype *PotentialArchetype::getNestedType(
17831784

17841785
// Retrieve the nested archetype anchor, which is the best choice (so far)
17851786
// for this nested type.
1786-
return getNestedArchetypeAnchor(nestedName, builder,
1787-
ArchetypeResolutionKind::AlwaysPartial);
1787+
return getNestedArchetypeAnchor(nestedName, builder, kind);
17881788
}
17891789

17901790
PotentialArchetype *PotentialArchetype::getNestedType(
@@ -1916,7 +1916,7 @@ PotentialArchetype *PotentialArchetype::getNestedArchetypeAnchor(
19161916

19171917
auto rep = getRepresentative();
19181918
if (rep != this) {
1919-
auto existingPA = rep->getNestedType(name, builder);
1919+
auto existingPA = rep->getNestedType(name, kind, builder);
19201920

19211921
auto sameNamedSource =
19221922
RequirementSource::forNestedTypeNameMatch(existingPA);
@@ -2040,7 +2040,7 @@ PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
20402040
if (assocType)
20412041
existingPA = rep->getNestedType(assocType, builder);
20422042
else
2043-
existingPA = rep->getNestedType(name, builder);
2043+
existingPA = rep->getNestedType(concreteDecl, builder);
20442044
}
20452045
}
20462046

@@ -2304,7 +2304,10 @@ void ArchetypeType::resolveNestedType(
23042304
auto parentPA =
23052305
builder.resolveArchetype(interfaceType,
23062306
ArchetypeResolutionKind::CompleteWellFormed);
2307-
auto memberPA = parentPA->getNestedType(nested.first, builder);
2307+
auto memberPA = parentPA->getNestedType(
2308+
nested.first,
2309+
ArchetypeResolutionKind::CompleteWellFormed,
2310+
builder);
23082311
auto result = memberPA->getTypeInContext(builder, genericEnv);
23092312
assert(!nested.second ||
23102313
nested.second->isEqual(result) ||
@@ -2903,13 +2906,13 @@ ConstraintResult GenericSignatureBuilder::addConformanceRequirement(
29032906

29042907
/// Perform typo correction on the given nested type, producing the
29052908
/// corrected name (if successful).
2906-
static Identifier typoCorrectNestedType(
2907-
GenericSignatureBuilder::PotentialArchetype *pa) {
2909+
static AssociatedTypeDecl *typoCorrectNestedType(
2910+
GenericSignatureBuilder::PotentialArchetype *pa) {
29082911
StringRef name = pa->getNestedName().str();
29092912

29102913
// Look through all of the associated types of all of the protocols
29112914
// to which the parent conforms.
2912-
llvm::SmallVector<Identifier, 2> bestMatches;
2915+
llvm::SmallVector<AssociatedTypeDecl *, 2> bestMatches;
29132916
unsigned bestEditDistance = UINT_MAX;
29142917
unsigned maxScore = (name.size() + 1) / 3;
29152918
for (auto proto : pa->getParent()->getConformsTo()) {
@@ -2927,21 +2930,21 @@ static Identifier typoCorrectNestedType(
29272930
bestMatches.clear();
29282931
}
29292932
if (dist == bestEditDistance)
2930-
bestMatches.push_back(assocType->getName());
2933+
bestMatches.push_back(assocType);
29312934
}
29322935
}
29332936

29342937
// FIXME: Look through the superclass.
29352938

29362939
// If we didn't find any matches at all, fail.
29372940
if (bestMatches.empty())
2938-
return Identifier();
2941+
return nullptr;
29392942

29402943
// Make sure that we didn't find more than one match at the best
29412944
// edit distance.
29422945
for (auto other : llvm::makeArrayRef(bestMatches).slice(1)) {
29432946
if (other != bestMatches.front())
2944-
return Identifier();
2947+
return nullptr;
29452948
}
29462949

29472950
return bestMatches.front();
@@ -2976,8 +2979,8 @@ ConstraintResult GenericSignatureBuilder::resolveUnresolvedType(
29762979
return ConstraintResult::Unresolved;
29772980

29782981
// Try to typo correct to a nested type name.
2979-
Identifier correction = typoCorrectNestedType(pa);
2980-
if (correction.empty()) {
2982+
auto correction = typoCorrectNestedType(pa);
2983+
if (!correction) {
29812984
pa->setInvalid();
29822985
return ConstraintResult::Conflicting;
29832986
}
@@ -2988,8 +2991,7 @@ ConstraintResult GenericSignatureBuilder::resolveUnresolvedType(
29882991

29892992
// Resolve the associated type and merge the potential archetypes.
29902993
auto replacement = pa->getParent()->getNestedType(correction, *this);
2991-
pa->resolveAssociatedType(replacement->getResolvedAssociatedType(),
2992-
*this);
2994+
pa->resolveAssociatedType(correction, *this);
29932995
addSameTypeRequirement(pa, replacement,
29942996
RequirementSource::forNestedTypeNameMatch(pa),
29952997
UnresolvedHandlingKind::GenerateConstraints);

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,22 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
131131
ArchetypeResolutionKind::CompleteWellFormed);
132132
assert(basePA && "Missing potential archetype for base");
133133

134+
// Local function to produce an error "no such member type" and return.
135+
auto invalidMemberType = [&] {
136+
// Complain that there is no suitable type.
137+
Identifier name = ref->getIdentifier();
138+
SourceLoc nameLoc = ref->getIdLoc();
139+
TC.diagnose(nameLoc, diag::invalid_member_type, name, baseTy)
140+
.highlight(baseRange);
141+
return ErrorType::get(TC.Context);
142+
};
143+
134144
// Retrieve the potential archetype for the nested type.
135-
auto nestedPA = basePA->getNestedType(ref->getIdentifier(), Builder);
145+
auto nestedPA = basePA->getNestedType(ref->getIdentifier(),
146+
ArchetypeResolutionKind::WellFormed,
147+
Builder);
148+
if (!nestedPA)
149+
return invalidMemberType();
136150

137151
// If this potential archetype was renamed due to typo correction,
138152
// complain and fix it.
@@ -145,7 +159,10 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
145159
nestedPA->setAlreadyDiagnosedRename();
146160

147161
// Go get the actual nested type.
148-
nestedPA = basePA->getNestedType(newName, Builder);
162+
nestedPA = basePA->getNestedType(newName,
163+
ArchetypeResolutionKind::WellFormed,
164+
Builder);
165+
assert(nestedPA && "Nested type should have been available");
149166
assert(!nestedPA->wasRenamed());
150167
}
151168

@@ -183,13 +200,7 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
183200
}
184201

185202
assert(nestedPA->isUnresolved() && "meaningless unresolved type");
186-
187-
// Complain that there is no suitable type.
188-
Identifier name = ref->getIdentifier();
189-
SourceLoc nameLoc = ref->getIdLoc();
190-
TC.diagnose(nameLoc, diag::invalid_member_type, name, baseTy)
191-
.highlight(baseRange);
192-
return ErrorType::get(TC.Context);
203+
return invalidMemberType();
193204
}
194205

195206
bool CompleteGenericTypeResolver::areSameType(Type type1, Type type2) {

validation-test/compiler_crashers_2_fixed/0109-sr4737.swift renamed to validation-test/compiler_crashers_2/0109-sr4737.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-frontend %s -typecheck
1+
// RUN: not --crash %target-swift-frontend %s -typecheck
22

33
// REQUIRES: long_test
44

0 commit comments

Comments
 (0)