Skip to content
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

Improve diagnostics for C-style parameter declaration with structural type #2768

Open
AppAppWorks opened this issue Aug 1, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@AppAppWorks
Copy link
Contributor

AppAppWorks commented Aug 1, 2024

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'"])
@AppAppWorks AppAppWorks added the enhancement New feature or request label Aug 1, 2024
@ahoppen
Copy link
Member

ahoppen commented Aug 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants