authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2025-02-15 20:03:04+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-26 14:41:33-05:00
logc75fdd96d2cb9bd211ff20c7e9ee2ef119cc189f
treeace11890aff81e32716f4f9bd227ba619b3c80fa
parent06a66745a0b5c666608482645bc61523618dab85

`@deprecated`: add tests


3 files changed, 117 insertions(+), 9 deletions(-)

src/Sema.zig+9-9
......@@ -1342,15 +1342,6 @@ fn analyzeBodyInner(
13421342 .extended => ext: {
13431343 const extended = datas[@intFromEnum(inst)].extended;
13441344 break :ext switch (extended.opcode) {
1345 .deprecated => {
1346 if (!mod.allow_deprecated) {
1347 const src_node: i32 = @bitCast(extended.operand);
1348 const src = block.nodeOffset(src_node);
1349 return sema.fail(block, src, "found deprecated code", .{});
1350 }
1351
1352 break :ext .void_value;
1353 },
13541345 // zig fmt: off
13551346 .struct_decl => try sema.zirStructDecl( block, extended, inst),
13561347 .enum_decl => try sema.zirEnumDecl( block, extended, inst),
......@@ -1414,6 +1405,15 @@ fn analyzeBodyInner(
14141405 i += 1;
14151406 continue;
14161407 },
1408 .deprecated => {
1409 if (!mod.allow_deprecated) {
1410 const src_node: i32 = @bitCast(extended.operand);
1411 const src = block.nodeOffset(src_node);
1412 return sema.fail(block, src, "found deprecated code", .{});
1413 }
1414
1415 break :ext .void_value;
1416 },
14171417 .disable_instrumentation => {
14181418 try sema.zirDisableInstrumentation();
14191419 i += 1;
test/compile_errors.zig+14
......@@ -250,4 +250,18 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
250250 ":1:5: error: expected expression, found 'invalid token'",
251251 });
252252 }
253
254 {
255 const case = ctx.obj("usage of deprecated code", b.graph.host);
256
257 case.addError(
258 \\const bad = @deprecated(42);
259 \\
260 \\pub export fn foo() usize {
261 \\ return bad;
262 \\}
263 , &[_][]const u8{
264 ":1:13: error: found deprecated code",
265 });
266 }
253267}
test/tests.zig+94
......@@ -1176,6 +1176,100 @@ pub fn addCliTests(b: *std.Build) *Step {
11761176 step.dependOn(&cleanup.step);
11771177 }
11781178
1179 {
1180 // Test `zig build -fallow-deprecated`.
1181
1182 const deprecated_check: std.Build.Step.Run.StdIo.Check = .{
1183 .expect_stderr_match = "found deprecated code",
1184 };
1185
1186 const tmp_path = b.makeTempPath();
1187
1188 // create custom main.zig file containing a deprecated decl
1189 {
1190 const new_main_src =
1191 \\const bad = @deprecated(42);
1192 \\
1193 \\pub fn main() u8 {
1194 \\ return bad;
1195 \\}
1196 \\
1197 \\test {
1198 \\ if (bad != 42) return error.Bad;
1199 \\}
1200 ;
1201
1202 var src_dir = std.fs.cwd().makeOpenPath(b.pathJoin(&.{ tmp_path, "src" }), .{}) catch unreachable;
1203 defer src_dir.close();
1204
1205 var main = src_dir.createFile("main.zig", .{}) catch unreachable;
1206 defer main.close();
1207
1208 main.writeAll(new_main_src) catch unreachable;
1209 }
1210
1211 const init_exe = b.addSystemCommand(&.{ b.graph.zig_exe, "init" });
1212 init_exe.setCwd(.{ .cwd_relative = tmp_path });
1213 init_exe.setName("zig init");
1214 init_exe.expectStdOutEqual("");
1215 init_exe.expectStdErrEqual("info: created build.zig\n" ++
1216 "info: created build.zig.zon\n" ++
1217 "info: preserving already existing file: src" ++ s ++ "main.zig\n" ++
1218 "info: created src" ++ s ++ "root.zig\n");
1219
1220 const run_test_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--color", "off" });
1221 run_test_bad.setCwd(.{ .cwd_relative = tmp_path });
1222 run_test_bad.setName("zig build test");
1223 run_test_bad.expectExitCode(1);
1224 run_test_bad.expectStdOutEqual("");
1225 run_test_bad.addCheck(deprecated_check);
1226 run_test_bad.step.dependOn(&init_exe.step);
1227
1228 const run_test = b.addSystemCommand(&.{
1229 b.graph.zig_exe,
1230 "build",
1231 "test",
1232 "--color",
1233 "off",
1234 "-fallow-deprecated",
1235 });
1236 run_test.setCwd(.{ .cwd_relative = tmp_path });
1237 run_test.setName("zig build test");
1238 run_test.expectExitCode(0);
1239 run_test.expectStdOutEqual("");
1240 run_test.expectStdErrEqual("");
1241 run_test.step.dependOn(&init_exe.step);
1242
1243 const run_build_bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "--color", "off" });
1244 run_build_bad.setCwd(.{ .cwd_relative = tmp_path });
1245 run_build_bad.setName("zig build test");
1246 run_build_bad.expectExitCode(1);
1247 run_build_bad.expectStdOutEqual("");
1248 run_build_bad.addCheck(deprecated_check);
1249 run_build_bad.step.dependOn(&init_exe.step);
1250
1251 const run_build = b.addSystemCommand(&.{
1252 b.graph.zig_exe,
1253 "build",
1254 "--color",
1255 "off",
1256 "-fallow-deprecated",
1257 });
1258 run_build.setCwd(.{ .cwd_relative = tmp_path });
1259 run_build.setName("zig build test");
1260 run_build.expectExitCode(0);
1261 run_build.expectStdOutEqual("");
1262 run_build.expectStdErrEqual("");
1263 run_build.step.dependOn(&init_exe.step);
1264
1265 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
1266 cleanup.step.dependOn(&run_test.step);
1267 cleanup.step.dependOn(&run_test_bad.step);
1268 cleanup.step.dependOn(&run_build.step);
1269 cleanup.step.dependOn(&run_build_bad.step);
1270
1271 step.dependOn(&cleanup.step);
1272 }
11791273 // Test Godbolt API
11801274 if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
11811275 const tmp_path = b.makeTempPath();