Skip to content

[5.7][Sema] Propagate nonisolated attribute from wrapped property to the synthesized projectedValue property #60454

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

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Aug 9, 2022

Description: We were not propagating the nonisolated attribute from the wrapped property to the synthesised projectedValue variable, thus triggering a main actor-isolated property '$<name>' can not be referenced from a non-isolated context error in some contexts when accessing the projectedValue.

Risk: Low, just adding a missing attribute to a synthesised variable
Review by: @xedin
Testing: PR Testing
Original PR: #60421

@theblixguy theblixguy added the r5.7 label Aug 9, 2022
@theblixguy theblixguy requested a review from xedin August 9, 2022 00:43
@theblixguy theblixguy requested a review from a team as a code owner August 9, 2022 00:43
@theblixguy
Copy link
Collaborator Author

@swift-ci please test

@xedin xedin requested a review from hborla August 9, 2022 00:48
@hborla
Copy link
Member

hborla commented Aug 9, 2022

Hmmm, I don't think it's correct that you can mark a wrapped property as nonisolated, because the accessors use the isolated backing storage variable, and it would not be safe to mark the backing storage variable as nonisolated. This seems to expose a bug in actor isolation checking, because it allows you to use actor-isolated state in a non-isolated context.

@theblixguy
Copy link
Collaborator Author

theblixguy commented Aug 9, 2022

Hmmm, I don't think it's correct that you can mark a wrapped property as nonisolated, because the accessors use the isolated backing storage variable, and it would not be safe to mark the backing storage variable as nonisolated. This seems to expose a bug in actor isolation checking, because it allows you to use actor-isolated state in a non-isolated context.

Ah okay, so we should disallow marking wrapped properties as nonisolated? We also allow marking the wrappedValue and projectedValue as nonisolated in the property wrapper (which also removes this error). Is that correct?

@xedin
Copy link
Contributor

xedin commented Aug 9, 2022

I thought that the reason why <name> is allowed is because backing property is transitively marked as nonisolated which seemed reasonable to me conceptually.

@hborla
Copy link
Member

hborla commented Aug 9, 2022

I thought that the reason why is allowed is because backing property is transitively marked as nonisolated which seemed reasonable to me conceptually.

The backing property cannot be marked as nonisolated because it's a stored var. This means you'd be able to mutate actor state from outside the actor, which is fundamentally unsafe.

@xedin
Copy link
Contributor

xedin commented Aug 9, 2022

Ah I see, I didn't realize that nonisolated is only allowed on computed properties.

@hborla
Copy link
Member

hborla commented Aug 9, 2022

Ah okay, so we should disallow marking wrapped properties as nonisolated?

Yes, I think so!

We also allow marking the wrappedValue and projectedValue as nonisolated in the property wrapper (which also removes this error). Is that correct?

This may be correct because actor isolation checking should ensure that the (hand written) accessors for var wrappedValue and var projectedValue only access other nonisolated state.

There's probably some design discussion to be had on the forums about this topic. For example, we could allow nonisolated wrapped properties if we make the backing storage a let constant, which seems reasonable to me, although we may want a way to explicitly say the backing storage is a let constant.

In any case, I think we shouldn't take this change for 5.7 and we should discuss this on the forums!

@xedin
Copy link
Contributor

xedin commented Aug 9, 2022

@theblixguy please revert it from main as well.

@theblixguy
Copy link
Collaborator Author

Thank you for the explanation! It makes sense to me. I will create a forums post to discuss this further.

For now, I will revert the PR on main (and close this one) and will open a new PR to disallow nonisolated on wrapped properties.

@theblixguy theblixguy closed this Aug 9, 2022
@theblixguy theblixguy deleted the propagate-nonisolated-attr-synthesized-projectedValue-5.7 branch August 9, 2022 16:09
@AnthonyLatsis AnthonyLatsis added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.7 labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.7
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants