authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-04-17 21:45:14-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-18 04:45:14+00:00
log187f0c1e262cbeb37d72c571381b5b94c1eb3a63
tree61938248f907629ec02bdfc4402bd8ff4f2dd8f7
parenta0de077600a35e41ce861398eaf3c8e832fee42e
signaturebadge-check Signed by PGP key B5690EEEBB952194

Sema: correctly make inferred allocs constant

Resolves: #19677

2 files changed, 21 insertions(+), 1 deletions(-)

src/Sema.zig+6-1
......@@ -4279,7 +4279,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
42794279 const const_ptr_ty = try sema.makePtrTyConst(final_ptr_ty);
42804280 const new_const_ptr = try mod.getCoerced(Value.fromInterned(ptr_val), const_ptr_ty);
42814281
4282 // Remap the ZIR oeprand to the resolved pointer value
4282 // Remap the ZIR operand to the resolved pointer value
42834283 sema.inst_map.putAssumeCapacity(inst_data.operand.toIndex().?, Air.internedToRef(new_const_ptr.toIntern()));
42844284
42854285 // Unless the block is comptime, `alloc_inferred` always produces
......@@ -4305,6 +4305,11 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
43054305 .data = .{ .ty = final_ptr_ty },
43064306 });
43074307
4308 if (ia1.is_const) {
4309 // Remap the ZIR operand to the pointer const
4310 sema.inst_map.putAssumeCapacity(inst_data.operand.toIndex().?, try sema.makePtrConst(block, ptr));
4311 }
4312
43084313 // Now we need to go back over all the store instructions, and do the logic as if
43094314 // the new result ptr type was available.
43104315
test/cases/compile_errors/assign_to_constant_destructure.zig created+15
......@@ -0,0 +1,15 @@
1export fn a() void {
2 const S = struct {
3 fn b() struct { usize, usize } {
4 return .{ 0, 0 };
5 }
6 };
7 const c, _ = S.b();
8 c += 10;
9}
10
11// error
12// backend=stage2
13// target=native
14//
15// :8:7: error: cannot assign to constant