Skip to content

Commit c6a95c6

Browse files
authored
Merge pull request #59152 from apple/egorzhdan/libswift-string-init
[cxx-interop][SwiftCompilerSources] Fix conversion between `std::string` and `Swift.String`
2 parents 9c09020 + be711e3 commit c6a95c6

File tree

9 files changed

+14
-21
lines changed

9 files changed

+14
-21
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
@_exported import BasicBridging
14+
import std
1415

1516
//===----------------------------------------------------------------------===//
1617
// StringRef
@@ -60,6 +61,13 @@ extension String {
6061
return c(BridgedStringRef(data: buffer.baseAddress, length: buffer.count))
6162
}
6263
}
64+
65+
/// Underscored to avoid name collision with the std overlay.
66+
/// To be replaced with an overlay call once the CI uses SDKs built with Swift 5.8.
67+
public init(_cxxString s: std.string) {
68+
self.init(cString: s.c_str())
69+
withExtendedLifetime(s) {}
70+
}
6371
}
6472

6573
extension Array {

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible, HasShortDescr
2525
public var function: Function { SILBasicBlock_getFunction(bridged).function }
2626

2727
public var description: String {
28-
var s = SILBasicBlock_debugDescription(bridged)
29-
return String(cString: s.c_str())
28+
String(_cxxString: SILBasicBlock_debugDescription(bridged))
3029
}
3130
public var shortDescription: String { name }
3231

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ final public class Function : CustomStringConvertible, HasShortDescription {
2121
}
2222

2323
final public var description: String {
24-
var s = SILFunction_debugDescription(bridged)
25-
return String(cString: s.c_str())
24+
String(_cxxString: SILFunction_debugDescription(bridged))
2625
}
2726

2827
public var shortDescription: String { name.string }

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
1919
}
2020

2121
public var description: String {
22-
var s = SILGlobalVariable_debugDescription(bridged)
23-
return String(cString: s.c_str())
22+
String(_cxxString: SILGlobalVariable_debugDescription(bridged))
2423
}
2524

2625
public var shortDescription: String { name.string }

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
3838
final public var function: Function { block.function }
3939

4040
final public var description: String {
41-
var s = SILNode_debugDescription(bridgedNode)
42-
return String(cString: s.c_str())
41+
String(_cxxString: SILNode_debugDescription(bridgedNode))
4342
}
4443

4544
final public var operands: OperandArray {
@@ -143,8 +142,7 @@ public class SingleValueInstruction : Instruction, Value {
143142

144143
public final class MultipleValueInstructionResult : Value {
145144
final public var description: String {
146-
var s = SILNode_debugDescription(bridgedNode)
147-
return String(cString: s.c_str())
145+
String(_cxxString: SILNode_debugDescription(bridgedNode))
148146
}
149147

150148
public var instruction: Instruction {

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public enum Ownership {
8181

8282
extension Value {
8383
public var description: String {
84-
var s = SILNode_debugDescription(bridgedNode)
85-
return String(cString: s.c_str())
84+
String(_cxxString: SILNode_debugDescription(bridgedNode))
8685
}
8786

8887
public var uses: UseList {

test/SILOptimizer/addr_escape_info.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
// REQUIRES: swift_in_compiler
44

5-
// rdar92963081
6-
// UNSUPPORTED: OS=linux-gnu
7-
85
sil_stage canonical
96

107
import Builtin

test/SILOptimizer/escape_info.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
// REQUIRES: swift_in_compiler
44

5-
// rdar92963081
6-
// UNSUPPORTED: OS=linux-gnu
7-
85

96
sil_stage canonical
107

test/SILOptimizer/ranges.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
// REQUIRES: swift_in_compiler
44

5-
// rdar92963081
6-
// UNSUPPORTED: OS=linux-gnu
7-
85

96
sil_stage canonical
107

0 commit comments

Comments
 (0)