Skip to content

Commit a155209

Browse files
committed
[consume] Fix consume parsing so we handle code completion and dollaridentifiers correctly.
This resulted from explorations around implementing the copy expr. rdar://109479131
1 parent e6f1691 commit a155209

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,15 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
409409
}
410410

411411
if (Tok.isContextualKeyword("consume")
412-
&& peekToken().isAny(tok::identifier, tok::kw_self)
412+
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
413+
tok::code_complete)
413414
&& !peekToken().isAtStartOfLine()) {
414415
Tok.setKind(tok::contextual_keyword);
415416

416417
SourceLoc consumeLoc = consumeToken();
417418
ParserResult<Expr> sub =
418419
parseExprSequenceElement(diag::expected_expr_after_move, isExprBasic);
419-
if (!sub.hasCodeCompletion() && !sub.isNull()) {
420+
if (!sub.isNull()) {
420421
sub = makeParserResult(new (Context) MoveExpr(consumeLoc, sub.get()));
421422
}
422423
return sub;

test/IDE/move_expr.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
func test(myParam: Int) {
5+
consume #^CONSUME^#
6+
// CONSUME: Decl[LocalVar]/Local: myParam[#Int#]; name=myParam
7+
}

test/Parse/move_expr.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,22 @@ struct Foo {
8181
_ = consume $wrapperTest // expected-error{{can only be applied to lvalues}}
8282
}
8383
}
84+
85+
func testParseConsumeWithDollarIdentifier() {
86+
class Klass {}
87+
let f: (Klass) -> () = {
88+
let _ = consume $0
89+
}
90+
_ = f
91+
}
92+
93+
class ParentKlass {}
94+
class ChildKlass : ParentKlass {}
95+
96+
func testAsBindingVariableInSwitch(_ x: ChildKlass) {
97+
switch x {
98+
case let consume as ParentKlass:
99+
_ = consume
100+
break
101+
}
102+
}

0 commit comments

Comments
 (0)