Skip to content

Instantly share code, notes, and snippets.

@memfrag
Created March 28, 2019 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memfrag/71e68335a39622e200a6eb99b7609e26 to your computer and use it in GitHub Desktop.
Save memfrag/71e68335a39622e200a6eb99b7609e26 to your computer and use it in GitHub Desktop.
Reproducing SR-10221
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