authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-30 02:56:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-30 02:56:42-04:00
loge9280c86a1fc79aec5936bb21c9469657555a7ed
treed46be8ed77adda6487f49f25d2d43a36ec98b18c
parent010b725bdef57eee969668087c8737e053b10e9f

compile error for not-aligned-enough pointer to cmpxchg

See #37

2 files changed, 19 insertions(+), 0 deletions(-)

src/ir.cpp+9
......@@ -13488,6 +13488,15 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct
1348813488
1348913489 TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type;
1349013490
13491 uint32_t align_bytes = ptr->value.type->data.pointer.alignment;
13492 uint64_t size_bytes = type_size(ira->codegen, child_type);
13493 if (align_bytes < size_bytes) {
13494 ir_add_error(ira, instruction->ptr,
13495 buf_sprintf("expected pointer alignment of at least %" ZIG_PRI_u64 ", found %" PRIu32,
13496 size_bytes, align_bytes));
13497 return ira->codegen->builtin_types.entry_invalid;
13498 }
13499
1349113500 IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type);
1349213501 if (type_is_invalid(casted_cmp_value->value.type))
1349313502 return ira->codegen->builtin_types.entry_invalid;
test/compile_errors.zig+10
......@@ -2051,4 +2051,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
20512051 ,
20522052 ".tmp_source.zig:2:35: error: expected type 'fn() align 8 -> i32', found 'fn() align 4 -> i32'");
20532053
2054 cases.add("passing a not-aligned-enough pointer to cmpxchg",
2055 \\const AtomicOrder = @import("builtin").AtomicOrder;
2056 \\export fn entry() -> bool {
2057 \\ var x: i32 align 1 = 1234;
2058 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
2059 \\ return x == 5678;
2060 \\}
2061 ,
2062 ".tmp_source.zig:4:23: error: expected pointer alignment of at least 4, found 1");
2063
20542064}