authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-17 19:41:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-17 19:41:11-04:00
logb16229da1de90295b4d173f1a0ddbca677e8cdf2
treeab8e9558ba662ab085eb35f3ec7365822db71cc4
parent78a9a465a372d25ef6f9a1f6bb1018160b8c94e1
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for @ptrCast 0 bit type to non-0 bit type


2 files changed, 30 insertions(+), 3 deletions(-)

src/ir.cpp+17-3
...@@ -11528,7 +11528,13 @@ static bool optional_value_is_null(ConstExprValue *val) {...@@ -11528,7 +11528,13 @@ static bool optional_value_is_null(ConstExprValue *val) {
11528static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {11528static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
11529 Error err;11529 Error err;
11530 IrInstruction *op1 = bin_op_instruction->op1->other;11530 IrInstruction *op1 = bin_op_instruction->op1->other;
11531 if (type_is_invalid(op1->value.type))
11532 return ira->codegen->builtin_types.entry_invalid;
11533
11531 IrInstruction *op2 = bin_op_instruction->op2->other;11534 IrInstruction *op2 = bin_op_instruction->op2->other;
11535 if (type_is_invalid(op2->value.type))
11536 return ira->codegen->builtin_types.entry_invalid;
11537
11532 AstNode *source_node = bin_op_instruction->base.source_node;11538 AstNode *source_node = bin_op_instruction->base.source_node;
1153311539
11534 IrBinOp op_id = bin_op_instruction->op_id;11540 IrBinOp op_id = bin_op_instruction->op_id;
...@@ -20190,11 +20196,19 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr...@@ -20190,11 +20196,19 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
20190 instruction->base.source_node, nullptr, ptr);20196 instruction->base.source_node, nullptr, ptr);
20191 casted_ptr->value.type = dest_type;20197 casted_ptr->value.type = dest_type;
2019220198
20193 // Keep the bigger alignment, it can only help-20199 if (type_has_bits(dest_type) && !type_has_bits(src_type)) {
20194 // unless the target is zero bits.20200 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
20195 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))20201 buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
20202 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
20203 add_error_note(ira->codegen, msg, ptr->source_node,
20204 buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));
20205 add_error_note(ira->codegen, msg, dest_type_value->source_node,
20206 buf_sprintf("'%s' has in-memory bits", buf_ptr(&dest_type->name)));
20196 return ira->codegen->builtin_types.entry_invalid;20207 return ira->codegen->builtin_types.entry_invalid;
20208 }
2019720209
20210 // Keep the bigger alignment, it can only help-
20211 // unless the target is zero bits.
20198 IrInstruction *result;20212 IrInstruction *result;
20199 if (src_align_bytes > dest_align_bytes && type_has_bits(dest_type)) {20213 if (src_align_bytes > dest_align_bytes && type_has_bits(dest_type)) {
20200 result = ir_align_cast(ira, casted_ptr, src_align_bytes, false);20214 result = ir_align_cast(ira, casted_ptr, src_align_bytes, false);
test/compile_errors.zig+13
...@@ -1,6 +1,19 @@...@@ -1,6 +1,19 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "@ptrCast a 0 bit type to a non- 0 bit type",
6 \\export fn entry() bool {
7 \\ var x: u0 = 0;
8 \\ const p = @ptrCast(?*u0, &x);
9 \\ return p == null;
10 \\}
11 ,
12 ".tmp_source.zig:3:15: error: '*u0' and '?*u0' do not have the same in-memory representation",
13 ".tmp_source.zig:3:31: note: '*u0' has no in-memory bits",
14 ".tmp_source.zig:3:24: note: '?*u0' has in-memory bits",
15 );
16
4 cases.add(17 cases.add(
5 "comparing a non-optional pointer against null",18 "comparing a non-optional pointer against null",
6 \\export fn entry() void {19 \\export fn entry() void {