mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
I also removed the -verify-sil-ownership flag in favor of a disable flag -disable-sil-ownership-verifier. I used this on only two tests that still need work to get them to pass with ownership, but whose problems are well understood, small corner cases. I am going to fix them in follow on commits. I detail them below: 1. SILOptimizer/definite_init_inout_super_init.swift. This is a test case where DI is supposed to error. The only problem is that we crash before we error since the code emitting by SILGen to trigger this error does not pass ownership invariants. I have spoken with JoeG about this and he suggested that I fix this earlier in the compiler. Since we do not run the ownership verifier without asserts enabled, this should not affect compiler users. Given that it has triggered DI errors previously I think it is safe to disable ownership here. 2. PrintAsObjC/extensions.swift. In this case, the signature generated by type lowering for one of the thunks here uses an unsafe +0 return value instead of doing an autorelease return. The ownership checker rightly flags this leak. This is going to require either an AST level change or a change to TypeLowering. I think it is safe to turn this off since it is such a corner case that it was found by a test that has nothing to do with it. rdar://43398898
140 lines
3.0 KiB
Swift
140 lines
3.0 KiB
Swift
// RUN: %target-swift-emit-sil %s -o /dev/null -verify
|
|
|
|
func testUnreachableAfterReturn() -> Int {
|
|
var x: Int = 3
|
|
return x
|
|
x += 1 //expected-warning {{code after 'return' will never be executed}}
|
|
}
|
|
|
|
func testUnreachableAfterIfReturn(a: Bool) -> Int {
|
|
if a {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
var _: Int = testUnreachableAfterReturn() // expected-warning {{will never be executed}}
|
|
}
|
|
|
|
func testUnreachableForAfterContinue(b: Bool) {
|
|
for _ in 0..<10 {
|
|
var y: Int = 300
|
|
y += 1
|
|
if b {
|
|
break
|
|
y += 1 // expected-warning {{code after 'break' will never be executed}}
|
|
}
|
|
continue
|
|
y -= 1 // expected-warning {{code after 'continue' will never be executed}}
|
|
}
|
|
}
|
|
|
|
func testUnreachableWhileAfterContinue(b: Bool) {
|
|
var i:Int = 0
|
|
while (i<10) {
|
|
var y: Int = 300
|
|
y += 1
|
|
if b {
|
|
break
|
|
y += 1 // expected-warning {{code after 'break' will never be executed}}
|
|
}
|
|
continue
|
|
i += 1 // expected-warning {{code after 'continue' will never be executed}}
|
|
}
|
|
}
|
|
|
|
func testBreakAndContinue() {
|
|
var m = 0
|
|
for _ in 0 ..< 10 {
|
|
m += 1
|
|
if m == 15 {
|
|
break
|
|
} else {
|
|
continue
|
|
}
|
|
m += 1 // expected-warning {{will never be executed}}
|
|
}
|
|
}
|
|
|
|
|
|
// <rdar://problem/20253447> `case let Case` without bindings incorrectly matches other cases
|
|
enum Tree {
|
|
case Leaf(Int)
|
|
case Branch(Int)
|
|
}
|
|
|
|
func testUnreachableCase1(a : Tree) {
|
|
switch a {
|
|
case let Leaf:
|
|
_ = Leaf
|
|
return
|
|
case .Branch(_): // expected-warning {{case will never be executed}}
|
|
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
|
|
return
|
|
}
|
|
}
|
|
|
|
func testUnreachableCase2(a : Tree) {
|
|
switch a {
|
|
case let Leaf:
|
|
_ = Leaf
|
|
fallthrough
|
|
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
|
|
return
|
|
}
|
|
}
|
|
|
|
func testUnreachableCase3(a : Tree) {
|
|
switch a {
|
|
case _:
|
|
break
|
|
case .Branch(_): // expected-warning {{case will never be executed}}
|
|
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
|
|
return
|
|
}
|
|
}
|
|
|
|
func testUnreachableCase4(a : Tree) {
|
|
switch a {
|
|
case .Leaf(_):
|
|
return
|
|
case .Branch(_):
|
|
return
|
|
}
|
|
}
|
|
|
|
func testUnreachableCase5(a : Tree) {
|
|
switch a {
|
|
case _:
|
|
break
|
|
default: // expected-warning {{default will never be executed}}
|
|
return
|
|
}
|
|
}
|
|
|
|
func testOptionalEvaluationBreak(a : Tree) {
|
|
class SR5763 { func foo() {} }
|
|
func createOptional() -> SR5763? { return SR5763() }
|
|
switch a {
|
|
case _:
|
|
break
|
|
createOptional()?.foo() // expected-warning {{code after 'break' will never be executed}}
|
|
}
|
|
}
|
|
|
|
func testUnreachableAfterThrow(e: Error) throws {
|
|
throw e
|
|
return // expected-warning {{code after 'throw' will never be executed}}
|
|
}
|
|
|
|
class TestThrowInInit {
|
|
required init(e: Error) throws {
|
|
throw e // no unreachable code diagnostic for the implicit return.
|
|
}
|
|
}
|
|
|
|
func sr6141() {
|
|
var bar: String? = ""
|
|
return;
|
|
bar?.append("x") // expected-warning{{code after 'return' will never be executed}}
|
|
}
|