swiftlang / swiftlang/swift-syntax
Improve diagnostics for C-style parameter declaration with structural type
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
### Description
New Swift programmers with a C-like language background or polyglot programmers often find themselves mistakenly using the C-style parameter declaration, e.g.
```swift
func cStyle(X x) {}
```
Currently there's a diagnostic emitted for this case:
```swift
DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"])
```
Undeniably this is the most unsurprising diagnostic for anyone except for the beginners.
Notwithstanding, when the type is structural,
```swift
func cStyleWithStructuralType(Array.Index i) {}
```
As structural types such as `Array.Index` cannot be identifiers, the parser should not recognize `Array.Index` as an identifier. However, the diagnostics currently emitted are suboptimal,
```swift
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':'"])
DiagnosticSpec(message: "unexpected code '.Index i' in parameter clause")
```
Applying the fix-it `"insert ':'"` will result in an undesirable outcome,
```swift
func cStyleWithStructuralType(Array: <#type#>.Index i)
```
This issue is also applicable to closure parameters and enum case parameters,
```swift
let cStyleWithStructuralType = { (Array.Index i) in }
case cStyleWithStructuralType(Array.Index i)
```
We should improve diagnostics for the situations above by emitting diagnostics like these,
```swift
DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"])
DiagnosticSpec(message: "'i' must precede 'Array.Index'", fixIts: ["move 'i' in front of 'Array.Index'"])
```
Contributor guide
Assessment
This issue has not been assessed yet.