Skip to content

Commit 8531361

Browse files
committed
[AST] Introduce ExistentialTypeRepr, which is the type repr for an
existential type spelled with `any`.
1 parent c75a1ec commit 8531361

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,11 @@ ERROR(unchecked_not_inheritance_clause,none,
46184618
ERROR(unchecked_not_existential,none,
46194619
"'unchecked' attribute cannot apply to non-protocol type %0", (Type))
46204620

4621+
ERROR(explicit_existential_not_supported,none,
4622+
"explicit 'any' not supported; use frontend flag "
4623+
"-enable-explicit-existential-types to enable this feature",
4624+
())
4625+
46214626
ERROR(nonisolated_let,none,
46224627
"'nonisolated' is meaningless on 'let' declarations because "
46234628
"they are immutable",

include/swift/AST/TypeRepr.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,34 @@ class NamedOpaqueReturnTypeRepr : public TypeRepr {
11581158
friend class TypeRepr;
11591159
};
11601160

1161+
/// A TypeRepr for an existential type spelled with \c any
1162+
///
1163+
/// Can appear anywhere a normal existential type would. This is
1164+
/// purely a more explicit spelling for existential types.
1165+
class ExistentialTypeRepr: public TypeRepr {
1166+
TypeRepr *Constraint;
1167+
SourceLoc AnyLoc;
1168+
1169+
public:
1170+
ExistentialTypeRepr(SourceLoc anyLoc, TypeRepr *constraint)
1171+
: TypeRepr(TypeReprKind::Existential), Constraint(constraint),
1172+
AnyLoc(anyLoc) {}
1173+
1174+
TypeRepr *getConstraint() const { return Constraint; }
1175+
1176+
static bool classof(const TypeRepr *T) {
1177+
return T->getKind() == TypeReprKind::Existential;
1178+
}
1179+
static bool classof(const ExistentialTypeRepr *T) { return true; }
1180+
1181+
private:
1182+
SourceLoc getStartLocImpl() const { return AnyLoc; }
1183+
SourceLoc getEndLocImpl() const { return Constraint->getEndLoc(); }
1184+
SourceLoc getLocImpl() const { return AnyLoc; }
1185+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1186+
friend class TypeRepr;
1187+
};
1188+
11611189
/// TypeRepr for a user-specified placeholder (essentially, a user-facing
11621190
/// representation of an anonymous type variable.
11631191
///
@@ -1285,6 +1313,7 @@ inline bool TypeRepr::isSimple() const {
12851313
case TypeReprKind::Composition:
12861314
case TypeReprKind::OpaqueReturn:
12871315
case TypeReprKind::NamedOpaqueReturn:
1316+
case TypeReprKind::Existential:
12881317
return false;
12891318
case TypeReprKind::SimpleIdent:
12901319
case TypeReprKind::GenericIdent:

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ TYPEREPR(Metatype, TypeRepr)
5555
TYPEREPR(Protocol, TypeRepr)
5656
TYPEREPR(OpaqueReturn, TypeRepr)
5757
TYPEREPR(NamedOpaqueReturn, TypeRepr)
58+
TYPEREPR(Existential, TypeRepr)
5859
TYPEREPR(Placeholder, TypeRepr)
5960
ABSTRACT_TYPEREPR(Specifier, TypeRepr)
6061
TYPEREPR(InOut, SpecifierTypeRepr)

include/swift/AST/Types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,8 @@ inline bool TypeBase::hasSimpleTypeRepr() const {
64366436

64376437
case TypeKind::ProtocolComposition: {
64386438
// 'Any', 'AnyObject' and single protocol compositions are simple
6439+
// FIXME: single protocol compositions spelled with `any` are not
6440+
// simple.
64396441
auto composition = cast<const ProtocolCompositionType>(this);
64406442
auto memberCount = composition->getMembers().size();
64416443
if (composition->hasExplicitAnyObject())

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,12 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
30953095
PrintWithColorRAII(OS, ParenthesisColor) << ')';
30963096
}
30973097

3098+
void visitExistentialTypeRepr(ExistentialTypeRepr *T) {
3099+
printCommon("type_existential");
3100+
printRec(T->getConstraint());
3101+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
3102+
}
3103+
30983104
void visitPlaceholderTypeRepr(PlaceholderTypeRepr *T) {
30993105
printCommon("type_placeholder");
31003106
PrintWithColorRAII(OS, ParenthesisColor) << ')';

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,10 @@ bool Traversal::visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
18631863
return doIt(T->getBase());
18641864
}
18651865

1866+
bool Traversal::visitExistentialTypeRepr(ExistentialTypeRepr *T) {
1867+
return doIt(T->getConstraint());
1868+
}
1869+
18661870
bool Traversal::visitPlaceholderTypeRepr(PlaceholderTypeRepr *T) {
18671871
return false;
18681872
}

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,7 @@ directReferencesForTypeRepr(Evaluator &evaluator,
23192319

23202320
case TypeReprKind::OpaqueReturn:
23212321
case TypeReprKind::NamedOpaqueReturn:
2322+
case TypeReprKind::Existential:
23222323
return { };
23232324

23242325
case TypeReprKind::Fixed:

lib/AST/TypeRepr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,12 @@ void OpaqueReturnTypeRepr::printImpl(ASTPrinter &Printer,
479479
printTypeRepr(Constraint, Printer, Opts);
480480
}
481481

482+
void ExistentialTypeRepr::printImpl(ASTPrinter &Printer,
483+
const PrintOptions &Opts) const {
484+
Printer.printKeyword("any", Opts, /*Suffix=*/" ");
485+
printTypeRepr(Constraint, Printer, Opts);
486+
}
487+
482488
SourceLoc NamedOpaqueReturnTypeRepr::getStartLocImpl() const {
483489
return GenericParams->getLAngleLoc();
484490
}

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,16 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
20582058
: ErrorType::get(getASTContext());
20592059
}
20602060

2061+
case TypeReprKind::Existential: {
2062+
if (!getASTContext().LangOpts.EnableExplicitExistentialTypes &&
2063+
!(options & TypeResolutionFlags::SilenceErrors)) {
2064+
diagnose(repr->getLoc(), diag::explicit_existential_not_supported);
2065+
}
2066+
2067+
auto *existential = cast<ExistentialTypeRepr>(repr);
2068+
return resolveType(existential->getConstraint(), options);
2069+
}
2070+
20612071
case TypeReprKind::NamedOpaqueReturn:
20622072
return resolveType(cast<NamedOpaqueReturnTypeRepr>(repr)->getBase(),
20632073
options);

0 commit comments

Comments
 (0)