authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-08 17:02:18-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-08 17:02:18-05:00
log558ae2f21a49ae5d75d1836cf86dbe4250d5fdbe
treefbb83e35a1070db0fc70dabfcaf144473d5747b9
parentddd9624e2d03b71754e1591637f0f4f835c01a35

fix a case of invalid ptr const-ness


4 files changed, 18 insertions(+), 4 deletions(-)

src/ir.cpp+5-2
...@@ -4349,7 +4349,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *...@@ -4349,7 +4349,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
43494349
4350 // We needed a pointer to a value, but we got a value. So we create4350 // We needed a pointer to a value, but we got a value. So we create
4351 // an instruction which just makes a const pointer of it.4351 // an instruction which just makes a const pointer of it.
4352 return ir_build_ref(irb, scope, value->source_node, value, true, false);4352 return ir_build_ref(irb, scope, value->source_node, value, lval.is_const, lval.is_volatile);
4353}4353}
43544354
4355static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *node,4355static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *node,
...@@ -9420,7 +9420,10 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru...@@ -9420,7 +9420,10 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
9420 return ira->codegen->builtin_types.entry_invalid;9420 return ira->codegen->builtin_types.entry_invalid;
94219421
9422 if (instr_is_comptime(ptr) && ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {9422 if (instr_is_comptime(ptr) && ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
9423 assert(ptr->value.data.x_ptr.mut != ConstPtrMutComptimeConst);9423 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst) {
9424 ir_add_error(ira, &store_ptr_instruction->base, buf_sprintf("cannot assign to constant"));
9425 return ira->codegen->builtin_types.entry_invalid;
9426 }
9424 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {9427 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
9425 if (instr_is_comptime(casted_value)) {9428 if (instr_is_comptime(casted_value)) {
9426 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);9429 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
test/cases/const_slice_child.zig+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const assert = @import("std").debug.assert;1const assert = @import("std").debug.assert;
22
3var argv: &&const u8 = undefined;3var argv: &const &const u8 = undefined;
44
5fn constSliceChild() {5fn constSliceChild() {
6 @setFnTest(this);6 @setFnTest(this);
test/cases/misc.zig+1-1
...@@ -450,7 +450,7 @@ fn pointerComparison() {...@@ -450,7 +450,7 @@ fn pointerComparison() {
450 const b = &a;450 const b = &a;
451 assert(ptrEql(b, b));451 assert(ptrEql(b, b));
452}452}
453fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {453fn ptrEql(a: &const []const u8, b: &const []const u8) -> bool {
454 a == b454 a == b
455}455}
456456
test/run_tests.cpp+11
...@@ -1713,6 +1713,17 @@ pub fn pass(in: []u8) -> []u8 {...@@ -1713,6 +1713,17 @@ pub fn pass(in: []u8) -> []u8 {
1713 return (*out)[0...1];1713 return (*out)[0...1];
1714}1714}
1715 )SOURCE", 1, ".tmp_source.zig:5:5: error: attempt to dereference non pointer type '[10]u8'");1715 )SOURCE", 1, ".tmp_source.zig:5:5: error: attempt to dereference non pointer type '[10]u8'");
1716
1717 add_compile_fail_case("pass const ptr to mutable ptr fn", R"SOURCE(
1718fn foo() -> bool {
1719 const a = ([]const u8)("a");
1720 const b = &a;
1721 return ptrEql(b, b);
1722}
1723fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
1724 return true;
1725}
1726 )SOURCE", 1, ".tmp_source.zig:5:19: error: expected type '&[]const u8', found '&const []const u8'");
1716}1727}
17171728
1718//////////////////////////////////////////////////////////////////////////////1729//////////////////////////////////////////////////////////////////////////////