fix: do not take the address of a comptime variable

Taking the address of a comptime variable is illegal and will soon lead to a
compile error in zig nightly. Taking the address of a const is ok.
This commit is contained in:
CJ van den Berg 2024-03-26 21:03:01 +01:00
parent e315c10d63
commit 106039c3ae

View file

@ -67,9 +67,10 @@ fn get_parser(comptime lang: []const u8) LangFn {
} }
fn ft_func_name(comptime lang: []const u8) []const u8 { fn ft_func_name(comptime lang: []const u8) []const u8 {
var func_name: [lang.len]u8 = undefined; var transform: [lang.len]u8 = undefined;
for (lang, 0..) |c, i| for (lang, 0..) |c, i|
func_name[i] = if (c == '-') '_' else c; transform[i] = if (c == '-') '_' else c;
const func_name = transform;
return &func_name; return &func_name;
} }
@ -98,7 +99,7 @@ fn vec(comptime args: anytype) []const []const u8 {
return cmd; return cmd;
} }
fn load_file_types(comptime Namespace: type) []FileType { fn load_file_types(comptime Namespace: type) []const FileType {
comptime switch (@typeInfo(Namespace)) { comptime switch (@typeInfo(Namespace)) {
.Struct => |info| { .Struct => |info| {
var count = 0; var count = 0;
@ -106,12 +107,12 @@ fn load_file_types(comptime Namespace: type) []FileType {
// @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name))); // @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name)));
count += 1; count += 1;
} }
var types: [count]FileType = undefined; var construct_types: [count]FileType = undefined;
var i = 0; var i = 0;
for (info.decls) |decl| { for (info.decls) |decl| {
const lang = decl.name; const lang = decl.name;
const args = @field(Namespace, lang); const args = @field(Namespace, lang);
types[i] = .{ construct_types[i] = .{
.color = if (@hasField(@TypeOf(args), "color")) args.color else 0xffffff, .color = if (@hasField(@TypeOf(args), "color")) args.color else 0xffffff,
.icon = if (@hasField(@TypeOf(args), "icon")) args.icon else "󱀫", .icon = if (@hasField(@TypeOf(args), "icon")) args.icon else "󱀫",
.name = lang, .name = lang,
@ -125,6 +126,7 @@ fn load_file_types(comptime Namespace: type) []FileType {
}; };
i += 1; i += 1;
} }
const types = construct_types;
return &types; return &types;
}, },
else => @compileError("expected tuple or struct type"), else => @compileError("expected tuple or struct type"),