Skip to content

[Parser] Parse (any P).self #40949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,14 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
return makeParserResult(new (Context) UnresolvedPatternExpr(pattern));
}

// 'any' followed by another identifier is an existential type.
if (Tok.isContextualKeyword("any") &&
peekToken().is(tok::identifier)) {
ParserResult<TypeRepr> ty = parseType();
auto *typeExpr = new (Context) TypeExpr(ty.get());
return makeParserResult(typeExpr);
}

LLVM_FALLTHROUGH;
case tok::kw_Self: // Self
return parseExprIdentifier();
Expand Down
12 changes: 12 additions & 0 deletions test/type/explicit_existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,15 @@ func typealiasMemberReferences(metatype: Wrapper.Type) {
let _: Wrapper.E.Protocol = metatype.E.self
let _: (any Wrapper.E).Type = metatype.E.self
}

func testAnyTypeExpr() {
let _: (any P).Type = (any P).self

func test(_: (any P).Type) {}
test((any P).self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth testing a few other cases like

(any P) & (any Q)
any (P & Q)

and some invalid ones like

any foo()

etc


// expected-error@+2 {{expected member name or constructor call after type name}}
// expected-note@+1 {{use '.self' to reference the type object}}
let invalid = any P
test(invalid)
}