Skip to content

Commit 094b9ca

Browse files
committed
[cxx-interop] ExpirementalFeature -> LanguageFeature.
1 parent 1c8486d commit 094b9ca

File tree

4 files changed

+6
-64
lines changed

4 files changed

+6
-64
lines changed

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct SourceLoc {
2727
guard bridged.isValid() else {
2828
return nil
2929
}
30-
#if hasFeature(NewCxxMethodSafetyHeuristics)
30+
#if $NewCxxMethodSafetyHeuristics
3131
self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self)
3232
#else
3333
self.locationInFile = bridged.__getOpaquePointerValueUnsafe().assumingMemoryBound(to: UInt8.self)
@@ -61,7 +61,7 @@ public struct CharSourceRange {
6161
}
6262

6363
public init?(bridged: swift.CharSourceRange) {
64-
#if hasFeature(NewCxxMethodSafetyHeuristics)
64+
#if $NewCxxMethodSafetyHeuristics
6565
guard let start = SourceLoc(bridged: bridged.getStart()) else {
6666
return nil
6767
}

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
6666
public var description: String { string }
6767

6868
public var count: Int {
69-
#if hasFeature(NewCxxMethodSafetyHeuristics)
69+
#if $NewCxxMethodSafetyHeuristics
7070
Int(_bridged.bytes_end() - _bridged.bytes_begin())
7171
#else
7272
Int(_bridged.__bytes_endUnsafe() - _bridged.__bytes_beginUnsafe())
7373
#endif
7474
}
7575

7676
public subscript(index: Int) -> UInt8 {
77-
#if hasFeature(NewCxxMethodSafetyHeuristics)
77+
#if $NewCxxMethodSafetyHeuristics
7878
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
7979
count: count)
8080
#else
@@ -85,7 +85,7 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
8585
}
8686

8787
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
88-
#if hasFeature(NewCxxMethodSafetyHeuristics)
88+
#if $NewCxxMethodSafetyHeuristics
8989
let lhsBuffer = UnsafeBufferPointer<UInt8>(
9090
start: lhs._bridged.bytes_begin(),
9191
count: lhs.count)

include/swift/Basic/Features.def

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ LANGUAGE_FEATURE(BuiltinStackAlloc, 0, "Builtin.stackAlloc", true)
9191
LANGUAGE_FEATURE(BuiltinUnprotectedStackAlloc, 0, "Builtin.unprotectedStackAlloc", true)
9292
LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline", true)
9393
LANGUAGE_FEATURE(BuiltinUnprotectedAddressOf, 0, "Builtin.unprotectedAddressOf", true)
94+
LANGUAGE_FEATURE(NewCxxMethodSafetyHeuristics, 0, "Only import C++ methods that return pointers (projections) on owned types as unsafe", true)
9495
SUPPRESSIBLE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability", true)
9596
LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment", true)
9697
LANGUAGE_FEATURE(BuiltinCreateTaskGroupWithFlags, 0, "Builtin.createTaskGroupWithFlags", true)
@@ -197,10 +198,6 @@ EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)
197198
/// signatures.
198199
EXPERIMENTAL_FEATURE(ImportSymbolicCXXDecls, false)
199200

200-
// Only import C++ methods that return pointers (projections) on owned types as
201-
// unsafe.
202-
EXPERIMENTAL_FEATURE(NewCxxMethodSafetyHeuristics, true)
203-
204201
/// Generate bindings for functions that 'throw' in the C++ section of the generated Clang header.
205202
EXPERIMENTAL_FEATURE(GenerateBindingsForThrowingFunctionsInCXX, false)
206203

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6774,66 +6774,11 @@ bool anySubobjectsSelfContained(const clang::CXXRecordDecl *decl) {
67746774
return false;
67756775
}
67766776

6777-
static bool legacyIsSafeUseOfCxxMethod(clang::CXXMethodDecl *method) {
6778-
// The user explicitly asked us to import this method.
6779-
if (hasUnsafeAPIAttr(method))
6780-
return true;
6781-
6782-
// If it's a static method, it cannot project anything. It's fine.
6783-
if (method->isOverloadedOperator() || method->isStatic() ||
6784-
isa<clang::CXXConstructorDecl>(decl))
6785-
return true;
6786-
6787-
if (isForeignReferenceType(method->getReturnType()))
6788-
return true;
6789-
6790-
// If it returns a pointer or reference, that's a projection.
6791-
if (method->getReturnType()->isPointerType() ||
6792-
method->getReturnType()->isReferenceType())
6793-
return false;
6794-
6795-
// Check if it's one of the known unsafe methods we currently
6796-
// mark as safe by default.
6797-
if (isUnsafeStdMethod(method))
6798-
return false;
6799-
6800-
// Try to figure out the semantics of the return type. If it's a
6801-
// pointer/iterator, it's unsafe.
6802-
if (auto returnType = dyn_cast<clang::RecordType>(
6803-
method->getReturnType().getCanonicalType())) {
6804-
if (auto cxxRecordReturnType =
6805-
dyn_cast<clang::CXXRecordDecl>(returnType->getDecl())) {
6806-
if (isSwiftClassType(cxxRecordReturnType))
6807-
return true;
6808-
if (hasIteratorAPIAttr(cxxRecordReturnType) ||
6809-
isIterator(cxxRecordReturnType)) {
6810-
return false;
6811-
}
6812-
6813-
// Mark this as safe to help our diganostics down the road.
6814-
if (!cxxRecordReturnType->getDefinition()) {
6815-
return true;
6816-
}
6817-
6818-
if (!hasCustomCopyOrMoveConstructor(cxxRecordReturnType) &&
6819-
!hasOwnedValueAttr(cxxRecordReturnType) &&
6820-
hasPointerInSubobjects(cxxRecordReturnType)) {
6821-
return false;
6822-
}
6823-
}
6824-
}
6825-
6826-
return true;
6827-
}
6828-
68296777
bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
68306778
SafeUseOfCxxDeclDescriptor desc) const {
68316779
const clang::Decl *decl = desc.decl;
68326780

68336781
if (auto method = dyn_cast<clang::CXXMethodDecl>(decl)) {
6834-
if (!desc.ctx.LangOpts.hasFeature(Feature::NewCxxMethodSafetyHeuristics))
6835-
return legacyIsSafeUseOfCxxMethod(method);
6836-
68376782
// The user explicitly asked us to import this method.
68386783
if (hasUnsafeAPIAttr(method))
68396784
return true;

0 commit comments

Comments
 (0)