Skip to content

Commit 53c6038

Browse files
committed
[SwiftParser] Experimental feature flags for _borrow and _move
_borrow and _move should be pased as and ownership operator only when a feature flag is enabled.
1 parent ee01462 commit 53c6038

File tree

6 files changed

+81
-19
lines changed

6 files changed

+81
-19
lines changed

CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public enum ExperimentalFeature: String, CaseIterable {
2222
case valueGenerics
2323
case abiAttribute
2424
case keypathWithMethodMembers
25+
case oldOwnershipOperatorSpellings
2526

2627
/// The name of the feature as it is written in the compiler's `Features.def` file.
2728
public var featureName: String {
@@ -44,6 +45,8 @@ public enum ExperimentalFeature: String, CaseIterable {
4445
return "ABIAttribute"
4546
case .keypathWithMethodMembers:
4647
return "KeypathWithMethodMembers"
48+
case .oldOwnershipOperatorSpellings:
49+
return "OldOwnershipOperatorSpellings"
4750
}
4851
}
4952

@@ -68,6 +71,8 @@ public enum ExperimentalFeature: String, CaseIterable {
6871
return "@abi attribute"
6972
case .keypathWithMethodMembers:
7073
return "keypaths with method members"
74+
case .oldOwnershipOperatorSpellings:
75+
return "`_move` and `_borrow` as owner ship operators"
7176
}
7277
}
7378

Sources/SwiftParser/Expressions.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,20 +481,9 @@ extension Parser {
481481
arena: self.arena
482482
)
483483
)
484-
case (._move, let handle)?:
485-
let moveKeyword = self.eat(handle)
486-
let sub = self.parseSequenceExpressionElement(
487-
flavor: flavor,
488-
pattern: pattern
489-
)
490-
return RawExprSyntax(
491-
RawConsumeExprSyntax(
492-
consumeKeyword: moveKeyword,
493-
expression: sub,
494-
arena: self.arena
495-
)
496-
)
484+
497485
case (._borrow, let handle)?:
486+
assert(self.experimentalFeatures.contains(.oldOwnershipOperatorSpellings))
498487
fallthrough
499488
case (.borrow, let handle)?:
500489
if !atContextualExpressionModifier() {
@@ -531,6 +520,9 @@ extension Parser {
531520
)
532521
)
533522

523+
case (._move, let handle)?:
524+
assert(self.experimentalFeatures.contains(.oldOwnershipOperatorSpellings))
525+
fallthrough
534526
case (.consume, let handle)?:
535527
if !atContextualExpressionModifier() {
536528
break EXPR_PREFIX

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ enum ExpressionModifierKeyword: TokenSpecSet {
705705
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
706706
switch PrepareForKeywordMatch(lexeme) {
707707
case TokenSpec(.await): self = .await
708-
case TokenSpec(._move): self = ._move
709-
case TokenSpec(._borrow): self = ._borrow
708+
case TokenSpec(._move) where experimentalFeatures.contains(.oldOwnershipOperatorSpellings): self = ._move
709+
case TokenSpec(._borrow) where experimentalFeatures.contains(.oldOwnershipOperatorSpellings): self = ._borrow
710710
case TokenSpec(.try): self = .try
711711
case TokenSpec(.borrow): self = .borrow
712712
case TokenSpec(.consume): self = .consume

Sources/SwiftParser/generated/ExperimentalFeatures.swift

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/SwiftParserTest/translated/BorrowExprTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// This test file has been translated from swift/test/Parse/borrow_expr.swift
1414

1515
import XCTest
16+
@_spi(ExperimentalLanguageFeatures) import SwiftParser
17+
import SwiftSyntax
1618

1719
final class BorrowExprTests: ParserTestCase {
1820
func testBorrowExpr1() {
@@ -23,7 +25,8 @@ final class BorrowExprTests: ParserTestCase {
2325
func testGlobal() {
2426
useString(_borrow global)
2527
}
26-
"""
28+
""",
29+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
2730
)
2831
}
2932

@@ -36,7 +39,8 @@ final class BorrowExprTests: ParserTestCase {
3639
t = String()
3740
useString(_borrow t)
3841
}
39-
"""
42+
""",
43+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
4044
)
4145
}
4246

Tests/SwiftParserTest/translated/MoveExprTests.swift

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// This test file has been translated from swift/test/Parse/move_expr.swift
1414

1515
import XCTest
16+
@_spi(ExperimentalLanguageFeatures) import SwiftParser
17+
import SwiftSyntax
1618

1719
final class MoveExprTests: ParserTestCase {
1820
func testMoveExpr1() {
@@ -22,7 +24,8 @@ final class MoveExprTests: ParserTestCase {
2224
func testGlobal() {
2325
let _ = _move global
2426
}
25-
"""
27+
""",
28+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
2629
)
2730
}
2831

@@ -33,7 +36,9 @@ final class MoveExprTests: ParserTestCase {
3336
let t = String()
3437
let _ = _move t
3538
}
36-
"""
39+
""",
40+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
41+
3742
)
3843
}
3944

@@ -45,7 +50,58 @@ final class MoveExprTests: ParserTestCase {
4550
t = String()
4651
let _ = _move t
4752
}
53+
""",
54+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
55+
)
56+
}
57+
58+
func testMoveExpr4() {
59+
assertParse(
60+
"""
61+
_move(t)
62+
""",
63+
substructure: FunctionCallExprSyntax(
64+
calledExpression: DeclReferenceExprSyntax(baseName: .identifier("_move")),
65+
leftParen: .leftParenToken(),
66+
arguments: [.init(expression: DeclReferenceExprSyntax(baseName: .identifier("t")))],
67+
rightParen: .rightParenToken()
68+
),
69+
experimentalFeatures: [.oldOwnershipOperatorSpellings]
70+
)
71+
}
72+
73+
func testMoveExpr5() {
74+
assertParse(
75+
"""
76+
_move(t)
77+
""",
78+
substructure: FunctionCallExprSyntax(
79+
calledExpression: DeclReferenceExprSyntax(baseName: .identifier("_move")),
80+
leftParen: .leftParenToken(),
81+
arguments: [.init(expression: DeclReferenceExprSyntax(baseName: .identifier("t")))],
82+
rightParen: .rightParenToken()
83+
),
84+
experimentalFeatures: []
85+
)
86+
}
87+
88+
func testMoveExpr6() {
89+
assertParse(
90+
"""
91+
_move1️⃣ t
92+
""",
93+
diagnostics: [
94+
DiagnosticSpec(
95+
message: "consecutive statements on a line must be separated by newline or ';'",
96+
fixIts: ["insert newline", "insert ';'"]
97+
),
98+
],
99+
fixedSource:
48100
"""
101+
_move
102+
t
103+
""",
104+
experimentalFeatures: []
49105
)
50106
}
51107

0 commit comments

Comments
 (0)