Skip to content

Commit bfd6d96

Browse files
authored
Merge pull request #64019 from jckarter/se-0366-consume-final-spelling
Implement `consume x` operator with the accepted SE-0366 syntax.
2 parents b3683c1 + ecceb02 commit bfd6d96

24 files changed

+506
-420
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,13 @@ ERROR(expected_expr_after_try, none,
13601360
ERROR(expected_expr_after_await, none,
13611361
"expected expression after 'await'", ())
13621362
ERROR(expected_expr_after_move, none,
1363-
"expected expression after '_move'", ())
1363+
"expected expression after 'consume'", ())
13641364
ERROR(expected_expr_after_borrow, none,
13651365
"expected expression after '_borrow'", ())
13661366

1367+
WARNING(move_consume_final_spelling, none,
1368+
"'_move' has been renamed to 'consume', and the '_move' spelling will be removed shortly", ())
1369+
13671370
// Cast expressions
13681371
ERROR(expected_type_after_is,none,
13691372
"expected type after 'is'", ())

include/swift/AST/DiagnosticsSIL.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,15 @@ ERROR(sil_moveonlychecker_missed_copy, none,
796796

797797
// move kills copyable values checker diagnostics
798798
ERROR(sil_movekillscopyablevalue_value_consumed_more_than_once, none,
799-
"'%0' used after being moved", (StringRef))
799+
"'%0' used after being consumed", (StringRef))
800800
NOTE(sil_movekillscopyablevalue_move_here, none,
801-
"move here", ())
801+
"consume here", ())
802802
NOTE(sil_movekillscopyablevalue_use_here, none,
803803
"use here", ())
804804
NOTE(sil_movekillscopyablevalue_value_consumed_in_loop, none,
805-
"cyclic move here. move will occur multiple times in the loop", ())
805+
"consume here would occur multiple times in loop", ())
806806
ERROR(sil_movekillscopyablevalue_move_applied_to_unsupported_move, none,
807-
"_move applied to value that the compiler does not support checking",
807+
"'consume' applied to value that the compiler does not support checking",
808808
())
809809

810810
#define UNDEFINE_DIAGNOSTIC_MACROS

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6805,7 +6805,7 @@ ERROR(concurrency_task_to_thread_model_global_actor_annotation,none,
68056805
ERROR(moveOnly_not_allowed_here,none,
68066806
"'moveOnly' only applies to structs or enums", ())
68076807
ERROR(move_expression_not_passed_lvalue,none,
6808-
"'move' can only be applied to lvalues", ())
6808+
"'consume' can only be applied to lvalues", ())
68096809
ERROR(borrow_expression_not_passed_lvalue,none,
68106810
"'borrow' can only be applied to lvalues", ())
68116811

include/swift/Parse/Parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ class Parser {
631631
return false;
632632

633633
// followed by either an identifier, `self`, or `Self`.
634-
return peekToken().isAny(tok::identifier, tok::kw_self, tok::kw_Self);
634+
return !peekToken().isAtStartOfLine()
635+
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::kw_Self);
635636
}
636637

637638
/// Read tokens until we get to one of the specified tokens, then

lib/Parse/ParseExpr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,27 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
406406
return sub;
407407
}
408408

