From 6f4499c9dc069e8a6d6b6113fc918407f9d577cd Mon Sep 17 00:00:00 2001 From: David Rubin Date: Tue, 9 Jun 2026 18:14:08 -0700 Subject: [PATCH] Sema: copy alignment for try pointer expression --- src/Sema.zig | 13 ++++--------- test/behavior/try.zig | 12 ++++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index a6a49d96c94975fc7782c21ffcb99f5fced19774..650ab8084dc5090e17c0e02d2cdaa6504cebf45c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -17590,15 +17590,10 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr const is_cold = sema.branch_hint == .cold; const operand_ty = sema.typeOf(operand); - const ptr_info = operand_ty.ptrInfo(zcu); - const res_ty = try pt.ptrType(.{ - .child = err_union_ty.errorUnionPayload(zcu).toIntern(), - .flags = .{ - .is_const = ptr_info.flags.is_const, - .is_volatile = ptr_info.flags.is_volatile, - .is_allowzero = ptr_info.flags.is_allowzero, - .address_space = ptr_info.flags.address_space, - }, + const res_ty = try pt.ptrType(info: { + var new = operand_ty.ptrInfo(zcu); + new.child = err_union_ty.errorUnionPayload(zcu).toIntern(); + break :info new; }); const res_ty_ref = Air.internedToRef(res_ty.toIntern()); try sema.air_extra.ensureUnusedCapacity(sema.gpa, @typeInfo(Air.TryPtr).@"struct".field_names.len + diff --git a/test/behavior/try.zig b/test/behavior/try.zig index df852abfa15d9c6618b15dd0ebfbfc23ae880208..649e22d5f94b1e30bd7892816b1ddbeb0da22db3 100644 --- a/test/behavior/try.zig +++ b/test/behavior/try.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const assert = std.debug.assert; const expect = std.testing.expect; test "try on error union" { @@ -197,3 +198,14 @@ test "try ptr propagation mutate" { try S.doTheTest(); try comptime S.doTheTest(); } + +test "try pointer expression alignment" { + const S = struct { + fn doTheTest(p: *align(1) (anyerror!u32)) !void { + comptime assert(@TypeOf(&(try p.*)) == *align(1) u32); + try expect((try p.*) == 10); + } + }; + var x: anyerror!u32 = 10; + try S.doTheTest(&x); +} -- 2.54.0