Skip to content

[6.2][silgen] Teach SILGen how to emit a forced dynamic member ref given nonisolated(nonsending) casts. #82082

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

Open
wants to merge 1 commit into
base: release/6.2
Choose a base branch
from

Conversation

gottesmm
Copy link
Contributor

@gottesmm gottesmm commented Jun 7, 2025

Explanation: This PR teaches SILGen how to emit a forced dynamic member ref given AST casts to nonisolated(nonsending) that occurs when emitting calls to objective-c methods through an AnyObject casted object. Specifically, I taught SILGen how to emit an AST like the following:

(force_value_expr implicit type="nonisolated(nonsending) (Date?) async -> Void" implicit_iuo_unwrap
  (open_existential_expr implicit type="(nonisolated(nonsending) (Date?) async  -> Void)?"
    (opaque_value_expr implicit type="AnyObject")
    (declref_expr type="AnyObject" decl="test.(file).repro()[email protected]:6:7" function_ref=unapplied)
    (optional_evaluation_expr type="(nonisolated(nonsending) (Date?) async -> Void)?"
      (inject_into_optional type="(nonisolated(nonsending) (Date?) async -> Void)?"
        (function_conversion_expr type="nonisolated(nonsending) (Date?) async -> Void"
          (bind_optional_expr type="(Date?) async -> Void" depth=0
            (dynamic_member_ref_expr type="((Date?) async -> Void)?" decl="__ObjC.(file).Foo.start(at:)"
              (opaque_value_expr type="AnyObject"))))))))

Since we are emitting an objc async function, there isn't an extra implicit parameter like if we were using a swift async function. So, I just reused code that was already used locally to look through these sorts of conversions. I just had to add to that code support for conversions that add nonisolated(nonsending). Previously it only supported looking through global actor conversions.

Scope: This changes how SILGen emits dynamic member given a cast in the AST which only occurs if a user casts an objective-c class to AnyObject and invokes the method. The implementation is similar to how we already handle casts to/form global actor so the change is not doing anything novel. It will not impact code that does not cast classes to AnyObject. So code that uses classes directly will not be impacted.

Resolves: rdar://152596823

Main PR: #82029

Risk: Low. This can only impact code that casts an imported objc class that is cast to an AnyObject and then whose methods are then invoked.

Testing: Added compiler tests

Reviewer: @rjmccall

…onisolated(nonsending) casts.

Specifically, I taught SILGen how to emit an AST like the following:

```
(force_value_expr implicit type="nonisolated(nonsending) (Date?) async -> Void" implicit_iuo_unwrap
  (open_existential_expr implicit type="(nonisolated(nonsending) (Date?) async  -> Void)?"
    (opaque_value_expr implicit type="AnyObject")
    (declref_expr type="AnyObject" decl="test.(file).repro()[email protected]:6:7" function_ref=unapplied)
    (optional_evaluation_expr type="(nonisolated(nonsending) (Date?) async -> Void)?"
      (inject_into_optional type="(nonisolated(nonsending) (Date?) async -> Void)?"
        (function_conversion_expr type="nonisolated(nonsending) (Date?) async -> Void"
          (bind_optional_expr type="(Date?) async -> Void" depth=0
            (dynamic_member_ref_expr type="((Date?) async -> Void)?" decl="__ObjC.(file).Foo.start(at:)"
              (opaque_value_expr type="AnyObject"))))))))
```

Since we are emitting an objc async function, there isn't an extra implicit
parameter like if we were using a swift async function. So, I just reused code
that was already used locally to look through these sorts of conversions. I
just had to add to that code support for conversions that add
nonisolated(nonsending). Previously it only supported looking through global
actor conversions.

rdar://152596823
(cherry picked from commit 662dbdb)
@gottesmm
Copy link
Contributor Author

gottesmm commented Jun 7, 2025

@swift-ci test

@gottesmm
Copy link
Contributor Author

gottesmm commented Jun 7, 2025

@swift-ci test macOS platform

@gottesmm
Copy link
Contributor Author

gottesmm commented Jun 8, 2025

macOS failed in lldb

@gottesmm
Copy link
Contributor Author

gottesmm commented Jun 8, 2025

@swift-ci test macOS platform

1 similar comment
@gottesmm
Copy link
Contributor Author

@swift-ci test macOS platform

@gottesmm gottesmm marked this pull request as ready for review June 10, 2025 16:16
@gottesmm gottesmm requested a review from a team as a code owner June 10, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant