diff --git a/src/ir.cpp b/src/ir.cpp index 2a8d6fe418..4a5f937a47 100644 --- a/src/ir.cpp +++ b/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); diff --git a/test/cases/const_slice_child.zig b/test/cases/const_slice_child.zig index 277da4be2b..21c9d03e50 100644 --- a/test/cases/const_slice_child.zig +++ b/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); diff --git a/test/cases/misc.zig b/test/cases/misc.zig index 94dab7e7cc..601194dd3f 100644 --- a/test/cases/misc.zig +++ b/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 } diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 1ef8543af3..cc951a2c11 100644 --- a/test/run_tests.cpp +++ 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'"); } //////////////////////////////////////////////////////////////////////////////