diff --git a/src/ir.cpp b/src/ir.cpp index f56ceaf4dd73643d85d71b105aaae411b1cfa8d8..a504d6c073856ae59cf62d75dfc4596852359546 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9451,6 +9451,10 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira, return ira->codegen->builtin_types.entry_void; } +static bool is_power_of_2(uint64_t x) { + return x != 0 && ((x & (~x + 1)) == x); +} + static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, IrInstructionSetGlobalAlign *instruction) { @@ -9461,7 +9465,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, if (!ir_resolve_usize(ira, align_value, &scalar_align)) return ira->codegen->builtin_types.entry_invalid; - // TODO error if not power of 2 + if (!is_power_of_2(scalar_align)) { + ir_add_error(ira, instruction->value, buf_sprintf("alignment value must be power of 2")); + return ira->codegen->builtin_types.entry_invalid; + } AstNode *source_node = instruction->base.source_node; if (tld_var->set_global_align_node) { diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 987e72aaf9655ceb0b30272fafed9b21c48a3ded..e48c080a54d2cee3379b9de4bcd36e9a0a016f9d 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1662,6 +1662,13 @@ fn foo() { } )SOURCE", 1, ".tmp_source.zig:3:24: error: integer value 753664 cannot be implicitly casted to type 'u16'"); + add_compile_fail_case("set global variable alignment to non power of 2", R"SOURCE( +const some_data: [100]u8 = { + @setGlobalAlign(some_data, 3); + undefined +}; + )SOURCE", 1, ".tmp_source.zig:3:32: error: alignment value must be power of 2"); + } //////////////////////////////////////////////////////////////////////////////