Description
Location
https://github.com/apple/swift-book/blob/main/TSPL.docc/ReferenceManual/Expressions.md?plain=1#L64
Description
An in-out-expression is defined as being & identifier.
This is incorrect, and can be seen on line 51 that it's meant to be & expression. (here: https://github.com/apple/swift-book/blob/main/TSPL.docc/ReferenceManual/Expressions.md?plain=1#L51)
I'm assuming this is just a simple error that was overlooked but I will leave a demonstration below of why the version with identifier isn't correct.
Given this example program:
enum A {}
struct MyStruct {
var a: A
}
func myFunc(_: inout A) {}
func myOtherFunc(s: inout MyStruct) {
myFunc(&s.a)
}
This is compliable code in swiftc.
Our point of concern is the use of an in-out expression as a parameter in the call to myFunc
There is no possible derivation of this subsection of the code given the production for in-out-expression with identifier rather than expression.
Let us look at some possible derivation paths for myFunc(&s.a)
Possible derivation path: (terminals in bold)
- -> function-call-expression
- -> postfix-expression function-call-argument-clause
- -> primary-expression function-call-argument-clause
- -> identifier generic-argument-clause? function-call-argument-clause
- -> myFunc generic-argument-clause? function-call-argument-clause
- -> myFunc function-call-argument-clause
- -> myFunc ( function-call-argument-list )
- -> myFunc ( function-call-argument )
- -> myFunc ( expression )
- -> myFunc ( try-operator? await-operator? prefix-expression infix-expressions? )
- -> myFunc ( await-operator? prefix-expression infix-expressions? )
- -> myFunc ( prefix-expression infix-expressions? )
- -> myFunc ( prefix-operator? postfix-expression infix-expressions? )
- -> myFunc ( postfix-expression infix-expressions? )
- -> myFunc ( explicit-member-expression infix-expressions? )
- -> myFunc ( postfix-expression . identifier generic-argument-clause? infix-expressions? )
- -> myFunc ( primary-expression . identifier generic-argument-clause? infix-expressions? )
- -> myFunc ( identifier generic-argument-clause? . identifier generic-argument-clause? infix-expressions? )
- -> myFunc ( ??? generic-argument-clause? . identifier generic-argument-clause? infix-expressions? )
???- cannot reach a production that will get us &s
Let's look at another possible derivation path.
Picking up from step 12 in the above derivation:
12) -> myFunc ( prefix-expression infix-expressions? )
13) -> myFunc ( in-out-expression infix-expressions? )
14) -> myFunc ( & identifier infix-expressions? )
15) -> myFunc ( & ??? infix-expressions? )
???- cannot reach a production that will get us &s.a
Correction
Line 64 should read in-out-expression → &
expression