[SR-284] Strange error caused by let/var at specific place in main file.
| Previous ID | SR-284 |
| Radar | rdar://23702526 |
| Original Reporter | @jepers |
| Type | Bug |
| Status | Resolved |
| Resolution | Done |
Additional Detail from JIRA
| Votes | 0 |
| Component/s | Compiler |
| Labels | Bug |
| Assignee | None |
| Priority | Medium |
md5: 49c6b20edc7481c19a41e118ce88ca23
is duplicated by:
- SR-2727 Script mode rules for top-level bindings are confusing and broken
- SR-7658 "not declared" error for declarations after statements in binary mode is confusing
relates to:
- SR-11534 Having a global variable in a wrong place causes a misleading error message
Issue Description:
The unrelated constant notEvenUsed somehow causes the error on struct S. The error will disappear if the constant is moved to another line or removed.
(This happens only in the main file.)
protocol P {
func +(lhs: Self, rhs: Self) -> Self
}
struct S : P { // Error: Type 'S' does not conform to protocol 'P'
var v: Int
}
let notEvenUsed = 1 // Move or comment out this line to remove above error.
func +(lhs: S, rhs: S) -> S { return S(v: lhs.v + rhs.v) }
In a main source file, declarations are evaluated in groups, but any actual code has to be run immediately, and top-level variables behave like locals rather than globals. That means we try to type-check P and S together before we've even parsed the +.
One way to fix this might be to parse the entire file up front, but then mark the declarations as "off-limits" somehow for type-checking purposes. That won't work for things like overload resolution, though.
More subtly, + is allowed to refer to x, but code that happens before x is declared could refer to the S: P conformance, leading to a use-before-initialization. I think this behaves correctly.
I think the restriction is correct, but the error message is lousy. This isn't the first time it's been filed.
(and I don't just mean Jens cloning Radars into JIRA. :-) )
Note that this compiles:
protocol P {
func +(lhs: Self, rhs: Self) -> Self
}
let notEvenUsed = 1
struct S : P { // Error: Type 'S' does not conform to protocol 'P'
var v: Int
}
func +(lhs: S, rhs: S) -> S { return S(v: lhs.v + rhs.v) }
Yes, that's compiled as
protocol P {
func +(lhs: Self, rhs: Self) -> Self
}
let notEvenUsed = 1
struct S : P {
var v: Int
}
func +(lhs: S, rhs: S) -> S { return S(v: lhs.v + rhs.v) }
each block of which is fine given the previous ones.
Dups include unsafe behavior caused by this.
I didn't find any radars with unsafe behavior, but I suspect its caused by DI not working across files. In any case that problem already exists even with the old intertwined parsing model.