authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-14 22:32:31+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-23 13:16:42-07:00
log35d82d31be3d2f2611049f41dc2616f898d70871
tree9dfb2332aca1201afb67aea6bf83a9e77bf05c9f
parent42ee364e7b698822a69cba4cd2bda17868657e05

Add `@inComptime` builtin

Resolves: #868

9 files changed, 53 insertions(+), 17 deletions(-)

doc/langref.html.in+11
......@@ -8587,6 +8587,17 @@ test "@hasDecl" {
85878587 {#see_also|Compile Variables|@embedFile#}
85888588 {#header_close#}
85898589
8590 {#header_open|@inComptime#}
8591 <pre>{#syntax#}@inComptime() bool{#endsyntax#}</pre>
8592 <p>
8593 Returns whether the builtin was run in a {#syntax#}comptime{#endsyntax#} context. The result is a compile-time constant.
8594 </p>
8595 <p>
8596 This can be used to provide alternative, comptime-friendly implementations of functions. It should not be used, for instance, to exclude certain functions from being evaluated at comptime.
8597 </p>
8598 {#see_also|comptime#}
8599 {#header_close#}
8600
85908601 {#header_open|@intCast#}
85918602 <pre>{#syntax#}@intCast(comptime DestType: type, int: anytype) DestType{#endsyntax#}</pre>
85928603 <p>
lib/std/crypto/sha2.zig+1-7
......@@ -71,12 +71,6 @@ const Sha256Params = Sha2Params32{
7171
7272const v4u32 = @Vector(4, u32);
7373
74// TODO: Remove once https://github.com/ziglang/zig/issues/868 is resolved.
75fn isComptime() bool {
76 var a: u8 = 0;
77 return @typeInfo(@TypeOf(.{a})).Struct.fields[0].is_comptime;
78}
79
8074/// SHA-224
8175pub const Sha224 = Sha2x32(Sha224Params);
8276
......@@ -203,7 +197,7 @@ fn Sha2x32(comptime params: Sha2Params32) type {
203197 s[i] = mem.readIntBig(u32, mem.asBytes(elem));
204198 }
205199
206 if (!isComptime()) {
200 if (!@inComptime()) {
207201 switch (builtin.cpu.arch) {
208202 .aarch64 => if (builtin.zig_backend != .stage2_c and comptime std.Target.aarch64.featureSetHas(builtin.cpu.features, .sha2)) {
209203 var x: v4u32 = d.s[0..4].*;
lib/std/fmt.zig+1-8
......@@ -1428,8 +1428,7 @@ pub fn formatInt(
14281428 var a: MinInt = abs_value;
14291429 var index: usize = buf.len;
14301430
1431 // TODO isComptime here because of https://github.com/ziglang/zig/issues/13335.
1432 if (base == 10 and !isComptime()) {
1431 if (base == 10) {
14331432 while (a >= 100) : (a = @divTrunc(a, 100)) {
14341433 index -= 2;
14351434 buf[index..][0..2].* = digits2(@intCast(usize, a % 100));
......@@ -1469,12 +1468,6 @@ pub fn formatInt(
14691468 return formatBuf(buf[index..], options, writer);
14701469}
14711470
1472// TODO: Remove once https://github.com/ziglang/zig/issues/868 is resolved.
1473fn isComptime() bool {
1474 var a: u8 = 0;
1475 return @typeInfo(@TypeOf(.{a})).Struct.fields[0].is_comptime;
1476}
1477
14781471pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, case: Case, options: FormatOptions) usize {
14791472 var fbs = std.io.fixedBufferStream(out_buf);
14801473 formatInt(value, base, case, options, fbs.writer()) catch unreachable;
src/AstGen.zig+1
......@@ -8174,6 +8174,7 @@ fn builtinCall(
81748174 .frame => return rvalue(gz, ri, try gz.addNodeExtended(.frame, node), node),
81758175 .frame_address => return rvalue(gz, ri, try gz.addNodeExtended(.frame_address, node), node),
81768176 .breakpoint => return rvalue(gz, ri, try gz.addNodeExtended(.breakpoint, node), node),
8177 .in_comptime => return rvalue(gz, ri, try gz.addNodeExtended(.in_comptime, node), node),
81778178
81788179 .type_info => return simpleUnOpType(gz, scope, ri, node, params[0], .type_info),
81798180 .size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .size_of),
src/BuiltinFn.zig+8
......@@ -58,6 +58,7 @@ pub const Tag = enum {
5858 has_decl,
5959 has_field,
6060 import,
61 in_comptime,
6162 int_cast,
6263 int_to_enum,
6364 int_to_error,
......@@ -560,6 +561,13 @@ pub const list = list: {
560561 .param_count = 1,
561562 },
562563 },
564 .{
565 "@inComptime",
566 .{
567 .tag = .in_comptime,
568 .param_count = 0,
569 },
570 },
563571 .{
564572 "@intCast",
565573 .{
src/Sema.zig+13
......@@ -1166,6 +1166,7 @@ fn analyzeBodyInner(
11661166 .work_item_id => try sema.zirWorkItem( block, extended, extended.opcode),
11671167 .work_group_size => try sema.zirWorkItem( block, extended, extended.opcode),
11681168 .work_group_id => try sema.zirWorkItem( block, extended, extended.opcode),
1169 .in_comptime => try sema.zirInComptime( block),
11691170 // zig fmt: on
11701171
11711172 .fence => {
......@@ -22466,6 +22467,18 @@ fn zirWorkItem(
2246622467 });
2246722468}
2246822469
22470fn zirInComptime(
22471 sema: *Sema,
22472 block: *Block,
22473) CompileError!Air.Inst.Ref {
22474 _ = sema;
22475 if (block.is_comptime) {
22476 return Air.Inst.Ref.bool_true;
22477 } else {
22478 return Air.Inst.Ref.bool_false;
22479 }
22480}
22481
2246922482fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
2247022483 if (block.is_comptime) {
2247122484 const msg = msg: {
src/Zir.zig+5-2
......@@ -1994,10 +1994,10 @@ pub const Inst = struct {
19941994 /// Implement builtin `@cVaArg`.
19951995 /// `operand` is payload index to `BinNode`.
19961996 c_va_arg,
1997 /// Implement builtin `@cVaStart`.
1997 /// Implement builtin `@cVaCopy`.
19981998 /// `operand` is payload index to `UnNode`.
19991999 c_va_copy,
2000 /// Implement builtin `@cVaStart`.
2000 /// Implement builtin `@cVaEnd`.
20012001 /// `operand` is payload index to `UnNode`.
20022002 c_va_end,
20032003 /// Implement builtin `@cVaStart`.
......@@ -2018,6 +2018,9 @@ pub const Inst = struct {
20182018 /// Implements the `@workGroupId` builtin.
20192019 /// `operand` is payload index to `UnNode`.
20202020 work_group_id,
2021 /// Implements the `@inComptime` builtin.
2022 /// `operand` is `src_node: i32`.
2023 in_comptime,
20212024
20222025 pub const InstData = struct {
20232026 opcode: Extended,
src/print_zir.zig+1
......@@ -466,6 +466,7 @@ const Writer = struct {
466466 .frame_address,
467467 .breakpoint,
468468 .c_va_start,
469 .in_comptime,
469470 => try self.writeExtNode(stream, extended),
470471
471472 .builtin_src => {
test/behavior/eval.zig+12
......@@ -1649,3 +1649,15 @@ test "early exit in container level const" {
16491649 };
16501650 try expect(S.value == 1);
16511651}
1652
1653test "@inComptime" {
1654 const S = struct {
1655 fn inComptime() bool {
1656 return @inComptime();
1657 }
1658 };
1659 try expectEqual(false, @inComptime());
1660 try expectEqual(true, comptime @inComptime());
1661 try expectEqual(false, S.inComptime());
1662 try expectEqual(true, comptime S.inComptime());
1663}