swift-book icon indicating copy to clipboard operation
swift-book copied to clipboard

copy Missing in Expressions, Keywords, and Grammar

Open tadbyt opened this issue 1 year ago • 0 comments

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/declarations#Borrowing-and-Consuming-Parameters https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure#Keywords-and-Punctuation https://docs.swift.org/swift-book/documentation/the-swift-programming-language/expressions#Prefix-Expressions https://docs.swift.org/swift-book/documentation/the-swift-programming-language/summaryofthegrammar

Description

In the Borrowing and Consuming Parameters subsection of the Declarations section, there is a discussion and examples related to the borrowing parameter modifier. The relevant discussion is:

By default, Swift uses a set of rules to automatically manage object lifetime across function calls, copying values when required. The default rules are designed to minimize overhead in most cases — if you want more specific control, you can apply the borrowing or consuming parameter modifier. In this case, use copy to explicitly mark copy operations.

This is demonstrated in the second example:

// As above, but this `isLessThan` also wants to record the smallest value
func isLessThan(lhs: borrowing A, rhs: borrowing A) -> Bool {
    if lhs < storedValue {
        storedValue = copy lhs
    } else if rhs < storedValue {
        storedValue = copy rhs
    }
    return lhs < rhs
}

This presents three issues in the document. First, copy is not identified as a keyword in the Keywords and Punctuation subsection of the Lexical Structure section. Second, copy isn't identified as a prefix operator in a subsection of Prefix Expressions in the Expressions section. Third, there isn't any grammar rule that has copy in a production.

Correction

  1. Add copy to the Keywords used in expressions and types.
  2. Add a subsection in the Prefixes subsection for copy expressions.
  3. Add a Grammar for a copy expression both at the end of the aforementioned section as well as in the Summary of the Grammar.
  4. Add a production rule to the Grammar of a prefix expression that references the new grammar both at the end of the aforementioned section as well as in the Summary of the Grammar.

tadbyt avatar Jan 30 '24 03:01 tadbyt