authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-12-08 15:43:14+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-08 16:54:32-08:00
log69195d0cd43468f213332c5792df04c941f8313d
treee46e9a16b56d4cde383570c2629e1999453dad62
parentd2700201149fad95c559741839cc2518ffcafa09

AstGen: add error for using inline loops in comptime only scopes


9 files changed, 24 insertions(+), 11 deletions(-)

deps/aro/aro/Attribute.zig+1-1
...@@ -643,7 +643,7 @@ pub const Tag = std.meta.DeclEnum(attributes);...@@ -643,7 +643,7 @@ pub const Tag = std.meta.DeclEnum(attributes);
643pub const Arguments = blk: {643pub const Arguments = blk: {
644 const decls = @typeInfo(attributes).Struct.decls;644 const decls = @typeInfo(attributes).Struct.decls;
645 var union_fields: [decls.len]ZigType.UnionField = undefined;645 var union_fields: [decls.len]ZigType.UnionField = undefined;
646 inline for (decls, &union_fields) |decl, *field| {646 for (decls, &union_fields) |decl, *field| {
647 field.* = .{647 field.* = .{
648 .name = decl.name,648 .name = decl.name,
649 .type = @field(attributes, decl.name),649 .type = @field(attributes, decl.name),
lib/std/hash/wyhash.zig+1-1
...@@ -224,7 +224,7 @@ test "test vectors" {...@@ -224,7 +224,7 @@ test "test vectors" {
224224
225test "test vectors at comptime" {225test "test vectors at comptime" {
226 comptime {226 comptime {
227 inline for (vectors) |e| {227 for (vectors) |e| {
228 try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));228 try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));
229 }229 }
230 }230 }
lib/std/meta.zig+1-1
...@@ -29,7 +29,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {...@@ -29,7 +29,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
29 const kvs = comptime build_kvs: {29 const kvs = comptime build_kvs: {
30 const EnumKV = struct { []const u8, T };30 const EnumKV = struct { []const u8, T };
31 var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;31 var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
32 inline for (@typeInfo(T).Enum.fields, 0..) |enumField, i| {32 for (@typeInfo(T).Enum.fields, 0..) |enumField, i| {
33 kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };33 kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
34 }34 }
35 break :build_kvs kvs_array[0..];35 break :build_kvs kvs_array[0..];
lib/std/meta/trailer_flags.zig+1-1
...@@ -21,7 +21,7 @@ pub fn TrailerFlags(comptime Fields: type) type {...@@ -21,7 +21,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
21 pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);21 pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);
22 pub const FieldValues = blk: {22 pub const FieldValues = blk: {
23 comptime var fields: [bit_count]Type.StructField = undefined;23 comptime var fields: [bit_count]Type.StructField = undefined;
24 inline for (@typeInfo(Fields).Struct.fields, 0..) |struct_field, i| {24 for (@typeInfo(Fields).Struct.fields, 0..) |struct_field, i| {
25 fields[i] = Type.StructField{25 fields[i] = Type.StructField{
26 .name = struct_field.name,26 .name = struct_field.name,
27 .type = ?struct_field.type,27 .type = ?struct_field.type,
lib/std/zig/system/NativeTargetInfo.zig+1-1
...@@ -280,7 +280,7 @@ fn detectAbiAndDynamicLinker(...@@ -280,7 +280,7 @@ fn detectAbiAndDynamicLinker(
280 assert(@intFromEnum(Target.Abi.none) == 0);280 assert(@intFromEnum(Target.Abi.none) == 0);
281 const fields = std.meta.fields(Target.Abi)[1..];281 const fields = std.meta.fields(Target.Abi)[1..];
282 var array: [fields.len]Target.Abi = undefined;282 var array: [fields.len]Target.Abi = undefined;
283 inline for (fields, 0..) |field, i| {283 for (fields, 0..) |field, i| {
284 array[i] = @field(Target.Abi, field.name);284 array[i] = @field(Target.Abi, field.name);
285 }285 }
286 break :blk array;286 break :blk array;
src/AstGen.zig+6
...@@ -6306,6 +6306,9 @@ fn whileExpr(...@@ -6306,6 +6306,9 @@ fn whileExpr(
6306 }6306 }
63076307
6308 const is_inline = while_full.inline_token != null;6308 const is_inline = while_full.inline_token != null;
6309 if (parent_gz.is_comptime and is_inline) {
6310 return astgen.failTok(while_full.inline_token.?, "redundant inline keyword in comptime scope", .{});
6311 }
6309 const loop_tag: Zir.Inst.Tag = if (is_inline) .block_inline else .loop;6312 const loop_tag: Zir.Inst.Tag = if (is_inline) .block_inline else .loop;
6310 const loop_block = try parent_gz.makeBlockInst(loop_tag, node);6313 const loop_block = try parent_gz.makeBlockInst(loop_tag, node);
6311 try parent_gz.instructions.append(astgen.gpa, loop_block);6314 try parent_gz.instructions.append(astgen.gpa, loop_block);
...@@ -6580,6 +6583,9 @@ fn forExpr(...@@ -6580,6 +6583,9 @@ fn forExpr(
6580 const need_result_rvalue = @as(LocTag, block_ri.rl) != @as(LocTag, ri.rl);6583 const need_result_rvalue = @as(LocTag, block_ri.rl) != @as(LocTag, ri.rl);
65816584
6582 const is_inline = for_full.inline_token != null;6585 const is_inline = for_full.inline_token != null;
6586 if (parent_gz.is_comptime and is_inline) {
6587 return astgen.failTok(for_full.inline_token.?, "redundant inline keyword in comptime scope", .{});
6588 }
6583 const tree = astgen.tree;6589 const tree = astgen.tree;
6584 const token_tags = tree.tokens.items(.tag);6590 const token_tags = tree.tokens.items(.tag);
6585 const node_tags = tree.nodes.items(.tag);6591 const node_tags = tree.nodes.items(.tag);
src/arch/x86/bits.zig+4-4
...@@ -26,22 +26,22 @@ pub const Register = enum(u8) {...@@ -26,22 +26,22 @@ pub const Register = enum(u8) {
26 /// x86 has. It is embedded in some instructions, such as the `B8 +rd` move26 /// x86 has. It is embedded in some instructions, such as the `B8 +rd` move
27 /// instruction, and is used in the R/M byte.27 /// instruction, and is used in the R/M byte.
28 pub fn id(self: Register) u3 {28 pub fn id(self: Register) u3 {
29 return @truncate(u3, @intFromEnum(self));29 return @truncate(@intFromEnum(self));
30 }30 }
3131
32 /// Convert from any register to its 32 bit alias.32 /// Convert from any register to its 32 bit alias.
33 pub fn to32(self: Register) Register {33 pub fn to32(self: Register) Register {
34 return @enumFromInt(Register, @as(u8, self.id()));34 return @enumFromInt(@as(u8, self.id()));
35 }35 }
3636
37 /// Convert from any register to its 16 bit alias.37 /// Convert from any register to its 16 bit alias.
38 pub fn to16(self: Register) Register {38 pub fn to16(self: Register) Register {
39 return @enumFromInt(Register, @as(u8, self.id()) + 8);39 return @enumFromInt(@as(u8, self.id()) + 8);
40 }40 }
4141
42 /// Convert from any register to its 8 bit alias.42 /// Convert from any register to its 8 bit alias.
43 pub fn to8(self: Register) Register {43 pub fn to8(self: Register) Register {
44 return @enumFromInt(Register, @as(u8, self.id()) + 16);44 return @enumFromInt(@as(u8, self.id()) + 16);
45 }45 }
4646
47 pub fn dwarfLocOp(reg: Register) u8 {47 pub fn dwarfLocOp(reg: Register) u8 {
test/cases/compile_errors/redundant_inline.zig created+7
...@@ -0,0 +1,7 @@
1comptime {
2 inline for ("foo") |_| {}
3}
4
5// error
6//
7// :2:5: error: redundant inline keyword in comptime scope
test/src/Cases.zig+2-2
...@@ -908,11 +908,11 @@ const TestManifestConfigDefaults = struct {...@@ -908,11 +908,11 @@ const TestManifestConfigDefaults = struct {
908 // TODO we should also specify ABIs explicitly as the backends are908 // TODO we should also specify ABIs explicitly as the backends are
909 // getting more and more complete909 // getting more and more complete
910 // Linux910 // Linux
911 inline for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {911 for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {
912 defaults = defaults ++ arch ++ "-linux" ++ ",";912 defaults = defaults ++ arch ++ "-linux" ++ ",";
913 }913 }
914 // macOS914 // macOS
915 inline for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {915 for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {
916 defaults = defaults ++ arch ++ "-macos" ++ ",";916 defaults = defaults ++ arch ++ "-macos" ++ ",";
917 }917 }
918 // Windows918 // Windows