Skip to content

[Parse] Accept 'self' after 'each' #67206

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
Jul 11, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,10 @@ ERROR(expected_expr_after_copy, none,
"expected expression after 'copy'", ())
ERROR(expected_expr_after_borrow, none,
"expected expression after '_borrow'", ())
ERROR(expected_expr_after_each, none,
"expected expression after 'each'", ())
ERROR(expected_expr_after_repeat, none,
"expected expression after 'repeat'", ())

WARNING(move_consume_final_spelling, none,
"'_move' has been renamed to 'consume', and the '_move' spelling will be removed shortly", ())
Expand Down
73 changes: 38 additions & 35 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,44 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
}
}

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

// 'repeat' as an expression prefix is a pack expansion expression.
if (Tok.is(tok::kw_repeat)) {
SourceLoc repeatLoc = consumeToken();
auto patternExpr = parseExprImpl(
diag::expected_expr_after_repeat, isExprBasic);
if (patternExpr.isNull())
return patternExpr;

auto *expansion =
PackExpansionExpr::create(Context, repeatLoc, patternExpr.get(),
/*genericEnv*/ nullptr);
return makeParserResult(expansion);
}
Comment on lines +478 to +489
Copy link
Member

Choose a reason for hiding this comment

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

The same comment that I left on https://github.com/apple/swift-syntax/pull/1895/files#r1259240007

I just noticed that we already never parse repeat as a function call in the following (not a regression introduced by this PR, this just made me realize it…

func repeat() {}
repeat()


// 'each' followed by another identifier is a pack element expr.
if (Tok.isContextualKeyword("each") &&
peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
tok::code_complete) &&
!peekToken().isAtStartOfLine()) {
SourceLoc loc = consumeToken();
ParserResult<Expr> ref =
parseExprSequenceElement(diag::expected_expr_after_each, isExprBasic);
if (ref.isNull())
return ref;

auto *packRef = PackElementExpr::create(Context, loc, ref.get());
return makeParserResult(packRef);
}

SourceLoc tryLoc;
bool hadTry = consumeIf(tok::kw_try, tryLoc);
llvm::Optional<Token> trySuffix;
Expand Down Expand Up @@ -546,19 +584,6 @@ ParserResult<Expr> Parser::parseExprUnary(Diag<> Message, bool isExprBasic) {
// First check to see if we have the start of a regex literal `/.../`.
tryLexRegexLiteral(/*forUnappliedOperator*/ false);

// 'repeat' as an expression prefix is a pack expansion expression.
if (Tok.is(tok::kw_repeat)) {
SourceLoc repeatLoc = consumeToken();
auto patternExpr = parseExpr(Message);
if (patternExpr.isNull())
return patternExpr;

auto *expansion =
PackExpansionExpr::create(Context, repeatLoc, patternExpr.get(),
/*genericEnv*/ nullptr);
return makeParserResult(expansion);
}

// Try parse an 'if' or 'switch' as an expression. Note we do this here in
// parseExprUnary as we don't allow postfix syntax to hang off such
// expressions to avoid ambiguities such as postfix '.member', which can
Expand Down Expand Up @@ -1715,28 +1740,6 @@ 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) &&
!peekToken().isAtStartOfLine()) {
ParserResult<TypeRepr> ty = parseType();
auto *typeExpr = new (Context) TypeExpr(ty.get());
return makeParserResult(typeExpr);
}

// 'each' followed by another identifier is a pack element expr.
if (Tok.isContextualKeyword("each") &&
peekToken().is(tok::identifier) &&
!peekToken().isAtStartOfLine()) {
SourceLoc loc = consumeToken();
ParserResult<Expr> ref = parseExpr(ID);
if (ref.isNull())
return ref;

auto *packRef = PackElementExpr::create(Context, loc, ref.get());
return makeParserResult(packRef);
}

LLVM_FALLTHROUGH;
}
case tok::kw_Self: // Self
Expand Down
4 changes: 2 additions & 2 deletions test/Interpreter/Inputs/variadic_generic_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public struct Predicate<each Input> {
builder: (repeat Variable<each Input>) -> Expr
) where Expr: Expression<Bool> {
self.variables = (repeat Variable<each Input>())
self.expression = builder(repeat each variables)
self.expression = builder(repeat each self.variables)
}

public func evaluate(
_ input: repeat each Input
) throws -> Bool {
return try expression.evaluate(
.init(repeat (each variables, each input))
.init(repeat (each self.variables, each input))
)
}
}
Expand Down