Skip to content

Commit 35263ef

Browse files
committed
[cxx-interop][SwiftCompilerSources] Do not use SWIFT_IMPORT_UNSAFE with BridgedOwnedString
`SWIFT_IMPORT_UNSAFE` is an escape hatch that can be used to make the Swift compiler ignore its usual safety heuristics for C++ types. `BridgedOwnedString` fits into the definition of a self-contained C++ type in Swift: it manages the lifetimes of its own fields. This removes the usages of `SWIFT_IMPORT_UNSAFE` for C++ functions that return `BridgedOwnedString`, and annotates `BridgedOwnedString` as a self-contained type.
1 parent 6236b25 commit 35263ef

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

include/swift/Basic/BasicBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class BridgedOwnedString {
234234
#endif
235235

236236
void destroy() const;
237-
};
237+
} SWIFT_SELF_CONTAINED;
238238

239239
SWIFT_NAME("getter:BridgedOwnedString.data(self:)")
240240
BRIDGED_INLINE

include/swift/Basic/Compiler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
#define SWIFT_IMPORT_UNSAFE
9191
#endif
9292

93+
/// Same as `SWIFT_SELF_CONTAINED` in <swift/bridging>.
94+
#if __has_attribute(swift_attr)
95+
#define SWIFT_SELF_CONTAINED __attribute__((swift_attr("import_owned")))
96+
#else
97+
#define SWIFT_SELF_CONTAINED
98+
#endif
99+
93100
#ifdef __GNUC__
94101
#define SWIFT_ATTRIBUTE_NORETURN __attribute__((noreturn))
95102
#elif defined(_MSC_VER)

include/swift/SIL/SILBridging.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ struct BridgedLifetimeDependenceInfo {
267267
BRIDGED_INLINE bool checkScope(SwiftInt index) const;
268268
BRIDGED_INLINE SwiftInt getTargetIndex() const;
269269

270-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
270+
BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
271271
};
272272

