Skip to content

Commit 617ef13

Browse files
authored
Merge pull request #40968 from hborla/5.6-parse-any-type-expr
[5.6][Parser] Parse `(any P).self`
2 parents 700a6e4 + 37061be commit 617ef13

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,14 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
16251625
return makeParserResult(new (Context) UnresolvedPatternExpr(pattern));
16261626
}
16271627

1628+
// 'any' followed by another identifier is an existential type.
1629+
if (Tok.isContextualKeyword("any") &&
1630+
peekToken().is(tok::identifier)) {
1631+
ParserResult<TypeRepr> ty = parseType();
1632+
auto *typeExpr = new (Context) TypeExpr(ty.get());
1633+
return makeParserResult(typeExpr);
1634+
}
1635+
16281636
LLVM_FALLTHROUGH;
16291637
case tok::kw_Self: // Self
16301638
return parseExprIdentifier();

test/type/explicit_existential.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,15 @@ func typealiasMemberReferences(metatype: Wrapper.Type) {
202202
let _: Wrapper.E.Protocol = metatype.E.self
203203
let _: (any Wrapper.E).Type = metatype.E.self
204204
}
205+
206+
func testAnyTypeExpr() {
207+
let _: (any P).Type = (any P).self
208+
209+
func test(_: (any P).Type) {}
210+
test((any P).self)
211+
212+
// expected-error@+2 {{expected member name or constructor call after type name}}
213+
// expected-note@+1 {{use '.self' to reference the type object}}
214+
let invalid = any P
215+
test(invalid)
216+
}

0 commit comments

Comments
 (0)