swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.28k stars 10.33k forks source link

Bad diagnostic for initialization of property wrapper with custom `init` #69066

Open AnthonyLatsis opened 11 months ago

AnthonyLatsis commented 11 months ago

Steps to reproduce

@propertyWrapper
struct Wrapper {
  var wrappedValue: Int

  init(i: Int) {}
}

do {
  @Wrapper var w: Int = 0
}
error: incorrect argument label in call (have 'wrappedValue:', expected 'i:') [wrong_argument_labels]
  @Wrapper var w: Int = 0
   ^                    ~
                        i

Expected behavior

For example, property wrapper type must provide an initializer with either no parameters or a first parameter with argument label 'wrappedValue'.

Environment

saehejkang commented 11 months ago

seems like a simple update to an error message? @AnthonyLatsis can I take this one?

AnthonyLatsis commented 11 months ago

seems like a simple update to an error message?

Not this time. Actually, that’s almost never the case, even when a bug report is labeled as https://github.com/apple/swift/labels/good%20first%20issue and requests an improvement to a specific diagnostic message. For those sorts of issues, we expect the contributor to have practical experience with or research the associated feature, come up with an improvement to the message and back it up with a rationale.

can I take this one?

Yes, if you feel like you can manage working on two issues at once.

saehejkang commented 11 months ago

Not this time. Actually, that’s almost never the case, even when a bug report is labeled as good first issue and requests an improvement to a specific diagnostic message.

So this is not something simple? The improvement to the message was added by you, so do I need to have practical experience or research with this update?

AnthonyLatsis commented 11 months ago

So this is not something simple?

I make this as one of the harder beginner issues. Knowledge of property wrapper semantics is required, and prior experience in this area of the compiler is a bonus that would facilitate learning and reduce guesswork.

For reference, I think the property wrapper in the example is malformed and that we should diagnose it rather than uses of it.

The improvement to the message was added by you

My example is just a proposition; it can be disputed, refined, or left as is, but rationalization on either course of action is expected.

saehejkang commented 11 months ago

Knowledge of property wrapper semantics is required

Since I have pretty much 0 knowledge with this area of the compiler, I will take a pass on this. I was looking for a simple issue that has to do with error messages and updates to see how it all works.

mininny commented 11 months ago

Or another diagnostic may be something like:

error: "missing argument label in call (expected 'i:')" fixit: "insert 'i:'?"
 @Wrapper var w: Int = 0

since @Wrapper(i: 0) var w: Int is valid as well

mininny commented 11 months ago

This seems to be somewhat similar to https://github.com/apple/swift/issues/68570. There are some parts in the compiler where it assumes that there's init(wrappedValue:) in the property wrapper, but doesn't emit enough information when it goes missing.

mininny commented 10 months ago

I'd like to work on this if there aren't any other ongoing work on this :)

AnthonyLatsis commented 10 months ago

since @Wrapper(i: 0) var w: Int is valid as well

Good point, I didn’t know init(wrappedValue:) is not a hard requirement in the absence of a default initializer. Then I think we need a tailored error message stating that this property wrapper initialization strategy only works with initializers that have a wrappedValue parameter, and a conditional fix-it to direct users to the correct initialization strategy when a different initializer can be called with the initial value alone.