[SR-14147] Diagnose suspicious weak references #56526
Labels
compiler
The Swift compiler in itself
diagnostics QoI
Bug: Diagnostics Quality of Implementation
new feature
Additional Detail from JIRA
md5: 398ecfadf7364a380cdee5741d5efa1b
Issue Description:
Issue a compiler warning when assigning an object to a weak references that will likely be destroyed before the weak reference is used.
The simple but common case should be easy to diagnose:
Weak reference are often used to capture closure arguments, so that needs to be handled as well.
This pattern can generally be recogized as:
a locally allocated object 'alloc_ref'
assignment to a weak property or box (for a closure) 'store_weak'
use of the property's parent object
no subsequent use of the local object
However, this pattern won't catch uses involving asynchronous execution:
We also should try to diagnose this pattern...
Here, 'dispatcher' is used (as self) in the same call that captures the weak reference. So the "no subsequent use" rule above does not apply here. The compiler would need to know that 'runBackground' is asynchronous.
Should we simply warn about any weak reference to a local variable passed to an asynchronous routine?
The text was updated successfully, but these errors were encountered: