| author | |
| committer | |
| log | 8f037db88552b5171807d4a9f8be6e8942aab8a1 |
| tree | d9985543d0373138ba2357529a139c3047a61511 |
| parent | 1f4a097117a75b053d620658df4cb4e81c40fe1a |
5 files changed, 26 insertions(+), 3 deletions(-)
src/AstGen.zig+3-1| ... | ... | @@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner |
| 2138 | 2138 | .alloc_inferred_mut, |
| 2139 | 2139 | .alloc_inferred_comptime, |
| 2140 | 2140 | .alloc_inferred_comptime_mut, |
| 2141 | .make_ptr_const, | |
| 2141 | 2142 | .array_cat, |
| 2142 | 2143 | .array_mul, |
| 2143 | 2144 | .array_type, |
| ... | ... | @@ -2754,12 +2755,13 @@ fn varDecl( |
| 2754 | 2755 | if (resolve_inferred_alloc != .none) { |
| 2755 | 2756 | _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node); |
| 2756 | 2757 | } |
| 2758 | const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node); | |
| 2757 | 2759 | const sub_scope = try block_arena.create(Scope.LocalPtr); |
| 2758 | 2760 | sub_scope.* = .{ |
| 2759 | 2761 | .parent = scope, |
| 2760 | 2762 | .gen_zir = gz, |
| 2761 | 2763 | .name = ident_name, |
| 2762 | .ptr = init_scope.rl_ptr, | |
| 2764 | .ptr = const_ptr, | |
| 2763 | 2765 | .token_src = name_token, |
| 2764 | 2766 | .maybe_comptime = true, |
| 2765 | 2767 | .id_cat = .@"local constant", |
src/Sema.zig+16| ... | ... | @@ -607,6 +607,7 @@ fn analyzeBodyInner( |
| 607 | 607 | .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)), |
| 608 | 608 | .alloc_mut => try sema.zirAllocMut(block, inst), |
| 609 | 609 | .alloc_comptime_mut => try sema.zirAllocComptime(block, inst), |
| 610 | .make_ptr_const => try sema.zirMakePtrConst(block, inst), | |
| 610 | 611 | .anyframe_type => try sema.zirAnyframeType(block, inst), |
| 611 | 612 | .array_cat => try sema.zirArrayCat(block, inst), |
| 612 | 613 | .array_mul => try sema.zirArrayMul(block, inst), |
| ... | ... | @@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr |
| 2409 | 2410 | return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src); |
| 2410 | 2411 | } |
| 2411 | 2412 | |
| 2413 | fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { | |
| 2414 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; | |
| 2415 | const ptr = sema.resolveInst(inst_data.operand); | |
| 2416 | const ptr_ty = sema.typeOf(ptr); | |
| 2417 | var ptr_info = ptr_ty.ptrInfo().data; | |
| 2418 | ptr_info.mutable = false; | |
| 2419 | const const_ptr_ty = try Type.ptr(sema.arena, sema.mod.getTarget(), ptr_info); | |
| 2420 | ||
| 2421 | if (try sema.resolveMaybeUndefVal(block, inst_data.src(), ptr)) |val| { | |
| 2422 | return sema.addConstant(const_ptr_ty, val); | |
| 2423 | } | |
| 2424 | try sema.requireRuntimeBlock(block, inst_data.src()); | |
| 2425 | return block.addBitCast(const_ptr_ty, ptr); | |
| 2426 | } | |
| 2427 | ||
| 2412 | 2428 | fn zirAllocInferredComptime( |
| 2413 | 2429 | sema: *Sema, |
| 2414 | 2430 | inst: Zir.Inst.Index, |
src/Zir.zig+6| ... | ... | @@ -954,6 +954,10 @@ pub const Inst = struct { |
| 954 | 954 | /// is the allocation that needs to have its type inferred. |
| 955 | 955 | /// Uses the `un_node` field. The AST node is the var decl. |
| 956 | 956 | resolve_inferred_alloc, |
| 957 | /// Turns a pointer coming from an `alloc`, `alloc_inferred`, `alloc_inferred_comptime` or | |
| 958 | /// `Extended.alloc` into a constant version of the same pointer. | |
| 959 | /// Uses the `un_node` union field. | |
| 960 | make_ptr_const, | |
| 957 | 961 | |
| 958 | 962 | /// Implements `resume` syntax. Uses `un_node` field. |
| 959 | 963 | @"resume", |
| ... | ... | @@ -993,6 +997,7 @@ pub const Inst = struct { |
| 993 | 997 | .alloc_inferred_mut, |
| 994 | 998 | .alloc_inferred_comptime, |
| 995 | 999 | .alloc_inferred_comptime_mut, |
| 1000 | .make_ptr_const, | |
| 996 | 1001 | .array_cat, |
| 997 | 1002 | .array_mul, |
| 998 | 1003 | .array_type, |
| ... | ... | @@ -1496,6 +1501,7 @@ pub const Inst = struct { |
| 1496 | 1501 | .alloc_inferred_comptime = .node, |
| 1497 | 1502 | .alloc_inferred_comptime_mut = .node, |
| 1498 | 1503 | .resolve_inferred_alloc = .un_node, |
| 1504 | .make_ptr_const = .un_node, | |
| 1499 | 1505 | |
| 1500 | 1506 | .@"resume" = .un_node, |
| 1501 | 1507 | .@"await" = .un_node, |
src/print_zir.zig+1| ... | ... | @@ -238,6 +238,7 @@ const Writer = struct { |
| 238 | 238 | .field_base_ptr, |
| 239 | 239 | .validate_array_init_ty, |
| 240 | 240 | .validate_struct_init_ty, |
| 241 | .make_ptr_const, | |
| 241 | 242 | => try self.writeUnNode(stream, inst), |
| 242 | 243 | |
| 243 | 244 | .ref, |
test/behavior/bugs/5474.zig-2| ... | ... | @@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" { |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | test "pointer-to-array constness for zero-size elements, const" { |
| 57 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | |
| 58 | ||
| 59 | 57 | try constant(); |
| 60 | 58 | comptime try constant(); |
| 61 | 59 | } |