Commit c75fdd96d2

Loris Cro <kappaloris@gmail.com>
2025-02-15 20:03:04
`@deprecated`: add tests
1 parent 06a6674
Changed files (3)
src/Sema.zig
@@ -1342,15 +1342,6 @@ fn analyzeBodyInner(
             .extended => ext: {
                 const extended = datas[@intFromEnum(inst)].extended;
                 break :ext switch (extended.opcode) {
-                    .deprecated => {
-                        if (!mod.allow_deprecated) {
-                            const src_node: i32 = @bitCast(extended.operand);
-                            const src = block.nodeOffset(src_node);
-                            return sema.fail(block, src, "found deprecated code", .{});
-                        }
-
-                        break :ext .void_value;
-                    },
                     // zig fmt: off
                     .struct_decl        => try sema.zirStructDecl(        block, extended, inst),
                     .enum_decl          => try sema.zirEnumDecl(          block, extended, inst),
@@ -1414,6 +1405,15 @@ fn analyzeBodyInner(
                         i += 1;
                         continue;
                     },
+                    .deprecated => {
+                        if (!mod.allow_deprecated) {
+                            const src_node: i32 = @bitCast(extended.operand);
+                            const src = block.nodeOffset(src_node);
+                            return sema.fail(block, src, "found deprecated code", .{});
+                        }
+
+                        break :ext .void_value;
+                    },
                     .disable_instrumentation => {
                         try sema.zirDisableInstrumentation();
                         i += 1;
test/compile_errors.zig
@@ -250,4 +250,18 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
             ":1:5: error: expected expression, found 'invalid token'",
         });
     }
+
+    {
+        const case = ctx.obj("usage of deprecated code", b.graph.host);
+
+        case.addError(
+            \\const bad = @deprecated(42);
+            \\
+            \\pub export fn foo() usize {
+            \\   return bad; 
+            \\}
+        , &[_][]const u8{
+            ":1:13: error: found deprecated code",
+        });
+    }
 }
test/tests.zig
@@ -1176,6 +1176,100 @@ pub fn addCliTests(b: *std.Build) *Step {
         step.dependOn(&cleanup.step);
     }
 
+    {
+        // Test `zig build -fallow-deprecated`.
+
+        const deprecated_check: std.Build.Step.Run.StdIo.Check = .{
+            .expect_stderr_match = "found deprecated code",
+        };
+
+        const tmp_path = b.makeTempPath();
+
+        // create custom main.zig file containing a deprecated decl
+        {
+            const new_main_src =
+                \\const bad = @deprecated(42);
+                \\
+                \\pub fn main() u8 {
+                \\   return bad;
+                \\}
+                \\
+                \\test {
+                \\   if (bad != 42) return error.Bad;   
+                \\}
+            ;
+
+            var src_dir = std.fs.cwd().makeOpenPath(b.pathJoin(&.{ tmp_path, "src" }), .{}) catch unreachable;
+            defer src_dir.close();
+
+            var main = src_dir.createFile("main.zig", .{}) catch unreachable;
+            defer main.close();
+
+            main.writeAll(new_main_src) catch unreachable;
+        }
+
+        const init_exe = b.addSystemCommand(&.{ b.graph.zig_exe, "init" });
+        init_exe.setCwd(.{ .cwd_relative = tmp_path });
+        init_exe.setName("zig init");
+        init_exe.expectStdOutEqual("");
+        init_exe.expectStdErrEqual("info: created build.zig\n" ++
+            "info: created build.zig.zon\n" ++
+            "info: preserving already existing file: src" ++ s ++ "main.zig\n" ++
+            "info: created src" ++ s ++ "root.zig\n");
+
+        const run_test_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--color", "off" });
+        run_test_bad.setCwd(.{ .cwd_relative = tmp_path });
+        run_test_bad.setName("zig build test");
+        run_test_bad.expectExitCode(1);
+        run_test_bad.expectStdOutEqual("");
+        run_test_bad.addCheck(deprecated_check);
+        run_test_bad.step.dependOn(&init_exe.step);
+
+        const run_test = b.addSystemCommand(&.{
+            b.graph.zig_exe,
+            "build",
+            "test",
+            "--color",
+            "off",
+            "-fallow-deprecated",
+        });
+        run_test.setCwd(.{ .cwd_relative = tmp_path });
+        run_test.setName("zig build test");
+        run_test.expectExitCode(0);
+        run_test.expectStdOutEqual("");
+        run_test.expectStdErrEqual("");
+        run_test.step.dependOn(&init_exe.step);
+
+        const run_build_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "--color", "off" });
+        run_build_bad.setCwd(.{ .cwd_relative = tmp_path });
+        run_build_bad.setName("zig build test");
+        run_build_bad.expectExitCode(1);
+        run_build_bad.expectStdOutEqual("");
+        run_build_bad.addCheck(deprecated_check);
+        run_build_bad.step.dependOn(&init_exe.step);
+
+        const run_build = b.addSystemCommand(&.{
+            b.graph.zig_exe,
+            "build",
+            "--color",
+            "off",
+            "-fallow-deprecated",
+        });
+        run_build.setCwd(.{ .cwd_relative = tmp_path });
+        run_build.setName("zig build test");
+        run_build.expectExitCode(0);
+        run_build.expectStdOutEqual("");
+        run_build.expectStdErrEqual("");
+        run_build.step.dependOn(&init_exe.step);
+
+        const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
+        cleanup.step.dependOn(&run_test.step);
+        cleanup.step.dependOn(&run_test_bad.step);
+        cleanup.step.dependOn(&run_build.step);
+        cleanup.step.dependOn(&run_build_bad.step);
+
+        step.dependOn(&cleanup.step);
+    }
     // Test Godbolt API
     if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
         const tmp_path = b.makeTempPath();