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-11478] Using a static var with a property wrapper in a class leads to a wrong error message #53878

Closed
ffried opened this issue Sep 16, 2019 · 12 comments
Assignees
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

@ffried
Copy link
Contributor

ffried commented Sep 16, 2019

Previous ID SR-11478
Radar rdar://problem/55532135
Original Reporter @ffried
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Happens with Xcode 11 GM as well as the current master build.
GM:

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0

Master Toolchain:

Apple Swift version 5.1-dev (LLVM c5340df2d1, Swift c7be2def90)
Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, PropertyWrappers
Assignee @theblixguy
Priority Medium

md5: 58900d576f93e36844162e5b8dc424cc

is duplicated by:

  • SR-11510 Static var not allowed in class when using property wrapper
  • SR-11801 Property Wrapper gives bad error message for type properties

Issue Description:

The following code leads to an error message, that is wrong in this context. I assume that the generated wrapper var (the one with the underline prefix) is generated as class var instead of static var even though the wrapped variable definition is static:

@propertyWrapper
struct Wrap<Value> {
   var wrappedValue: Value
}

final class ABC {
   @Wrap
   static var bool: Bool = true
}

Error that results from this code:

s51_staticPropertyWrappers.swift:8:15: error: class stored properties not supported in classes; did you mean 'static'?
   static var bool: Bool = true
              ^
@belkadan
Copy link
Contributor

We should also consider whether writing class var with a property wrapper gives you a static stored property or an error. Error is certainly more conservative.

@ffried
Copy link
Contributor Author

ffried commented Sep 16, 2019

The error would make sense then and match the current behavior when writing class var str = "string".
For me it was really a surprise, because I was replacing the following (working) code:

private static var _bool = Wrap(wrappedValue: true)
static var bool: Bool { return _bool.wrappedValue }

with above's version using @propertyWrapper.

@theblixguy
Copy link
Collaborator

Just to be clear - is the bug that we're generating the wrong diagnostic or that we're generating a diagnostic instead of allowing it to compile? At the moment, we're calling to getCorrectStaticSpellingForDecl() when getting the spelling for the backing property's pattern binding and since our context is a class, we're returning class instead of static.

@belkadan
Copy link
Contributor

We shouldn't generate any diagnostic at all in the static case. It's not the spelling that matters so much as the non-final-ity; generating the backing property as class final rather than static would also be fine. (But matching how the regular property is written would also make sense.)

@theblixguy
Copy link
Collaborator

I totally forgot that we can use static wrappers (duh!!). Here's a fix: #27198

@belkadan
Copy link
Contributor

Right, but at that point we have the question about how class var should behave, and also class final var.

@theblixguy
Copy link
Collaborator

With my patch, it mimics the original property (and context):

  1. class var inside final class - ok

  2. class var inside non-final class - error

  3. static var inside class - ok

@ffried
Copy link
Contributor Author

ffried commented Sep 16, 2019

Wow, that was quick, thank you @theblixguy!
IMHO, what you describe is fine. What about final class var in non-final class (which would be the same as static just written differently)?

@theblixguy
Copy link
Collaborator

You'll get an error -the backing variable is using the same static spelling as the original variable.

@ffried
Copy link
Contributor Author

ffried commented Sep 16, 2019

Ah, my bad. I thought that final class var would work today (and is treated the same as static var). But that is already giving an error - in which case it makes sense to also generate an error now of course. Thanks for clarifying.

@theblixguy
Copy link
Collaborator

Fixed on master. Please verify using the next available master snapshot!

@DougGregor
Copy link
Member

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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

4 participants