diff --git a/releasenotes.md b/releasenotes.md index 0bd8a5537..b1a0f0559 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -121,6 +121,7 @@ - Recursive definitions not discovered when initializer is access on other const #2817 - Slice overrun detected late hit codegen assert #2822 - Compile time dereference of a constant slice was too generous #2821 +- Constant deref of subscript had inserted checks #2818 ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 93153c2a2..06d38e1e0 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -780,7 +780,7 @@ static inline void llvm_emit_subscript(GenContext *c, BEValue *value, Expr *expr } llvm_emit_subscript_addr(c, value, expr); - if (safe_mode_enabled() && value->alignment > 1 && !c->emitting_load_store_check && parent_type->type_kind != TYPE_ARRAY) + if (!llvm_is_global_eval(c) && safe_mode_enabled() && value->alignment > 1 && !c->emitting_load_store_check && parent_type->type_kind != TYPE_ARRAY) { LLVMValueRef as_int = LLVMBuildPtrToInt(c->builder, value->value, llvm_get_type(c, type_usz), ""); LLVMValueRef align = llvm_const_int(c, type_usz, value->alignment); diff --git a/test/test_suite/globals/taddr_subscript_global.c3t b/test/test_suite/globals/taddr_subscript_global.c3t new file mode 100644 index 000000000..acf9a0c6c --- /dev/null +++ b/test/test_suite/globals/taddr_subscript_global.c3t @@ -0,0 +1,14 @@ +// #target: macos-x64 +module test; +int a; +fn int main() +{ + static any[] x = { &&((&a + 1)[2]), }; + return 0; +} + +/* #expect: test.ll + +@test.a = global i32 0, align 4 +@.__const_slice = private unnamed_addr global [1 x %any] [%any { ptr getelementptr inbounds (i8, ptr getelementptr (i8, ptr @test.a, i64 4), i64 8), i64 ptrtoint (ptr @"$ct.int" to i64) }], align 16 +@main.x = internal unnamed_addr global %"any[]" { ptr @.__const_slice, i64 1 }, align 8 \ No newline at end of file