swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-284] Strange error caused by let/var at specific place in main file.

Open jepers opened this issue 10 years ago • 8 comments

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) }

jepers avatar Dec 17 '15 21:12 jepers

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.

belkadan avatar Dec 18 '15 00:12 belkadan

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.

jckarter avatar Dec 18 '15 00:12 jckarter

I think the restriction is correct, but the error message is lousy. This isn't the first time it's been filed.

belkadan avatar Dec 18 '15 01:12 belkadan

(and I don't just mean Jens cloning Radars into JIRA. :-) )

belkadan avatar Dec 18 '15 01:12 belkadan

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) }

jepers avatar Dec 19 '15 05:12 jepers

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.

belkadan avatar Dec 20 '15 04:12 belkadan

Dups include unsafe behavior caused by this.

belkadan avatar Sep 22 '16 18:09 belkadan

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.

https://github.com/apple/swift/pull/26636

slavapestov avatar Dec 05 '19 16:12 slavapestov