Files
carbon-lang/toolchain/check/testdata/interop/cpp/enum/anonymous.carbon
T
Jon Ross-PerkinsandDana Jansens 5e3bb523f8 Add builtin functions for destroy, with special requirements in facet types (#6035)
This is in support of a goal of changing the blanket `destroy` impl to
use (roughly):

```
private fn CanAggregateDestroy() -> type = "type.can_aggregate_destroy";

// Handles aggregate type destruction.
impl forall [AggregateDestroyT:! CanAggregateDestroy()] AggregateDestroyT as Destroy {
  fn Op[addr self: Self*]() = "type.aggregate_destroy";
}
```

That isn't done here because there's still other issues that migrating
raises. What this *does* do is add the builtin functions, and in
particular, support to `FacetTypeInfo` to make `CanAggregateDestroy`
work.

The "special requirement" approach in `FacetTypeInfo` allows us to
support restricting a blanket impl under the current approach of impls.
Maybe we'll find a cleaner approach that can work in the future, but
this fits into the current model by propagating similar to other
requirements. I'm using an enum mask because we have a number of similar
things to add (e.g. copy, move) but I'm not sure we need a full vector.

A few alternatives considered were:

- Supporting syntax more like `where .Self impls
TypeCanAggregateDestroy(.Self, SupportedInterface,
UnsupportedInterface)`. I think it'd be a little cleaner, but requires
better compile-time evaluation in order to assess the type of the call.
Right now it's expected to be a `FacetType` too early to make this work,
and I was concerned about pouring too much more time down this route.
- Providing an actual interface, in particular doing name lookup back
into `Core.` for an interface. This would've added name lookup overhead,
and the question of whether an `impl` exists.
- Generating an interface. This avoids the name lookup, but would still
raise the question of whether an `impl` should also be generated. Work
I've previously done generating interfaces for class destruction also
feels complex to both write and understand (an unfortunate issue).
- Still modeling as an `ImplsConstraint`, for example by defining a
special `InterfaceId::CanAggregateDestroy = -2` similar to what we do on
other ids. I was hesitant because of how this expands the number of
modes of `InterfaceId`, and things for consuming code to watch out for,
for what feels like a relatively niche set of use-cases that are only
interface-like.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-09-15 17:03:43 +00:00

160 lines
9.3 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/enum/anonymous.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/enum/anonymous.carbon
// TODO: Tests marked as `fail_todo_5891_` to fixed as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
// --- anon.h
enum { a, b, c };
void F(decltype(a));
class C {
public:
enum { d, e, f };
void F(decltype(d));
};
// --- fail_todo_5891_copy_enum.carbon
library "[[@TEST_NAME]]";
import Cpp library "anon.h";
//@dump-sem-ir-begin
fn G() {
Cpp.F(Cpp.b);
// CHECK:STDERR: fail_todo_5891_copy_enum.carbon:[[@LINE+5]]:3: error: missing object argument in method call [MissingObjectInMethodCall]
// CHECK:STDERR: Cpp.C.C().F(Cpp.C.e);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_5891_copy_enum.carbon: note: calling function declared here [InCallToFunction]
// CHECK:STDERR:
Cpp.C.C().F(Cpp.C.e);
}
//@dump-sem-ir-end
// CHECK:STDOUT: --- fail_todo_5891_copy_enum.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
// CHECK:STDOUT: %.4f0: type = class_type @.1 [concrete]
// CHECK:STDOUT: %.557: type = cpp_overload_set_type @C.C [concrete]
// CHECK:STDOUT: %empty_struct.56a: %.557 = struct_value () [concrete]
// CHECK:STDOUT: %int_1.81a: %.4f0 = int_value 1 [concrete]
// CHECK:STDOUT: %ptr.793: type = ptr_type %.4f0 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.type.eda1ac.1: type = fn_type @F__carbon_thunk.1 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.0cd6a8.1: %F__carbon_thunk.type.eda1ac.1 = struct_value () [concrete]
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: %.d40: type = cpp_overload_set_type @C__carbon_thunk [concrete]
// CHECK:STDOUT: %empty_struct.e73: %.d40 = struct_value () [concrete]
// CHECK:STDOUT: %ptr.d9e: type = ptr_type %C [concrete]
// CHECK:STDOUT: %C__carbon_thunk.type: type = fn_type @C__carbon_thunk [concrete]
// CHECK:STDOUT: %C__carbon_thunk: %C__carbon_thunk.type = struct_value () [concrete]
// CHECK:STDOUT: %.bb7: type = class_type @.2 [concrete]
// CHECK:STDOUT: %.9a3: type = cpp_overload_set_type @C.F [concrete]
// CHECK:STDOUT: %empty_struct.acc: %.9a3 = struct_value () [concrete]
// CHECK:STDOUT: %int_1.1d6: %.bb7 = int_value 1 [concrete]
// CHECK:STDOUT: %ptr.73d: type = ptr_type %.bb7 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.type.eda1ac.2: type = fn_type @F__carbon_thunk.2 [concrete]
// CHECK:STDOUT: %F__carbon_thunk.0cd6a8.2: %F__carbon_thunk.type.eda1ac.2 = struct_value () [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.39a: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%.bb7) [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bd3: %T.as.Destroy.impl.Op.type.39a = struct_value () [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.37e: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%C) [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.a28: %T.as.Destroy.impl.Op.type.37e = struct_value () [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.088: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%.4f0) [concrete]
// CHECK:STDOUT: %T.as.Destroy.impl.Op.dda: %T.as.Destroy.impl.Op.type.088 = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
// CHECK:STDOUT: .F = %.4a9
// CHECK:STDOUT: .b = %int_1.81a
// CHECK:STDOUT: .C = %C.decl
// CHECK:STDOUT: import Cpp//...
// CHECK:STDOUT: }
// CHECK:STDOUT: %.4a9: %.557 = cpp_overload_set_value @C.C [concrete = constants.%empty_struct.56a]
// CHECK:STDOUT: %int_1.81a: %.4f0 = int_value 1 [concrete = constants.%int_1.81a]
// CHECK:STDOUT: %F__carbon_thunk.decl.e1b8ec.1: %F__carbon_thunk.type.eda1ac.1 = fn_decl @F__carbon_thunk.1 [concrete = constants.%F__carbon_thunk.0cd6a8.1] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
// CHECK:STDOUT: %.40b: %.d40 = cpp_overload_set_value @C__carbon_thunk [concrete = constants.%empty_struct.e73]
// CHECK:STDOUT: %C__carbon_thunk.decl: %C__carbon_thunk.type = fn_decl @C__carbon_thunk [concrete = constants.%C__carbon_thunk] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.951: %.9a3 = cpp_overload_set_value @C.F [concrete = constants.%empty_struct.acc]
// CHECK:STDOUT: %int_1.1d6: %.bb7 = int_value 1 [concrete = constants.%int_1.1d6]
// CHECK:STDOUT: %F__carbon_thunk.decl.e1b8ec.2: %F__carbon_thunk.type.eda1ac.2 = fn_decl @F__carbon_thunk.2 [concrete = constants.%F__carbon_thunk.0cd6a8.2] {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {} {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @G() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %Cpp.ref.loc8_3: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %F.ref.loc8: %.557 = name_ref F, imports.%.4a9 [concrete = constants.%empty_struct.56a]
// CHECK:STDOUT: %Cpp.ref.loc8_9: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %b.ref: %.4f0 = name_ref b, imports.%int_1.81a [concrete = constants.%int_1.81a]
// CHECK:STDOUT: %.loc8_12.1: ref %.4f0 = temporary_storage
// CHECK:STDOUT: %.loc8_12.2: ref %.4f0 = temporary %.loc8_12.1, %b.ref
// CHECK:STDOUT: %addr.loc8_14: %ptr.793 = addr_of %.loc8_12.2
// CHECK:STDOUT: %F__carbon_thunk.call.loc8: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.1(%addr.loc8_14)
// CHECK:STDOUT: %Cpp.ref.loc15_3: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %C.ref.loc15_6: type = name_ref C, imports.%C.decl [concrete = constants.%C]
// CHECK:STDOUT: %C.ref.loc15_8: %.d40 = name_ref C, imports.%.40b [concrete = constants.%empty_struct.e73]
// CHECK:STDOUT: %.loc15_11.1: ref %C = temporary_storage
// CHECK:STDOUT: %addr.loc15_11.1: %ptr.d9e = addr_of %.loc15_11.1
// CHECK:STDOUT: %C__carbon_thunk.call: init %empty_tuple.type = call imports.%C__carbon_thunk.decl(%addr.loc15_11.1)
// CHECK:STDOUT: %.loc15_11.2: init %C = in_place_init %C__carbon_thunk.call, %.loc15_11.1
// CHECK:STDOUT: %.loc15_11.3: ref %C = temporary %.loc15_11.1, %.loc15_11.2
// CHECK:STDOUT: %F.ref.loc15: %.9a3 = name_ref F, imports.%.951 [concrete = constants.%empty_struct.acc]
// CHECK:STDOUT: %Cpp.ref.loc15_15: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %C.ref.loc15_18: type = name_ref C, imports.%C.decl [concrete = constants.%C]
// CHECK:STDOUT: %e.ref: %.bb7 = name_ref e, imports.%int_1.1d6 [concrete = constants.%int_1.1d6]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %.loc15_20.1: ref %.bb7 = temporary_storage
// CHECK:STDOUT: %.loc15_20.2: ref %.bb7 = temporary %.loc15_20.1, %e.ref
// CHECK:STDOUT: %addr.loc15_22: %ptr.73d = addr_of %.loc15_20.2
// CHECK:STDOUT: %F__carbon_thunk.call.loc15: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.2(<error>, %addr.loc15_22)
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc15_20: <bound method> = bound_method %.loc15_20.2, constants.%T.as.Destroy.impl.Op.bd3
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %bound_method.loc15_20: <bound method> = bound_method %.loc15_20.2, %T.as.Destroy.impl.Op.specific_fn.1
// CHECK:STDOUT: %addr.loc15_20: %ptr.73d = addr_of %.loc15_20.2
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc15_20: init %empty_tuple.type = call %bound_method.loc15_20(%addr.loc15_20)
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc15_11: <bound method> = bound_method %.loc15_11.3, constants.%T.as.Destroy.impl.Op.a28
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %bound_method.loc15_11: <bound method> = bound_method %.loc15_11.3, %T.as.Destroy.impl.Op.specific_fn.2
// CHECK:STDOUT: %addr.loc15_11.2: %ptr.d9e = addr_of %.loc15_11.3
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc15_11: init %empty_tuple.type = call %bound_method.loc15_11(%addr.loc15_11.2)
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc8: <bound method> = bound_method %.loc8_12.2, constants.%T.as.Destroy.impl.Op.dda
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %.loc8_12.2, %T.as.Destroy.impl.Op.specific_fn.3
// CHECK:STDOUT: %addr.loc8_12: %ptr.793 = addr_of %.loc8_12.2
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc8: init %empty_tuple.type = call %bound_method.loc8(%addr.loc8_12)
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: