Skip to content

Commit 9f849a5

Browse files
committed
[Changelog] Add changelog entry for forward declaration rule change
1 parent 6e80446 commit 9f849a5

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,61 @@
33
> [!NOTE]
44
> This is in reverse chronological order, so newer entries are added to the top.
55
6-
## Swift (next)
6+
## Swift 6.3
7+
8+
* The checking for illegal forward references to local variables is now consistent regardless of
9+
whether the reference appears in a closure. Previously the type-checker could incorrectly permit
10+
forward references within a closure that it would reject outside of the closure, however this
11+
is not something that can be supported in general in the type-checker. In most cases this should
12+
have no impact since such invalid references would have already been rejected by later diagnostic
13+
passes in the compiler. However there are a couple of cases that were previously legal that are
14+
now illegal.
15+
16+
These include:
17+
18+
- `lazy` local variables with initializers that forward reference a local variable in a closure,
19+
or local variables with attached macros that relocate the initializer into an accessor, e.g:
20+
21+
```swift
22+
func foo() {
23+
lazy var x = { y } // error: use of local variable 'y' before its declaration
24+
let y = 0
25+
}
26+
```
27+
28+
```swift
29+
func foo() {
30+
@LazyLikeMacro var x = { y } // error: use of local variable 'y' before its declaration
31+
let y = 0
32+
}
33+
```
34+
35+
- Forward references to local computed variables from a closure, e.g:
36+
37+
```swift
38+
func foo() {
39+
var x = { y } // error: use of local variable 'y' before its declaration
40+
var y: Int { 0 }
41+
}
42+
```
43+
44+
Both cases were already invalid if there was no closure involved. These are now consistently
45+
rejected.
46+
47+
This then allows for consistent shadowing behavior inside and outside of closures, allowing the
48+
following previously illegal case to become legal:
49+
50+
```swift
51+
struct S {
52+
var x: Int
53+
func foo() {
54+
let x = x // Already legal, refers to `self.x`.
55+
}
56+
func bar() {
57+
let x = { x } // Previously illegal, now also refers to `self.x`.
58+
}
59+
}
60+
```
761

862
* [SE-0491][]:
963
You can now use a module selector to specify which module Swift should look inside to find a named declaration. A

0 commit comments

Comments
 (0)