Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ArraySlice<Substring> in a Set causes a flaky crash #59865

Open
abdulowork opened this issue Jul 2, 2022 · 1 comment
Open

Using ArraySlice<Substring> in a Set causes a flaky crash #59865

abdulowork opened this issue Jul 2, 2022 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@abdulowork
Copy link
Contributor

Describe the bug
Using ArraySlice<Substring> in a Set or as a Dictionary key crashes sometimes with Fatal error: Duplicate elements of type 'ArraySlice<Substring>'. The crash is not stable but wrapping it in a loop makes it easy to reproduce.

In the provided reproduction example changing any of the following fixes the crash:

  1. Mapping ArraySlice<Substring> to ArraySlice<String>
  2. Removing 🫠 emoji
  3. Changing import A.C to import D.C

To Reproduce

  1. Create the following main.swift file:
    import Foundation
    
    var tryCrashSwiftHashingAttempt = 0
    
    while true {
        tryCrashSwiftHashingAttempt += 1
        
        let example = """
            🫠
            import A.B
            import A.C
            """
        
        let lines = example.components(
            separatedBy: CharacterSet(charactersIn: "\u{000A}\u{000D}")
        )
        
        func splitImports(contents: String) -> [ArraySlice<Substring>] {
            let importIdentifier = contents.split(separator: " ").last!
            let importedParts = importIdentifier.split(separator: ".")
            
            return stride(from: 0, to: importedParts.count, by: 1).map {
                importedParts[0..<importedParts.count - $0]
            }
        }
        
        var mySet = Set<ArraySlice<Substring>>()
        
        let lastLine           = lines[lines.index(lines.endIndex, offsetBy: -1)]
        let lastBeforeLastLine = lines[lines.index(lines.endIndex, offsetBy: -2)]
        
        [lastLine, lastBeforeLastLine].forEach { line in
            splitImports(contents: line).forEach { slice in
                mySet.insert(slice)
            }
        }
        
        print(tryCrashSwiftHashingAttempt)
    }
  2. Compile with swiftc main.swift or swiftc -O main.swift.
  3. Run ./main
  4. Within a couple of seconds the process should crash with:
    Fatal error: Duplicate elements of type 'ArraySlice<Substring>' were found in a Set.
    This usually means either that the type violates Hashable's requirements, or
    that members of such a set were mutated after insertion.
    

Expected behavior
The process doesn't crash

Screenshots
Screenshot 2022-07-02 at 18 52 09

Screenshot 2022-07-02 at 18 51 28 1

Environment (please complete the following information):

  • OS: macOS 12.5
  • Xcode Version:
    • 13.4
    • 14 beta 1
@abdulowork abdulowork added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jul 2, 2022
@YOCKOW
Copy link
Member

YOCKOW commented Jul 3, 2022

🤔 I suspect that this is related to #59388.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants