Skip to content

Commit 89083ee

Browse files
authored
Merge pull request #64343 from hyp/explicit-objc-unavail
[interop][SwiftToCxx] explicitly tell the user that @objc decls can't…
2 parents 1dfd1ed + 9f86ff9 commit 89083ee

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,8 @@ ERROR(expose_inside_unexposed_decl,none,
16091609
ERROR(expose_invalid_name_pattern_init,none,
16101610
"invalid declaration name '%0' specified in an @_expose attribute; "
16111611
"exposed initializer name must start with 'init'", (StringRef))
1612+
ERROR(expose_unsupported_objc_decl_to_cxx,none,
1613+
"@objc %0 %1 can not yet be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
16121614
ERROR(expose_unsupported_async_decl_to_cxx,none,
16131615
"async %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
16141616
ERROR(expose_unsupported_actor_isolated_to_cxx,none,

include/swift/AST/SwiftNameTranslation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ getNameForCxx(const ValueDecl *VD,
6666
enum RepresentationKind { Representable, Unsupported };
6767

6868
enum RepresentationError {
69+
UnrepresentableObjC,
6970
UnrepresentableAsync,
7071
UnrepresentableIsolatedInActor,
7172
UnrepresentableRequiresClientEmission,

lib/AST/SwiftNameTranslation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
200200

201201
swift::cxx_translation::DeclRepresentation
202202
swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
203+
if (VD->isObjC())
204+
return {Unsupported, UnrepresentableObjC};
203205
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
204206
return {Unsupported, UnrepresentableIsolatedInActor};
205207
Optional<CanGenericSignature> genericSignature;
@@ -241,8 +243,6 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
241243
bool swift::cxx_translation::isVisibleToCxx(const ValueDecl *VD,
242244
AccessLevel minRequiredAccess,
243245
bool checkParent) {
244-
if (VD->isObjC())
245-
return false;
246246
// Do not expose anything from _Concurrency module yet.
247247
if (VD->getModuleContext()->ValueDecl::getName().getBaseIdentifier() ==
248248
VD->getASTContext().Id_Concurrency)

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,9 +2013,7 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
20132013

20142014
void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
20152015
auto *VD = cast<ValueDecl>(D);
2016-
// Expose cannot be mixed with '@objc'/'@_cdecl' declarations.
2017-
if (VD->isObjC())
2018-
diagnose(attr->getLocation(), diag::expose_only_non_other_attr, "@objc");
2016+
// Expose cannot be mixed with '@_cdecl' declarations.
20192017
if (VD->getAttrs().hasAttribute<CDeclAttr>())
20202018
diagnose(attr->getLocation(), diag::expose_only_non_other_attr, "@_cdecl");
20212019

@@ -2038,6 +2036,10 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
20382036
if (repr.isUnsupported()) {
20392037
using namespace cxx_translation;
20402038
switch (*repr.error) {
2039+
case UnrepresentableObjC:
2040+
diagnose(attr->getLocation(), diag::expose_unsupported_objc_decl_to_cxx,
2041+
VD->getDescriptiveKind(), VD);
2042+
break;
20412043
case UnrepresentableAsync:
20422044
diagnose(attr->getLocation(), diag::expose_unsupported_async_decl_to_cxx,
20432045
VD->getDescriptiveKind(), VD);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -verify -clang-header-expose-decls=has-expose-attr -disable-availability-checking -emit-clang-header-path %t/functions.h
3+
4+
// RUN: cat %s | grep -v _expose > %t/clean.swift
5+
// RUN: %target-swift-frontend %t/clean.swift -typecheck -module-name Functions -clang-header-expose-decls=all-public -disable-availability-checking -emit-clang-header-path %t/header.h
6+
// RUN: %FileCheck %s < %t/header.h
7+
8+
// REQUIRES: objc_interop
9+
10+
// CHECK-NOT: Unsupported
11+
// CHECK: supported
12+
13+
import Foundation
14+
15+
public func supported() {}
16+
17+
@objc
18+
@_expose(Cxx) // expected-error {{@objc class 'UnsupportedClass' can not yet be exposed to C++}}
19+
public class UnsupportedClass: NSObject {
20+
override public init() {
21+
x = 0
22+
}
23+
24+
let x: Int
25+
}

0 commit comments

Comments
 (0)