Files
swift-mirror/test/Interop/Cxx/foreign-reference/not-any-object.swift
Egor Zhdan fd04cc3a4e [cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly
This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type.

Swift optimizes the amount of metadata emitted for `_ContiguousArrayStorage<Element>` by reusing `_ContiguousArrayStorage<AnyObject>` whenever possible (see `getContiguousArrayStorageType`). However, C++ foreign reference types are not `AnyObject`s, since they have custom retain/release operations.

This change disables the `_ContiguousArrayStorage` metadata optimization for C++ reference types, which makes sure that `swift_arrayDestroy` will call the correct release operation for elements of `[MyCxxRefType]`.

rdar://127154770
2024-05-15 19:20:44 +01:00

33 lines
984 B
Swift

// RUN: rm -rf %t
// 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(); }
};
//--- test.swift
import Test;
public func test(_ _: AnyObject) {}
// TODO: make this a better error.
test(Empty.create()) // expected-error {{type of expression is ambiguous without a type annotation}}
test([Empty.create()][0]) // expected-error {{argument type 'Any' expected to be an instance of a class or class-constrained type}}