Skip to content

Commit

Permalink
AstGen: add error for using inline loops in comptime only scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu authored and andrewrk committed Dec 9, 2023
1 parent d270020 commit 69195d0
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion deps/aro/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/std/hash/wyhash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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..];
Expand Down
2 changes: 1 addition & 1 deletion lib/std/meta/trailer_flags.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/zig/system/NativeTargetInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/arch/x86/bits.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions test/cases/compile_errors/redundant_inline.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
comptime {
inline for ("foo") |_| {}
}

// error
//
// :2:5: error: redundant inline keyword in comptime scope
4 changes: 2 additions & 2 deletions test/src/Cases.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69195d0

Please sign in to comment.