273273
struct BridgedLifetimeDependenceInfoArray {
@@ -313,7 +313,7 @@ struct BridgedASTType {
313313
}
314314
#endif
315315

316-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
316+
BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
317317

318318
BRIDGED_INLINE bool hasTypeParameter() const;
319319

@@ -394,7 +394,7 @@ struct BridgedType {
394394
}
395395
#endif
396396

397-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
397+
BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
398398
BRIDGED_INLINE bool isNull() const;
399399
BRIDGED_INLINE bool isAddress() const;
400400
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getAddressType() const;
@@ -486,7 +486,7 @@ struct BridgedValue {
486486

487487
Kind getKind() const;
488488
BRIDGED_INLINE swift::ValueBase * _Nonnull getSILValue() const;
489-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
489+
BridgedOwnedString getDebugDescription() const;
490490
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedOperand getFirstUse() const;
491491
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getType() const;
492492
BRIDGED_INLINE Ownership getOwnership() const;
@@ -582,7 +582,7 @@ struct BridgedLocation {
582582
}
583583
#endif
584584

585-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
585+
BridgedOwnedString getDebugDescription() const;
586586
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getAutogeneratedLocation() const;
587587
BRIDGED_INLINE bool hasValidLineNumber() const;
588588
BRIDGED_INLINE bool isAutoGenerated() const;
@@ -638,7 +638,7 @@ struct BridgedFunction {
638638
SWIFT_IMPORT_UNSAFE BridgedFunction() {}
639639
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::SILFunction * _Nonnull getFunction() const;
640640
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
641-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
641+
BridgedOwnedString getDebugDescription() const;
642642
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getLocation() const;
643643
BRIDGED_INLINE bool hasOwnership() const;
644644
BRIDGED_INLINE bool hasLoweredAddresses() const;
@@ -732,7 +732,7 @@ struct BridgedGlobalVar {
732732
BridgedGlobalVar(SwiftObject obj) : obj(obj) {}
733733
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::SILGlobalVariable * _Nonnull getGlobal() const;
734734
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl getDecl() const;
735-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
735+
BridgedOwnedString getDebugDescription() const;
736736
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
737737
BRIDGED_INLINE bool isLet() const;
738738
BRIDGED_INLINE void setLet(bool value) const;
@@ -849,7 +849,7 @@ struct BridgedInstruction {
849849
#endif
850850

851851
BridgedInstruction(SwiftObject obj) : obj(obj) {}
852-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
852+
BridgedOwnedString getDebugDescription() const;
853853
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedInstruction getNext() const;
854854
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedInstruction getPrevious() const;
855855
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock getParent() const;
@@ -1111,7 +1111,7 @@ struct BridgedBasicBlock {
11111111
}
11121112
#endif
11131113

1114-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1114+
BridgedOwnedString getDebugDescription() const;
11151115
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedBasicBlock getNext() const;
11161116
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedBasicBlock getPrevious() const;
11171117
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedFunction getFunction() const;
@@ -1151,7 +1151,7 @@ struct BridgedSuccessorArray {
11511151
struct BridgedVTableEntry {
11521152
const swift::SILVTableEntry * _Nonnull entry;
11531153

1154-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1154+
BridgedOwnedString getDebugDescription() const;
11551155
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedFunction getImplementation() const;
11561156
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedVTableEntry advanceBy(SwiftInt index) const;
11571157
};
@@ -1164,7 +1164,7 @@ struct BridgedVTableEntryArray {
11641164
struct BridgedVTable {
11651165
const swift::SILVTable * _Nonnull vTable;
11661166

1167-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1167+
BridgedOwnedString getDebugDescription() const;
11681168
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedVTableEntryArray getEntries() const;
11691169
};
11701170

@@ -1185,7 +1185,7 @@ struct BridgedWitnessTableEntry {
11851185
}
11861186
#endif
11871187

1188-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1188+
BridgedOwnedString getDebugDescription() const;
11891189
BRIDGED_INLINE Kind getKind() const;
11901190
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedFunction getMethodFunction() const;
11911191
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedWitnessTableEntry advanceBy(SwiftInt index) const;
@@ -1199,7 +1199,7 @@ struct BridgedWitnessTableEntryArray {
11991199
struct BridgedWitnessTable {
12001200
const swift::SILWitnessTable * _Nonnull table;
12011201

1202-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1202+
BridgedOwnedString getDebugDescription() const;
12031203
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedWitnessTableEntryArray getEntries() const;
12041204
};
12051205

@@ -1210,7 +1210,7 @@ struct OptionalBridgedWitnessTable {
12101210
struct BridgedDefaultWitnessTable {
12111211
const swift::SILDefaultWitnessTable * _Nonnull table;
12121212

1213-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getDebugDescription() const;
1213+
BridgedOwnedString getDebugDescription() const;
12141214
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedWitnessTableEntryArray getEntries() const;
12151215
};
12161216

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ struct BridgedPassContext {
206206
Lowered
207207
};
208208

209-
SWIFT_IMPORT_UNSAFE BridgedOwnedString getModuleDescription() const;
209+
BridgedOwnedString getModuleDescription() const;
210210
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedChangeNotificationHandler asNotificationHandler() const;
211211
BRIDGED_INLINE void notifyDependencyOnBodyOf(BridgedFunction otherFunction) const;
212212
BRIDGED_INLINE SILStage getSILStage() const;
@@ -251,12 +251,12 @@ struct BridgedPassContext {
251251
BridgedFunction function) const;
252252
bool specializeClassMethodInst(BridgedInstruction cm) const;
253253
bool specializeAppliesInFunction(BridgedFunction function, bool isMandatory) const;
254-
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleOutlinedVariable(BridgedFunction function) const;
255-
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleAsyncRemoved(BridgedFunction function) const;
256-
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
254+
BridgedOwnedString mangleOutlinedVariable(BridgedFunction function) const;
255+
BridgedOwnedString mangleAsyncRemoved(BridgedFunction function) const;
256+
BridgedOwnedString mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
257257
SwiftInt numDeadArgs,
258258
BridgedFunction function) const;
259-
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleWithClosureArgs(BridgedValueArray closureArgs,
259+
BridgedOwnedString mangleWithClosureArgs(BridgedValueArray closureArgs,
260260
BridgedArrayRef closureArgIndices,
261261
BridgedFunction applySiteCallee) const;
262262

0 commit comments

Comments
 (0)