Skip to content

Commit

Permalink
AstGen: add error for redundant comptime var in comptime scope (zigla…
Browse files Browse the repository at this point in the history
  • Loading branch information
wrongnull authored Jan 10, 2024
1 parent 157cdae commit 4a1a5ee
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/std/json/scanner_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const number_test_stems = .{
.{ "", "e0", "E0", "e+0", "e-0", "e9999999999999999999999999999" },
};
const number_test_items = blk: {
comptime var ret: []const []const u8 = &[_][]const u8{};
var ret: []const []const u8 = &[_][]const u8{};
for (number_test_stems[0]) |s0| {
for (number_test_stems[1]) |s1| {
for (number_test_stems[2]) |s2| {
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 @@ -20,7 +20,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;
var fields: [bit_count]Type.StructField = undefined;
for (@typeInfo(Fields).Struct.fields, 0..) |struct_field, i| {
fields[i] = Type.StructField{
.name = struct_field.name,
Expand Down
2 changes: 2 additions & 0 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,8 @@ fn varDecl(
return &sub_scope.base;
},
.keyword_var => {
if (var_decl.comptime_token != null and gz.is_comptime)
return astgen.failTok(var_decl.comptime_token.?, "'comptime var' is redundant in comptime scope", .{});
const is_comptime = var_decl.comptime_token != null or gz.is_comptime;
var resolve_inferred_alloc: Zir.Inst.Ref = .none;
const alloc: Zir.Inst.Ref, const result_info: ResultInfo = if (var_decl.ast.type_node != 0) a: {
Expand Down
7 changes: 7 additions & 0 deletions test/cases/compile_errors/redundant_comptime_var.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
comptime {
comptime var x = undefined;
}

// error
//
// :2:5: error: 'comptime var' is redundant in comptime scope

0 comments on commit 4a1a5ee

Please sign in to comment.