Change array syntax from [T; N] to array(T, N) (#4981)

In line with the proposal in #4682, this changes the array syntax to be
array(T, N). `array` is a builtin keyword which must be followed by
parens containing two expressions and a separating comma.

The array type expression is still fully builtin, it does not forward to
a Core.Array library type yet. It merely adds the `ArrayType`
instruction, as was done with the previous syntax.

Followup work will change the instruction to reference to Core.Array,
once the library type exists and can be used directly.

---------

Co-authored-by: zygoloid <richard@metafoo.co.uk>
This commit is contained in:
Dana Jansens
2025-02-20 22:42:47 +00:00
committed by GitHub
co-authored by zygoloid
parent fc7b0016ce
commit 24bde46181
125 changed files with 1912 additions and 1889 deletions
+12 -16
View File
@@ -11,27 +11,23 @@
namespace Carbon::Check {
auto HandleParseNode(Context& /*context*/, Parse::ArrayExprStartId /*node_id*/)
auto HandleParseNode(Context& /*context*/,
Parse::ArrayExprOpenParenId /*node_id*/) -> bool {
return true;
}
auto HandleParseNode(Context& /*context*/,
Parse::ArrayExprKeywordId /*node_id*/) -> bool {
return true;
}
auto HandleParseNode(Context& /*context*/, Parse::ArrayExprCommaId /*node_id*/)
-> bool {
return true;
}
auto HandleParseNode(Context& context, Parse::ArrayExprSemiId node_id) -> bool {
context.node_stack().Push(node_id);
return true;
}
auto HandleParseNode(Context& context, Parse::ArrayExprId node_id) -> bool {
// TODO: Handle array type with undefined bound.
if (context.node_stack()
.PopAndDiscardSoloNodeIdIf<Parse::NodeKind::ArrayExprSemi>()) {
context.node_stack().PopAndIgnore();
return context.TODO(node_id, "HandleArrayExprWithoutBounds");
}
auto bound_inst_id = context.node_stack().PopExpr();
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::ArrayExprSemi>();
auto [element_type_node_id, element_type_inst_id] =
context.node_stack().PopExprWithNodeId();
@@ -43,7 +39,7 @@ auto HandleParseNode(Context& context, Parse::ArrayExprId node_id) -> bool {
// call to compile-time-only function" error.
//
// TODO: Should we support runtime-phase bounds in cases such as:
// comptime fn F(n: i32) -> type { return [i32; n]; }
// comptime fn F(n: i32) -> type { return array(i32; n); }
if (!context.constant_values().Get(bound_inst_id).is_constant()) {
CARBON_DIAGNOSTIC(InvalidArrayExpr, Error, "array bound is not a constant");
context.emitter().Emit(bound_inst_id, InvalidArrayExpr);