-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Diagnose access to the underlying storage of a lazy variable #33144
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
[Sema] Diagnose access to the underlying storage of a lazy variable #33144
Conversation
Good catch, thank you! |
Not 100% sure we should diagnose this in Lexer/Parser. What if a user actually declares @propertyWrapper
struct Wrapper {
var wrappedValue: Int { 1 }
var projectedValue: Int { 1 }
}
struct MyStruct {
@Wrapper var __lazy_storage_$_foo
func test() {
_ = $__lazy_storage_$_foo // Should be OK.
}
} I know It's fairly unrealistic, but still. BTW, I noticed this crashes the compiler: @propertyWrapper
struct Wrapper {
var wrappedValue: Int { 1 }
var projectedValue: Int { 1 }
}
struct MyStruct {
@Wrapper var __lazy_storage_$_foo
lazy var foo
} |
Hmm, that's interesting. I think one solution might be to do this in Sema instead. We have
It doesn't crash for me on master (I am on
but I'll update again and re-check. |
Ah, sorry. The crasher was missing the initializer. @propertyWrapper
struct Wrapper {
var wrappedValue: Int { 1 }
var projectedValue: Int { 1 }
}
struct MyStruct {
@Wrapper var __lazy_storage_$_foo
lazy var foo = 1
} |
Oh yeah, that reproduces for me! I previously some work in this area (#31915) so I'll take a look at it soon. |
I have re-implemented this in Sema (in MiscDiagnostics). Does this approach seem okay? |
Thanks! @swift-ci please smoke test |
@swift-ci please smoke test |
Since #21996, the name of the underlying storage variable of a
lazy var
has been changed to$__lazy_storage_$_{property_name}
. You couldn't actually write an identifier in (Swift) source beginning with an$
before (except anonymous closure params like$0
and etc), but since the introduction of property wrappers, you can do that and this has inadvertently allowed access to thelazy var
storage and made it possible to "reset" it.So, detect use of
$__lazy_storage_$_{property_name}
identifier in (Swift) source and emit a tailored error diagnostic.