mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
AstGen: add error for using inline loops in comptime only scopes
This commit is contained in:
parent
d270020114
commit
69195d0cd4
2
deps/aro/aro/Attribute.zig
vendored
2
deps/aro/aro/Attribute.zig
vendored
@ -643,7 +643,7 @@ pub const Tag = std.meta.DeclEnum(attributes);
|
|||||||
pub const Arguments = blk: {
|
pub const Arguments = blk: {
|
||||||
const decls = @typeInfo(attributes).Struct.decls;
|
const decls = @typeInfo(attributes).Struct.decls;
|
||||||
var union_fields: [decls.len]ZigType.UnionField = undefined;
|
var union_fields: [decls.len]ZigType.UnionField = undefined;
|
||||||
inline for (decls, &union_fields) |decl, *field| {
|
for (decls, &union_fields) |decl, *field| {
|
||||||
field.* = .{
|
field.* = .{
|
||||||
.name = decl.name,
|
.name = decl.name,
|
||||||
.type = @field(attributes, decl.name),
|
.type = @field(attributes, decl.name),
|
||||||
|
|||||||
@ -224,7 +224,7 @@ test "test vectors" {
|
|||||||
|
|
||||||
test "test vectors at comptime" {
|
test "test vectors at comptime" {
|
||||||
comptime {
|
comptime {
|
||||||
inline for (vectors) |e| {
|
for (vectors) |e| {
|
||||||
try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));
|
try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
|
|||||||
const kvs = comptime build_kvs: {
|
const kvs = comptime build_kvs: {
|
||||||
const EnumKV = struct { []const u8, T };
|
const EnumKV = struct { []const u8, T };
|
||||||
var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined;
|
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) };
|
kvs_array[i] = .{ enumField.name, @field(T, enumField.name) };
|
||||||
}
|
}
|
||||||
break :build_kvs kvs_array[0..];
|
break :build_kvs kvs_array[0..];
|
||||||
|
|||||||
@ -21,7 +21,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
|
|||||||
pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);
|
pub const ActiveFields = std.enums.EnumFieldStruct(FieldEnum, bool, false);
|
||||||
pub const FieldValues = blk: {
|
pub const FieldValues = blk: {
|
||||||
comptime var fields: [bit_count]Type.StructField = undefined;
|
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{
|
fields[i] = Type.StructField{
|
||||||
.name = struct_field.name,
|
.name = struct_field.name,
|
||||||
.type = ?struct_field.type,
|
.type = ?struct_field.type,
|
||||||
|
|||||||
@ -280,7 +280,7 @@ fn detectAbiAndDynamicLinker(
|
|||||||
assert(@intFromEnum(Target.Abi.none) == 0);
|
assert(@intFromEnum(Target.Abi.none) == 0);
|
||||||
const fields = std.meta.fields(Target.Abi)[1..];
|
const fields = std.meta.fields(Target.Abi)[1..];
|
||||||
var array: [fields.len]Target.Abi = undefined;
|
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);
|
array[i] = @field(Target.Abi, field.name);
|
||||||
}
|
}
|
||||||
break :blk array;
|
break :blk array;
|
||||||
|
|||||||
@ -6306,6 +6306,9 @@ fn whileExpr(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const is_inline = while_full.inline_token != null;
|
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_tag: Zir.Inst.Tag = if (is_inline) .block_inline else .loop;
|
||||||
const loop_block = try parent_gz.makeBlockInst(loop_tag, node);
|
const loop_block = try parent_gz.makeBlockInst(loop_tag, node);
|
||||||
try parent_gz.instructions.append(astgen.gpa, loop_block);
|
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 need_result_rvalue = @as(LocTag, block_ri.rl) != @as(LocTag, ri.rl);
|
||||||
|
|
||||||
const is_inline = for_full.inline_token != null;
|
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 tree = astgen.tree;
|
||||||
const token_tags = tree.tokens.items(.tag);
|
const token_tags = tree.tokens.items(.tag);
|
||||||
const node_tags = tree.nodes.items(.tag);
|
const node_tags = tree.nodes.items(.tag);
|
||||||
|
|||||||
@ -26,22 +26,22 @@ pub const Register = enum(u8) {
|
|||||||
/// x86 has. It is embedded in some instructions, such as the `B8 +rd` move
|
/// x86 has. It is embedded in some instructions, such as the `B8 +rd` move
|
||||||
/// instruction, and is used in the R/M byte.
|
/// instruction, and is used in the R/M byte.
|
||||||
pub fn id(self: Register) u3 {
|
pub fn id(self: Register) u3 {
|
||||||
return @truncate(u3, @intFromEnum(self));
|
return @truncate(@intFromEnum(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert from any register to its 32 bit alias.
|
/// Convert from any register to its 32 bit alias.
|
||||||
pub fn to32(self: Register) Register {
|
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.
|
/// Convert from any register to its 16 bit alias.
|
||||||
pub fn to16(self: Register) Register {
|
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.
|
/// Convert from any register to its 8 bit alias.
|
||||||
pub fn to8(self: Register) Register {
|
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 {
|
pub fn dwarfLocOp(reg: Register) u8 {
|
||||||
|
|||||||
7
test/cases/compile_errors/redundant_inline.zig
Normal file
7
test/cases/compile_errors/redundant_inline.zig
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
comptime {
|
||||||
|
inline for ("foo") |_| {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// error
|
||||||
|
//
|
||||||
|
// :2:5: error: redundant inline keyword in comptime scope
|
||||||
@ -908,11 +908,11 @@ const TestManifestConfigDefaults = struct {
|
|||||||
// TODO we should also specify ABIs explicitly as the backends are
|
// TODO we should also specify ABIs explicitly as the backends are
|
||||||
// getting more and more complete
|
// getting more and more complete
|
||||||
// Linux
|
// Linux
|
||||||
inline for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {
|
for (&[_][]const u8{ "x86_64", "arm", "aarch64" }) |arch| {
|
||||||
defaults = defaults ++ arch ++ "-linux" ++ ",";
|
defaults = defaults ++ arch ++ "-linux" ++ ",";
|
||||||
}
|
}
|
||||||
// macOS
|
// macOS
|
||||||
inline for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {
|
for (&[_][]const u8{ "x86_64", "aarch64" }) |arch| {
|
||||||
defaults = defaults ++ arch ++ "-macos" ++ ",";
|
defaults = defaults ++ arch ++ "-macos" ++ ",";
|
||||||
}
|
}
|
||||||
// Windows
|
// Windows
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user