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 *
43494349
43504350 // We needed a pointer to a value, but we got a value. So we create
43514351 // 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);
43534353}
43544354
43554355static 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
94209420 return ira->codegen->builtin_types.entry_invalid;
94219421
94229422 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 }
94249427 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
94259428 if (instr_is_comptime(casted_value)) {
94269429 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
test/cases/const_slice_child.zig+1-1
......@@ -1,6 +1,6 @@
11const assert = @import("std").debug.assert;
22
3var argv: &&const u8 = undefined;
3var argv: &const &const u8 = undefined;
44
55fn constSliceChild() {
66 @setFnTest(this);
test/cases/misc.zig+1-1
......@@ -450,7 +450,7 @@ fn pointerComparison() {
450450 const b = &a;
451451 assert(ptrEql(b, b));
452452}
453fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
453fn ptrEql(a: &const []const u8, b: &const []const u8) -> bool {
454454 a == b
455455}
456456
test/run_tests.cpp+11
......@@ -1713,6 +1713,17 @@ pub fn pass(in: []u8) -> []u8 {
17131713 return (*out)[0...1];
17141714}
17151715 )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'");
17161727}
17171728
17181729//////////////////////////////////////////////////////////////////////////////