usize indexing only
This commit is contained in:
+87
-1
@@ -1867,6 +1867,92 @@ many_item_pointer_slices_require_end_bound :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, end_bound_errors, 2)
|
||||
}
|
||||
|
||||
@(test)
|
||||
indices_and_slice_bounds_require_usize :: proc(t: ^testing.T) {
|
||||
text := `Index :: distinct u32
|
||||
main func() void {
|
||||
values [2]i32 := [1, 2]
|
||||
index := 1
|
||||
start := 0
|
||||
end := 1
|
||||
small u32 := 0
|
||||
id Index := Index(0)
|
||||
_ = values[index]
|
||||
_ = values[start..end]
|
||||
_ = values[small]
|
||||
_ = values[id]
|
||||
_ = values[small..]
|
||||
_ = values[..id]
|
||||
}
|
||||
`
|
||||
source_file := source.Source{path="test.bro", text=text}
|
||||
diagnostics := source.init_diagnostics(&source_file)
|
||||
defer source.destroy_diagnostics(&diagnostics)
|
||||
symbols := symbol.init_table()
|
||||
defer symbol.destroy_table(&symbols)
|
||||
stream := lexer.lex(&source_file, &diagnostics, &symbols)
|
||||
defer delete(stream.items)
|
||||
ast_module := parser.parse(&stream, &source_file, &diagnostics)
|
||||
defer ast.destroy_module(&ast_module)
|
||||
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
|
||||
defer hir.destroy_module(&hir_module)
|
||||
|
||||
index_errors, bound_errors := 0, 0
|
||||
for diagnostic in diagnostics.items {
|
||||
index_errors += 1 if strings.contains(diagnostic.message, "index must have type usize") else 0
|
||||
bound_errors += 1 if strings.contains(diagnostic.message, "slice bound must have type usize") else 0
|
||||
}
|
||||
testing.expect_value(t, len(diagnostics.items), 4)
|
||||
testing.expect_value(t, index_errors, 2)
|
||||
testing.expect_value(t, bound_errors, 2)
|
||||
}
|
||||
|
||||
@(test)
|
||||
comptime_indices_infer_usize_and_reject_explicit_integer_types :: proc(t: ^testing.T) {
|
||||
text := `good func($items [2]u8) u8 {
|
||||
start := 0
|
||||
end := 1
|
||||
part :: items[start..end]
|
||||
return part[start]
|
||||
}
|
||||
bad_index func($items [2]u8) u8 {
|
||||
cursor u32 := 0
|
||||
return items[cursor]
|
||||
}
|
||||
bad_bound func($items [2]u8) u8 {
|
||||
start u32 := 0
|
||||
return items[start..][0]
|
||||
}
|
||||
GOOD :: $good([7, 8])
|
||||
BAD_INDEX :: $bad_index([7, 8])
|
||||
BAD_BOUND :: $bad_bound([7, 8])
|
||||
main func() void {
|
||||
_ = GOOD
|
||||
_ = BAD_INDEX
|
||||
_ = BAD_BOUND
|
||||
}
|
||||
`
|
||||
source_file := source.Source{path="test.bro", text=text}
|
||||
diagnostics := source.init_diagnostics(&source_file)
|
||||
defer source.destroy_diagnostics(&diagnostics)
|
||||
symbols := symbol.init_table()
|
||||
defer symbol.destroy_table(&symbols)
|
||||
stream := lexer.lex(&source_file, &diagnostics, &symbols)
|
||||
defer delete(stream.items)
|
||||
ast_module := parser.parse(&stream, &source_file, &diagnostics)
|
||||
defer ast.destroy_module(&ast_module)
|
||||
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
|
||||
defer hir.destroy_module(&hir_module)
|
||||
|
||||
testing.expect_value(t, len(diagnostics.items), 2)
|
||||
index_error, bound_error := false, false
|
||||
for diagnostic in diagnostics.items {
|
||||
index_error = index_error || strings.contains(diagnostic.message, "index must have type usize, got u32")
|
||||
bound_error = bound_error || strings.contains(diagnostic.message, "slice bound must have type usize, got u32")
|
||||
}
|
||||
testing.expect(t, index_error && bound_error)
|
||||
}
|
||||
|
||||
@(test)
|
||||
layout_builtins_compile_and_run :: proc(t: ^testing.T) {
|
||||
directory := "/tmp/brolang-test-layout-builtins"
|
||||
@@ -6107,7 +6193,7 @@ milestone_24_rejects_invalid_forms :: proc(t: ^testing.T) {
|
||||
}
|
||||
cases := [?]Case{
|
||||
{"examples/programs/index_signed_error", "/tmp/brolang-test-index-signed-error"},
|
||||
{"examples/programs/index_int_constraint_error", "/tmp/brolang-test-index-int-constraint-error"},
|
||||
{"examples/programs/index_unsigned_error", "/tmp/brolang-test-index-unsigned-error"},
|
||||
{"examples/programs/scalar_cast_error", "/tmp/brolang-test-scalar-cast-error"},
|
||||
{"examples/programs/array_const_size_error", "/tmp/brolang-test-array-const-size-error"},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user