Skip to content

Commit 782641a

Browse files
committed
[cxx-interop] ExpirementalFeature -> LanguageFeature.
1 parent 8677fab commit 782641a

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)
@@ -205,10 +206,6 @@ EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)
205206
/// signatures.
206207
EXPERIMENTAL_FEATURE(ImportSymbolicCXXDecls, false)
207208

208-
// Only import C++ methods that return pointers (projections) on owned types as
209-
// unsafe.
210-
EXPERIMENTAL_FEATURE(NewCxxMethodSafetyHeuristics, true)
211-
212209
/// Generate bindings for functions that 'throw' in the C++ section of the generated Clang header.
213210
EXPERIMENTAL_FEATURE(GenerateBindingsForThrowingFunctionsInCXX, false)
214211

lib/ClangImporter/ClangImporter.cpp

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

6823-
static bool legacyIsSafeUseOfCxxMethod(clang::CXXMethodDecl *method) {
6824-
// The user explicitly asked us to import this method.
6825-
if (hasUnsafeAPIAttr(method))
6826-
return true;
6827-
6828-
// If it's a static method, it cannot project anything. It's fine.
6829-
if (method->isOverloadedOperator() || method->isStatic() ||
6830-
isa<clang::CXXConstructorDecl>(decl))
6831-
return true;
6832-
6833-
if (isForeignReferenceType(method->getReturnType()))
6834-
return true;
6835-
6836-
// If it returns a pointer or reference, that's a projection.
6837-
if (method->getReturnType()->isPointerType() ||
6838-
method->getReturnType()->isReferenceType())
6839-
return false;
6840-
6841-
// Check if it's one of the known unsafe methods we currently
6842-
// mark as safe by default.
6843-
if (isUnsafeStdMethod(method))
6844-
return false;
6845-
6846-
// Try to figure out the semantics of the return type. If it's a
6847-
// pointer/iterator, it's unsafe.
6848-
if (auto returnType = dyn_cast<clang::RecordType>(
6849-
method->getReturnType().getCanonicalType())) {
6850-
if (auto cxxRecordReturnType =
6851-
dyn_cast<clang::CXXRecordDecl>(returnType->getDecl())) {
6852-
if (isSwiftClassType(cxxRecordReturnType))
6853-
return true;
6854-
if (hasIteratorAPIAttr(cxxRecordReturnType) ||
6855-
isIterator(cxxRecordReturnType)) {
6856-
return false;
6857-
}
6858-
6859-
// Mark this as safe to help our diganostics down the road.
6860-
if (!cxxRecordReturnType->getDefinition()) {
6861-
return true;
6862-
}
6863-
6864-
if (!hasCustomCopyOrMoveConstructor(cxxRecordReturnType) &&
6865-
!hasOwnedValueAttr(cxxRecordReturnType) &&
6866-
hasPointerInSubobjects(cxxRecordReturnType)) {
6867-
return false;
6868-
}
6869-
}
6870-
}
6871-
6872-
return true;
6873-
}
6874-
68756823
bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
68766824
SafeUseOfCxxDeclDescriptor desc) const {
68776825
const clang::Decl *decl = desc.decl;
68786826

68796827
if (auto method = dyn_cast<clang::CXXMethodDecl>(decl)) {
6880-
if (!desc.ctx.LangOpts.hasFeature(Feature::NewCxxMethodSafetyHeuristics))
6881-
return legacyIsSafeUseOfCxxMethod(method);
6882-
68836828
// The user explicitly asked us to import this method.
68846829
if (hasUnsafeAPIAttr(method))
68856830
return true;

0 commit comments

Comments
 (0)