Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8831,6 +8831,29 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator,
ClangTypeEscapability({recordDecl->getTypeForDecl(), nullptr}),
CxxEscapability::Unknown) != CxxEscapability::Unknown)
return ExplicitSafety::Safe;

// A template class is unsafe if any of its type arguments are unsafe.
// Note that this does not rely on the record being defined.
if (const auto *ctsd =
dyn_cast<clang::ClassTemplateSpecializationDecl>(recordDecl)) {
for (auto arg : ctsd->getTemplateArgs().asArray()) {
switch (arg.getKind()) {
case clang::TemplateArgument::Type:
if (hasUnsafeType(evaluator, arg.getAsType()))
return ExplicitSafety::Unsafe;
break;
case clang::TemplateArgument::Pack:
for (auto pkArg : arg.getPackAsArray()) {
if (pkArg.getKind() == clang::TemplateArgument::Type &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can packs be nested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not as far as I'm aware.

hasUnsafeType(evaluator, pkArg.getAsType()))
return ExplicitSafety::Unsafe;
}
break;
default:
continue;
}
}
}

// If we don't have a definition, leave it unspecified.
recordDecl = recordDecl->getDefinition();
Expand All @@ -8844,7 +8867,7 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator,
return ExplicitSafety::Unsafe;
}
}

// Check the fields.
for (auto field : recordDecl->fields()) {
if (hasUnsafeType(evaluator, field->getType()))
Expand Down
34 changes: 0 additions & 34 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,40 +2300,6 @@ namespace {
}
}

if (const auto *ctsd =
dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
for (auto arg : ctsd->getTemplateArgs().asArray()) {
auto done = false;
auto checkUnsafe = [&](clang::TemplateArgument tyArg) {
if (tyArg.getKind() != clang::TemplateArgument::Type)
return;

auto safety =
evaluateOrDefault(Impl.SwiftContext.evaluator,
ClangTypeExplicitSafety({tyArg.getAsType()}),
ExplicitSafety::Unspecified);

if (safety == ExplicitSafety::Unsafe) {
result->addAttribute(new (Impl.SwiftContext)
UnsafeAttr(/*implicit=*/true));
done = true;
}
};

if (arg.getKind() == clang::TemplateArgument::Pack) {
for (auto pkArg : arg.getPackAsArray()) {
checkUnsafe(pkArg);
if (done)
break;
}
} else {
checkUnsafe(arg);
}
if (done)
break;
}
}

bool isNonEscapable = false;
if (evaluateOrDefault(
Impl.SwiftContext.evaluator,
Expand Down
15 changes: 15 additions & 0 deletions test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ using TTakePtr = TTake<int *>;
using TTakeSafeTuple = TTake<SafeTuple>;
using TTakeUnsafeTuple = TTake<UnsafeTuple>;

// An escapability or explicit safety annotation means a type is considered safe
// even if it would otherwise be considered unsafe.
template <typename> struct SWIFT_ESCAPABLE TEscape {};
template <typename> struct __attribute__((swift_attr("safe"))) TSafe { void *ptr; };

using TEscapePtr = TEscape<int *>;
using TEscapeUnsafeTuple = TEscape<UnsafeTuple>;
using TSafePtr = TSafe<int *>;
using TSafeTuple = TSafe<UnsafeTuple>;

struct HoldsShared {
SharedObject* obj;

Expand Down Expand Up @@ -212,3 +222,8 @@ func useTTakeUnsafeTuple(x: TTakeUnsafeTuple) {
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
_ = x // expected-note{{reference to parameter 'x' involves unsafe type}}
}

func useTEscapePtr(x: TEscapePtr) { _ = x }
func useTEscapeUnsafeTuple(x: TEscapeUnsafeTuple) { _ = x }
func useTSafePtr(x: TSafePtr) { _ = x }
func useTSafeTuple(x: TSafeTuple) { _ = x }