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

This commit is contained in:
Veikka Tuominen 2023-12-08 15:43:14 +02:00 committed by Andrew Kelley
parent d270020114
commit 69195d0cd4
9 changed files with 24 additions and 11 deletions

View File

@ -643,7 +643,7 @@ pub const Tag = std.meta.DeclEnum(attributes);
pub const Arguments = blk: {
const decls = @typeInfo(attributes).Struct.decls;
var union_fields: [decls.len]ZigType.UnionField = undefined;
inline for (decls, &union_fields) |decl, *field| {
for (decls, &union_fields) |decl, *field| {
field.* = .{
.name = decl.name,
.type = @field(attributes, decl.name),

View File

@ -224,7 +224,7 @@ test "test vectors" {
test "test vectors at comptime" {
comptime {
inline for (vectors) |e| {
for (vectors) |e| {
try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));
}
}

View File

@ -29,7 +29,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
const kvs = comptime build_kvs: {
const EnumKV = struct { []const u8, T };
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
inline for (@typeInfo(T).Enum.fields, 0..) |enumField, i| {
for (@typeInfo(T).Enum.fields, 0..) |enumField, i| {
kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
}
break :build_kvs kvs_array[0..];

View File

@ -21,7 +21,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);
pub const FieldValues = blk: {
comptime var fields: [bit_count]Type.StructField = undefined;
inline for (@typeInfo(Fields).Struct.fields, 0..) |struct_field, i| {
for (@typeInfo(Fields).Struct.fields, 0..) |struct_field, i| {
fields[i] = Type.StructField{
.name = struct_field.name,
.type = ?struct_field.type,

View File

@ -280,7 +280,7 @@ fn detectAbiAndDynamicLinker(
assert(@intFromEnum(Target.Abi.none) == 0);
const fields = std.meta.fields(Target.Abi)[1..];
var array: [fields.len]Target.Abi = undefined;
inline for (fields, 0..) |field, i| {
for (fields, 0..) |field, i| {
array[i] = @field(Target.Abi, field.name);
}
break :blk array;

View File

@ -6306,6 +6306,9 @@ fn whileExpr(
}
const is_inline = while_full.inline_token != null;
if (parent_gz.is_comptime and is_inline) {
return astgen.failTok(while_full.inline_token.?, "redundant inline keyword in comptime scope", .{});
}
const loop_tag: Zir.Inst.Tag = if (is_inline) .block_inline else .loop;
const loop_block = try parent_gz.makeBlockInst(loop_tag, node);
try parent_gz.instructions.append(astgen.gpa, loop_block);
@ -6580,6 +6583,9 @@ fn forExpr(
const need_result_rvalue = @as(LocTag, block_ri.rl) != @as(LocTag, ri.rl);
const is_inline = for_full.inline_token != null;
if (parent_gz.is_comptime and is_inline) {
return astgen.failTok(for_full.inline_token.?, "redundant inline keyword in comptime scope", .{});
}
const tree = astgen.tree;
const token_tags = tree.tokens.items(.tag);
const node_tags = tree.nodes.items(.tag);

View File

@ -26,22 +26,22 @@ pub const Register = enum(u8) {
/// x86 has. It is embedded in some instructions, such as the `B8 +rd` move
/// instruction, and is used in the R/M byte.
pub fn id(self: Register) u3 {
return @truncate(u3, @intFromEnum(self));
return @truncate(@intFromEnum(self));
}
/// Convert from any register to its 32 bit alias.
pub fn to32(self: Register) Register {
return @enumFromInt(Register, @as(u8, self.id()));
return @enumFromInt(@as(u8, self.id()));
}
/// Convert from any register to its 16 bit alias.
pub fn to16(self: Register) Register {
return @enumFromInt(Register, @as(u8, self.id()) + 8);
return @enumFromInt(@as(u8, self.id()) + 8);
}
/// Convert from any register to its 8 bit alias.
pub fn to8(self: Register) Register {
return @enumFromInt(Register, @as(u8, self.id()) + 16);
return @enumFromInt(@as(u8, self.id()) + 16);
}
pub fn dwarfLocOp(reg: Register) u8 {

View File

@ -0,0 +1,7 @@
comptime {
inline for ("foo") |_| {}
}
// error
//
// :2:5: error: redundant inline keyword in comptime scope

View File

@ -908,11 +908,11 @@ const TestManifestConfigDefaults = struct {
// TODO we should also specify ABIs explicitly as the backends are
// getting more and more complete
// Linux
inline for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {
for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {
defaults = defaults ++ arch ++ "-linux" ++ ",";
}
// macOS
inline for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {
for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {
defaults = defaults ++ arch ++ "-macos" ++ ",";
}
// Windows