Skip to content

Commit 0be01c4

Browse files
authored
Merge pull request #67097 from hyp/eng/revert-8e0c17b27470de49fecf19d5a222ab247984d677
Revert "[interop] do not import functions whose return type is not imported"
2 parents e12d53e + 6c6092e commit 0be01c4

File tree

8 files changed

+39
-98
lines changed

8 files changed

+39
-98
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ NOTE(record_field_not_imported, none, "field %0 unavailable (cannot import)", (c
256256
NOTE(invoked_func_not_imported, none, "function %0 unavailable (cannot import)", (const clang::NamedDecl*))
257257
NOTE(record_method_not_imported, none, "method %0 unavailable (cannot import)", (const clang::NamedDecl*))
258258
NOTE(objc_property_not_imported, none, "property %0 unavailable (cannot import)", (const clang::NamedDecl*))
259-
NOTE(unsupported_return_type, none, "C++ function %0 is unavailable: return type is unavailable in Swift", (const clang::NamedDecl*))
260259

261260
NOTE(placeholder_for_forward_declared_interface_member_access_failure, none,
262261
"class '%0' will be imported as an opaque placeholder class and may be "

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,23 @@ void swift::conformToCxxSequenceIfNeeded(
565565
}
566566
}
567567

568+
static bool isStdSetType(const clang::CXXRecordDecl *clangDecl) {
569+
return isStdDecl(clangDecl, {"set", "unordered_set", "multiset"});
570+
}
571+
572+
bool swift::isUnsafeStdMethod(const clang::CXXMethodDecl *methodDecl) {
573+
auto parentDecl =
574+
dyn_cast<clang::CXXRecordDecl>(methodDecl->getDeclContext());
575+
if (!parentDecl)
576+
return false;
577+
if (!isStdSetType(parentDecl))
578+
return false;
579+
if (methodDecl->getDeclName().isIdentifier() &&
580+
methodDecl->getName() == "insert")
581+
return true;
582+
return false;
583+
}
584+
568585
void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
569586
NominalTypeDecl *decl,
570587
const clang::CXXRecordDecl *clangDecl) {
@@ -576,7 +593,7 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
576593

577594
// Only auto-conform types from the C++ standard library. Custom user types
578595
// might have a similar interface but different semantics.
579-
if (!isStdDecl(clangDecl, {"set", "unordered_set", "multiset"}))
596+
if (!isStdSetType(clangDecl))
580597
return;
581598

582599
auto valueType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(

lib/ClangImporter/ClangDerivedConformances.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace swift {
2020

2121
bool isIterator(const clang::CXXRecordDecl *clangDecl);
2222

23+
bool isUnsafeStdMethod(const clang::CXXMethodDecl *methodDecl);
24+
2325
/// If the decl is a C++ input iterator, synthesize a conformance to the
2426
/// UnsafeCxxInputIterator protocol, which is defined in the Cxx module.
2527
void conformToCxxIteratorIfNeeded(ClangImporter::Implementation &impl,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5711,7 +5711,13 @@ importName(const clang::NamedDecl *D,
57115711

57125712
Type ClangImporter::importFunctionReturnType(
57135713
const clang::FunctionDecl *clangDecl, DeclContext *dc) {
5714-
if (auto imported = Impl.importFunctionReturnType(clangDecl, dc).getType())
5714+
bool isInSystemModule =
5715+
cast<ClangModuleUnit>(dc->getModuleScopeContext())->isSystemModule();
5716+
bool allowNSUIntegerAsInt =
5717+
Impl.shouldAllowNSUIntegerAsInt(isInSystemModule, clangDecl);
5718+
if (auto imported =
5719+
Impl.importFunctionReturnType(dc, clangDecl, allowNSUIntegerAsInt)
5720+
.getType())
57155721
return imported;
57165722
return dc->getASTContext().getNeverType();
57175723
}
@@ -6794,6 +6800,11 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
67946800
method->getReturnType()->isReferenceType())
67956801
return false;
67966802

6803+
// Check if it's one of the known unsafe methods we currently
6804+
// mark as safe by default.
6805+
if (isUnsafeStdMethod(method))
6806+
return false;
6807+
67976808
// Try to figure out the semantics of the return type. If it's a
67986809
// pointer/iterator, it's unsafe.
67996810
if (auto returnType = dyn_cast<clang::RecordType>(

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,16 +3162,13 @@ namespace {
31623162

31633163
if (auto recordType = dyn_cast<clang::RecordType>(
31643164
decl->getReturnType().getCanonicalType())) {
3165-
if (recordHasReferenceSemantics(recordType->getDecl())) {
3166-
Impl.addImportDiagnostic(
3167-
decl,
3168-
Diagnostic(diag::reference_passed_by_value,
3169-
Impl.SwiftContext.AllocateCopy(
3170-
recordType->getDecl()->getNameAsString()),
3171-
"the return"),
3172-
decl->getLocation());
3173-
return true;
3174-
}
3165+
Impl.addImportDiagnostic(
3166+
decl, Diagnostic(diag::reference_passed_by_value,
3167+
Impl.SwiftContext.AllocateCopy(
3168+
recordType->getDecl()->getNameAsString()),
3169+
"the return"),
3170+
decl->getLocation());
3171+
return recordHasReferenceSemantics(recordType->getDecl());
31753172
}
31763173

31773174
return false;
@@ -3557,14 +3554,6 @@ namespace {
35573554
func->setAccess(AccessLevel::Public);
35583555
}
35593556

3560-
if (!isa<clang::CXXConstructorDecl>(decl) && !importedType) {
3561-
if (!Impl.importFunctionReturnType(decl, result->getDeclContext())) {
3562-
Impl.addImportDiagnostic(
3563-
decl, Diagnostic(diag::unsupported_return_type, decl),
3564-
decl->getSourceRange().getBegin());
3565-
return nullptr;
3566-
}
3567-
}
35683557
result->setIsObjC(false);
35693558
result->setIsDynamic(false);
35703559

lib/ClangImporter/ImportType.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,15 +2075,6 @@ applyImportTypeAttrs(ImportTypeAttrs attrs, Type type,
20752075
return type;
20762076
}
20772077

2078-
ImportedType ClangImporter::Implementation::importFunctionReturnType(
2079-
const clang::FunctionDecl *clangDecl, DeclContext *dc) {
2080-
bool isInSystemModule =
2081-
cast<ClangModuleUnit>(dc->getModuleScopeContext())->isSystemModule();
2082-
bool allowNSUIntegerAsInt =
2083-
shouldAllowNSUIntegerAsInt(isInSystemModule, clangDecl);
2084-
return importFunctionReturnType(dc, clangDecl, allowNSUIntegerAsInt);
2085-
}
2086-
20872078
ImportedType ClangImporter::Implementation::importFunctionReturnType(
20882079
DeclContext *dc, const clang::FunctionDecl *clangDecl,
20892080
bool allowNSUIntegerAsInt) {

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13721372
const clang::FunctionDecl *clangDecl,
13731373
bool allowNSUIntegerAsInt);
13741374

1375-
ImportedType importFunctionReturnType(const clang::FunctionDecl *clangDecl,
1376-
DeclContext *dc);
1377-
13781375
/// Import the parameter list for a function
13791376
///
13801377
/// \param clangDecl The underlying declaration, if any; should only be

test/Interop/Cxx/class/returns-unavailable-class.swift

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)