usize indexing only

This commit is contained in:
2026-08-11 20:12:12 +02:00
parent fa53ca2219
commit c2343b54bb
8 changed files with 216 additions and 53 deletions
+35 -29
View File
@@ -5108,7 +5108,8 @@ infer_compound_expr :: proc(
return types.child_type(value, store) if types.is_pointer(value, store) else types.INVALID
case .Index:
value := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
_ = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types)
_ = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types, types.USIZE)
_ = record_demand(checker, expr.right, types.USIZE, locals, local_types, pkg, file)
item, ok := types.container(value, store)
return item.child if ok else types.INVALID
case .Slice:
@@ -5124,7 +5125,8 @@ infer_compound_expr :: proc(
}
for bound in expr.args {
if bound != ast.INVALID_EXPR {
_ = infer_nested_expr(checker, bound, locals, pkg, file, demanded, local_types)
_ = infer_nested_expr(checker, bound, locals, pkg, file, demanded, local_types, types.USIZE)
_ = record_demand(checker, bound, types.USIZE, locals, local_types, pkg, file)
}
}
preserve := item.has_sentinel && expr.args[1] == ast.INVALID_EXPR
@@ -8100,36 +8102,35 @@ build_scalar_cast :: proc(
diagnostic=source.INVALID_DIAGNOSTIC,
})
}
normalize_index_expr :: proc(checker: ^Checker, value: hir.Expr_Id, span: source.Span) -> hir.Expr_Id {
store := &checker.module.types
current := value
current_type := checker.module.exprs[current].type
representation, distinct_ok := types.distinct_scalar_backing(current_type, store)
if distinct_ok && types.is_concrete_integer(representation) {
for {
backing, ok := types.distinct_backing(current_type, store)
if !ok {
break
}
current = add_hir_expr(checker, hir.Expr{
kind=.Retype,
span=span,
type=backing,
left=current,
target=hir.INVALID_REF,
right=hir.INVALID_EXPR,
diagnostic=source.INVALID_DIAGNOSTIC,
})
current_type = backing
}
normalize_index_expr :: proc(
checker: ^Checker,
value: hir.Expr_Id,
span: source.Span,
label: string,
) -> hir.Expr_Id {
if value == hir.INVALID_EXPR {
return value
}
current_expr := &checker.module.exprs[current]
if _, invalid := invalid_expr_diagnostic(checker, value); invalid {
return value
}
current_expr := &checker.module.exprs[value]
if current_expr.kind == .Integer &&
fits_integer_type(i128(current_expr.integer), types.USIZE, checker.target) {
current_expr.type = types.USIZE
return current
return value
}
return coerce_expr(checker, current, types.USIZE, span)
if !types.equal(current_expr.type, types.USIZE) {
id := source.addf(
checker.diagnostics,
span,
"%s must have type usize, got %s",
label,
type_label(checker, current_expr.type),
)
return invalid_hir_expr(checker, span, id, types.USIZE)
}
return value
}
@@ -8690,7 +8691,7 @@ build_compound_expr :: proc(
case .Index:
container := build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
index := build_nested_expr(checker, expr.right, locals, global_reads, calls, types.INVALID, pkg, file)
index = normalize_index_expr(checker, index, expr.span)
index = normalize_index_expr(checker, index, expr.span, "index")
if invalid, propagated := propagate_invalid_expr(checker, expr.span, container, index); propagated {
return invalid
}
@@ -8734,7 +8735,12 @@ build_compound_expr :: proc(
for bound, index in expr.args {
if bound != ast.INVALID_EXPR {
bounds[index] = build_nested_expr(checker, bound, locals, global_reads, calls, types.INVALID, pkg, file)
bounds[index] = normalize_index_expr(checker, bounds[index], checker.ast_module.exprs[bound].span)
bounds[index] = normalize_index_expr(
checker,
bounds[index],
checker.ast_module.exprs[bound].span,
"slice bound",
)
if invalid, propagated := propagate_invalid_expr(checker, expr.span, bounds[index]); propagated {
delete(bounds, checker.allocator)
return invalid
+85 -15
View File
@@ -291,6 +291,7 @@ Ct_Binding :: struct {
value: Ct_Value_Id,
cell: Ct_Cell_Id,
mutable: bool,
open_integer: bool,
}
Ct_Error_Refinement :: struct {
@@ -473,9 +474,23 @@ ct_extend_place :: proc(
return ct_add_place(state, base.cell, value_type, writable, extended[:])
}
ct_bind_value :: proc(state: ^Ct_State, name: symbol.Id, value_type: types.Type, value: Ct_Value_Id, mutable: bool) {
ct_bind_value :: proc(
state: ^Ct_State,
name: symbol.Id,
value_type: types.Type,
value: Ct_Value_Id,
mutable: bool,
open_integer := false,
) {
cell := ct_add_cell(state, value, mutable)
append(&state.bindings, Ct_Binding{name=name, type=value_type, value=value, cell=cell, mutable=mutable})
append(&state.bindings, Ct_Binding{
name=name,
type=value_type,
value=value,
cell=cell,
mutable=mutable,
open_integer=open_integer,
})
}
ct_pop_bindings :: proc(state: ^Ct_State, start: int) {
@@ -581,6 +596,32 @@ ct_binding_value :: proc(state: ^Ct_State, index: int) -> Ct_Value_Id {
return binding.value
}
ct_contextualize_open_integer_binding :: proc(state: ^Ct_State, index: int, expected: types.Type) {
if expected != types.USIZE || index < 0 || index >= len(state.bindings) ||
!state.bindings[index].open_integer {
return
}
value_id := ct_binding_value(state, index)
if value_id == INVALID_CT_VALUE || int(value_id) >= len(state.values) {
return
}
value := state.values[value_id]
if value.kind != .Integer ||
!fits_integer_type(value.integer, expected, state.checker.target) {
return
}
value.type = expected
contextual := ct_add_value(state, value)
binding := &state.bindings[index]
binding.type = expected
binding.value = contextual
binding.open_integer = false
if binding.cell != INVALID_CT_CELL && int(binding.cell) < len(state.cells) &&
state.cells[binding.cell].live {
state.cells[binding.cell].value = contextual
}
}
ct_binding_place :: proc(state: ^Ct_State, index: int) -> Ct_Place_Id {
if index < 0 || index >= len(state.bindings) {
return INVALID_CT_PLACE
@@ -1315,6 +1356,7 @@ ct_eval_expr :: proc(
case .Name:
if !symbol.is_valid(expr.qualifier) {
if index, ok := ct_find_binding_index(state, expr.name); ok {
ct_contextualize_open_integer_binding(state, index, expected)
return ct_observe_value(state, ct_binding_value(state, index), expr.span)
}
if value, ok := current_comptime_value(checker, expr.name); ok {
@@ -1452,16 +1494,19 @@ ct_eval_expr :: proc(
}
return ct_eval_field_value(state, base_id, expr.name, expr.span)
case .Index:
index_id, index_flow, index_ok := ct_eval_expr(state, expr.right, types.INVALID, depth+1)
index_id, index_flow, index_ok := ct_eval_expr(state, expr.right, types.USIZE, depth+1)
if !index_ok || index_flow.kind != .Normal {
return INVALID_CT_VALUE, index_flow, index_ok
}
index_type := state.values[index_id].type
index_representation := types.runtime_representation(index_type, store)
index_literal := is_numeric_constant_expr(checker, expr.right)
if !types.is_concrete_integer(index_representation) ||
!index_literal && !can_implicitly_convert_type(checker, index_representation, types.USIZE) {
return INVALID_CT_VALUE, ct_flow(.Normal), ct_fail(state, .Not_Comptime, expr.span, "comptime index must be coercible to usize")
if !types.equal(index_type, types.USIZE) {
return INVALID_CT_VALUE, ct_flow(.Normal), ct_failf(
state,
.Not_Comptime,
checker.ast_module.exprs[expr.right].span,
"index must have type usize, got %s",
type_label(checker, index_type),
)
}
index_value, index_is_int := ct_integer_value(state, index_id)
if !index_is_int || index_value < 0 || index_value > i128(0x7fff_ffff) {
@@ -1962,16 +2007,19 @@ ct_eval_slice_expr :: proc(state: ^Ct_State, expr: ast.Expr, depth: int) -> (Ct_
if bound == ast.INVALID_EXPR {
continue
}
value, bound_flow, bound_ok := ct_eval_expr(state, bound, types.INVALID, depth+1)
value, bound_flow, bound_ok := ct_eval_expr(state, bound, types.USIZE, depth+1)
if !bound_ok || bound_flow.kind != .Normal {
return INVALID_CT_VALUE, bound_flow, bound_ok
}
bound_type := state.values[value].type
bound_representation := types.runtime_representation(bound_type, store)
bound_literal := is_numeric_constant_expr(checker, bound)
if !types.is_concrete_integer(bound_representation) ||
!bound_literal && !can_implicitly_convert_type(checker, bound_representation, types.USIZE) {
return INVALID_CT_VALUE, ct_flow(.Normal), ct_fail(state, .Not_Comptime, expr.span, "slice bounds must be coercible to usize")
if !types.equal(bound_type, types.USIZE) {
return INVALID_CT_VALUE, ct_flow(.Normal), ct_failf(
state,
.Not_Comptime,
checker.ast_module.exprs[bound].span,
"slice bound must have type usize, got %s",
type_label(checker, bound_type),
)
}
integer, integer_ok := ct_integer_value(state, value)
if !integer_ok || integer < 0 || integer > i128(0x7fff_ffff) {
@@ -2352,6 +2400,16 @@ ct_eval_place :: proc(
if !index_ok || index_flow.kind != .Normal {
return INVALID_CT_PLACE, types.INVALID, false, index_flow, index_ok
}
index_type := state.values[index_value].type
if !types.equal(index_type, types.USIZE) {
return INVALID_CT_PLACE, types.INVALID, false, ct_flow(.Normal), ct_failf(
state,
.Not_Comptime,
checker.ast_module.exprs[expr.right].span,
"index must have type usize, got %s",
type_label(checker, index_type),
)
}
index_int, int_ok := ct_integer_value(state, index_value)
if !int_ok || index_int < 0 || index_int > i128(0x7fff_ffff) {
return INVALID_CT_PLACE, types.INVALID, false, ct_flow(.Normal), ct_fail(state, .Not_Comptime, expr.span, "comptime index must be a non-negative integer")
@@ -4983,6 +5041,11 @@ ct_exec_statements :: proc(
} else {
declared := type_from_syntax(checker, statement.type, state.pkg, state.file, active_state=state)
expected := declared if types.is_valid(declared) && !types.is_void(declared) else types.INVALID
open_integer := false
if !is_runtime_type(checker, declared) {
constant := eval_integer_constant_in_context(checker, statement.expr, state.pkg, state.file)
open_integer = constant.kind == .Value
}
value, expr_flow, expr_ok := ct_eval_expr(state, statement.expr, expected, depth+1)
ok = expr_ok
flow = expr_flow
@@ -4991,7 +5054,14 @@ ct_exec_statements :: proc(
value, ok = ct_coerce_value(state, value, expected, statement.span)
}
if ok && statement.name != checker.sink_symbol {
ct_bind_value(state, statement.name, state.values[value].type, value, !statement.immutable)
ct_bind_value(
state,
statement.name,
state.values[value].type,
value,
!statement.immutable,
open_integer=open_integer,
)
}
}
}