Skip to content

[cxx-interop] Mark C++ functions with unavailable return type as unav… #67235

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
Jul 12, 2023
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
5 changes: 3 additions & 2 deletions include/swift/AST/ClangModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ class ClangModuleLoader : public ModuleLoader {

virtual bool isUnsafeCXXMethod(const FuncDecl *func) = 0;

virtual Type importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) = 0;
virtual llvm::Optional<Type>
importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) = 0;

virtual Type importVarDeclType(const clang::VarDecl *clangDecl,
VarDecl *swiftDecl,
Expand Down
5 changes: 3 additions & 2 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ class ClangImporter final : public ClangModuleLoader {
const clang::NamedDecl *D,
clang::DeclarationName givenName = clang::DeclarationName()) override;

Type importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) override;
llvm::Optional<Type>
importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) override;

Type importVarDeclType(const clang::VarDecl *clangDecl,
VarDecl *swiftDecl,
Expand Down
7 changes: 4 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5648,8 +5648,9 @@ importName(const clang::NamedDecl *D,
getDeclName();
}

Type ClangImporter::importFunctionReturnType(
const clang::FunctionDecl *clangDecl, DeclContext *dc) {
llvm::Optional<Type>
ClangImporter::importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) {
bool isInSystemModule =
cast<ClangModuleUnit>(dc->getModuleScopeContext())->isSystemModule();
bool allowNSUIntegerAsInt =
Expand All @@ -5658,7 +5659,7 @@ Type ClangImporter::importFunctionReturnType(
Impl.importFunctionReturnType(dc, clangDecl, allowNSUIntegerAsInt)
.getType())
return imported;
return dc->getASTContext().getNeverType();
return {};
}

Type ClangImporter::importVarDeclType(
Expand Down
15 changes: 14 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2129,8 +2129,21 @@ ResultTypeRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
if (!resultTyRepr && decl->getClangDecl() &&
isa<clang::FunctionDecl>(decl->getClangDecl())) {
auto clangFn = cast<clang::FunctionDecl>(decl->getClangDecl());
return ctx.getClangModuleLoader()->importFunctionReturnType(
auto returnType = ctx.getClangModuleLoader()->importFunctionReturnType(
clangFn, decl->getDeclContext());
if (returnType)
return *returnType;
// Mark the imported Swift function as unavailable.
// That will ensure that the function will not be
// usable from Swift, even though it is imported.
if (!decl->getAttrs().isUnavailable(ctx)) {
StringRef unavailabilityMsgRef = "return type is unavailable in Swift";
auto ua =
AvailableAttr::createPlatformAgnostic(ctx, unavailabilityMsgRef);
decl->getAttrs().add(ua);
}

return ctx.getNeverType();
}

// Nothing to do if there's no result type.
Expand Down
76 changes: 76 additions & 0 deletions test/Interop/Cxx/class/returns-unavailable-class.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %target-swift-ide-test -print-module -module-to-print=CxxModule -I %t/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

// RUN: %target-swift-frontend -typecheck -verify -I %t/Inputs -enable-experimental-cxx-interop %t/test.swift

//--- Inputs/module.modulemap
module CxxTypes {
header "types.h"
requires cplusplus
}

module CxxModule {
header "header.h"
requires cplusplus
}

//--- Inputs/types.h

template<class T>
class TemplateInTypesModule {
public:
T x, y;
};

//--- Inputs/header.h

#pragma clang module import CxxTypes

class Struct {
public:
int x, y;

TemplateInTypesModule<int> returnsClassInTypesModules() const;

void takesClassInTypesModules(TemplateInTypesModule<int>) const;
void takesClassInTypesModulesRef(const TemplateInTypesModule<int> &) const;
};

// CHECK: struct Struct {
// CHECK-NEXT: init()
// CHECK-NEXT: init(x: Int32, y: Int32)
// CHECK-NEXT: func returnsClassInTypesModules() -> Never
// CHECK-NEXT: var x: Int32
// CHECK-NEXT: var y: Int32
// CHECK-NEXT: }

TemplateInTypesModule<int> funcWithClassInTypesModules();
void funcWithClassInTypesModulesParam(TemplateInTypesModule<int>);
void funcWithClassInTypesModulesParamRef(const TemplateInTypesModule<int> &);

class StructPrivateDestructor {
public:
StructPrivateDestructor();
private:
~StructPrivateDestructor();
};

StructPrivateDestructor returnsStructPrivateDestructor();

//--- test.swift

import CxxModule

func test() {
funcWithClassInTypesModules() // expected-error {{'funcWithClassInTypesModules()' is unavailable: return type is unavailable in Swift}}
Struct().returnsClassInTypesModules() // expected-error {{'returnsClassInTypesModules()' is unavailable: return type is unavailable in Swift}}
}

func test2() {
funcWithClassInTypesModules() // expected-error {{'funcWithClassInTypesModules()' is unavailable: return type is unavailable in Swift}}
}

func testPrivateDesType() {
returnsStructPrivateDestructor() // expected-error {{'returnsStructPrivateDestructor()' is unavailable: return type is unavailable in Swift}}
}