Skip to content

Commit 82c076c

Browse files
committed
[cxx-interop] C++ reference types are not AnyObjects
C++ foreign reference types have custom reference counting mechanisms, so they cannot conform to `AnyObject`. Currently Swift's type system treats C++ FRTs as `AnyObject`s on non-Darwin platforms, which is incorrect. This change makes sure the behavior is consistent with Darwin platform, i.e. a cast of C++ FRT to `AnyObject` is rejected by the typechecker. rdar://136664617
1 parent 5925df7 commit 82c076c

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7580,6 +7580,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
75807580
// Class and protocol metatypes are interoperable with certain Objective-C
75817581
// runtime classes, but only when ObjC interop is enabled.
75827582

7583+
// Foreign reference types do *not* conform to AnyObject.
7584+
if (type1->isForeignReferenceType() && type2->isAnyObject())
7585+
return getTypeMatchFailure(locator);
7586+
75837587
if (getASTContext().LangOpts.EnableObjCInterop) {
75847588
// These conversions are between concrete types that don't need further
75857589
// resolution, so we can consider them immediately solved.
@@ -7589,10 +7593,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
75897593
type1, type2, locator);
75907594
return getTypeMatchSuccess();
75917595
};
7592-
7593-
// Foreign reference types do *not* conform to AnyObject.
7594-
if (type1->isForeignReferenceType() && type2->isAnyObject())
7595-
return getTypeMatchFailure(locator);
75967596

75977597
if (auto meta1 = type1->getAs<MetatypeType>()) {
75987598
if (meta1->getInstanceType()->mayHaveSuperclass()

test/Interop/Cxx/foreign-reference/not-any-object.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22
// RUN: split-file %s %t
33
// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unknown -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop
44

5-
// REQUIRES: objc_interop
6-
75
//--- Inputs/module.modulemap
86
module Test {
97
header "test.h"
108
requires cplusplus
119
}
1210

1311
//--- Inputs/test.h
14-
#include <stdlib.h>
15-
16-
inline void* operator new(unsigned long, void* p) { return p; }
17-
1812
struct __attribute__((swift_attr("import_reference")))
1913
__attribute__((swift_attr("retain:immortal")))
2014
__attribute__((swift_attr("release:immortal"))) Empty {
21-
static Empty *create() { return new (malloc(sizeof(Empty))) Empty(); }
15+
static Empty *create() { return new Empty(); }
2216
};
2317

2418
//--- test.swift

0 commit comments

Comments
 (0)