From 8ed390c394afc6b75567108b87aa3f5fe5c81d9c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 16 Nov 2024 23:08:54 +0100 Subject: [PATCH] A distinct inline pointer type can now participate in pointer arithmetics. --- releasenotes.md | 1 + src/compiler/compiler_internal.h | 17 ++++++++++++++++- src/compiler/llvm_codegen_type.c | 2 +- test/test_suite/distinct/distinct_add.c3 | 7 +++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/distinct/distinct_add.c3 diff --git a/releasenotes.md b/releasenotes.md index df037f8d4..50252e404 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -10,6 +10,7 @@ - Fix bug where `a > 0 ? f() : g()` could cause a compiler crash if both returned `void!`. - `@builtin` was not respected for generic modules #1617. - Fix issue writing a single byte in the WriteBuffer +- A distinct inline pointer type can now participate in pointer arithmetics. ### Stdlib changes - Add `io::MultiReader`, `io::MultiWriter`, and `io::TeeReader` structs. diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 0312ac665..ecd263f27 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -2698,10 +2698,25 @@ INLINE CanonicalType *type_pointer_type(Type *type) return res->pointer; } +static inline Type *type_flat_distinct_inline(Type *type); + INLINE bool type_is_pointer_like(Type *type) { TypeKind kind = type->type_kind; - return kind == TYPE_POINTER || (kind == TYPE_VECTOR && type->array.base->canonical->type_kind == TYPE_POINTER); + if (kind == TYPE_DISTINCT) + { + type = type_flat_distinct_inline(type); + kind = type->type_kind; + } + switch (kind) + { + case TYPE_POINTER: + return true; + case TYPE_VECTOR: + return type_is_pointer_like(type->array.base->canonical); + default: + return false; + } } INLINE bool type_is_pointer_vector(Type *type) diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index 24ca77994..0e5d064d8 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -276,7 +276,7 @@ LLVMTypeRef llvm_func_type(GenContext *context, FunctionPrototype *prototype) LLVMTypeRef llvm_get_pointee_type(GenContext *c, Type *any_type) { - any_type = any_type->canonical; + any_type = type_lowering(any_type); ASSERT0(any_type->type_kind == TYPE_POINTER); if (any_type == type_voidptr) return llvm_get_type(c, type_char); return llvm_get_type(c, any_type->pointer); diff --git a/test/test_suite/distinct/distinct_add.c3 b/test/test_suite/distinct/distinct_add.c3 new file mode 100644 index 000000000..2481e9e50 --- /dev/null +++ b/test/test_suite/distinct/distinct_add.c3 @@ -0,0 +1,7 @@ +import std; +fn void main() +{ + ZString a = "abc"; + ZString b = a + 1; + io::printfn("%s", b); +}