From 7929bd7938f79f6d8bdd4cbcf43ac095096d3b5c Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 4 Aug 2023 02:05:48 -0700 Subject: [PATCH] update for zig 0.11.0 --- .github/workflows/zig.yml | 2 +- README.md | 4 +-- build.zig | 9 +++--- deps.zig | 64 +++++++++++++++++++++++---------------- src/main.zig | 10 +++--- src/rules/dupe_import.zig | 2 +- src/rules/unused.zig | 11 ++++--- zig.mod | 2 +- zigmod.lock | 4 +-- 9 files changed, 60 insertions(+), 48 deletions(-) diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml index 3056173..8ba43d8 100644 --- a/.github/workflows/zig.yml +++ b/.github/workflows/zig.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Zig uses: goto-bus-stop/setup-zig@v2 with: - version: "0.11.0-dev.1570+693b12f8e" + version: "0.11.0" - run: zig version - run: zig env diff --git a/README.md b/README.md index 6d9e457..e80cf12 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ $ zigmod aq install 1/nektro/ziglint ``` ## Built With -- Zig master `0.11.0-dev.1570+693b12f8e` +- Zig master `0.11.0` - [Zigmod](https://github.com/nektro/zigmod) package manager - See [`zig.mod`](./zig.mod) @@ -46,7 +46,7 @@ jobs: - name: Setup Zig uses: goto-bus-stop/setup-zig@v1 with: - version: "0.11.0-dev.1570+693b12f8e" + version: "0.11.0" - uses: nektro/actions-setup-zigmod@v1 - run: zigmod aq install 1/nektro/ziglint diff --git a/build.zig b/build.zig index bdeec6d..98a8110 100644 --- a/build.zig +++ b/build.zig @@ -3,14 +3,13 @@ const deps = @import("./deps.zig"); pub fn build(b: *std.build.Builder) void { const target = b.standardTargetOptions(.{}); + const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug; - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ .name = "ziglint", .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize }); + const exe = b.addExecutable(.{ .name = "ziglint", .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = mode }); deps.addAllTo(exe); - exe.install(); + b.installArtifact(exe); - const run_cmd = exe.run(); + const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); diff --git a/deps.zig b/deps.zig index 1c6d0b7..3f21652 100644 --- a/deps.zig +++ b/deps.zig @@ -13,7 +13,12 @@ pub const GitExactStep = struct { pub fn create(b: *std.build.Builder, url: string, commit: string) *GitExactStep { var result = b.allocator.create(GitExactStep) catch @panic("memory"); result.* = GitExactStep{ - .step = std.build.Step.init(.custom, b.fmt("git clone {s} @ {s}", .{ url, commit }), b.allocator, make), + .step = std.build.Step.init(.{ + .id = .custom, + .name = b.fmt("git clone {s} @ {s}", .{ url, commit }), + .owner = b, + .makeFn = make, + }), .builder = b, .url = url, .commit = commit, @@ -22,7 +27,7 @@ pub const GitExactStep = struct { var urlpath = url; urlpath = trimPrefix(u8, urlpath, "https://"); urlpath = trimPrefix(u8, urlpath, "git://"); - const repopath = b.fmt("{s}/zigmod/deps/git/{s}/{s}", .{ b.cache_root, urlpath, commit }); + const repopath = b.fmt("{s}/zigmod/deps/git/{s}/{s}", .{ b.cache_root.path.?, urlpath, commit }); flip(std.fs.cwd().access(repopath, .{})) catch return result; var clonestep = std.build.RunStep.create(b, "clone"); @@ -32,24 +37,26 @@ pub const GitExactStep = struct { var checkoutstep = std.build.RunStep.create(b, "checkout"); checkoutstep.addArgs(&.{ "git", "-C", repopath, "checkout", "-q", commit }); result.step.dependOn(&checkoutstep.step); + checkoutstep.step.dependOn(&clonestep.step); return result; } - fn make(step: *std.build.Step) !void { + fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void { _ = step; + _ = prog_node; } }; pub fn fetch(exe: *std.build.LibExeObjStep) void { - const b = exe.builder; + const b = exe.step.owner; inline for (comptime std.meta.declarations(package_data)) |decl| { const path = &@field(package_data, decl.name).entry; - const root = if (@field(package_data, decl.name).store) |_| b.cache_root else "."; + const root = if (@field(package_data, decl.name).store) |_| b.cache_root.path.? else "."; if (path.* != null) path.* = b.fmt("{s}/zigmod/deps{s}", .{ root, path.*.? }); } - exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-extras", "3fe700ebc8ff66966abe145166bfdf546b3a8422").step); - exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-flag", "07ea6a3daa950f7bbd8bbd60c0cc2251806fde95").step); + exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-extras", "8628846c832086d97f28c54eab157cda389c319a").step); + exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-flag", "4a03000239d5e05062cf9344a08e0dc43ad8ebd1").step); exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-range", "4b2f12808aa09be4b27a163efc424dd4e0415992").step); } @@ -68,7 +75,7 @@ fn flip(foo: anytype) !void { pub fn addAllTo(exe: *std.build.LibExeObjStep) void { checkMinZig(builtin.zig_version, exe); fetch(exe); - const b = exe.builder; + const b = exe.step.owner; @setEvalBranchQuota(1_000_000); for (packages) |pkg| { const moddep = pkg.zp(b); @@ -78,22 +85,22 @@ pub fn addAllTo(exe: *std.build.LibExeObjStep) void { var vcpkg = false; inline for (comptime std.meta.declarations(package_data)) |decl| { const pkg = @as(Package, @field(package_data, decl.name)); - const root = if (pkg.store) |st| b.fmt("{s}/zigmod/deps/{s}", .{ b.cache_root, st }) else "."; + const root = if (pkg.store) |st| b.fmt("{s}/zigmod/deps/{s}", .{ b.cache_root.path.?, st }) else "."; for (pkg.system_libs) |item| { exe.linkSystemLibrary(item); llc = true; } for (pkg.frameworks) |item| { - if (!builtin.target.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item})); + if (!builtin.target.isDarwin()) @panic(exe.step.owner.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item})); exe.linkFramework(item); llc = true; } for (pkg.c_include_dirs) |item| { - exe.addIncludePath(b.fmt("{s}/{s}", .{ root, item })); + exe.addIncludePath(.{.path = b.fmt("{s}/{s}", .{ root, item })}); llc = true; } for (pkg.c_source_files) |item| { - exe.addCSourceFile(b.fmt("{s}/{s}", .{ root, item }), pkg.c_source_flags); + exe.addCSourceFile(.{ .file = .{ .path = b.fmt("{s}/{s}", .{ root, item }) }, .flags = pkg.c_source_flags }); llc = true; } vcpkg = vcpkg or pkg.vcpkg; @@ -113,52 +120,57 @@ pub const Package = struct { system_libs: []const string = &.{}, frameworks: []const string = &.{}, vcpkg: bool = false, + module: ?ModuleDependency = null, - pub fn zp(self: *const Package, b: *std.build.Builder) ModuleDependency { + pub fn zp(self: *Package, b: *std.build.Builder) ModuleDependency { var temp: [100]ModuleDependency = undefined; - for (self.deps) |item, i| { + for (self.deps, 0..) |item, i| { temp[i] = item.zp(b); } - return .{ + if (self.module) |mod| { + return mod; + } + const result = ModuleDependency{ .name = self.name, .module = b.createModule(.{ .source_file = .{ .path = self.entry.? }, .dependencies = b.allocator.dupe(ModuleDependency, temp[0..self.deps.len]) catch @panic("oom"), }), }; + self.module = result; + return result; } }; fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void { - const min = std.SemanticVersion.parse("0.11.0-dev.1570+693b12f8e") catch return; - if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min})); + const min = std.SemanticVersion.parse("0.11.0") catch return; + if (current.order(min).compare(.lt)) @panic(exe.step.owner.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min})); } pub const package_data = struct { pub var _8dglro8ootvr = Package{ }; + pub var _f7dubzb7cyqe = Package{ + .store = "/git/github.com/nektro/zig-extras/8628846c832086d97f28c54eab157cda389c319a", + .name = "extras", + .entry = "/git/github.com/nektro/zig-extras/8628846c832086d97f28c54eab157cda389c319a/src/lib.zig", + }; pub var _tnj3qf44tpeq = Package{ .store = "/git/github.com/nektro/zig-range/4b2f12808aa09be4b27a163efc424dd4e0415992", .name = "range", .entry = "/git/github.com/nektro/zig-range/4b2f12808aa09be4b27a163efc424dd4e0415992/src/lib.zig", }; - pub var _f7dubzb7cyqe = Package{ - .store = "/git/github.com/nektro/zig-extras/3fe700ebc8ff66966abe145166bfdf546b3a8422", - .name = "extras", - .entry = "/git/github.com/nektro/zig-extras/3fe700ebc8ff66966abe145166bfdf546b3a8422/src/lib.zig", - .deps = &[_]*Package{ &_tnj3qf44tpeq }, - }; pub var _pm68dn67ppvl = Package{ - .store = "/git/github.com/nektro/zig-flag/07ea6a3daa950f7bbd8bbd60c0cc2251806fde95", + .store = "/git/github.com/nektro/zig-flag/4a03000239d5e05062cf9344a08e0dc43ad8ebd1", .name = "flag", - .entry = "/git/github.com/nektro/zig-flag/07ea6a3daa950f7bbd8bbd60c0cc2251806fde95/src/lib.zig", + .entry = "/git/github.com/nektro/zig-flag/4a03000239d5e05062cf9344a08e0dc43ad8ebd1/src/lib.zig", .deps = &[_]*Package{ &_f7dubzb7cyqe, &_tnj3qf44tpeq }, }; pub var _root = Package{ }; }; -pub const packages = [_]*const Package{ +pub const packages = [_]*Package{ &package_data._tnj3qf44tpeq, &package_data._pm68dn67ppvl, }; diff --git a/src/main.zig b/src/main.zig index f9db49b..36c551c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -23,7 +23,7 @@ const Rule = enum { pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); - defer std.debug.assert(!gpa.deinit()); + defer std.debug.assert(gpa.deinit() == .ok); // @@ -79,7 +79,7 @@ pub fn main() !void { var walker = try dir.walk(alloc); defer walker.deinit(); while (try walker.next()) |item| { - if (item.kind != .File) continue; + if (item.kind != .file) continue; try doFile(alloc, dir.dir, item.path, rulestorun.items, out); } } @@ -110,7 +110,7 @@ fn doFile(alloc: std.mem.Allocator, dir: std.fs.Dir, path: string, rules: []cons }; for (rules) |jtem| { - try linters[@enumToInt(jtem)](alloc2, path, &src, out); + try linters[@intFromEnum(jtem)](alloc2, path, &src, out); } } @@ -131,7 +131,7 @@ pub const Loc = struct { pub fn locToLoc(source: [:0]const u8, loc: std.zig.Token.Loc) Loc { var line: usize = 1; var pos: usize = 1; - for (range(loc.start)) |_, i| { + for (range(loc.start), 0..) |_, i| { pos += 1; if (source[i] != '\n') continue; line += 1; @@ -172,7 +172,7 @@ pub const Source = struct { }; fn removeItem(comptime T: type, haystack: *std.ArrayList(T), needle: T) ?T { - for (haystack.items) |item, i| { + for (haystack.items, 0..) |item, i| { if (item == needle) return haystack.orderedRemove(i); } return null; diff --git a/src/rules/dupe_import.zig b/src/rules/dupe_import.zig index 36b7210..066f8d9 100644 --- a/src/rules/dupe_import.zig +++ b/src/rules/dupe_import.zig @@ -9,7 +9,7 @@ pub fn work(alloc: std.mem.Allocator, file_name: []const u8, src: *main.Source, var map = std.StringHashMap(main.Loc).init(alloc); defer map.deinit(); - for (tokens) |tok, i| { + for (tokens, 0..) |tok, i| { if (i + 4 >= tokens.len) break; const a = tokens[i + 0].tag == .builtin; diff --git a/src/rules/unused.zig b/src/rules/unused.zig index 8f56582..f55f1eb 100644 --- a/src/rules/unused.zig +++ b/src/rules/unused.zig @@ -227,6 +227,7 @@ fn checkValueForName(ast: std.zig.Ast, search_name: string, node: NodeIndex, wri .add_sat, .sub_sat, .shl_sat, + .for_range, => { if (try checkValueForName(ast, search_name, data.lhs, writer, file_name, node)) return true; if (try checkValueForName(ast, search_name, data.rhs, writer, file_name, node)) return true; @@ -299,11 +300,11 @@ fn checkValueForName(ast: std.zig.Ast, search_name: string, node: NodeIndex, wri .sentinel, .elem_type, }), - .@"for" => try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.forFull(node), &.{ - .cond_expr, - .then_expr, - .else_expr, - }), + .@"for" => { + if (try checkValuesForName(ast, search_name, ast.forFull(node).ast.inputs, writer, file_name, node)) return true; + if (try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.forFull(node), &.{ .then_expr, .else_expr })) return true; + return false; + }, .@"while" => try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.whileFull(node), &.{ .cond_expr, .cont_expr, diff --git a/zig.mod b/zig.mod index ff1a071..11c6fc5 100644 --- a/zig.mod +++ b/zig.mod @@ -2,7 +2,7 @@ id: 8dglro8ootvrhtua84qcmx402ywi2dw0d1u8nfx3oc42z8dw name: ziglint license: MIT description: A linting suite for Zig -min_zig_version: 0.11.0-dev.1570+693b12f8e +min_zig_version: 0.11.0 bin: True provides: ["ziglint"] root_dependencies: diff --git a/zigmod.lock b/zigmod.lock index 67cc14f..b289873 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,4 +1,4 @@ 2 -git https://github.com/nektro/zig-extras commit-3fe700ebc8ff66966abe145166bfdf546b3a8422 -git https://github.com/nektro/zig-flag commit-07ea6a3daa950f7bbd8bbd60c0cc2251806fde95 +git https://github.com/nektro/zig-extras commit-8628846c832086d97f28c54eab157cda389c319a +git https://github.com/nektro/zig-flag commit-4a03000239d5e05062cf9344a08e0dc43ad8ebd1 git https://github.com/nektro/zig-range commit-4b2f12808aa09be4b27a163efc424dd4e0415992