Skip to content

Commit b859ac1

Browse files
committed
RequirementMachine: Unwrap outer TypeAliasType on protocol typealiases
A protocol typealias 'typealias T = X' introduces a same-type requirement 'Self.T == Self.X'. However the right hand side of the requirement was wrapped in a sugared TypeAliasType. This meant if the requirement surfaced in a redundancy diagnostic, it would print as 'Self.T == Self.T'. It could also trigger a request cycle via isInterestingTypealias() in DiagnosticEngine.cpp.
1 parent 48b491d commit b859ac1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,13 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
712712
if (assocTypes.contains(typeAliasDecl->getName()))
713713
continue;
714714

715+
// The structural type of a typealias will always be a TypeAliasType,
716+
// so unwrap it to avoid a requirement that prints as 'Self.T == Self.T'
717+
// in diagnostics.
715718
auto underlyingType = typeAliasDecl->getStructuralType();
719+
if (auto *aliasType = dyn_cast<TypeAliasType>(underlyingType.getPointer()))
720+
underlyingType = aliasType->getSinglyDesugaredType();
721+
716722
if (underlyingType->is<UnboundGenericType>())
717723
continue;
718724

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on
2+
3+
protocol P : Sequence {
4+
typealias Element = Iterator.Element
5+
// expected-warning@-1 {{typealias overriding associated type 'Element' from protocol 'Sequence' is better expressed as same-type constraint on the protocol}}
6+
// expected-warning@-2 {{redundant same-type constraint 'Self.Element' == 'Self.Iterator.Element'}}
7+
}

0 commit comments

Comments
 (0)