Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
Created December 30, 2014 12:43
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 airspeedswift/9abca0419f5c337a5b41 to your computer and use it in GitHub Desktop.
Save airspeedswift/9abca0419f5c337a5b41 to your computer and use it in GitHub Desktop.
Different calculation results between -Onone and -O with inout
// import Foundation
struct S {
var i: Int
}
func f(inout s: S) -> Int {
s.i += 1
return s.i
}
var s = S(i: 0)
// let d = NSDate()
let r1 = f(&s)
let r2 = f(&s)
println("r1 = \(r1), r2 = \(r2)")
@airspeedswift
Copy link
Author

~/src [3] % swiftc -Onone optbug.swift; ./optbug
r1 = 1, r2 = 2
~/src % swiftc -O optbug.swift; ./optbug
r1 = 1, r2 = 1

Incidentally, uncommenting that NSDate line makes the problem go away...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment