Skip to content

Conversation

@aeu
Copy link
Contributor

@aeu aeu commented Nov 26, 2025

Resolves

#48759

Summary

This PR improves the diagnostic emitted when a static member is referenced through an instance.
The previous diagnostic was misleading because it stated that the static member “cannot be used on an instance,” without clarifying the correct usage.
This change updates the diagnostic to explicitly state that static members must be referenced on the type.

Changes made

  1. Added a new diagnostic to include/swift/AST/DiagnosticsSema.def
  2. Updated lib/Sema/CSDiagnostics.cpp to extract the relevant tokens and emit the updated message
  3. Added test coverage in test/Sema/static_member_on_instance.swift

Original Example (from Issue)

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

Previous Output

$ swiftc 48759.swift 
48759.swift:3:15: error: static member 'cvar' cannot be used on instance of type 'HasStatic'
1 | struct HasStatic {
2 |     func foo() {
3 |         print(cvar)
  |               `- error: static member 'cvar' cannot be used on instance of type 'HasStatic'
4 |     }
5 |     static let cvar = 123

Updated Output

$ swiftc 48759.swift 
48759.swift:3:15: error: static member 'cvar' can only be used on the type 'HasStatic', not on the instance self
1 | struct HasStatic {
2 |     func foo() {
3 |         print(cvar)
  |               `- error: static member 'cvar' can only be used on the type 'HasStatic', not on the instance self
4 |     }
5 |     static let cvar = 123

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! I left a comment inline regarding the code itself and I think the new wording might not actually make things clearer.

auto SR = semantic->getSourceRange();
if (SR.isValid()) {
auto CSR = Lexer::getCharSourceRangeFromSourceRange(SM,SR);
instanceName = SM.extractText(CSR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether the diagnostic is any better but regardless of that this is not the way to get the instance reference, it should be recorded as part of recording a fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly we had some previous attempts at re-phrasing this diagnostic too, you might want to look for the discussion older PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly we had some previous attempts at re-phrasing this diagnostic too, you might want to look for the discussion older PRs.

Hi @xedin - I was looking at #48759 which referenced SR-6207 and that was where I got the diagnostic suggestion from user @huonw (below) which is what I implemented.

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

I found another discussion in #12644 in which @jrose-apple was in agreement with Huon's recommendation.

I'm open to suggestions though, I was unable to think of anything better myself so I just went with what was in the original defect.

Copy link
Contributor Author

@aeu aeu Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... but regardless of that this is not the way to get the instance reference, it should be recorded as part of recording a fix.

Hi @xedin - If I am reading your suggestion correctly, I should create a fix and record it with the base during constraint solving and then have the emitter read it rather than pulling it from the source text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants