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

[SR-13133] Add special-case diagnostic for initializing a type with only synthesized public initializers. #55579

Open
typesanitizer opened this issue Jul 1, 2020 · 1 comment

Comments

@typesanitizer
Copy link

Previous ID SR-13133
Radar rdar://problem/65003078
Original Reporter @typesanitizer
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee None
Priority Medium

md5: dd3ad03e817a4595803ce815de3025bd

Issue Description:

If you look at the forum thread: https://forums.swift.org/t/struct-initializer-in-extension/38063/4

There is a public type exposed from a module with no hand-written public initializers. So the compiler ends up suggesting:

// In library module
public struct PageMetadata: Codable {
    public let page: Int
    public let per: Int
    public let total: Int
}

// In client module
let pm = PageMetadata(page: 1, per: 1, total: 1)
                     --
Extra arguments at positions #​1, #​2, #​3 in call
Missing argument for parameter 'from' in call

Looking at the source code for the library leads to confusion. Where did this 'from' parameter come from? And why can't I access the typical initializer? The library's code itself seems to be using {{PageMetadata.init(page:per:total) }}just fine, so what's the issue?

Both of these have reasonable answers, but that's not clear from the diagnostic. Instead, we should say something like (straw-man example):

PageMetadata can only be initialized using 'PageMetadata.init(from:Decoder)'

I think this case should only trigger when all the public initializers of the type are synthesized.

In case we know that the initializer the user is trying to access is internal (I don't know if we serialize this information in swiftmodules/swiftinterfaces), we should say so (straw-man example):

error: PageMetadata.init(page:per:total:) cannot be accessed as it is internal to Vapor
error: PageMetadata can only be initialized using 'PageMetadata.init(from:Decoder)'
@typesanitizer
Copy link
Author

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant