Skip to content

[PropertyWrappers] Mark the property as invalid when there is a mismatch with wrappedValue type #27995

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

Merged
merged 5 commits into from
Nov 5, 2019

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Oct 31, 2019

When the property's type does not match the wrappedValue property's type, mark the property as invalid. Otherwise, we will end up producing a bunch of incorrect diagnostics later on as well as crashing in certain cases.

For example, this little snippet crashes the compiler:

@propertyWrapper 
struct Wrapper {
  var wrappedValue: Int
}

class Test {
  @Wrapper var user: Int?
}

because we're trying to synthesise a setter that assigns a value of type Int? into a storage of type Int. We also end up emitting a bunch of incorrect diagnostics, such as suggesting the user to force unwrap the property declaration or provide a default value (which isn't possible/make sense).

In addition, we emit another diagnostic about failing to convert the argument type, for the synthesised wrappedValue initializer. This diagnostic isn't valuable because (1) we already inform the user about the type mismatch between the property's type and the wrappedValue type and (2) the diagnostic is attached to the synthesised init, but appears on the property and can be confusing to the user. This diagnostic is now disabled for a synthesised wrappedValue init.

Resolves SR-11684
Resolves SR-11600
Resolves rdar://problem/56785162
Resolves rdar://problem/56345248
Resolves rdar://problem/56190606

@theblixguy theblixguy requested a review from DougGregor October 31, 2019 19:49
@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@hborla hborla requested a review from xedin November 1, 2019 16:20
@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

@DougGregor can you take a quick look at this? I'd like to merge this before tomorrow's cutoff. Thank you!

…to match the type of the wrappedValue property

Otherwise, we will end up producing wrong diagnostics as well as crashing later on in certain cases
…e is an argument type mismatch in synthesized wrappedValue init

This is because we already emit a diagostic to tell the user that the property's type does not match the wrappedValue type, so this diagnostic can be a bit confusing especially because the initializer is synthesized
…pe::get()) as setInvalid automatically does that
@theblixguy
Copy link
Collaborator Author

theblixguy commented Nov 4, 2019

I just noticed that this fixes compiler crasher SR-11600 as well. Great! The diagnostics can definitely be improved for wrapper composition though (mostly its the diagnostics around the synthesised wrappedValue init that needs a bit of special-casing/tweaking).

@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy theblixguy merged commit 5ff654d into swiftlang:master Nov 5, 2019
@theblixguy theblixguy deleted the fix/SR-11684 branch November 5, 2019 01:25
@DougGregor
Copy link
Member

Thank you!

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.

4 participants