Skip to content

Set<E>.contains(Set<E>) unexpectedly sometimes returns true #804

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

Closed
northnose opened this issue Feb 5, 2025 · 1 comment
Closed

Set<E>.contains(Set<E>) unexpectedly sometimes returns true #804

northnose opened this issue Feb 5, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@northnose
Copy link

northnose commented Feb 5, 2025

Description

The documentation of Set.contains(_:) states:

Returns a Boolean value that indicates whether the given element exists in the set.

Given two Sets of String (or other element), then, I would expect that s1.contains(s2) always returns false because s1 contains String elements and does not contain any Set<String> elements. But this is not the case; it actually returns true if the underlying ordering of s2 is found in the identical order in s1.

In the test case code, there are many possible permutations of output, but here are two representative lines of output:

["b", "a", "c"] contains ["b", "a"]: true

Notice in this one above, the elements of the second set ["b", "a"] appears in this exact order in the first one

["a", "c", "b"] contains ["a", "b"]: false

In this second example above, the elements of the second set do not appear in the same order in the first set (there is a "c" in between)

Reproduction

import Foundation

for _ in 0..<1000 {
    let s1 = Set(["a", "b", "c"])
    let s2 = Set(["b", "a"])
    print("\(s1) contains \(s2): \(s1.contains(s2))")
    _ = Set<String>([]) // this seems to increase randomness of the internal ordering
}

Expected behavior

Expected: always returns false because s1 never contains the set s2 as an element
Actual: often returns true

Environment

$ swiftc -version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

No response

@northnose northnose added the bug Something isn't working label Feb 5, 2025
@northnose northnose changed the title Set.contains(Set) unexpectedly sometimes returns true Set<E>.contains(Set<E>) unexpectedly sometimes returns true Feb 5, 2025
@hamishknight hamishknight transferred this issue from swiftlang/swift Feb 6, 2025
@hamishknight
Copy link
Contributor

Duplicate of #796

@hamishknight hamishknight marked this as a duplicate of #796 Feb 6, 2025
@hamishknight hamishknight closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants