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

Bad diagnostic about memberwise init not being public #78362

Open
AnthonyLatsis opened this issue Dec 24, 2024 · 5 comments
Open

Bad diagnostic about memberwise init not being public #78362

AnthonyLatsis opened this issue Dec 24, 2024 · 5 comments
Assignees
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself conformances Feature → protocol: protocol conformances diagnostics QoI Bug: Diagnostics Quality of Implementation fix-its Feature: diagnostic fix-its good first issue Good for newcomers memberwise init Feature: Memberwise structure initializers swift 6.0 type checker Area → compiler: Semantic analysis

Comments

@AnthonyLatsis
Copy link
Collaborator

Description

No response

Reproduction

public struct Number: RawRepresentable {
  public let rawValue: Int
}
398 | public struct Number: RawRepresentable {
    |               `- error: initializer 'init(rawValue:)' must be declared public because it matches a requirement in public protocol 'RawRepresentable' [witness_not_accessible_proto]
399 |   public let rawValue: Int
400 | }

Expected behavior

This diagnostic is not great:

  1. What initializer? (The implicit memberwise initializer).
  2. How do I or can I at all declare it public? (An implicit memberwise initializer cannot be public).
  3. What should I do to make this work? (Declare an explicit initializer + fix-it).

Environment

Swift version 6.2-dev (LLVM 1a588e5b044c46f, Swift 55189ba)

Additional information

No response

@AnthonyLatsis AnthonyLatsis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself good first issue Good for newcomers diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis conformances Feature → protocol: protocol conformances fix-its Feature: diagnostic fix-its access control Feature → modifiers: Access control and access levels memberwise init Feature: Memberwise structure initializers swift 6.0 labels Dec 24, 2024
@devnumber7
Copy link

Hello!, I would like to be assigned on this issue.

@vlkhvnn
Copy link

vlkhvnn commented Dec 24, 2024

Hello, can I also be assigned on this issue?

@devnumber7
Copy link

Hello,
I am a little lost here and I could not find anything in the swift/include/swift/AST/DiagnosticsSema.def

This is my first time poking around the swift source and I wanted to make sure if I am looking at the right places. Not sure if I missed it but in the DiagnosticsSema.def I could not find anything related to "must be declared public because it matches a requirement in public protocol"

@jamieQ
Copy link
Contributor

jamieQ commented Dec 28, 2024

it can be a little tricky at first to reverse lookup the source from the diagnostic text because many of them are string 'templates' and the nouns & adjectives get substituted in. in this case the relevant diagnostic message is likely this one. you can then use the diagnostic ID (type_witness_not_accessible_proto in this case) to better determine where the relevant callsites for addressing this issue are.

@devnumber7
Copy link

devnumber7 commented Dec 28, 2024

So from what I have learned, this issue would require a simple boolean flag to append the diagnostic message. Now from my understanding the rawRepresentable protocol requires that the initializer must be declared public and you cannot have that with the implicit memberwise initialization. So a "fix-it" option would be to just add the code snippet for explicit public initialization. My only question is, which files would I have to change to have the "fix-it" option for the Xcode.

Also, is there any testing guidelines that I need to follow to on my side for the context based implementation of the diagnosis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself conformances Feature → protocol: protocol conformances diagnostics QoI Bug: Diagnostics Quality of Implementation fix-its Feature: diagnostic fix-its good first issue Good for newcomers memberwise init Feature: Memberwise structure initializers swift 6.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants