swiftlang/swift

[SR-6207] Misleading error for unqualified use of static variable in method

Open

#48,759 opened on 2017年10月24日

GitHub で見る
 (8 comments) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
bugcompilerdiagnostics qualityexpressionsgood first issuestatic declarationsswift 6.0type checker

説明

Previous ID SR-6207
Radar rdar://problem/24291352
Original Reporter @huonw
Type Bug
Status In Progress
Resolution

Swift 4

Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee None
Priority Medium

md5: 47c00d1618aecdca414e977e2fbf19c9

Issue Description:

struct HasStatic {
    func foo() {
        print(cvar)
    }
    static let cvar = 123
}

hasstatic.swift:3:11: error: static member 'cvar' cannot be used on instance of type 'HasStatic'
    print(cvar)
          ^~~~
          HasStatic.

The fixit helps, but it still isn't obvious what's going wrong. The compiler is acting like the use of cvar was written self.cvar, and hints at this with "an instance of", but it's easy to gloss over the subtlety. The text could be tweaked, maybe like

static member 'cvar' can only be used on the type 'HasStatic', not the instance 'X'

where the X is the relevant parent value. (e.g. self for this case, or foo.bar for foo.bar.cvar).

コントリビューターガイド