Commit 558ae2f21a

Andrew Kelley <superjoe30@gmail.com>
2017-03-08 23:02:18
fix a case of invalid ptr const-ness
1 parent ddd9624
Changed files (4)
src/ir.cpp
@@ -4349,7 +4349,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
 
     // We needed a pointer to a value, but we got a value. So we create
     // an instruction which just makes a const pointer of it.
-    return ir_build_ref(irb, scope, value->source_node, value, true, false);
+    return ir_build_ref(irb, scope, value->source_node, value, lval.is_const, lval.is_volatile);
 }
 
 static 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
         return ira->codegen->builtin_types.entry_invalid;
 
     if (instr_is_comptime(ptr) && ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
-        assert(ptr->value.data.x_ptr.mut != ConstPtrMutComptimeConst);
+        if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst) {
+            ir_add_error(ira, &store_ptr_instruction->base, buf_sprintf("cannot assign to constant"));
+            return ira->codegen->builtin_types.entry_invalid;
+        }
         if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
             if (instr_is_comptime(casted_value)) {
                 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
test/cases/const_slice_child.zig
@@ -1,6 +1,6 @@
 const assert = @import("std").debug.assert;
 
-var argv: &&const u8 = undefined;
+var argv: &const &const u8 = undefined;
 
 fn constSliceChild() {
     @setFnTest(this);
test/cases/misc.zig
@@ -450,7 +450,7 @@ fn pointerComparison() {
     const b = &a;
     assert(ptrEql(b, b));
 }
-fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
+fn ptrEql(a: &const []const u8, b: &const []const u8) -> bool {
     a == b
 }
 
test/run_tests.cpp
@@ -1713,6 +1713,17 @@ pub fn pass(in: []u8) -> []u8 {
     return (*out)[0...1];
 }
     )SOURCE", 1, ".tmp_source.zig:5:5: error: attempt to dereference non pointer type '[10]u8'");
+
+    add_compile_fail_case("pass const ptr to mutable ptr fn", R"SOURCE(
+fn foo() -> bool {
+    const a = ([]const u8)("a");
+    const b = &a;
+    return ptrEql(b, b);
+}
+fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
+    return true;
+}
+    )SOURCE", 1, ".tmp_source.zig:5:19: error: expected type '&[]const u8', found '&const []const u8'");
 }
 
 //////////////////////////////////////////////////////////////////////////////