Skip to content

Conversation

@gottesmm
Copy link
Contributor

@gottesmm gottesmm commented Nov 1, 2024

This problem comes up with the following example:

class A {
    var description = ""
}

class B {
    let a = A()

    func b() {
        let asdf = ""
        Task { @MainActor in
            a.description = asdf // Sending 'asdf' risks causing data races
        }
    }
}

The specific issue is that the closure we generate actually includes an implicit(any) parameter at the SIL level which occurs after the callee operand but before the captures. This caused the captured variable index from the AST and the one we compute from the partial_apply to differ by 1. So we need to subtract 1 in such a case. That is why we used to print 'asdf' instead of 'a' above.

DISCUSSION: This shows an interesting difference between SIL applied arg indices and AST indices. SIL applied arg indices would include the implicit(any) parameter since it is a parameter in the SIL function type. In contrast, this doesn't show up in the formal AST parameters or captures. To make it easier to reason about this, I added a new API to ApplySite called ApplySite::getASTAppliedArgIndex and added large comments to getASTAppliedArgIndex and getAppliedArgIndex that explains the issue.

rdar://136593706
#76648

…ndices to SIL level parameter indices.

This problem comes up with the following example:

```swift
class A {
    var description = ""
}

class B {
    let a = A()

    func b() {
        let asdf = ""
        Task { @mainactor in
            a.description = asdf // Sending 'asdf' risks causing data races
        }
    }
}
```

The specific issue is that the closure we generate actually includes an
implicit(any) parameter at the SIL level which occurs after the callee operand
but before the captures. This caused the captured variable index from the AST
and the one we compute from the partial_apply to differ by 1. So we need to
subtract 1 in such a case. That is why we used to print 'asdf' instead of 'a'
above.

DISCUSSION: This shows an interesting difference between SIL applied arg indices
and AST indices. SIL applied arg indices would include the implicit(any)
parameter since it is a parameter in the SIL function type. In contrast, this
doesn't show up in the formal AST parameters or captures. To make it easier to
reason about this, I added a new API to ApplySite called
ApplySite::getASTAppliedArgIndex and added large comments to
getASTAppliedArgIndex and getAppliedArgIndex that explains the issue.

rdar://136593706
swiftlang#76648
@gottesmm
Copy link
Contributor Author

gottesmm commented Nov 1, 2024

@swift-ci smoke test

@gottesmm gottesmm merged commit 1966980 into swiftlang:main Nov 2, 2024
@gottesmm gottesmm deleted the rdar136593706 branch November 2, 2024 03:03
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