mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 07:25:51 +01:00
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>
140 lines
7.3 KiB
Plaintext
140 lines
7.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/int.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/array/import.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/import.carbon
|
|
|
|
// --- library.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() -> array(i32, 42);
|
|
|
|
// --- user.carbon
|
|
|
|
import library "library";
|
|
|
|
fn G(n: i32) -> i32 {
|
|
//@dump-sem-ir-begin
|
|
return F()[n];
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
// --- fail_todo_symbolic_decl.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {
|
|
let val:! array(i32, 1);
|
|
}
|
|
|
|
class C {}
|
|
|
|
// CHECK:STDERR: fail_todo_symbolic_decl.carbon:[[@LINE+4]]:26: error: expression is runtime; expected constant [EvalRequiresConstantValue]
|
|
// CHECK:STDERR: impl C as I where .val = (1,) {}
|
|
// CHECK:STDERR: ^~~~
|
|
// CHECK:STDERR:
|
|
impl C as I where .val = (1,) {}
|
|
|
|
// --- fail_todo_import_symbolic_decl.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
import library "symbolic_decl";
|
|
|
|
fn F() -> array(i32, 1) {
|
|
//@dump-sem-ir-begin
|
|
// CHECK:STDERR: fail_todo_import_symbolic_decl.carbon:[[@LINE+4]]:11: error: cannot convert type `C` into type implementing `I` [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: return (C as I).val;
|
|
// CHECK:STDERR: ^~~~~~
|
|
// CHECK:STDERR:
|
|
return (C as I).val;
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
// CHECK:STDOUT: --- user.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
|
|
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
|
|
// CHECK:STDOUT: %N: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic]
|
|
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
|
|
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
|
|
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: %int_42: Core.IntLiteral = int_value 42 [concrete]
|
|
// CHECK:STDOUT: %array_type: type = array_type %int_42, %i32 [concrete]
|
|
// CHECK:STDOUT: %ptr.830: type = ptr_type %array_type [concrete]
|
|
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
|
|
// CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.type.afd: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.6cd: %Int.as.Copy.impl.Op.type.afd = struct_value () [symbolic]
|
|
// CHECK:STDOUT: %Copy.impl_witness.a32: <witness> = impl_witness imports.%Copy.impl_witness_table.1ed, @Int.as.Copy.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.type.276: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.f59: %Int.as.Copy.impl.Op.type.276 = struct_value () [concrete]
|
|
// CHECK:STDOUT: %Copy.facet.c49: %Copy.type = facet_value %i32, (%Copy.impl_witness.a32) [concrete]
|
|
// CHECK:STDOUT: %.7fa: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet.c49 [concrete]
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.f59, @Int.as.Copy.impl.Op(%int_32) [concrete]
|
|
// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.e2a: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%array_type) [concrete]
|
|
// CHECK:STDOUT: %T.as.Destroy.impl.Op.95f: %T.as.Destroy.impl.Op.type.e2a = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Main.F: %F.type = import_ref Main//library, F, loaded [concrete = constants.%F]
|
|
// CHECK:STDOUT: %Core.import_ref.d0f6: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.afd) = import_ref Core//prelude/parts/int, loc17_31, loaded [symbolic = @Int.as.Copy.impl.%Int.as.Copy.impl.Op (constants.%Int.as.Copy.impl.Op.6cd)]
|
|
// CHECK:STDOUT: %Copy.impl_witness_table.1ed = impl_witness_table (%Core.import_ref.d0f6), @Int.as.Copy.impl [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @G(%n.param: %i32) -> %i32 {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, imports.%Main.F [concrete = constants.%F]
|
|
// CHECK:STDOUT: %.loc6_12.1: ref %array_type = temporary_storage
|
|
// CHECK:STDOUT: %F.call: init %array_type = call %F.ref() to %.loc6_12.1
|
|
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
|
|
// CHECK:STDOUT: %.loc6_12.2: ref %array_type = temporary %.loc6_12.1, %F.call
|
|
// CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
|
|
// CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
|
|
// CHECK:STDOUT: %.loc6_15.1: ref %i32 = array_index %.loc6_12.2, %n.ref
|
|
// CHECK:STDOUT: %.loc6_15.2: %i32 = bind_value %.loc6_15.1
|
|
// CHECK:STDOUT: %impl.elem0: %.7fa = impl_witness_access constants.%Copy.impl_witness.a32, element0 [concrete = constants.%Int.as.Copy.impl.Op.f59]
|
|
// CHECK:STDOUT: %bound_method.loc6_15.1: <bound method> = bound_method %.loc6_15.2, %impl.elem0
|
|
// CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn]
|
|
// CHECK:STDOUT: %bound_method.loc6_15.2: <bound method> = bound_method %.loc6_15.2, %specific_fn
|
|
// CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc6_15.2(%.loc6_15.2)
|
|
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc6_12.2, constants.%T.as.Destroy.impl.Op.95f
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %bound_method.loc6_12: <bound method> = bound_method %.loc6_12.2, %T.as.Destroy.impl.Op.specific_fn
|
|
// CHECK:STDOUT: %addr: %ptr.830 = addr_of %.loc6_12.2
|
|
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc6_12(%addr)
|
|
// CHECK:STDOUT: return %Int.as.Copy.impl.Op.call to %return
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_todo_import_symbolic_decl.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
|
|
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
|
|
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
|
|
// CHECK:STDOUT: %array_type: type = array_type %int_1, %i32 [concrete]
|
|
// CHECK:STDOUT: %C: type = class_type @C [concrete]
|
|
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Main.I: type = import_ref Main//symbolic_decl, I, loaded [concrete = constants.%I.type]
|
|
// CHECK:STDOUT: %Main.C: type = import_ref Main//symbolic_decl, C, loaded [concrete = constants.%C]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @F() -> %return.param: %array_type {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %C.ref: type = name_ref C, imports.%Main.C [concrete = constants.%C]
|
|
// CHECK:STDOUT: %I.ref: type = name_ref I, imports.%Main.I [concrete = constants.%I.type]
|
|
// CHECK:STDOUT: %val.ref: <error> = name_ref val, <error> [concrete = <error>]
|
|
// CHECK:STDOUT: return <error> to %return
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|