Skip to content

Commit f995144

Browse files
authored
Merge pull request #85906 from egorzhdan/egorzhdan/reland-cxx-string-cs
Reapply "[ConstraintSystem] C++ Interop: Binding a string literal to `std.string` shouldn't increase the score"
2 parents da8b721 + 84e7f82 commit f995144

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ IDENTIFIER(Any)
3535
IDENTIFIER(ArrayLiteralElement)
3636
IDENTIFIER(asLocalActor)
3737
IDENTIFIER(atIndexedSubscript)
38+
IDENTIFIER(basic_string)
3839
IDENTIFIER_(bridgeToObjectiveC)
3940
IDENTIFIER(buildArray)
4041
IDENTIFIER(buildBlock)

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
11161116
/// Check if this is a ObjCBool type from the Objective-C module.
11171117
bool isObjCBool();
11181118

1119+
/// Check if this is a std.string type from C++.
1120+
bool isCxxString();
1121+
11191122
/// Check if this is the type Unicode.Scalar from the Swift standard library.
11201123
bool isUnicodeScalar();
11211124

lib/AST/Type.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "swift/AST/Types.h"
4444
#include "swift/Basic/Assertions.h"
4545
#include "swift/Basic/Compiler.h"
46+
#include "clang/AST/DeclCXX.h"
4647
#include "llvm/ADT/APFloat.h"
4748
#include "llvm/ADT/STLExtras.h"
4849
#include "llvm/ADT/SmallPtrSet.h"
@@ -1302,6 +1303,21 @@ bool TypeBase::isObjCBool() {
13021303
return module->getName().is("ObjectiveC") && NTD->getName().is("ObjCBool");
13031304
}
13041305

1306+
bool TypeBase::isCxxString() {
1307+
auto *nominal = getAnyNominal();
1308+
if (!nominal)
1309+
return false;
1310+
1311+
auto *clangDecl =
1312+
dyn_cast_or_null<clang::CXXRecordDecl>(nominal->getClangDecl());
1313+
if (!clangDecl)
1314+
return false;
1315+
1316+
auto &ctx = nominal->getASTContext();
1317+
return clangDecl->isInStdNamespace() && clangDecl->getIdentifier() &&
1318+
ctx.Id_basic_string.is(clangDecl->getName());
1319+
}
1320+
13051321
bool TypeBase::isUnicodeScalar() {
13061322
if (!is<StructType>())
13071323
return false;

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,24 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
293293

294294
// If the protocol has a default type, check it.
295295
if (auto defaultType = TypeChecker::getDefaultType(literalProtocol, DC)) {
296-
// Check whether the nominal types match. This makes sure that we
297-
// properly handle Array vs. Array<T>.
298-
if (defaultType->getAnyNominal() != type->getAnyNominal()) {
299-
increaseScore(SK_NonDefaultLiteral, locator);
296+
auto isDefaultType = [&defaultType](Type type) {
297+
// Check whether the nominal types match. This makes sure that we
298+
// properly handle Array vs. Array<T>.
299+
return defaultType->getAnyNominal() == type->getAnyNominal();
300+
};
301+
302+
if (!isDefaultType(type)) {
303+
// Treat `std.string` as a default type just like we do
304+
// Swift standard library `String`. This helps to disambiguate
305+
// operator overloads that use `std.string` vs. a custom C++
306+
// type that conforms to `ExpressibleByStringLiteral` as well.
307+
bool isCxxDefaultType =
308+
literalProtocol->isSpecificProtocol(
309+
KnownProtocolKind::ExpressibleByStringLiteral) &&
310+
type->isCxxString();
311+
312+
increaseScore(SK_NonDefaultLiteral, locator,
313+
isCxxDefaultType ? 1 : 2);
300314
}
301315
}
302316

test/Interop/Cxx/stdlib/Inputs/std-string.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@ struct HasMethodThatReturnsString {
66
};
77

88
inline std::string takesStringWithDefaultArg(std::string s = "abc") { return s; }
9+
10+
struct StringBox {
11+
std::string value;
12+
13+
friend bool operator==(const StringBox &lhs, const std::string &rhs) {
14+
return lhs.value == rhs;
15+
}
16+
17+
friend bool operator==(const std::string &lhs, const StringBox &rhs) {
18+
return rhs == lhs;
19+
}
20+
21+
StringBox operator+(const StringBox &rhs) const {
22+
return {value + rhs.value};
23+
}
24+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-swift-emit-silgen -verify -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s | %FileCheck %s
2+
3+
import CxxStdlib
4+
import StdString
5+
6+
extension StringBox: @retroactive ExpressibleByStringLiteral {
7+
public init(stringLiteral value: String) {
8+
self.value = std.string(value)
9+
}
10+
}
11+
12+
func takesAny(_: Any) {}
13+
14+
// CHECK-LABEL: sil hidden [ossa] @$s4main4testyyF
15+
// CHECK: // function_ref static std{{.*}}basic_string<CChar, std{{.*}}char_traits<CChar>, std{{.*}}allocator<CChar>>.== infix(_:_:)
16+
// CHECK: // function_ref static std{{.*}}basic_string<CChar, std{{.*}}char_traits<CChar>, std{{.*}}allocator<CChar>>.== infix(_:_:)
17+
// CHECK: // function_ref static String.== infix(_:_:)
18+
// CHECK: // function_ref static String.+ infix(_:_:)
19+
// CHECK: // function_ref static StringBox.+ infix(_:_:)
20+
// CHECK-NEXT: %{{.*}} = function_ref @$sSo9StringBoxV1poiyA2B_ABtFZ
21+
// CHECK: // function_ref takesAny(_:)
22+
// CHECK-NEXT: %{{.*}} = function_ref @$s4main8takesAnyyyypF
23+
// CHECK: } // end sil function '$s4main4testyyF'
24+
func test() {
25+
let cxxString: std.string = ""
26+
let _ = cxxString == "def" // Ok
27+
let _ = "def" == cxxString // Ok
28+
let _ = "def" == "hello" // Ok
29+
30+
takesAny("abc" + "def")
31+
takesAny("abc" + "def" + "xyz")
32+
takesAny(StringBox("abc") + "def" + "xyz")
33+
takesAny("abc" + StringBox("def") + "xyz")
34+
}

0 commit comments

Comments
 (0)