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-12013] ‘wrappedValue’ not being the first parameter of initializers of Property Wrapper makes mixed initialization invalid #54450

Open
swift-ci opened this issue Jan 11, 2020 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12013
Radar rdar://problem/58861150
Original Reporter wayne2046 (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 11.3 (11C29) Playground
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
Target: x86_64-apple-darwin18.7.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, PropertyWrappers
Assignee None
Priority Medium

md5: 291e78349afba012c209e9db4140d930

Issue Description:

@propertyWrapper
struct SmallNumber {
    private var maximum: Int
    private var number: Int
    var wrappedValue: Int {
        get { number }
        set { number = min(newValue, maximum) }
    }
    init(maximum: Int, wrappedValue: Int) {
        self.maximum = maximum
        number = min(wrappedValue, maximum)
    }
}
struct NarrowRectangle {
    @SmallNumber(maximum: 5, wrappedValue: 2) var height: Int
    @SmallNumber(maximum: 4, wrappedValue: 3) var width: Int
}
var narrow = NarrowRectangle()
print(narrow.height, narrow.width)
narrow.height = 10; narrow.width = 10
print(narrow.height, narrow.width)

The Swift language reference does not mention that 'wrappedValue' must be the first parameter of initializers of Property Wrapper. In the above snippet, although the 'wrappedValue' is not the first parameter of the initializer, the code output the desired result as following:

2 3
5 4

But if we use mixed form of syntactic sugar for initialization of Property Wrapper, then errors occurs.

struct MixedRectangle {
    @SmallNumber(maximum: 9) var width: Int = 2
}
Error: Argument 'maximum' must precede argument 'wrappedValue'
@theblixguy
Copy link
Collaborator

Works fine on 5.2. Could you please verify using the latest 5.2 development snapshot?

@swift-ci
Copy link
Collaborator Author

Comment by Wayne2046 (JIRA)

The same error on 5.2-dev.

@theblixguy
Copy link
Collaborator

Can you try a trunk (master) snapshot?

@swift-ci
Copy link
Collaborator Author

Comment by Owen Voorhees (JIRA)

I can reproduce this with a build of master from Jan. 7 (667fc05). I don't think anything that could have fixed this has gone in since then, I could be mistaken though.

@theblixguy
Copy link
Collaborator

Hmm, I am on Swift version 5.2-dev (Swift 51b8914211) [Target: x86_64-apple-darwin19.0.0] and it works fine, but I haven't pulled from master since a week or two, so perhaps something regressed. I'll pull in from master again and see what's going on.

@theblixguy
Copy link
Collaborator

Okay, my bad. I was not trying the @SmallNumber(maximum: 9) var width: Int = 2 example that you provided at the bottom. I can confirm this reproduces on master as well.

So, I don't think this is a bug, because wrappedValue must be the first argument of the initializer. The compiler synthesises an implicit call to the initializer with wrappedValue as first argument (see this). Perhaps we should emit a diagnostic for this.

The obvious fix is to rearrange the arguments of your initializer (or offer two with different order).

@beccadax
Copy link
Contributor

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

3 participants