Skip to content

[cxx-interop] Fix a regression making std::string unsafe #80305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions include/swift/ClangImporter/ClangImporterRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ enum class CxxEscapability { Escapable, NonEscapable, Unknown };
struct EscapabilityLookupDescriptor final {
const clang::Type *type;
ClangImporter::Implementation *impl;
// Only explicitly ~Escapable annotated types are considered ~Escapable.
// This is for backward compatibility, so we continue to import aggregates
// containing pointers as Escapable types.
bool annotationOnly = true;

friend llvm::hash_code hash_value(const EscapabilityLookupDescriptor &desc) {
Expand Down
8 changes: 4 additions & 4 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
Expand Down Expand Up @@ -5348,8 +5349,6 @@ ClangTypeEscapability::evaluate(Evaluator &evaluator,
return hadUnknown ? CxxEscapability::Unknown : CxxEscapability::Escapable;
}
}
if (desc.annotationOnly)
return CxxEscapability::Unknown;
if (desugared->isArrayType()) {
auto elemTy = cast<clang::ArrayType>(desugared)
->getElementType()
Expand All @@ -5363,7 +5362,8 @@ ClangTypeEscapability::evaluate(Evaluator &evaluator,
// Base cases
if (desugared->isAnyPointerType() || desugared->isBlockPointerType() ||
desugared->isMemberPointerType() || desugared->isReferenceType())
return CxxEscapability::NonEscapable;
return desc.annotationOnly ? CxxEscapability::Unknown
: CxxEscapability::NonEscapable;
if (desugared->isScalarType())
return CxxEscapability::Escapable;
return CxxEscapability::Unknown;
Expand Down Expand Up @@ -8508,7 +8508,7 @@ ExplicitSafety ClangDeclExplicitSafety::evaluate(
// Enums are always safe.
if (isa<clang::EnumDecl>(decl))
return ExplicitSafety::Safe;

// If it's not a record, leave it unspecified.
auto recordDecl = dyn_cast<clang::RecordDecl>(decl);
if (!recordDecl)
Expand Down
4 changes: 4 additions & 0 deletions test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func useSafeParams(x: Owner, y: View, z: SafeEscapableAggregate, c: MyContainer)
}

func useCfType(x: CFArray) {
_ = x
}

func useString(x: std.string) {
_ = x
}

func useVecOfPtr(x: VecOfPtr) {
Expand All @@ -102,9 +104,11 @@ func useVecOfPtr(x: VecOfPtr) {
}

func useVecOfInt(x: VecOfInt) {
_ = x
}

func useSafeTuple(x: SafeTuple) {
_ = x
}

func useUnsafeTuple(x: UnsafeTuple) {
Expand Down