Skip to content

Commit e3a5477

Browse files
committed
[SwiftCompilerSources] Disfavor overload of == that takes StringRef
This overload is disfavored to make sure that it's only used for cases that don't involve literals, for that `==(StringRef, StaticString) -> Bool` is preferred. Otherwise these overloads are going to be ambiguous because both `StringRef`, `StaticString` conform to `ExpressibleByStringLiteral`. Consider the following example: ```swift func test(lhs: StringRef) { lhs == "<<test>>" } ``` The type-checker used to pick `==(StringRef, StringRef)` overload in this case because it has homogenous parameter types but this is no longer the case because this behavior was too aggressive and led to sub-optimal choices by completely skipping other viable overloads. Since `StaticString` already represents literals it's better to use a standard library type and reserve the `(StringRef, StringRef)` overload to when the literals are not involved. Resolves: rdar://154719565
1 parent 576a378 commit e3a5477

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren, Express
110110
}
111111
}
112112

113+
/// This overload is disfavored to make sure that it's only used for cases that don't involve literals, for that
114+
/// `==(StringRef, StaticString) -> Bool` is preferred. Otherwise these overloads are
115+
/// going to be ambiguous because both StringRef, StaticString conform to `ExpressibleByStringLiteral`.
116+
@_disfavoredOverload
113117
public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
114118
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
115119
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)

0 commit comments

Comments
 (0)