Skip to content

[5.10][cxx-interop] disallow use of non-trivial C++ types in @objc declarat… #69343

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
Oct 24, 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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -6025,6 +6025,8 @@ NOTE(not_objc_error_protocol_composition,none,
"in Objective-C", ())
NOTE(not_objc_empty_tuple,none,
"empty tuple type cannot be represented in Objective-C", ())
NOTE(not_objc_non_trivial_cxx_class,none,
"non-trivial C++ classes cannot be represented in Objective-C", ())
NOTE(not_objc_tuple,none,
"tuples cannot be represented in Objective-C", ())
NOTE(not_objc_swift_class,none,
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3096,6 +3096,17 @@ getForeignRepresentable(Type type, ForeignLanguage language,

case ForeignLanguage::ObjectiveC:
if (isa<StructDecl>(nominal) || isa<EnumDecl>(nominal)) {
// Non-trivial C++ classes and structures are not
// supported by @objc attribute, even though they can
// be represented in Objective-C++.
if (auto *cxxRec = dyn_cast_or_null<clang::CXXRecordDecl>(
nominal->getClangDecl())) {
if (cxxRec->hasNonTrivialCopyConstructor() ||
cxxRec->hasNonTrivialMoveConstructor() ||
cxxRec->hasNonTrivialDestructor())
return failure();
}

// Optional structs are not representable in (Objective-)C if they
// originally came from C, whether or not they are bridged, unless they
// came from swift_newtype. If they are defined in Swift, they are only
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ static void diagnoseTypeNotRepresentableInObjC(const DeclContext *DC,

// Special diagnostic for structs.
if (auto *SD = T->getStructOrBoundGenericStruct()) {
if (isa_and_nonnull<clang::CXXRecordDecl>(SD->getClangDecl())) {
// This can be a non-trivial C++ record.
diags.diagnose(TypeRange.Start, diag::not_objc_non_trivial_cxx_class)
.highlight(TypeRange)
.limitBehavior(behavior);
return;
}
diags.diagnose(TypeRange.Start, diag::not_objc_swift_struct)
.highlight(TypeRange)
.limitBehavior(behavior);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -typecheck -I %t/Inputs -cxx-interoperability-mode=default -verify %t/test.swift

// REQUIRES: objc_interop

//--- Inputs/header.h

class Trivial {
public:
int x;
};

class NonTrivial {
public:
NonTrivial(const NonTrivial &other) : x(other.x) {}
~NonTrivial() { }

private:
int x;
};

struct NonTrivialDestrOnly {
~NonTrivialDestrOnly() { }

private:
int x;
};

//--- Inputs/module.modulemap

module NonTrivial {
header "header.h"
export *
}

//--- test.swift

import Foundation
import NonTrivial

@objc
class ObjCObject: NSObject {
@objc var prop: NonTrivial // expected-error {{property cannot be marked @objc because its type cannot be represented in Objective-C}}
// expected-note@-1 {{non-trivial C++ classes cannot be represented in Objective-C}}

@objc var trivProp: Trivial

override init() { fatalError() }

@objc func getNonTrivial() -> NonTrivialDestrOnly { // expected-error {{method cannot be marked @objc because its result type cannot be represented in Objective-C}}
// expected-note@-1 {{non-trivial C++ classes cannot be represented in Objective-C}}
fatalError()
}

@objc func getTrivial() -> Trivial {
return Trivial(x: 11)
}
}