swiftlang/swift-syntax

Improve diagnostics for C-style parameter declaration with structural type

AppAppWorks opened this issue · 1 comments

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.

func cStyle(X x) {}

Currently there's a diagnostic emitted for this case:

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,

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,

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,

func cStyleWithStructuralType(Array: <#type#>.Index i)

This issue is also applicable to closure parameters and enum case parameters,

let cStyleWithStructuralType = { (Array.Index i) in }

case cStyleWithStructuralType(Array.Index i)

We should improve diagnostics for the situations above by emitting diagnostics like these,

DiagnosticSpec(message: "expected ':' in parameter", fixIts: ["insert ':'"])
DiagnosticSpec(message: "'i' must precede 'Array.Index'", fixIts: ["move 'i' in front of 'Array.Index'"])

Synced to Apple’s issue tracker as rdar://132963346