From 59a1590955aa37ebbea724403fdd22ce910cbac5 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 30 Jun 2025 21:41:35 +0200 Subject: [PATCH] Hex string formatter check incorrectly rejected slices. --- releasenotes.md | 1 + src/compiler/compiler_internal.h | 5 +++-- src/compiler/sema_expr.c | 4 ++-- test/test_suite/statements/format_hex_array.c3 | 12 ++++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 test/test_suite/statements/format_hex_array.c3 diff --git a/releasenotes.md b/releasenotes.md index f37d4462a..a67c127e3 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -9,6 +9,7 @@ ### Fixes - mkdir/rmdir would not work properly with substring paths on non-windows platforms. +- Hex string formatter check incorrectly rejected slices. ### Stdlib changes diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 9dfdae4f6..026e85f7c 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -3164,10 +3164,10 @@ static inline Type *type_flatten_no_export(Type *type) } } -static inline bool type_flat_is_char_array(Type *type) +static inline bool type_flat_is_char_array_slice(Type *type) { type = type_flatten(type); - if (type->type_kind != TYPE_ARRAY) return false; + if (type->type_kind != TYPE_ARRAY && type->type_kind != TYPE_SLICE) return false; switch (type->array.base->type_kind) { case TYPE_I8: @@ -3178,6 +3178,7 @@ static inline bool type_flat_is_char_array(Type *type) } } + INLINE Type *type_vector_type(Type *type) { Type *flatten = type_flatten(type); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 22dbdc5b2..ee82d9359 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2081,9 +2081,9 @@ NEXT_FLAG: goto NEXT; case 'H': case 'h': - if (!type_flat_is_char_array(type)) + if (!type_flat_is_char_array_slice(type)) { - RETURN_SEMA_ERROR(vaargs[idx], "Expected a char array here."); + RETURN_SEMA_ERROR(vaargs[idx], "Expected a char array or slice here."); } goto NEXT; default: diff --git a/test/test_suite/statements/format_hex_array.c3 b/test/test_suite/statements/format_hex_array.c3 new file mode 100644 index 000000000..4aa044880 --- /dev/null +++ b/test/test_suite/statements/format_hex_array.c3 @@ -0,0 +1,12 @@ +import std::io; + +macro convert_string_to_hex(String s) +{ + io::printfn("%02hx", (char[])s); +} + +fn void main() { + char[3] x = x"0305ff"; + io::printfn("%02h", x); + convert_string_to_hex("colors"); +}