Skip to content
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

[SR-15013] Issue diagnostic for defer relying on incorrect scoping behavior #57342

Closed
typesanitizer opened this issue Aug 3, 2021 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@typesanitizer
Copy link

Previous ID SR-15013
Radar rdar://problem/81456588
Original Reporter @typesanitizer
Type Bug
Status Closed
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: d23188547e5854b410cbffd95f343a9b

Issue Description:

do {
    defer { print(a) } // OK
    let a = 0
} 

Reported originally here: https://forums.swift.org/t/defer-can-take-variables-declared-after-itself-bug-or-feature/50821

We should emit a warning (mark it with .warnUntilSwiftVersion(6)) for situations where the semantics will change in Swift 6:

  1. When it will become an error (due to the scoping rules being fixed and there being no matching binding in scope after the change).

  2. When it will start referring to a different value (e.g. due to shadowing, it refers to some binding now but will refer to a different binding after the change).

Along with these test cases, we should probably also have a negative test to make sure this warning isn't emitted when the binding won't change under the new rules.

@typesanitizer
Copy link
Author

@swift-ci create

@CodaFi
Copy link
Member

CodaFi commented Aug 6, 2021

We shouldn't warn here. This was an error before Swift 5.2 because it's a soundness hole. Slava has cleaned the semantics up so that this errors properly in this case and in many others, which closes the regression here.

@CodaFi
Copy link
Member

CodaFi commented Aug 6, 2021

Note: ToT offers

```
<stdin>:2:19: error: use of local variable 'a' before its declaration
defer { print(a) } // OK
^
<stdin>:3:9: note: 'a' declared here
let a = 0
^
```

@CodaFi
Copy link
Member

CodaFi commented Aug 6, 2021

#36441

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants