Undefined behavior in simple optimized code with mutating function
Originator: | gvr.oleg | ||
Number: | rdar://39242494 | Date Originated: | 06.04.2018 |
Status: | Resolved | Resolved: | Swift 4.2 |
Product: | Swift | Product Version: | Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1) |
Classification: | Bug | Reproducible: | Always |
Area: Swift Summary: I found undefined behavior (crash sometimes / wrong result) when use mutating func with compiler optimizations. Steps to Reproduce: 1. Create src.swift: import Foundation extension Array { mutating func mutatingFunc(_ object: Any) { return } } func someFunc() -> [[String]] { var arr = ["1", "2", "3"] let str = "5" if arr.contains(str) { arr.mutatingFunc(str) return [arr, [str]] } return [arr] } print(someFunc()) 2. Compile with optimizations: swiftc src.swift -O 3. Run Expected Results: [["1", "2", "3"]] Actual Results: [[]] OR 51638 segmentation fault OR [["1", "2", "3"]] Version/Build: Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1) Target: x86_64-apple-darwin17.5.0 XCode 9.3 Configuration: macOS 10.13.4 (17E199) Related links: https://bugs.swift.org/browse/SR-7354 https://github.com/apple/swift/pull/15840
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!