mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +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>
187 lines
14 KiB
Plaintext
187 lines
14 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/full.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/array/bound_values.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/bound_values.carbon
|
|
|
|
// --- addition.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
var a:
|
|
//@dump-sem-ir-begin
|
|
array(i32, 1 + 2)
|
|
//@dump-sem-ir-end
|
|
= (1, 2, 3);
|
|
let b: array(i32, 3)* = &a;
|
|
|
|
// --- unsigned.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
var a:
|
|
//@dump-sem-ir-begin
|
|
array(i32, 3 as u32)
|
|
//@dump-sem-ir-end
|
|
= (1, 2, 3);
|
|
let b: array(i32, 3)* = &a;
|
|
|
|
// --- fail_negative.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_negative.carbon:[[@LINE+4]]:19: error: array bound of -1 is negative [ArrayBoundNegative]
|
|
// CHECK:STDERR: var a: array(i32, -1);
|
|
// CHECK:STDERR: ^~
|
|
// CHECK:STDERR:
|
|
var a: array(i32, -1);
|
|
|
|
// --- fail_overflow.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:19: error: array bound of 39999999999999999993 is too large [ArrayBoundTooLarge]
|
|
// CHECK:STDERR: var a: array(i32, 39999999999999999993);
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
var a: array(i32, 39999999999999999993);
|
|
|
|
// --- fail_invalid_type_with_overflow.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_invalid_type_with_overflow.carbon:[[@LINE+7]]:14: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
|
|
// CHECK:STDERR: var b: array(1, 39999999999999999993);
|
|
// CHECK:STDERR: ^
|
|
// CHECK:STDERR: fail_invalid_type_with_overflow.carbon:[[@LINE+4]]:14: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
|
|
// CHECK:STDERR: var b: array(1, 39999999999999999993);
|
|
// CHECK:STDERR: ^
|
|
// CHECK:STDERR:
|
|
var b: array(1, 39999999999999999993);
|
|
|
|
// CHECK:STDOUT: --- addition.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.5b8: Core.IntLiteral = int_value 1 [concrete]
|
|
// CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
|
|
// CHECK:STDOUT: %AddWith.type.a45: type = facet_type <@AddWith, @AddWith(Core.IntLiteral)> [concrete]
|
|
// CHECK:STDOUT: %AddWith.Op.type.0ee: type = fn_type @AddWith.Op, @AddWith(Core.IntLiteral) [concrete]
|
|
// CHECK:STDOUT: %AddWith.impl_witness: <witness> = impl_witness imports.%AddWith.impl_witness_table [concrete]
|
|
// CHECK:STDOUT: %AddWith.facet: %AddWith.type.a45 = facet_value Core.IntLiteral, (%AddWith.impl_witness) [concrete]
|
|
// CHECK:STDOUT: %.133: type = fn_type_with_self_type %AddWith.Op.type.0ee, %AddWith.facet [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.type: type = fn_type @Core.IntLiteral.as.AddWith.impl.Op [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op: %Core.IntLiteral.as.AddWith.impl.Op.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.bound: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.AddWith.impl.Op [concrete]
|
|
// CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
|
|
// CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Core.import_ref.abd = import_ref Core//prelude/operators/arithmetic, loc95_57, unloaded
|
|
// CHECK:STDOUT: %Core.import_ref.fdc: %Core.IntLiteral.as.AddWith.impl.Op.type = import_ref Core//prelude/operators/arithmetic, loc96_42, loaded [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op]
|
|
// CHECK:STDOUT: %AddWith.impl_witness_table = impl_witness_table (%Core.import_ref.abd, %Core.import_ref.fdc), @Core.IntLiteral.as.AddWith.impl [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: %.loc6_21: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
|
|
// 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: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
|
|
// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
|
|
// CHECK:STDOUT: %impl.elem1: %.133 = impl_witness_access constants.%AddWith.impl_witness, element1 [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op]
|
|
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem1 [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op.bound]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.call: init Core.IntLiteral = call %bound_method(%int_1, %int_2) [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %.loc6_18.1: Core.IntLiteral = value_of_initializer %Core.IntLiteral.as.AddWith.impl.Op.call [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %.loc6_18.2: Core.IntLiteral = converted %Core.IntLiteral.as.AddWith.impl.Op.call, %.loc6_18.1 [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.2, %i32.loc6 [concrete = constants.%array_type]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @__global_init() {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- unsigned.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_3.1ba: Core.IntLiteral = int_value 3 [concrete]
|
|
// CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
|
|
// CHECK:STDOUT: %As.type.45b: type = facet_type <@As, @As(%u32)> [concrete]
|
|
// CHECK:STDOUT: %As.Convert.type.b94: type = fn_type @As.Convert, @As(%u32) [concrete]
|
|
// CHECK:STDOUT: %To.c80: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.972: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To.c80) [symbolic]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.276: %Core.IntLiteral.as.As.impl.Convert.type.972 = struct_value () [symbolic]
|
|
// CHECK:STDOUT: %From: Core.IntLiteral = bind_symbolic_name From, 0 [symbolic]
|
|
// CHECK:STDOUT: %As.impl_witness.556: <witness> = impl_witness imports.%As.impl_witness_table.e4e, @Core.IntLiteral.as.As.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.847: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.51e: %Core.IntLiteral.as.As.impl.Convert.type.847 = struct_value () [concrete]
|
|
// CHECK:STDOUT: %As.facet: %As.type.45b = facet_value Core.IntLiteral, (%As.impl_witness.556) [concrete]
|
|
// CHECK:STDOUT: %.97b: type = fn_type_with_self_type %As.Convert.type.b94, %As.facet [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_3.1ba, %Core.IntLiteral.as.As.impl.Convert.51e [concrete]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.51e, @Core.IntLiteral.as.As.impl.Convert(%int_32) [concrete]
|
|
// CHECK:STDOUT: %bound_method.4fe: <bound method> = bound_method %int_3.1ba, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
|
|
// CHECK:STDOUT: %int_3.d14: %u32 = int_value 3 [concrete]
|
|
// CHECK:STDOUT: %ImplicitAs.type.7a9: type = facet_type <@ImplicitAs, @ImplicitAs(Core.IntLiteral)> [concrete]
|
|
// CHECK:STDOUT: %ImplicitAs.Convert.type.71e: type = fn_type @ImplicitAs.Convert, @ImplicitAs(Core.IntLiteral) [concrete]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.type.b7e: type = fn_type @UInt.as.ImplicitAs.impl.Convert, @UInt.as.ImplicitAs.impl(%From) [symbolic]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.138: %UInt.as.ImplicitAs.impl.Convert.type.b7e = struct_value () [symbolic]
|
|
// CHECK:STDOUT: %ImplicitAs.impl_witness.f6b: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.bd4, @UInt.as.ImplicitAs.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.type.af7: type = fn_type @UInt.as.ImplicitAs.impl.Convert, @UInt.as.ImplicitAs.impl(%int_32) [concrete]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.e1c: %UInt.as.ImplicitAs.impl.Convert.type.af7 = struct_value () [concrete]
|
|
// CHECK:STDOUT: %ImplicitAs.facet.701: %ImplicitAs.type.7a9 = facet_value %u32, (%ImplicitAs.impl_witness.f6b) [concrete]
|
|
// CHECK:STDOUT: %.a92: type = fn_type_with_self_type %ImplicitAs.Convert.type.71e, %ImplicitAs.facet.701 [concrete]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_3.d14, %UInt.as.ImplicitAs.impl.Convert.e1c [concrete]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %UInt.as.ImplicitAs.impl.Convert.e1c, @UInt.as.ImplicitAs.impl.Convert(%int_32) [concrete]
|
|
// CHECK:STDOUT: %bound_method.709: <bound method> = bound_method %int_3.d14, %UInt.as.ImplicitAs.impl.Convert.specific_fn [concrete]
|
|
// CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Core.import_ref.611: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.972) = import_ref Core//prelude/types/uint, loc37_40, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.276)]
|
|
// CHECK:STDOUT: %As.impl_witness_table.e4e = impl_witness_table (%Core.import_ref.611), @Core.IntLiteral.as.As.impl [concrete]
|
|
// CHECK:STDOUT: %Core.import_ref.4da: @UInt.as.ImplicitAs.impl.%UInt.as.ImplicitAs.impl.Convert.type (%UInt.as.ImplicitAs.impl.Convert.type.b7e) = import_ref Core//prelude/types/uint, loc32_44, loaded [symbolic = @UInt.as.ImplicitAs.impl.%UInt.as.ImplicitAs.impl.Convert (constants.%UInt.as.ImplicitAs.impl.Convert.138)]
|
|
// CHECK:STDOUT: %ImplicitAs.impl_witness_table.bd4 = impl_witness_table (%Core.import_ref.4da), @UInt.as.ImplicitAs.impl [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: %.loc6_24: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
|
|
// CHECK:STDOUT: %int_32.loc6_11: 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: %int_3.loc6: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %int_32.loc6_21: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
|
|
// CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(constants.%int_32) [concrete = constants.%u32]
|
|
// CHECK:STDOUT: %impl.elem0.loc6_18.1: %.97b = impl_witness_access constants.%As.impl_witness.556, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.51e]
|
|
// CHECK:STDOUT: %bound_method.loc6_18.1: <bound method> = bound_method %int_3.loc6, %impl.elem0.loc6_18.1 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
|
|
// CHECK:STDOUT: %specific_fn.loc6_18.1: <specific function> = specific_function %impl.elem0.loc6_18.1, @Core.IntLiteral.as.As.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
|
|
// CHECK:STDOUT: %bound_method.loc6_18.2: <bound method> = bound_method %int_3.loc6, %specific_fn.loc6_18.1 [concrete = constants.%bound_method.4fe]
|
|
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %u32 = call %bound_method.loc6_18.2(%int_3.loc6) [concrete = constants.%int_3.d14]
|
|
// CHECK:STDOUT: %.loc6_18.1: %u32 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_3.d14]
|
|
// CHECK:STDOUT: %.loc6_18.2: %u32 = converted %int_3.loc6, %.loc6_18.1 [concrete = constants.%int_3.d14]
|
|
// CHECK:STDOUT: %impl.elem0.loc6_18.2: %.a92 = impl_witness_access constants.%ImplicitAs.impl_witness.f6b, element0 [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.e1c]
|
|
// CHECK:STDOUT: %bound_method.loc6_18.3: <bound method> = bound_method %.loc6_18.2, %impl.elem0.loc6_18.2 [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.bound]
|
|
// CHECK:STDOUT: %specific_fn.loc6_18.2: <specific function> = specific_function %impl.elem0.loc6_18.2, @UInt.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.specific_fn]
|
|
// CHECK:STDOUT: %bound_method.loc6_18.4: <bound method> = bound_method %.loc6_18.2, %specific_fn.loc6_18.2 [concrete = constants.%bound_method.709]
|
|
// CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.call: init Core.IntLiteral = call %bound_method.loc6_18.4(%.loc6_18.2) [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %.loc6_18.3: Core.IntLiteral = value_of_initializer %UInt.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %.loc6_18.4: Core.IntLiteral = converted %.loc6_18.2, %.loc6_18.3 [concrete = constants.%int_3.1ba]
|
|
// CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.4, %i32.loc6 [concrete = constants.%array_type]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @__global_init() {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|