Skip to content

[SR-13098] Diagnostic improvement: encountering an unexpected statement at type scope #55544

@typesanitizer

Description

@typesanitizer
Previous ID SR-13098
Radar rdar://problem/64947436
Original Reporter @typesanitizer
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, Parser
Assignee dfsweeney (JIRA)
Priority Medium

md5: 8298353d748ac718f859d5f973f05863

Issue Description:

Consider the following minimal example:

struct T {
    var x: Int
}
struct S {
    var t: T
    t.x = 3
    // error: expected 'func' keyword in instance method declaration
    // error: expected '(' in argument list of function declaration
    // error: consecutive declarations on a line must be separated by ';'
    // error: expected declaration
    // error: expected '{' in body of function declaration
    // error: invalid redeclaration of 't()'
}

A more realistic version can be seen here: https://developer.apple.com/forums/thread/651253

The cascade of errors is quite intimidating. We should:

1. Avoid the cascade of errors, hopefully providing only one or two errors.
2. Provide a more useful diagnostic. For example, we might want to point out that assignments are not permitted at class scope, they must be inside a function. We might also want to say that we expected a property or a function declaration here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselfimprovementparserArea → compiler: The legacy C++ parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions