authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-17 19:28:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-17 19:28:33-04:00
log2e0b114fdce1a10b8433438d8e2251cf708a72ec
treebde37363bf88c19995da326706fa0db191d523b2
parent216e14891ea5fa1a88804d9781ef779d448d1220

add compile error for intToPtr with a 0-bit ptr

See #323

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

src/ir.cpp+7
...@@ -12473,6 +12473,13 @@ static TypeTableEntry *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstr...@@ -12473,6 +12473,13 @@ static TypeTableEntry *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstr
12473 return ira->codegen->builtin_types.entry_invalid;12473 return ira->codegen->builtin_types.entry_invalid;
12474 }12474 }
1247512475
12476 type_ensure_zero_bits_known(ira->codegen, dest_type);
12477 if (!type_has_bits(dest_type)) {
12478 ir_add_error(ira, dest_type_value,
12479 buf_sprintf("type '%s' has 0 bits and cannot store information", buf_ptr(&dest_type->name)));
12480 return ira->codegen->builtin_types.entry_invalid;
12481 }
12482
12476 IrInstruction *target = instruction->target->other;12483 IrInstruction *target = instruction->target->other;
12477 if (type_is_invalid(target->value.type))12484 if (type_is_invalid(target->value.type))
12478 return ira->codegen->builtin_types.entry_invalid;12485 return ira->codegen->builtin_types.entry_invalid;
test/run_tests.cpp+7
...@@ -1895,6 +1895,13 @@ export fn entry() {...@@ -1895,6 +1895,13 @@ export fn entry() {
1895 const foo = Arch.x86;1895 const foo = Arch.x86;
1896}1896}
1897 )SOURCE", 1, ".tmp_source.zig:3:21: error: container 'Arch' has no member called 'x86'");1897 )SOURCE", 1, ".tmp_source.zig:3:21: error: container 'Arch' has no member called 'x86'");
1898
1899 add_compile_fail_case("int to ptr of 0 bits", R"SOURCE(
1900export fn foo() {
1901 var x: usize = 0x1000;
1902 var y: &void = @intToPtr(&void, x);
1903}
1904 )SOURCE", 1, ".tmp_source.zig:4:31: error: type '&void' has 0 bits and cannot store information");
1898}1905}
18991906
1900//////////////////////////////////////////////////////////////////////////////1907//////////////////////////////////////////////////////////////////////////////