authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-02-18 13:17:48+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-04-07 15:07:55+02:00
log9181ecd95133b42da75c6c5e8456830d93a9f7a9
tree5215d467bf8cdefc366efdfce018e7978361b13d
parent129de47a71b954b1118ce188f7032ad726491f53

Sema: fix runtime call of inline fn with comptime-known comptime-only ret type


3 files changed, 18 insertions(+), 4 deletions(-)

lib/std/math/nextafter.zig+1-1
...@@ -144,7 +144,7 @@ test "int" {...@@ -144,7 +144,7 @@ test "int" {
144}144}
145145
146test "float" {146test "float" {
147 @setEvalBranchQuota(2000);147 @setEvalBranchQuota(3000);
148148
149 // normal -> normal149 // normal -> normal
150 try expect(nextAfter(f16, 0x1.234p0, 2.0) == 0x1.238p0);150 try expect(nextAfter(f16, 0x1.234p0, 2.0) == 0x1.238p0);
src/Sema.zig+3-3
...@@ -7525,10 +7525,12 @@ fn analyzeCall(...@@ -7525,10 +7525,12 @@ fn analyzeCall(
75257525
7526 var is_generic_call = func_ty_info.is_generic;7526 var is_generic_call = func_ty_info.is_generic;
7527 var is_comptime_call = block.is_comptime or modifier == .compile_time;7527 var is_comptime_call = block.is_comptime or modifier == .compile_time;
7528 var is_inline_call = is_comptime_call or modifier == .always_inline or func_ty_info.cc == .Inline;
7528 var comptime_reason: ?*const Block.ComptimeReason = null;7529 var comptime_reason: ?*const Block.ComptimeReason = null;
7529 if (!is_comptime_call) {7530 if (!is_inline_call and !is_comptime_call) {
7530 if (sema.typeRequiresComptime(Type.fromInterned(func_ty_info.return_type))) |ct| {7531 if (sema.typeRequiresComptime(Type.fromInterned(func_ty_info.return_type))) |ct| {
7531 is_comptime_call = ct;7532 is_comptime_call = ct;
7533 is_inline_call = ct;
7532 if (ct) {7534 if (ct) {
7533 comptime_reason = &.{ .comptime_ret_ty = .{7535 comptime_reason = &.{ .comptime_ret_ty = .{
7534 .block = block,7536 .block = block,
...@@ -7542,8 +7544,6 @@ fn analyzeCall(...@@ -7542,8 +7544,6 @@ fn analyzeCall(
7542 else => |e| return e,7544 else => |e| return e,
7543 }7545 }
7544 }7546 }
7545 var is_inline_call = is_comptime_call or modifier == .always_inline or
7546 func_ty_info.cc == .Inline;
75477547
7548 if (sema.func_is_naked and !is_inline_call and !is_comptime_call) {7548 if (sema.func_is_naked and !is_inline_call and !is_comptime_call) {
7549 const msg = msg: {7549 const msg = msg: {
test/behavior/fn.zig+14
...@@ -604,3 +604,17 @@ test "comptime parameters don't have to be marked comptime if only called at com...@@ -604,3 +604,17 @@ test "comptime parameters don't have to be marked comptime if only called at com
604 };604 };
605 comptime std.debug.assert(S.foo(5, 6) == 11);605 comptime std.debug.assert(S.foo(5, 6) == 11);
606}606}
607
608test "inline function with comptime-known comptime-only return type called at runtime" {
609 const S = struct {
610 inline fn foo(x: *i32, y: *const i32) type {
611 x.* = y.*;
612 return f32;
613 }
614 };
615 var a: i32 = 0;
616 const b: i32 = 111;
617 const T = S.foo(&a, &b);
618 try expectEqual(111, a);
619 try expectEqual(f32, T);
620}