Files
swift-mirror/test/Interop/Cxx/stdlib/custom-expressible-by-string-literal.swift
Pavel Yaskevich cd9c37cac6 [ConstraintSystem] C++ Interop: Binding a string literal to std.string shouldn't increase the score
Since this is a C++ stdlib type we need make sure that any overloads
that use it are preferred over custom types that also conform to
`ExpressibleByStringLiteral` when argument is a string literal.

This is important for operators like `==` which could be heterogenous
and have a custom C++ type that conforms to `ExpressibleByStringLiteral`
on either side together with `std.string` i.e.
`==(std.string, const CustomString &)`, such overloads should only
be selected if argument passed to `CustomString` is non-literal because
literals are convered by a stdlib `==(std.string, std.string)` overload.
2025-07-24 14:08:15 -07:00

23 lines
876 B
Swift

// RUN: %target-swift-emit-silgen -verify -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s | %FileCheck %s
import CxxStdlib
import StdString
extension StringBox: @retroactive ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.value = std.string(value)
}
}
// CHECK-LABEL: sil hidden [ossa] @$s4main4testyyF
// CHECK: // function_ref static std{{.*}}basic_string<CChar, std{{.*}}char_traits<CChar>, std{{.*}}allocator<CChar>>.== infix(_:_:)
// CHECK: // function_ref static std{{.*}}basic_string<CChar, std{{.*}}char_traits<CChar>, std{{.*}}allocator<CChar>>.== infix(_:_:)
// CHECK: // function_ref static String.== infix(_:_:)
// CHECK: } // end sil function '$s4main4testyyF'
func test() {
let cxxString: std.string = ""
let _ = cxxString == "def" // Ok
let _ = "def" == cxxString // Ok
let _ = "def" == "hello" // Ok
}