[SR-10207] Immediate execution of defer warning shouldn't be given in a direct-to-storage context #52607
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
type checker
Area → compiler: Semantic analysis
Environment
Swift version 5.0-dev (LLVM 94d957ca75, Swift 1d2e2803af)
Target: x86_64-apple-darwin18.2.0
Additional Detail from JIRA
md5: 1f82adedcfc33c7205d4e4b6ab5566dd
duplicates:
Issue Description:
For the following code we produce a warning that the
defer
will be executed immediately:The attached fix-it suggests replacing with a
do
statement, however that has the unfortunate consequence of changing the program's behaviour, as now the access tofoo
is done direct-to-storage and the property observer is no longer called. It's a shame thatdefer
doesn't also perform a direct-to-storage access, but unfortunately quite a few people now rely on that behaviour.Therefore I think we should limit this warning to only apply to contexts where property accesses are done normally, such as methods.
Interestingly enough, the warning already doesn't trigger on a
defer
that's the last statement of an initialiser, but I couldn't find specific logic that excludes this case. But even then, the exclusion isn't broad enough, as it doesn't cover the following case:The text was updated successfully, but these errors were encountered: