swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.36k forks source link

[SR-15816] Retrieving a value from a Substring-keyed Dictionary is broken #58093

Open saagarjha opened 2 years ago

saagarjha commented 2 years ago
Previous ID SR-15816
Radar None
Original Reporter @saagarjha
Type Bug
Environment I can reproduce this intermittently when compiling in debug mode in Xcode 13.3 beta, as well as on the nightly toolchain. Here is a Godbolt capturing this:
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 797e776870dd42d821f41a7a36d192af

Issue Description:

When using two equal Substring s to lookup a value in a Dictionary, I found that sometimes the lookup will return nil for one of the keys. Here is a sample program that reproduces this (sometimes–I think it's reading garbage?):

let path = "./Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/DeviceTypes/iPhone Xʀ.simdevicetype"[...]
func parentDirectory<S: StringProtocol>(of path: S) -> S.SubSequence {
    return path[..<path.lastIndex(of: "/")!]
}
let parent1 = "./Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/DeviceTypes"[...]
let parent2 = parentDirectory(of: path)
var dict = [Substring: Int]()
dict[parent1] = 1
print(parent1 == parent2, dict[parent1], dict[parent2])

Here is the broken output:

true Optional(1) nil
Catfish-Man commented 2 years ago

I believe this should be fixed in https://github.com/apple/swift/pull/58948