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...@@ -32,7 +32,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
32/// Looks up the supplied fields in the given enum type.32/// Looks up the supplied fields in the given enum type.
33/// Uses only the field names, field values are ignored.33/// Uses only the field names, field values are ignored.
34/// The result array is in the same order as the input.34/// 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 {
36 comptime {36 comptime {
37 var result: [fields.len]E = undefined;37 var result: [fields.len]E = undefined;
38 for (fields, 0..) |f, i| {38 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...@@ -2005,7 +2005,7 @@ pub fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, case: Case, optio
2005 return buf[0..formatIntBuf(buf, value, base, case, options)];2005 return buf[0..formatIntBuf(buf, value, base, case, options)];
2006}2006}
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 {
2009 comptime {2009 comptime {
2010 var buf: [count(fmt, args):0]u8 = undefined;2010 var buf: [count(fmt, args):0]u8 = undefined;
2011 _ = bufPrint(&buf, fmt, args) catch unreachable;2011 _ = bufPrint(&buf, fmt, args) catch unreachable;
...@@ -2016,8 +2016,8 @@ pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt,...@@ -2016,8 +2016,8 @@ pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt,
20162016
2017test "comptimePrint" {2017test "comptimePrint" {
2018 @setEvalBranchQuota(2000);2018 @setEvalBranchQuota(2000);
2019 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));2019 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
2020 try std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));2020 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
2021}2021}
20222022
2023test "parse u64 digit too big" {2023test "parse u64 digit too big" {
lib/std/mem.zig+1-2
...@@ -3492,8 +3492,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {...@@ -3492,8 +3492,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
3492 if (comptime !trait.is(.Pointer)(B) or3492 if (comptime !trait.is(.Pointer)(B) or
3493 (meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8))3493 (meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8))
3494 {3494 {
3495 comptime var buf: [100]u8 = undefined;3495 @compileError(std.fmt.comptimePrint("expected *[{}]u8, passed " ++ @typeName(B), .{size}));
3496 @compileError(std.fmt.bufPrint(&buf, "expected *[{}]u8, passed " ++ @typeName(B), .{size}) catch unreachable);
3497 }3496 }
34983497
3499 return CopyPtrAttrs(B, .One, T);3498 return CopyPtrAttrs(B, .One, T);
lib/std/os/windows/user32.zig+5-7
...@@ -28,13 +28,11 @@ const POINT = windows.POINT;...@@ -28,13 +28,11 @@ const POINT = windows.POINT;
28const HCURSOR = windows.HCURSOR;28const HCURSOR = windows.HCURSOR;
29const HBRUSH = windows.HBRUSH;29const 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) {31inline fn selectSymbol(comptime function_static: anytype, function_dynamic: *const @TypeOf(function_static), comptime os: std.Target.Os.WindowsVersion) *const @TypeOf(function_static) {
32 comptime {32 const sym_ok = comptime builtin.os.isAtLeast(.windows, os);
33 const sym_ok = builtin.os.isAtLeast(.windows, os);33 if (sym_ok == true) return function_static;
34 if (sym_ok == true) return function_static;34 if (sym_ok == null) return function_dynamic;
35 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");
36 if (sym_ok == false) @compileError("Target OS range does not support function, at least " ++ @tagName(os) ++ " is required");
37 }
38}36}
3937
40// === Messages ===38// === Messages ===