Skip to content

[SwiftParser] Experimental feature flags for _borrow and _move #3018

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
Mar 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ExperimentalFeature: String, CaseIterable {
case valueGenerics
case abiAttribute
case keypathWithMethodMembers
case oldOwnershipOperatorSpellings

/// The name of the feature as it is written in the compiler's `Features.def` file.
public var featureName: String {
Expand All @@ -44,6 +45,8 @@ public enum ExperimentalFeature: String, CaseIterable {
return "ABIAttribute"
case .keypathWithMethodMembers:
return "KeypathWithMethodMembers"
case .oldOwnershipOperatorSpellings:
return "OldOwnershipOperatorSpellings"
}
}

Expand All @@ -68,6 +71,8 @@ public enum ExperimentalFeature: String, CaseIterable {
return "@abi attribute"
case .keypathWithMethodMembers:
return "keypaths with method members"
case .oldOwnershipOperatorSpellings:
return "`_move` and `_borrow` as ownership operators"
}
}

Expand Down
18 changes: 5 additions & 13 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,9 @@ extension Parser {
arena: self.arena
)
)
case (._move, let handle)?:
let moveKeyword = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawConsumeExprSyntax(
consumeKeyword: moveKeyword,
expression: sub,
arena: self.arena
)
)

case (._borrow, let handle)?:
assert(self.experimentalFeatures.contains(.oldOwnershipOperatorSpellings))
Copy link
Member

Choose a reason for hiding this comment

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

Could you make these preconditions?

/// How to choose `assert` vs. `precondition`:
/// - Wherever possible, it is preferable to emit a diagnostic instead of
/// using `precondition`. This way the parser won't crash if the condition is
/// violated.
/// - If you think the diagnostic added above should never be emitted, it is
/// fine to also emit an `assertionFailure` in debug builds to make it easier
/// to debug the unexpected diagnostic.
/// - If in doubt always use `precondition`
/// - `assert` should only be used if checking the assertion has a non-trivial
/// cost and provides little benefit in terms of safety in release builds.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not really sure it worth, although it trivial, The branch is a result of the subject expression, where the experimentalFeatures is handled, which is close enough for not checking seriously. Also even if somehow we enter this branch without oldOwnershipOperatorSpellings feature in release builds, we still have atContextualExpressionModifier() condtion check below so the check is just to make sure the condition check in ExpressionModifierKeyword.init(lexeme:) is working.

FWIW, looking at the existing code, do expression uses precodition but then statement uses assert.

fallthrough
case (.borrow, let handle)?:
if !atContextualExpressionModifier() {
Expand Down Expand Up @@ -531,6 +520,9 @@ extension Parser {
)
)

case (._move, let handle)?:
assert(self.experimentalFeatures.contains(.oldOwnershipOperatorSpellings))
fallthrough
case (.consume, let handle)?:
if !atContextualExpressionModifier() {
break EXPR_PREFIX
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParser/TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ enum ExpressionModifierKeyword: TokenSpecSet {
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await): self = .await
case TokenSpec(._move): self = ._move
case TokenSpec(._borrow): self = ._borrow
case TokenSpec(._move) where experimentalFeatures.contains(.oldOwnershipOperatorSpellings): self = ._move
case TokenSpec(._borrow) where experimentalFeatures.contains(.oldOwnershipOperatorSpellings): self = ._borrow
case TokenSpec(.try): self = .try
case TokenSpec(.borrow): self = .borrow
case TokenSpec(.consume): self = .consume
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1557,18 +1557,18 @@ final class ExpressionTests: ParserTestCase {
)
}

func testMoveExpression() {
assertParse("_move msg")
assertParse("use(_move msg)")
assertParse("_move msg")
assertParse("let b = (_move self).buffer")
func testConsumeExpression() {
assertParse("consume msg")
assertParse("use(consume msg)")
assertParse("consume msg")
assertParse("let b = (consume self).buffer")
}

func testBorrowExpression() {
assertParse("_borrow msg")
assertParse("use(_borrow msg)")
assertParse("_borrow msg")
assertParse("let b = (_borrow self).buffer")
assertParse("borrow msg")
assertParse("use(borrow msg)")
assertParse("borrow msg")
assertParse("let b = (borrow self).buffer")
assertParse("borrow msg")
assertParse("use(borrow msg)")
assertParse("borrow(msg)")
Expand Down
8 changes: 6 additions & 2 deletions Tests/SwiftParserTest/translated/BorrowExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

// This test file has been translated from swift/test/Parse/borrow_expr.swift

@_spi(ExperimentalLanguageFeatures) import SwiftParser
import SwiftSyntax
import XCTest

final class BorrowExprTests: ParserTestCase {
Expand All @@ -23,7 +25,8 @@ final class BorrowExprTests: ParserTestCase {
func testGlobal() {
useString(_borrow global)
}
"""
""",
experimentalFeatures: [.oldOwnershipOperatorSpellings]
)
}

Expand All @@ -36,7 +39,8 @@ final class BorrowExprTests: ParserTestCase {
t = String()
useString(_borrow t)
}
"""
""",
experimentalFeatures: [.oldOwnershipOperatorSpellings]
)
}

Expand Down
60 changes: 58 additions & 2 deletions Tests/SwiftParserTest/translated/MoveExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

// This test file has been translated from swift/test/Parse/move_expr.swift

@_spi(ExperimentalLanguageFeatures) import SwiftParser
import SwiftSyntax
import XCTest

final class MoveExprTests: ParserTestCase {
Expand All @@ -22,7 +24,8 @@ final class MoveExprTests: ParserTestCase {
func testGlobal() {
let _ = _move global
}
"""
""",
experimentalFeatures: [.oldOwnershipOperatorSpellings]
)
}

Expand All @@ -33,7 +36,9 @@ final class MoveExprTests: ParserTestCase {
let t = String()
let _ = _move t
}
"""
""",
experimentalFeatures: [.oldOwnershipOperatorSpellings]

)
}

Expand All @@ -45,7 +50,58 @@ final class MoveExprTests: ParserTestCase {
t = String()
let _ = _move t
}
""",
experimentalFeatures: [.oldOwnershipOperatorSpellings]
)
}

func testMoveExpr4() {
assertParse(
"""
_move(t)
""",
substructure: FunctionCallExprSyntax(
calledExpression: DeclReferenceExprSyntax(baseName: .identifier("_move")),
leftParen: .leftParenToken(),
arguments: [.init(expression: DeclReferenceExprSyntax(baseName: .identifier("t")))],
rightParen: .rightParenToken()
),
experimentalFeatures: [.oldOwnershipOperatorSpellings]
)
}

func testMoveExpr5() {
assertParse(
"""
_move(t)
""",
substructure: FunctionCallExprSyntax(
calledExpression: DeclReferenceExprSyntax(baseName: .identifier("_move")),
leftParen: .leftParenToken(),
arguments: [.init(expression: DeclReferenceExprSyntax(baseName: .identifier("t")))],
rightParen: .rightParenToken()
),
experimentalFeatures: []
)
}

func testMoveExpr6() {
assertParse(
"""
_move1️⃣ t
""",
diagnostics: [
DiagnosticSpec(
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
)
],
fixedSource:
"""
_move
t
""",
experimentalFeatures: []
)
}

Expand Down