Formatting option "%h" now supports pointers.

This commit is contained in:
Christoffer Lerno
2025-07-08 11:43:49 +02:00
parent a2122e0153
commit 26d5cc694a
5 changed files with 23 additions and 3 deletions

View File

@@ -495,6 +495,11 @@ fn usz? Formatter.vprintf(&self, String format, any[] anys)
out = ((char*)current.ptr)[:current.type.sizeof];
break;
}
if (current.type.kindof == POINTER)
{
out = ((*(char**)current.ptr))[:current.type.inner.sizeof];
break;
}
total_len += self.out_substr("<INVALID>")!;
continue;
}

View File

@@ -12,6 +12,7 @@
- Catch accidental `foo == BAR;` where `foo = BAR;` was most likely intended. #2274
- Improve error message when doing a rethrow in a function that doesn't return an optional.
- Add `--list-asm` to view all supported `asm` instructions.
- Formatting option "%h" now supports pointers.
### Fixes
- mkdir/rmdir would not work properly with substring paths on non-windows platforms.

View File

@@ -3241,9 +3241,10 @@ static inline Type *type_flatten_no_export(Type *type)
}
}
static inline bool type_flat_is_char_array_slice(Type *type)
static inline bool type_flat_is_valid_for_arg_h(Type *type)
{
type = type_flatten(type);
if (type->type_kind == TYPE_POINTER) return true;
if (type->type_kind != TYPE_ARRAY && type->type_kind != TYPE_SLICE) return false;
switch (type->array.base->type_kind)
{

View File

@@ -2168,9 +2168,9 @@ NEXT_FLAG:
goto NEXT;
case 'H':
case 'h':
if (!type_flat_is_char_array_slice(type))
if (!type_flat_is_valid_for_arg_h(type))
{
RETURN_SEMA_ERROR(vaargs[idx], "Expected a char array or slice here.");
RETURN_SEMA_ERROR(vaargs[idx], "Expected a pointer, char array or slice here.");
}
goto NEXT;
default:

View File

@@ -0,0 +1,13 @@
module test;
import std;
struct Foo
{
int x, y;
}
fn int main()
{
Foo f = { 33, 12323 };
io::printfn("%h", &f);
return 0;
}