swiftlang/swift

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

開放

#48,759 建立於 2017年10月24日

 (8 則留言) (0 個反應) (0 位負責人)Swift (10,719 個分叉)batch import
bugcompilerdiagnostics qualityexpressionsgood first issuestatic declarationsswift 6.0type checker

倉庫指標

星標
 (69,989 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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).

貢獻者指南