Skip to content

[cxx-interop] C++ reference types are not AnyObjects #74185

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
Sep 26, 2024
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
8 changes: 4 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7580,6 +7580,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// Class and protocol metatypes are interoperable with certain Objective-C
// runtime classes, but only when ObjC interop is enabled.

// Foreign reference types do *not* conform to AnyObject.
if (type1->isForeignReferenceType() && type2->isAnyObject())
return getTypeMatchFailure(locator);

if (getASTContext().LangOpts.EnableObjCInterop) {
// These conversions are between concrete types that don't need further
// resolution, so we can consider them immediately solved.
Expand All @@ -7589,10 +7593,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
type1, type2, locator);
return getTypeMatchSuccess();
};

// Foreign reference types do *not* conform to AnyObject.
if (type1->isForeignReferenceType() && type2->isAnyObject())
return getTypeMatchFailure(locator);

if (auto meta1 = type1->getAs<MetatypeType>()) {
if (meta1->getInstanceType()->mayHaveSuperclass()
Expand Down
8 changes: 1 addition & 7 deletions test/Interop/Cxx/foreign-reference/not-any-object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
// RUN: split-file %s %t
// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unknown -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop

// REQUIRES: objc_interop

//--- Inputs/module.modulemap
module Test {
header "test.h"
requires cplusplus
}

//--- Inputs/test.h
#include <stdlib.h>

inline void* operator new(unsigned long, void* p) { return p; }

struct __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:immortal")))
__attribute__((swift_attr("release:immortal"))) Empty {
static Empty *create() { return new (malloc(sizeof(Empty))) Empty(); }
static Empty *create() { return new Empty(); }
};

//--- test.swift
Expand Down