-
Notifications
You must be signed in to change notification settings - Fork 379
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
[AccessorMacro] 'let' declarations cannot be computed properties #2491
Comments
Tracked in Apple’s issue tracker as rdar://122690037 |
I've investigated how such an accessor macro will behave in the compiler and it seems that accessor macro can bypass Such a code: struct A {
@constantOne()
let x: Int = 0
}
let a = A()
print(a.x) compiles with no errors and it prints |
I thought I had left a comment here but apparently didn’t. I think that we should disallow accessor macros on |
@ahoppen Hmm… I was actually expecting that I might have a use case in a future example. Suppose I created some kind of @Storage struct Person {
let id: String
var name: String
} This might then expand to something like: struct Person {
var id: String {
get {
self.storage.id
}
}
var name: String {
get {
self.storage.name
}
set {
self.storage.name = newValue
}
}
private final class Storage {
let id: String
var name: String
// TODO: INIT...
}
private var storage = Storage()
} Obviously there would need to be more code there to handle initialization correctly… but the basic idea would be that a macro here could synthesize a getter on my If the @Storage struct Person {
var id: String
var name: String
} Conceptually… it doesn't really make sense for this @Storage struct Person {
@ReadOnly var id: String
var name: String
} Which then kind of scrambles up the public interface and requirements for a developer to make use of this hypothetical I do not (yet) have a repo I can show to you implementing this pattern… but I did have something in mind that would have expected If you see the original test code I linked against it was the diff to correctly move around the case when a variable is initialized with an initial value when declared. So I actually thought there was a precedent to "transforming" the variable declaration (in this case removing the initial value). My first thought when filing this task was that applying an |
My main concern here is that currently any |
@ahoppen Hmm… well… if the POV consensus is that accessor macros applied to Macros "are" Swift… but are also opportunities to step outside conventional Swift (that might not otherwise compile directly or easily). I guess I looked at Macros (and also Result Builders) as a place to bend the language around and where we give engineers flexibility (taking into account that flexibility needs to be managed responsibly and can harm as much as help). But I can see how there come times to move the debate back in favor of "legit" Swift code. If we did plan to disallow this functionality… would you consider giving engineers a heads up (even before a diff is ready)? I feel like some engineers might have discovered this hack and might be trying to ship something that expects macros on |
Disallowing getters on |
Description
The following Swift code fails to compile:
It looks like our current
AccessorMacro
implementation adds computed getters tolet
variables without transforming thelet
tovar
:This test fails:
Should we update the
AccessorMacro
implementation to produce Swift more consistent with what engineers are able to compile? Should we be transforming thatlet
to avar
?Steps to Reproduce
No response
The text was updated successfully, but these errors were encountered: