Created
March 28, 2019 18:31
-
-
Save memfrag/71e68335a39622e200a6eb99b7609e26 to your computer and use it in GitHub Desktop.
Reproducing SR-10221
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
enum Animal { | |
case aardvark(NSWindow.StyleMask) | |
case bear | |
case cat | |
} | |
func isNotCat(_ animal: Animal) -> Bool { | |
switch animal { | |
case .cat: | |
return false | |
default: | |
return true | |
} | |
} | |
var globalIsNotCat: Bool = true | |
func reproduceBug(_ animal: Animal) { | |
// | |
// This reproduces the bug when all of the following is true: | |
// | |
// 1. The file is compiled with `swiftc -O` | |
// | |
// 2. The enum Animal has a case with an associated value | |
// that is an Objective-C enum. | |
// | |
// 3. The variable (globalIsNotCat) that the result of | |
// isNotCat(...) is assigned to is not in the scope | |
// of the reproduceBug(...) function. | |
// | |
// 4. There is an `if` that checks the variable. | |
// | |
// 5. There is a print("") in the `if` following the first | |
// print that prints the value. | |
// | |
// Change one of these things and the printed value is correct. | |
// | |
globalIsNotCat = isNotCat(animal) | |
// Correct -> prints true, Bug -> prints false | |
print(globalIsNotCat) | |
if globalIsNotCat { | |
print("") | |
} | |
} | |
reproduceBug(.bear) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment