Skip to content

Commit d671a65

Browse files
committed
[Generic signature builder] paper over problems with generic typealiases in protocols.
Generic typealiases are entirely broken as archetypes, but some things mostly worked with them before introducing ResolvedType, so let's restore that behaviour.
1 parent d514e01 commit d671a65

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,11 @@ class GenericSignatureBuilder {
630630
///
631631
/// This returns either a non-typealias potential archetype or a Type, if \c
632632
/// type is concrete.
633-
ResolvedType resolve(UnresolvedType type);
633+
// FIXME: the hackTypeFromGenericTypeAlias is just temporarily patching over
634+
// problems with generic typealiases (see the comment on the ResolvedType
635+
// function)
636+
ResolvedType resolve(UnresolvedType type,
637+
bool hackTypeFromGenericTypeAlias = false);
634638

635639
/// \brief Dump all of the requirements, both specified and inferred.
636640
LLVM_ATTRIBUTE_DEPRECATED(

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@ struct GenericSignatureBuilder::ResolvedType {
525525
return ResolvedType(t);
526526
}
527527

528+
// FIXME: this probably shouldn't exist, the potential archetype modelling of
529+
// generic typealiases is fundamentally broken (aka they're not modelled at
530+
// all), but some things with them mostly work, so we just maintain that,
531+
// despite this causing crashes and weird behaviour.
532+
static ResolvedType forConcreteTypeFromGenericTypeAlias(Type t) {
533+
return ResolvedType(t);
534+
}
535+
528536
static ResolvedType forPotentialArchetype(PotentialArchetype *pa) {
529537
assert(!(pa->getParent() && pa->getTypeAliasDecl()) &&
530538
"typealias is only considered resolved when new");
@@ -1347,12 +1355,17 @@ auto GenericSignatureBuilder::resolveArchetype(Type type) -> PotentialArchetype
13471355
return nullptr;
13481356
}
13491357

1350-
auto GenericSignatureBuilder::resolve(UnresolvedType paOrT) -> ResolvedType {
1358+
auto GenericSignatureBuilder::resolve(UnresolvedType paOrT,
1359+
bool hackTypeFromGenericTypeAlias)
1360+
-> ResolvedType {
13511361
auto pa = paOrT.dyn_cast<PotentialArchetype *>();
13521362
if (auto type = paOrT.dyn_cast<Type>()) {
13531363
pa = resolveArchetype(type);
1354-
if (!pa)
1364+
if (!pa) {
1365+
if (hackTypeFromGenericTypeAlias)
1366+
return ResolvedType::forConcreteTypeFromGenericTypeAlias(type);
13551367
return ResolvedType::forConcreteType(type);
1368+
}
13561369
}
13571370

13581371
pa = pa->getRepresentative();
@@ -1370,12 +1383,17 @@ auto GenericSignatureBuilder::resolve(UnresolvedType paOrT) -> ResolvedType {
13701383
// The right-hand side of the typealias could itself be an archetype
13711384
// (e.g. protocol P { associatedtype A; typealias B = A }), so we need to
13721385
// resolve that. However, the archetype should always be resolved far enough
1373-
// upon creation to not be another type alias (verified by the assertion
1374-
// below), and hence this function doesn't need to be recursive..
1386+
// upon creation to not be another type alias (verified by the ResolvedType
1387+
// constructors below), and hence this function doesn't need to be recursive.
13751388
auto concrete = pa->getConcreteType();
13761389
auto rhsPA = resolveArchetype(concrete);
1377-
if (!rhsPA)
1390+
if (!rhsPA) {
1391+
// FIXME: same as hackTypeFromGenericTypeAlias
1392+
if (pa->getTypeAliasDecl()->getGenericParams())
1393+
return ResolvedType::forConcreteTypeFromGenericTypeAlias(concrete);
1394+
13781395
return ResolvedType::forConcreteType(concrete);
1396+
}
13791397

13801398
return ResolvedType::forPotentialArchetype(rhsPA);
13811399
}

0 commit comments

Comments
 (0)