From a040ccb42f1b34ba612c975b7030ccdcbb3f8086 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 4 Jun 2022 12:20:28 +0300 Subject: Sema: fix coerce result ptr outside of functions --- test/behavior/basic.zig | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/behavior/basic.zig') diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index 3129091091..b45f5a0b49 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const assert = std.debug.assert; const mem = std.mem; const expect = std.testing.expect; const expectEqualStrings = std.testing.expectEqualStrings; @@ -1053,3 +1054,11 @@ test "const alloc with comptime known initializer is made comptime known" { if (u.a == 0) @compileError("bad"); } } + +comptime { + // coerce result ptr outside a function + const S = struct { a: comptime_int }; + var s: S = undefined; + s = S{ .a = 1 }; + assert(s.a == 1); +} -- cgit v1.2.3 From 8fa88c88c28420d89392a9984748070d35f18321 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 5 Jun 2022 20:08:02 +0300 Subject: AstGen: fix coercion scope type when stores are eliminated --- src/AstGen.zig | 3 +++ test/behavior/basic.zig | 12 ++++++++++++ 2 files changed, 15 insertions(+) (limited to 'test/behavior/basic.zig') diff --git a/src/AstGen.zig b/src/AstGen.zig index ab5befa4ba..2fa2960ce1 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -9875,6 +9875,9 @@ const GenZir = struct { errdefer as_scope.unstack(); as_scope.rl_ptr = try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr); + // `rl_ty_inst` needs to be set in case the stores to `rl_ptr` are eliminated. + as_scope.rl_ty_inst = dest_type; + return as_scope; } diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index b45f5a0b49..dc0e5aaf30 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -1062,3 +1062,15 @@ comptime { s = S{ .a = 1 }; assert(s.a == 1); } + +test "switch inside @as gets correct type" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + var a: u32 = 0; + var b: [2]u32 = undefined; + b[0] = @as(u32, switch (a) { + 1 => 1, + else => 0, + }); +} -- cgit v1.2.3 From 84000aa820de2d00571f7e8fde5d3973e2fdc441 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 5 Jun 2022 20:36:56 +0300 Subject: Sema: fix inline call of func using ret_ptr with comptime only type --- src/Sema.zig | 2 +- test/behavior/basic.zig | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'test/behavior/basic.zig') diff --git a/src/Sema.zig b/src/Sema.zig index 015b50ce4b..5d70ba3a7f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2526,7 +2526,7 @@ fn zirRetPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. const src: LazySrcLoc = .{ .node_offset = inst_data }; try sema.requireFunctionBlock(block, src); - if (block.is_comptime) { + if (block.is_comptime or try sema.typeRequiresComptime(block, src, sema.fn_ret_ty)) { const fn_ret_ty = try sema.resolveTypeFields(block, src, sema.fn_ret_ty); return sema.analyzeComptimeAlloc(block, fn_ret_ty, 0, src); } diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index dc0e5aaf30..a69df862c1 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -1074,3 +1074,15 @@ test "switch inside @as gets correct type" { else => 0, }); } + +test "inline call of function with a switch inside the return statement" { + const S = struct { + inline fn foo(x: anytype) @TypeOf(x) { + return switch (x) { + 1 => 1, + else => unreachable, + }; + } + }; + try expect(S.foo(1) == 1); +} -- cgit v1.2.3