Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array types with dependent bounds. #4751

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions toolchain/check/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
}

// Check that the tuple is the right size.
uint64_t array_bound = sem_ir.GetArrayBoundValue(array_type.bound_id);
std::optional<uint64_t> array_bound =
sem_ir.GetArrayBoundValue(array_type.bound_id);
if (!array_bound) {
// TODO: Should this fall back to using `ImplicitAs`?
CARBON_DIAGNOSTIC(ArrayInitDependentBound, Error,
"cannot initialize array with dependent bound from a "
"list of initializers");
context.emitter().Emit(value_loc_id, ArrayInitDependentBound);
return SemIR::ErrorInst::SingletonInstId;
}
if (tuple_elem_types.size() != array_bound) {
CARBON_DIAGNOSTIC(
ArrayInitFromLiteralArgCountMismatch, Error,
Expand All @@ -252,7 +261,7 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
literal_elems.empty()
? ArrayInitFromExprArgCountMismatch
: ArrayInitFromLiteralArgCountMismatch,
array_bound, tuple_elem_types.size());
*array_bound, tuple_elem_types.size());
return SemIR::ErrorInst::SingletonInstId;
}

Expand All @@ -273,7 +282,7 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
// TODO: Annotate diagnostics coming from here with the array element index,
// if initializing from a tuple literal.
llvm::SmallVector<SemIR::InstId> inits;
inits.reserve(array_bound + 1);
inits.reserve(*array_bound + 1);
for (auto [i, src_type_id] : llvm::enumerate(tuple_elem_types)) {
// TODO: This call recurses back into conversion. Switch to an iterative
// approach.
Expand Down
14 changes: 8 additions & 6 deletions toolchain/check/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,13 +1500,15 @@ static auto TryEvalInstInContext(EvalContext& eval_context,
eval_context, inst,
[&](SemIR::ArrayType result) {
auto bound_id = array_type.bound_id;
auto int_bound =
eval_context.insts().TryGetAs<SemIR::IntValue>(result.bound_id);
auto bound_inst = eval_context.insts().Get(result.bound_id);
auto int_bound = bound_inst.TryAs<SemIR::IntValue>();
if (!int_bound) {
// TODO: Permit symbolic array bounds. This will require fixing
// callers of `GetArrayBoundValue`.
eval_context.context().TODO(bound_id, "symbolic array bound");
return false;
CARBON_CHECK(eval_context.constant_values()
.Get(result.bound_id)
.is_symbolic(),
"Unexpected inst {0} for template constant int",
bound_inst);
return true;
}
// TODO: We should check that the size of the resulting array type
// fits in 64 bits, not just that the bound does. Should we use a
Expand Down
122 changes: 122 additions & 0 deletions toolchain/check/testdata/array/init_dependent_bound.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/array/init_dependent_bound.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/init_dependent_bound.carbon

// --- fail_init_dependent_bound.carbon

library "[[@TEST_NAME]]";

fn F(N:! i32) {
// CHECK:STDERR: fail_init_dependent_bound.carbon:[[@LINE+4]]:23: error: cannot initialize array with dependent bound from a list of initializers [ArrayInitDependentBound]
// CHECK:STDERR: var arr: [i32; N] = (1, 2, 3);
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR:
var arr: [i32; N] = (1, 2, 3);
}

// --- fail_todo_init_template_dependent_bound.carbon

library "[[@TEST_NAME]]";

// TODO: This should be valid.
// CHECK:STDERR: fail_todo_init_template_dependent_bound.carbon:[[@LINE+3]]:6: error: semantics TODO: `HandleTemplate` [SemanticsTodo]
// CHECK:STDERR: fn G(template N:! i32) {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
fn G(template N:! i32) {
var arr: [i32; N] = (1, 2, 3);
}

fn H() { G(3); }

// CHECK:STDOUT: --- fail_init_dependent_bound.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
// CHECK:STDOUT: %N.2: %i32 = bind_symbolic_name N, 0 [symbolic]
// CHECK:STDOUT: %N.patt.2: %i32 = symbolic_binding_pattern N, 0 [symbolic]
// CHECK:STDOUT: %F.type: type = fn_type @F [template]
// CHECK:STDOUT: %F: %F.type = struct_value () [template]
// CHECK:STDOUT: %Convert.type.9: type = fn_type @Convert.3, @impl.2(%int_32) [template]
// CHECK:STDOUT: %Convert.9: %Convert.type.9 = struct_value () [template]
// CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %N.2, %Convert.9 [symbolic]
// CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.3(%int_32) [symbolic]
// CHECK:STDOUT: %int.convert_checked: init Core.IntLiteral = call %Convert.specific_fn(%N.2) [symbolic]
// CHECK:STDOUT: %array_type: type = array_type %int.convert_checked, %i32 [symbolic]
// CHECK:STDOUT: %require_complete.3: <witness> = require_complete_type %array_type [symbolic]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template]
// CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template]
// CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
// CHECK:STDOUT: .Int = %import_ref.1
// CHECK:STDOUT: .ImplicitAs = %import_ref.5
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
// CHECK:STDOUT: %N.patt.loc4_6.1: %i32 = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_6.2 (constants.%N.patt.2)]
// CHECK:STDOUT: %N.param_patt: %i32 = value_param_pattern %N.patt.loc4_6.1, runtime_param<invalid> [symbolic = %N.patt.loc4_6.2 (constants.%N.patt.2)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %N.param: %i32 = value_param runtime_param<invalid>
// CHECK:STDOUT: %.loc4: type = splice_block %i32 [template = constants.%i32] {
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %N.loc4_6.1: %i32 = bind_symbolic_name N, 0, %N.param [symbolic = %N.loc4_6.2 (constants.%N.2)]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @F(%N.loc4_6.1: %i32) {
// CHECK:STDOUT: %N.loc4_6.2: %i32 = bind_symbolic_name N, 0 [symbolic = %N.loc4_6.2 (constants.%N.2)]
// CHECK:STDOUT: %N.patt.loc4_6.2: %i32 = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_6.2 (constants.%N.patt.2)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %N.loc4_6.2, constants.%Convert.9 [symbolic = %Convert.bound (constants.%Convert.bound)]
// CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.3(constants.%int_32) [symbolic = %Convert.specific_fn (constants.%Convert.specific_fn)]
// CHECK:STDOUT: %int.convert_checked: init Core.IntLiteral = call %Convert.specific_fn(%N.loc4_6.2) [symbolic = %int.convert_checked (constants.%int.convert_checked)]
// CHECK:STDOUT: %array_type: type = array_type %int.convert_checked, %i32 [symbolic = %array_type (constants.%array_type)]
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type @F.%array_type (%array_type) [symbolic = %require_complete (constants.%require_complete.3)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn(%N.param_patt: %i32) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %arr.var: ref @F.%array_type (%array_type) = var arr
// CHECK:STDOUT: %arr: ref @F.%array_type (%array_type) = bind_name arr, %arr.var
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1]
// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2]
// CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
// CHECK:STDOUT: %.loc9: %tuple.type = tuple_literal (%int_1, %int_2, %int_3)
// CHECK:STDOUT: assign %arr.var, <error>
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @F(constants.%N.2) {
// CHECK:STDOUT: %N.loc4_6.2 => constants.%N.2
// CHECK:STDOUT: %N.patt.loc4_6.2 => constants.%N.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_todo_init_template_dependent_bound.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {}
// CHECK:STDOUT:
Loading
Loading