409+
if (Tok.isContextualKeyword("consume")
410+
&& peekToken().isAny(tok::identifier, tok::kw_self)
411+
&& !peekToken().isAtStartOfLine()) {
412+
Tok.setKind(tok::contextual_keyword);
413+
414+
SourceLoc consumeLoc = consumeToken();
415+
ParserResult<Expr> sub =
416+
parseExprSequenceElement(diag::expected_expr_after_move, isExprBasic);
417+
if (!sub.hasCodeCompletion() && !sub.isNull()) {
418+
sub = makeParserResult(new (Context) MoveExpr(consumeLoc, sub.get()));
419+
}
420+
return sub;
421+
}
422+
409423
if (Context.LangOpts.hasFeature(Feature::MoveOnly)) {
410424
if (Tok.isContextualKeyword("_move")) {
411425
Tok.setKind(tok::contextual_keyword);
412426
SourceLoc awaitLoc = consumeToken();
427+
diagnose(Tok, diag::move_consume_final_spelling)
428+
.fixItReplace(awaitLoc, "consume");
429+
413430
ParserResult<Expr> sub =
414431
parseExprSequenceElement(diag::expected_expr_after_move, isExprBasic);
415432
if (!sub.hasCodeCompletion() && !sub.isNull()) {

test/Constraints/moveonly_constraints.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func testBasic(_ mo: __shared MO) {
101101
func checkBasicBoxes() {
102102
let mo = MO()
103103

104-
let vb = ValBox(_move mo) // expected-error 2{{move-only type 'MO' cannot be used with generics yet}}
104+
let vb = ValBox(consume mo) // expected-error 2{{move-only type 'MO' cannot be used with generics yet}}
105105
_ = vb.get()
106106
_ = vb.val
107107

test/DebugInfo/move_function_dbginfo.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public var falseValue: Bool { false }
8080
public func copyableValueTest() {
8181
let k = Klass()
8282
k.doSomething()
83-
let m = _move k
83+
let m = consume k
8484
m.doSomething()
8585
}
8686

@@ -126,7 +126,7 @@ public func copyableValueTest() {
126126
// DWARF-NEXT: DW_AT_type (
127127
public func copyableArgTest(_ k: __owned Klass) {
128128
k.doSomething()
129-
let m = _move k
129+
let m = consume k
130130
m.doSomething()
131131
}
132132

@@ -168,7 +168,7 @@ public func copyableArgTest(_ k: __owned Klass) {
168168
public func copyableVarTest() {
169169
var k = Klass()
170170
k.doSomething()
171-
let m = _move k
171+
let m = consume k
172172
m.doSomething()
173173
k = Klass()
174174
k.doSomething()
@@ -211,7 +211,7 @@ public func copyableVarTest() {
211211
// DWARF-NEXT: DW_AT_type (
212212
public func copyableVarArgTest(_ k: inout Klass) {
213213
k.doSomething()
214-
let m = _move k
214+
let m = consume k
215215
m.doSomething()
216216
k = Klass()
217217
k.doSomething()
@@ -261,7 +261,7 @@ public func copyableVarArgTest(_ k: inout Klass) {
261261
public func addressOnlyValueTest<T : P>(_ x: T) {
262262
let k = x
263263
k.doSomething()
264-
let m = _move k
264+
let m = consume k
265265
m.doSomething()
266266
}
267267

@@ -303,7 +303,7 @@ public func addressOnlyValueTest<T : P>(_ x: T) {
303303
// DWARF-NEXT: DW_AT_type (
304304
public func addressOnlyValueArgTest<T : P>(_ k: __owned T) {
305305
k.doSomething()
306-
let m = _move k
306+
let m = consume k
307307
m.doSomething()
308308
}
309309

@@ -348,7 +348,7 @@ public func addressOnlyValueArgTest<T : P>(_ k: __owned T) {
348348
public func addressOnlyVarTest<T : P>(_ x: T) {
349349
var k = x
350350
k.doSomething()
351-
let m = _move k
351+
let m = consume k
352352
m.doSomething()
353353
k = x
354354
k.doSomething()
@@ -393,7 +393,7 @@ public func addressOnlyVarTest<T : P>(_ x: T) {
393393
// DWARF-NEXT: DW_AT_artificial (true)
394394
public func addressOnlyVarArgTest<T : P>(_ k: inout T, _ x: T) {
395395
k.doSomething()
396-
let m = _move k
396+
let m = consume k
397397
m.doSomething()
398398
k = x
399399
k.doSomething()
@@ -416,7 +416,7 @@ public func copyableValueCCFlowTest() {
416416
let k = Klass()
417417
k.doSomething()
418418
if trueValue {
419-
let m = _move k
419+
let m = consume k
420420
m.doSomething()
421421
}
422422
}
@@ -433,7 +433,7 @@ public func copyableValueCCFlowTest() {
433433
public func copyableValueArgCCFlowTest(_ k: __owned Klass) {
434434
k.doSomething()
435435
if trueValue {
436-
let m = _move k
436+
let m = consume k
437437
m.doSomething()
438438
}
439439
}
@@ -463,7 +463,7 @@ public func copyableVarTestCCFlowReinitOutOfBlockTest() {
463463
var k = Klass()
464464
k.doSomething()
465465
if trueValue {
466-
let m = _move k
466+
let m = consume k
467467
m.doSomething()
468468
}
469469
k = Klass()
@@ -493,7 +493,7 @@ public func copyableVarTestCCFlowReinitOutOfBlockTest() {
493493
public func copyableVarArgTestCCFlowReinitOutOfBlockTest(_ k: inout Klass) {
494494
k.doSomething()
495495
if trueValue {
496-
let m = _move k
496+
let m = consume k
497497
m.doSomething()
498498
}
499499
k = Klass()
@@ -528,7 +528,7 @@ public func copyableVarTestCCFlowReinitInBlockTest() {
528528
var k = Klass()
529529
k.doSomething()
530530
if trueValue {
531-
let m = _move k
531+
let m = consume k
532532
m.doSomething()
533533
k = Klass()
534534
}
@@ -562,7 +562,7 @@ public func copyableVarTestCCFlowReinitInBlockTest() {
562562
public func copyableVarArgTestCCFlowReinitInBlockTest(_ k: inout Klass) {
563563
k.doSomething()
564564
if trueValue {
565-
let m = _move k
565+
let m = consume k
566566
m.doSomething()
567567
k = Klass()
568568
}
@@ -594,7 +594,7 @@ public func addressOnlyVarTestCCFlowReinitOutOfBlockTest<T : P>(_ x: T.Type) {
594594
var k = T.value
595595
k.doSomething()
596596
if trueValue {
597-
let m = _move k
597+
let m = consume k
598598
m.doSomething()
599599
}
600600
k = T.value
@@ -625,7 +625,7 @@ public func addressOnlyVarTestCCFlowReinitOutOfBlockTest<T : P>(_ x: T.Type) {
625625
public func addressOnlyVarArgTestCCFlowReinitOutOfBlockTest<T : P>(_ k: inout (any P), _ x: T.Type) {
626626
k.doSomething()
627627
if trueValue {
628-
let m = _move k
628+
let m = consume k
629629
m.doSomething()
630630
}
631631
k = T.value
@@ -659,7 +659,7 @@ public func addressOnlyVarTestCCFlowReinitInBlockTest<T : P>(_ x: T.Type) {
659659
var k = T.value
660660
k.doSomething()
661661
if trueValue {
662-
let m = _move k
662+
let m = consume k
663663
m.doSomething()
664664
k = T.value
665665
}
@@ -692,7 +692,7 @@ public func addressOnlyVarTestCCFlowReinitInBlockTest<T : P>(_ x: T.Type) {
692692
public func addressOnlyVarArgTestCCFlowReinitInBlockTest<T : P>(_ k: inout (any P), _ x: T.Type) {
693693
k.doSomething()
694694
if trueValue {
695-
let m = _move k
695+
let m = consume k
696696
m.doSomething()
697697
k = T.value
698698
}

test/DebugInfo/move_function_dbginfo_async.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public func forceSplit5() async {}
8282
// DWARF-NEXT: DW_AT_name ("msg")
8383
public func letSimpleTest<T>(_ msg: __owned T) async {
8484
await forceSplit()
85-
use(_move msg)
85+
use(consume msg)
8686
}
8787

8888
// CHECK-LABEL: define swifttailcc void @"$s27move_function_dbginfo_async13varSimpleTestyyxz_xtYalF"(%swift.context* swiftasync %0, %swift.opaque* %1, %swift.opaque* noalias %2, %swift.type* %T)
@@ -146,7 +146,7 @@ public func letSimpleTest<T>(_ msg: __owned T) async {
146146
// DWARF: DW_TAG_formal_parameter
147147
// DWARF-NEXT: DW_AT_name ("msg")
148148
//
149-
// We reinitialize our value in this funclet and then _move it and then
149+
// We reinitialize our value in this funclet and then consume it and then
150150
// reinitialize it again. So we have two different live ranges. Sadly, we don't
151151
// validate that the first live range doesn't start at the beginning of the
152152
// function. But we have lldb tests to validate that.
@@ -161,7 +161,7 @@ public func letSimpleTest<T>(_ msg: __owned T) async {
161161
// DWARF-SAME: DW_OP_entry_value([[ASYNC_REG]]), DW_OP_plus_uconst 0x[[MSG_LOC]], DW_OP_plus_uconst 0x8, DW_OP_deref
162162
// DWARF-NEXT: DW_AT_name ("msg")
163163
//
164-
// We did not _move the value again here, so we just get a normal entry value for
164+
// We did not consume the value again here, so we just get a normal entry value for
165165
// the entire function.
166166
//
167167
// DWARF: DW_AT_linkage_name ("$s3out13varSimpleTestyyxz_xtYalFTQ4_")
@@ -179,10 +179,10 @@ public func letSimpleTest<T>(_ msg: __owned T) async {
179179
// Change name to varSimpleTestArg
180180
public func varSimpleTest<T>(_ msg: inout T, _ msg2: T) async {
181181
await forceSplit()
182-
use(_move msg)
182+
use(consume msg)
183183
await forceSplit()
184184
msg = msg2
185-
let msg3 = _move msg
185+
let msg3 = consume msg
186186
let _ = msg3
187187
msg = msg2
188188
await forceSplit()
@@ -277,7 +277,7 @@ public func varSimpleTestVar() async {
277277
var k = Klass()
278278
k.doSomething()
279279
await forceSplit()
280-
let m = _move k
280+
let m = consume k
281281
m.doSomething()
282282
await forceSplit()
283283
k = Klass()
@@ -383,7 +383,7 @@ public func varSimpleTestVar() async {
383383
public func letArgCCFlowTrueTest<T>(_ msg: __owned T) async {
384384
await forceSplit1()
385385
if trueValue {
386-
use(_move msg)
386+
use(consume msg)
387387
await forceSplit2()
388388
} else {
389389
await forceSplit3()
@@ -534,7 +534,7 @@ public func letArgCCFlowTrueTest<T>(_ msg: __owned T) async {
534534
public func varArgCCFlowTrueTest<T : P>(_ msg: inout T) async {
535535
await forceSplit1()
536536
if trueValue {
537-
use(_move msg)
537+
use(consume msg)
538538
await forceSplit2()
539539
} else {
540540
await forceSplit3()

test/Interpreter/moveonly_bufferview.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Array {
3131
}
3232

3333
func testBufferView(_ x: __owned [Int]) {
34-
var y = _move x
34+
var y = consume x
3535
// CHECK: 1
3636
// CHECK: 2
3737
// CHECK: 3
@@ -46,7 +46,7 @@ func testBufferView(_ x: __owned [Int]) {
4646
func getBool() -> Bool { return true }
4747

4848
func testConditionalBufferView(_ x: __owned [Int]) {
49-
(_move x).withUnsafeBufferPointer {
49+
(consume x).withUnsafeBufferPointer {
5050
let y = BufferView(ptr: $0)
5151
// CHECK: 4
5252
// CHECK: 5

0 commit comments

Comments
 (0)