zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e9280c86a1fc79aec5936bb21c9469657555a7ed (tree)
parent 010b725bdef57eee969668087c8737e053b10e9f
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Wed, 30 Aug 2017 02:56:42 -0400

compile error for not-aligned-enough pointer to cmpxchg

See #37

Diffstat:
Msrc/ir.cpp | 9+++++++++
Mtest/compile_errors.zig | 10++++++++++
2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -13488,6 +13488,15 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type; + uint32_t align_bytes = ptr->value.type->data.pointer.alignment; + uint64_t size_bytes = type_size(ira->codegen, child_type); + if (align_bytes < size_bytes) { + ir_add_error(ira, instruction->ptr, + buf_sprintf("expected pointer alignment of at least %" ZIG_PRI_u64 ", found %" PRIu32, + size_bytes, align_bytes)); + return ira->codegen->builtin_types.entry_invalid; + } + IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type); if (type_is_invalid(casted_cmp_value->value.type)) return ira->codegen->builtin_types.entry_invalid; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -2051,4 +2051,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:2:35: error: expected type 'fn() align 8 -> i32', found 'fn() align 4 -> i32'"); + cases.add("passing a not-aligned-enough pointer to cmpxchg", + \\const AtomicOrder = @import("builtin").AtomicOrder; + \\export fn entry() -> bool { + \\ var x: i32 align 1 = 1234; + \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} + \\ return x == 5678; + \\} + , + ".tmp_source.zig:4:23: error: expected pointer alignment of at least 4, found 1"); + }