authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-14 17:29:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-18 19:51:18-07:00
log77fdd76c16196441dce0f38ccea2eae01436c4be
tree3300be415c21a5b5903d2cf6e5893abb2a19f5b9
parent1e207f1edd500707032be093a5c8a91f0e16609a

std: fix uses of comptime blocks in non-inline functions

ccf670c made using `return` from within a comptime block in a non-inline function illegal, since it is a use of runtime control flow in a comptime block. It is allowed if the function in question is `inline`, since no actual control flow occurs in this case. A few functions from std (notably `std.fmt.comptimePrint`) needed to be marked `inline` to support this change.

4 files changed, 10 insertions(+), 13 deletions(-)

lib/std/enums.zig+1-1
......@@ -32,7 +32,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
3232/// Looks up the supplied fields in the given enum type.
3333/// Uses only the field names, field values are ignored.
3434/// The result array is in the same order as the input.
35pub fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) []const E {
35pub inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) []const E {
3636 comptime {
3737 var result: [fields.len]E = undefined;
3838 for (fields, 0..) |f, i| {
lib/std/fmt.zig+3-3
......@@ -2005,7 +2005,7 @@ pub fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, case: Case, optio
20052005 return buf[0..formatIntBuf(buf, value, base, case, options)];
20062006}
20072007
2008pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
2008pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
20092009 comptime {
20102010 var buf: [count(fmt, args):0]u8 = undefined;
20112011 _ = bufPrint(&buf, fmt, args) catch unreachable;
......@@ -2016,8 +2016,8 @@ pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt,
20162016
20172017test "comptimePrint" {
20182018 @setEvalBranchQuota(2000);
2019 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));
2020 try std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
2019 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
2020 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
20212021}
20222022
20232023test "parse u64 digit too big" {
lib/std/mem.zig+1-2
......@@ -3492,8 +3492,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
34923492 if (comptime !trait.is(.Pointer)(B) or
34933493 (meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8))
34943494 {
3495 comptime var buf: [100]u8 = undefined;
3496 @compileError(std.fmt.bufPrint(&buf, "expected *[{}]u8, passed " ++ @typeName(B), .{size}) catch unreachable);
3495 @compileError(std.fmt.comptimePrint("expected *[{}]u8, passed " ++ @typeName(B), .{size}));
34973496 }
34983497
34993498 return CopyPtrAttrs(B, .One, T);
lib/std/os/windows/user32.zig+5-7
......@@ -28,13 +28,11 @@ const POINT = windows.POINT;
2828const HCURSOR = windows.HCURSOR;
2929const HBRUSH = windows.HBRUSH;
3030
31fn selectSymbol(comptime function_static: anytype, function_dynamic: *const @TypeOf(function_static), comptime os: std.Target.Os.WindowsVersion) *const @TypeOf(function_static) {
32 comptime {
33 const sym_ok = builtin.os.isAtLeast(.windows, os);
34 if (sym_ok == true) return function_static;
35 if (sym_ok == null) return function_dynamic;
36 if (sym_ok == false) @compileError("Target OS range does not support function, at least " ++ @tagName(os) ++ " is required");
37 }
31inline fn selectSymbol(comptime function_static: anytype, function_dynamic: *const @TypeOf(function_static), comptime os: std.Target.Os.WindowsVersion) *const @TypeOf(function_static) {
32 const sym_ok = comptime builtin.os.isAtLeast(.windows, os);
33 if (sym_ok == true) return function_static;
34 if (sym_ok == null) return function_dynamic;
35 if (sym_ok == false) @compileError("Target OS range does not support function, at least " ++ @tagName(os) ++ " is required");
3836}
3937
4038// === Messages ===