Skip to content

Commit 4389cc9

Browse files
committed
Revert "[cxx-interop] Mark class templates with unsafe type arguments as unsafe"
This reverts commit afaa499.
1 parent ad8b5a5 commit 4389cc9

File tree

5 files changed

+36
-161
lines changed

5 files changed

+36
-161
lines changed

include/swift/ClangImporter/ClangImporterRequests.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -624,51 +624,6 @@ class CxxValueSemantics
624624
void simple_display(llvm::raw_ostream &out, CxxValueSemanticsDescriptor desc);
625625
SourceLoc extractNearestSourceLoc(CxxValueSemanticsDescriptor desc);
626626

627-
struct ClangTypeExplicitSafetyDescriptor final {
628-
clang::QualType type;
629-
630-
ClangTypeExplicitSafetyDescriptor(clang::QualType type) : type(type) {}
631-
632-
friend llvm::hash_code
633-
hash_value(const ClangTypeExplicitSafetyDescriptor &desc) {
634-
return llvm::hash_combine(desc.type.getTypePtrOrNull());
635-
}
636-
637-
friend bool operator==(const ClangTypeExplicitSafetyDescriptor &lhs,
638-
const ClangTypeExplicitSafetyDescriptor &rhs) {
639-
return lhs.type == rhs.type;
640-
}
641-
642-
friend bool operator!=(const ClangTypeExplicitSafetyDescriptor &lhs,
643-
const ClangTypeExplicitSafetyDescriptor &rhs) {
644-
return !(lhs == rhs);
645-
}
646-
};
647-
648-
void simple_display(llvm::raw_ostream &out,
649-
ClangTypeExplicitSafetyDescriptor desc);
650-
SourceLoc extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc);
651-
652-
/// Determine the safety of the given Clang declaration.
653-
class ClangTypeExplicitSafety
654-
: public SimpleRequest<ClangTypeExplicitSafety,
655-
ExplicitSafety(ClangTypeExplicitSafetyDescriptor),
656-
RequestFlags::Cached> {
657-
public:
658-
using SimpleRequest::SimpleRequest;
659-
660-
// Source location
661-
SourceLoc getNearestLoc() const { return SourceLoc(); };
662-
bool isCached() const { return true; }
663-
664-
private:
665-
friend SimpleRequest;
666-
667-
// Evaluation.
668-
ExplicitSafety evaluate(Evaluator &evaluator,
669-
ClangTypeExplicitSafetyDescriptor desc) const;
670-
};
671-
672627
struct CxxDeclExplicitSafetyDescriptor final {
673628
const clang::Decl *decl;
674629
bool isClass;

include/swift/ClangImporter/ClangImporterTypeIDZone.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ SWIFT_REQUEST(ClangImporter, ClangTypeEscapability,
4848
SWIFT_REQUEST(ClangImporter, CxxValueSemantics,
4949
CxxValueSemanticsKind(CxxValueSemanticsDescriptor), Cached,
5050
NoLocationInfo)
51-
SWIFT_REQUEST(ClangImporter, ClangTypeExplicitSafety,
52-
ExplicitSafety(ClangTypeExplicitSafetyDescriptor), Cached,
53-
NoLocationInfo)
5451
SWIFT_REQUEST(ClangImporter, ClangDeclExplicitSafety,
5552
ExplicitSafety(CxxDeclExplicitSafetyDescriptor), Cached,
5653
NoLocationInfo)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8723,51 +8723,6 @@ SourceLoc swift::extractNearestSourceLoc(SafeUseOfCxxDeclDescriptor desc) {
87238723
return SourceLoc();
87248724
}
87258725

8726-
void swift::simple_display(llvm::raw_ostream &out,
8727-
ClangTypeExplicitSafetyDescriptor desc) {
8728-
out << "Checking if type '" << desc.type.getAsString()
8729-
<< "' is explicitly safe.\n";
8730-
}
8731-
8732-
SourceLoc swift::extractNearestSourceLoc(ClangTypeExplicitSafetyDescriptor desc) {
8733-
return SourceLoc();
8734-
}
8735-
8736-
ExplicitSafety ClangTypeExplicitSafety::evaluate(
8737-
Evaluator &evaluator, ClangTypeExplicitSafetyDescriptor desc) const {
8738-
auto clangType = desc.type;
8739-
8740-
// Handle pointers.
8741-
auto pointeeType = clangType->getPointeeType();
8742-
if (!pointeeType.isNull()) {
8743-
// Function pointers are okay.
8744-
if (pointeeType->isFunctionType())
8745-
return ExplicitSafety::Safe;
8746-
8747-
// Pointers to record types are okay if they come in as foreign reference
8748-
// types.
8749-
if (auto *recordDecl = pointeeType->getAsRecordDecl()) {
8750-
if (hasImportAsRefAttr(recordDecl))
8751-
return ExplicitSafety::Safe;
8752-
}
8753-
8754-
// All other pointers are considered unsafe.
8755-
return ExplicitSafety::Unsafe;
8756-
}
8757-
8758-
// Handle records recursively.
8759-
if (auto recordDecl = clangType->getAsTagDecl()) {
8760-
// If we reached this point the types is not imported as a shared reference,
8761-
// so we don't need to check the bases whether they are shared references.
8762-
return evaluateOrDefault(evaluator,
8763-
ClangDeclExplicitSafety({recordDecl, false}),
8764-
ExplicitSafety::Unspecified);
8765-
}
8766-
8767-
// Everything else is safe.
8768-
return ExplicitSafety::Safe;
8769-
}
8770-
87718726
void swift::simple_display(llvm::raw_ostream &out,
87728727
CxxDeclExplicitSafetyDescriptor desc) {
87738728
out << "Checking if '";
@@ -8839,11 +8794,43 @@ CustomRefCountingOperationResult CustomRefCountingOperation::evaluate(
88398794

88408795
/// Check whether the given Clang type involves an unsafe type.
88418796
static bool hasUnsafeType(Evaluator &evaluator, clang::QualType clangType) {
8797+
// Handle pointers.
8798+
auto pointeeType = clangType->getPointeeType();
8799+
if (!pointeeType.isNull()) {
8800+
// Function pointers are okay.
8801+
if (pointeeType->isFunctionType())
8802+
return false;
8803+
8804+
// Pointers to record types are okay if they come in as foreign reference
8805+
// types.
8806+
if (auto recordDecl = pointeeType->getAsRecordDecl()) {
8807+
if (hasImportAsRefAttr(recordDecl))
8808+
return false;
8809+
}
8810+
8811+
// All other pointers are considered unsafe.
8812+
return true;
8813+
}
88428814

8843-
auto safety =
8844-
evaluateOrDefault(evaluator, ClangTypeExplicitSafety({clangType}),
8845-
ExplicitSafety::Unspecified);
8846-
return safety == ExplicitSafety::Unsafe;
8815+
// Handle records recursively.
8816+
if (auto recordDecl = clangType->getAsTagDecl()) {
8817+
// If we reached this point the types is not imported as a shared reference,
8818+
// so we don't need to check the bases whether they are shared references.
8819+
auto safety = evaluateOrDefault(
8820+
evaluator, ClangDeclExplicitSafety({recordDecl, false}),
8821+
ExplicitSafety::Unspecified);
8822+
switch (safety) {
8823+
case ExplicitSafety::Unsafe:
8824+
return true;
8825+
8826+
case ExplicitSafety::Safe:
8827+
case ExplicitSafety::Unspecified:
8828+
return false;
8829+
}
8830+
}
8831+
8832+
// Everything else is safe.
8833+
return false;
88478834
}
88488835

88498836
ExplicitSafety

lib/ClangImporter/ImportDecl.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#include "clang/AST/RecordLayout.h"
7070
#include "clang/AST/RecursiveASTVisitor.h"
7171
#include "clang/AST/StmtVisitor.h"
72-
#include "clang/AST/TemplateBase.h"
7372
#include "clang/AST/Type.h"
7473
#include "clang/Basic/Specifiers.h"
7574
#include "clang/Basic/TargetInfo.h"
@@ -2302,40 +2301,6 @@ namespace {
23022301
}
23032302
}
23042303

2305-
if (const auto *ctsd =
2306-
dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
2307-
for (auto arg : ctsd->getTemplateArgs().asArray()) {
2308-
auto done = false;
2309-
auto checkUnsafe = [&](clang::TemplateArgument tyArg) {
2310-
if (tyArg.getKind() != clang::TemplateArgument::Type)
2311-
return;
2312-
2313-
auto safety =
2314-
evaluateOrDefault(Impl.SwiftContext.evaluator,
2315-
ClangTypeExplicitSafety({tyArg.getAsType()}),
2316-
ExplicitSafety::Unspecified);
2317-
2318-
if (safety == ExplicitSafety::Unsafe) {
2319-
result->addAttribute(new (Impl.SwiftContext)
2320-
UnsafeAttr(/*implicit=*/true));
2321-
done = true;
2322-
}
2323-
};
2324-
2325-
if (arg.getKind() == clang::TemplateArgument::Pack) {
2326-
for (auto pkArg : arg.getPackAsArray()) {
2327-
checkUnsafe(pkArg);
2328-
if (done)
2329-
break;
2330-
}
2331-
} else {
2332-
checkUnsafe(arg);
2333-
}
2334-
if (done)
2335-
break;
2336-
}
2337-
}
2338-
23392304
bool isNonEscapable = false;
23402305
if (evaluateOrDefault(
23412306
Impl.SwiftContext.evaluator,

test/Interop/Cxx/class/safe-interop-mode.swift

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ struct OwnedData {
8383
void takeSharedObject(SharedObject *) const;
8484
};
8585

86-
// A class template that throws away its type argument.
87-
//
88-
// If this template is instantiated with an unsafe type, it should be considered
89-
// unsafe even if that type is never used.
90-
template <typename> struct TTake {};
91-
92-
using TTakeInt = TTake<int>;
93-
using TTakePtr = TTake<int *>;
94-
using TTakeSafeTuple = TTake<SafeTuple>;
95-
using TTakeUnsafeTuple = TTake<UnsafeTuple>;
96-
9786
struct HoldsShared {
9887
SharedObject* obj;
9988

@@ -195,21 +184,3 @@ func useSharedReference(frt: DerivedFromSharedObject, h: HoldsShared) {
195184
let _ = frt
196185
let _ = h.getObj()
197186
}
198-
199-
func useTTakeInt(x: TTakeInt) {
200-
_ = x
201-
}
202-
203-
func useTTakePtr(x: TTakePtr) {
204-
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
205-
_ = x // expected-note{{reference to parameter 'x' involves unsafe type}}
206-
}
207-
208-
func useTTakeSafeTuple(x: TTakeSafeTuple) {
209-
_ = x
210-
}
211-
212-
func useTTakeUnsafeTuple(x: TTakeUnsafeTuple) {
213-
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
214-
_ = x // expected-note{{reference to parameter 'x' involves unsafe type}}
215-
}

0 commit comments

Comments
 (0)