Files
carbon-lang/toolchain/check/testdata/const/import.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

117 lines
7.8 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/const/import.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/const/import.carbon
// --- implicit.carbon
library "[[@TEST_NAME]]";
class C {}
fn F() -> const C;
var a_ref: const C = F();
var a_ptr_ref: const C* = &a_ref;
// --- implicit.impl.carbon
impl library "[[@TEST_NAME]]";
// Take a reference to avoid unsupported copy logic. This still validates the
// `const` is handled.
//@dump-sem-ir-begin
var a: const C* = &a_ref;
var a_ptr: const C* = a_ptr_ref;
//@dump-sem-ir-end
// CHECK:STDOUT: --- implicit.impl.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
// CHECK:STDOUT: %T.8b3: type = bind_symbolic_name T, 0 [symbolic]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.type.222: type = fn_type @ptr.as.Copy.impl.Op, @ptr.as.Copy.impl(%T.8b3) [symbolic]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.3ef: %ptr.as.Copy.impl.Op.type.222 = struct_value () [symbolic]
// CHECK:STDOUT: %const.668: type = const_type %C [concrete]
// CHECK:STDOUT: %ptr.801: type = ptr_type %const.668 [concrete]
// CHECK:STDOUT: %pattern_type.c0d: type = pattern_type %ptr.801 [concrete]
// CHECK:STDOUT: %pattern_type.6af: type = pattern_type %const.668 [concrete]
// CHECK:STDOUT: %addr: %ptr.801 = addr_of imports.%a_ref.var [concrete]
// CHECK:STDOUT: %Copy.impl_witness.fc3: <witness> = impl_witness imports.%Copy.impl_witness_table.573, @ptr.as.Copy.impl(%const.668) [concrete]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.type.906: type = fn_type @ptr.as.Copy.impl.Op, @ptr.as.Copy.impl(%const.668) [concrete]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.ae9: %ptr.as.Copy.impl.Op.type.906 = struct_value () [concrete]
// CHECK:STDOUT: %Copy.facet.c03: %Copy.type = facet_value %ptr.801, (%Copy.impl_witness.fc3) [concrete]
// CHECK:STDOUT: %.26e: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet.c03 [concrete]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.bound: <bound method> = bound_method %addr, %ptr.as.Copy.impl.Op.ae9 [concrete]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %ptr.as.Copy.impl.Op.ae9, @ptr.as.Copy.impl.Op(%const.668) [concrete]
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %addr, %ptr.as.Copy.impl.Op.specific_fn [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Main.C: type = import_ref Main//implicit, C, loaded [concrete = constants.%C]
// CHECK:STDOUT: %Main.a_ref: ref %const.668 = import_ref Main//implicit, a_ref, loaded [concrete = %a_ref.var]
// CHECK:STDOUT: %Main.a_ptr_ref: ref %ptr.801 = import_ref Main//implicit, a_ptr_ref, loaded [concrete = %a_ptr_ref.var]
// CHECK:STDOUT: %Main.import_ref.790: @ptr.as.Copy.impl.%ptr.as.Copy.impl.Op.type (%ptr.as.Copy.impl.Op.type.222) = import_ref Main//implicit, inst195 [indirect], loaded [symbolic = @ptr.as.Copy.impl.%ptr.as.Copy.impl.Op (constants.%ptr.as.Copy.impl.Op.3ef)]
// CHECK:STDOUT: %Copy.impl_witness_table.573 = impl_witness_table (%Main.import_ref.790), @ptr.as.Copy.impl [concrete]
// CHECK:STDOUT: %a_ref.patt: %pattern_type.6af = binding_pattern a_ref [concrete]
// CHECK:STDOUT: %a_ref.var_patt: %pattern_type.6af = var_pattern %a_ref.patt [concrete]
// CHECK:STDOUT: %a_ref.var: ref %const.668 = var %a_ref.var_patt [concrete]
// CHECK:STDOUT: %a_ptr_ref.patt: %pattern_type.c0d = binding_pattern a_ptr_ref [concrete]
// CHECK:STDOUT: %a_ptr_ref.var_patt: %pattern_type.c0d = var_pattern %a_ptr_ref.patt [concrete]
// CHECK:STDOUT: %a_ptr_ref.var: ref %ptr.801 = var %a_ptr_ref.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a.patt: %pattern_type.c0d = binding_pattern a [concrete]
// CHECK:STDOUT: %a.var_patt: %pattern_type.c0d = var_pattern %a.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a.var: ref %ptr.801 = var %a.var_patt [concrete]
// CHECK:STDOUT: %.loc6: type = splice_block %ptr.loc6 [concrete = constants.%ptr.801] {
// CHECK:STDOUT: %C.ref.loc6: type = name_ref C, imports.%Main.C [concrete = constants.%C]
// CHECK:STDOUT: %const.loc6: type = const_type %C.ref.loc6 [concrete = constants.%const.668]
// CHECK:STDOUT: %ptr.loc6: type = ptr_type %const.loc6 [concrete = constants.%ptr.801]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a: ref %ptr.801 = bind_name a, %a.var [concrete = %a.var]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %a_ptr.patt: %pattern_type.c0d = binding_pattern a_ptr [concrete]
// CHECK:STDOUT: %a_ptr.var_patt: %pattern_type.c0d = var_pattern %a_ptr.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a_ptr.var: ref %ptr.801 = var %a_ptr.var_patt [concrete]
// CHECK:STDOUT: %.loc7: type = splice_block %ptr.loc7 [concrete = constants.%ptr.801] {
// CHECK:STDOUT: %C.ref.loc7: type = name_ref C, imports.%Main.C [concrete = constants.%C]
// CHECK:STDOUT: %const.loc7: type = const_type %C.ref.loc7 [concrete = constants.%const.668]
// CHECK:STDOUT: %ptr.loc7: type = ptr_type %const.loc7 [concrete = constants.%ptr.801]
// CHECK:STDOUT: }
// CHECK:STDOUT: %a_ptr: ref %ptr.801 = bind_name a_ptr, %a_ptr.var [concrete = %a_ptr.var]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %a_ref.ref: ref %const.668 = name_ref a_ref, imports.%Main.a_ref [concrete = imports.%a_ref.var]
// CHECK:STDOUT: %addr: %ptr.801 = addr_of %a_ref.ref [concrete = constants.%addr]
// CHECK:STDOUT: %impl.elem0.loc6: %.26e = impl_witness_access constants.%Copy.impl_witness.fc3, element0 [concrete = constants.%ptr.as.Copy.impl.Op.ae9]
// CHECK:STDOUT: %bound_method.loc6_19.1: <bound method> = bound_method %addr, %impl.elem0.loc6 [concrete = constants.%ptr.as.Copy.impl.Op.bound]
// CHECK:STDOUT: %specific_fn.loc6: <specific function> = specific_function %impl.elem0.loc6, @ptr.as.Copy.impl.Op(constants.%const.668) [concrete = constants.%ptr.as.Copy.impl.Op.specific_fn]
// CHECK:STDOUT: %bound_method.loc6_19.2: <bound method> = bound_method %addr, %specific_fn.loc6 [concrete = constants.%bound_method]
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.call.loc6: init %ptr.801 = call %bound_method.loc6_19.2(%addr) [concrete = constants.%addr]
// CHECK:STDOUT: assign file.%a.var, %ptr.as.Copy.impl.Op.call.loc6
// CHECK:STDOUT: %a_ptr_ref.ref: ref %ptr.801 = name_ref a_ptr_ref, imports.%Main.a_ptr_ref [concrete = imports.%a_ptr_ref.var]
// CHECK:STDOUT: %.loc7: %ptr.801 = bind_value %a_ptr_ref.ref
// CHECK:STDOUT: %impl.elem0.loc7: %.26e = impl_witness_access constants.%Copy.impl_witness.fc3, element0 [concrete = constants.%ptr.as.Copy.impl.Op.ae9]
// CHECK:STDOUT: %bound_method.loc7_23.1: <bound method> = bound_method %.loc7, %impl.elem0.loc7
// CHECK:STDOUT: %specific_fn.loc7: <specific function> = specific_function %impl.elem0.loc7, @ptr.as.Copy.impl.Op(constants.%const.668) [concrete = constants.%ptr.as.Copy.impl.Op.specific_fn]
// CHECK:STDOUT: %bound_method.loc7_23.2: <bound method> = bound_method %.loc7, %specific_fn.loc7
// CHECK:STDOUT: %ptr.as.Copy.impl.Op.call.loc7: init %ptr.801 = call %bound_method.loc7_23.2(%.loc7)
// CHECK:STDOUT: assign file.%a_ptr.var, %ptr.as.Copy.impl.Op.call.loc7
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: