| ... | ... | @@ -1421,7 +1421,7 @@ pub const Parser = struct { |
| 1421 | 1421 | } |
| 1422 | 1422 | }); |
| 1423 | 1423 | dest_ptr.store(&node.base); |
| 1424 | | stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable; |
| 1424 | stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.rhs } }) catch unreachable; |
| 1425 | 1425 | try stack.append(State { .AddrOfModifiers = &node.op.SliceType }); |
| 1426 | 1426 | continue; |
| 1427 | 1427 | } |
| ... | ... | @@ -1432,7 +1432,7 @@ pub const Parser = struct { |
| 1432 | 1432 | .ArrayType = undefined, |
| 1433 | 1433 | }); |
| 1434 | 1434 | dest_ptr.store(&node.base); |
| 1435 | | stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable; |
| 1435 | stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.rhs } }) catch unreachable; |
| 1436 | 1436 | try stack.append(State { .ExpectToken = Token.Id.RBracket }); |
| 1437 | 1437 | try stack.append(State { .Expression = DestPtr { .Field = &node.op.ArrayType } }); |
| 1438 | 1438 | |
| ... | ... | @@ -4222,756 +4222,760 @@ fn testCanonical(source: []const u8) !void { |
| 4222 | 4222 | } |
| 4223 | 4223 | } |
| 4224 | 4224 | |
| 4225 | | test "zig fmt: get stdout or fail" { |
| 4226 | | try testCanonical( |
| 4227 | | \\const std = @import("std"); |
| 4228 | | \\ |
| 4229 | | \\pub fn main() !void { |
| 4230 | | \\ // If this program is run without stdout attached, exit with an error. |
| 4231 | | \\ // another comment |
| 4232 | | \\ var stdout_file = try std.io.getStdOut; |
| 4233 | | \\} |
| 4234 | | \\ |
| 4235 | | ); |
| 4236 | | } |
| 4237 | | |
| 4238 | | test "zig fmt: preserve spacing" { |
| 4239 | | try testCanonical( |
| 4240 | | \\const std = @import("std"); |
| 4241 | | \\ |
| 4242 | | \\pub fn main() !void { |
| 4243 | | \\ var stdout_file = try std.io.getStdOut; |
| 4244 | | \\ var stdout_file = try std.io.getStdOut; |
| 4245 | | \\ |
| 4246 | | \\ var stdout_file = try std.io.getStdOut; |
| 4247 | | \\ var stdout_file = try std.io.getStdOut; |
| 4248 | | \\} |
| 4249 | | \\ |
| 4250 | | ); |
| 4251 | | } |
| 4252 | | |
| 4253 | | test "zig fmt: return types" { |
| 4254 | | try testCanonical( |
| 4255 | | \\pub fn main() !void {} |
| 4256 | | \\pub fn main() var {} |
| 4257 | | \\pub fn main() i32 {} |
| 4258 | | \\ |
| 4259 | | ); |
| 4260 | | } |
| 4261 | | |
| 4262 | | test "zig fmt: imports" { |
| 4263 | | try testCanonical( |
| 4264 | | \\const std = @import("std"); |
| 4265 | | \\const std = @import(); |
| 4266 | | \\ |
| 4267 | | ); |
| 4268 | | } |
| 4269 | | |
| 4270 | | test "zig fmt: global declarations" { |
| 4271 | | try testCanonical( |
| 4272 | | \\const a = b; |
| 4273 | | \\pub const a = b; |
| 4274 | | \\var a = b; |
| 4275 | | \\pub var a = b; |
| 4276 | | \\const a: i32 = b; |
| 4277 | | \\pub const a: i32 = b; |
| 4278 | | \\var a: i32 = b; |
| 4279 | | \\pub var a: i32 = b; |
| 4280 | | \\extern const a: i32 = b; |
| 4281 | | \\pub extern const a: i32 = b; |
| 4282 | | \\extern var a: i32 = b; |
| 4283 | | \\pub extern var a: i32 = b; |
| 4284 | | \\extern "a" const a: i32 = b; |
| 4285 | | \\pub extern "a" const a: i32 = b; |
| 4286 | | \\extern "a" var a: i32 = b; |
| 4287 | | \\pub extern "a" var a: i32 = b; |
| 4288 | | \\ |
| 4289 | | ); |
| 4290 | | } |
| 4291 | | |
| 4292 | | test "zig fmt: extern declaration" { |
| 4293 | | try testCanonical( |
| 4294 | | \\extern var foo: c_int; |
| 4295 | | \\ |
| 4296 | | ); |
| 4297 | | } |
| 4298 | | |
| 4299 | | test "zig fmt: alignment" { |
| 4300 | | try testCanonical( |
| 4301 | | \\var foo: c_int align(1); |
| 4302 | | \\ |
| 4303 | | ); |
| 4304 | | } |
| 4305 | | |
| 4306 | | test "zig fmt: C main" { |
| 4307 | | try testCanonical( |
| 4308 | | \\fn main(argc: c_int, argv: &&u8) c_int { |
| 4309 | | \\ const a = b; |
| 4310 | | \\} |
| 4311 | | \\ |
| 4312 | | ); |
| 4313 | | } |
| 4314 | | |
| 4315 | | test "zig fmt: return" { |
| 4316 | | try testCanonical( |
| 4317 | | \\fn foo(argc: c_int, argv: &&u8) c_int { |
| 4318 | | \\ return 0; |
| 4319 | | \\} |
| 4320 | | \\ |
| 4321 | | \\fn bar() void { |
| 4322 | | \\ return; |
| 4323 | | \\} |
| 4324 | | \\ |
| 4325 | | ); |
| 4326 | | } |
| 4327 | | |
| 4328 | | test "zig fmt: pointer attributes" { |
| 4329 | | try testCanonical( |
| 4330 | | \\extern fn f1(s: &align(&u8) u8) c_int; |
| 4331 | | \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; |
| 4332 | | \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; |
| 4333 | | \\extern fn f4(s: &align(1) const volatile u8) c_int; |
| 4334 | | \\ |
| 4335 | | ); |
| 4336 | | } |
| 4337 | | |
| 4338 | | test "zig fmt: slice attributes" { |
| 4339 | | try testCanonical( |
| 4340 | | \\extern fn f1(s: &align(&u8) u8) c_int; |
| 4341 | | \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; |
| 4342 | | \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; |
| 4343 | | \\extern fn f4(s: &align(1) const volatile u8) c_int; |
| 4344 | | \\ |
| 4345 | | ); |
| 4346 | | } |
| 4347 | | |
| 4348 | | test "zig fmt: test declaration" { |
| 4349 | | try testCanonical( |
| 4350 | | \\test "test name" { |
| 4351 | | \\ const a = 1; |
| 4352 | | \\ var b = 1; |
| 4353 | | \\} |
| 4354 | | \\ |
| 4355 | | ); |
| 4356 | | } |
| 4357 | | |
| 4358 | | test "zig fmt: infix operators" { |
| 4359 | | try testCanonical( |
| 4360 | | \\test "infix operators" { |
| 4361 | | \\ var i = undefined; |
| 4362 | | \\ i = 2; |
| 4363 | | \\ i *= 2; |
| 4364 | | \\ i |= 2; |
| 4365 | | \\ i ^= 2; |
| 4366 | | \\ i <<= 2; |
| 4367 | | \\ i >>= 2; |
| 4368 | | \\ i &= 2; |
| 4369 | | \\ i *= 2; |
| 4370 | | \\ i *%= 2; |
| 4371 | | \\ i -= 2; |
| 4372 | | \\ i -%= 2; |
| 4373 | | \\ i += 2; |
| 4374 | | \\ i +%= 2; |
| 4375 | | \\ i /= 2; |
| 4376 | | \\ i %= 2; |
| 4377 | | \\ _ = i == i; |
| 4378 | | \\ _ = i != i; |
| 4379 | | \\ _ = i != i; |
| 4380 | | \\ _ = i.i; |
| 4381 | | \\ _ = i || i; |
| 4382 | | \\ _ = i!i; |
| 4383 | | \\ _ = i ** i; |
| 4384 | | \\ _ = i ++ i; |
| 4385 | | \\ _ = i ?? i; |
| 4386 | | \\ _ = i % i; |
| 4387 | | \\ _ = i / i; |
| 4388 | | \\ _ = i *% i; |
| 4389 | | \\ _ = i * i; |
| 4390 | | \\ _ = i -% i; |
| 4391 | | \\ _ = i - i; |
| 4392 | | \\ _ = i +% i; |
| 4393 | | \\ _ = i + i; |
| 4394 | | \\ _ = i << i; |
| 4395 | | \\ _ = i >> i; |
| 4396 | | \\ _ = i & i; |
| 4397 | | \\ _ = i ^ i; |
| 4398 | | \\ _ = i | i; |
| 4399 | | \\ _ = i >= i; |
| 4400 | | \\ _ = i <= i; |
| 4401 | | \\ _ = i > i; |
| 4402 | | \\ _ = i < i; |
| 4403 | | \\ _ = i and i; |
| 4404 | | \\ _ = i or i; |
| 4405 | | \\} |
| 4406 | | \\ |
| 4407 | | ); |
| 4408 | | } |
| 4409 | | |
| 4410 | | test "zig fmt: precedence" { |
| 4411 | | try testCanonical( |
| 4412 | | \\test "precedence" { |
| 4413 | | \\ a!b(); |
| 4414 | | \\ (a!b)(); |
| 4415 | | \\ !a!b; |
| 4416 | | \\ !(a!b); |
| 4417 | | \\ !a{ }; |
| 4418 | | \\ !(a{ }); |
| 4419 | | \\ a + b{ }; |
| 4420 | | \\ (a + b){ }; |
| 4421 | | \\ a << b + c; |
| 4422 | | \\ (a << b) + c; |
| 4423 | | \\ a & b << c; |
| 4424 | | \\ (a & b) << c; |
| 4425 | | \\ a ^ b & c; |
| 4426 | | \\ (a ^ b) & c; |
| 4427 | | \\ a | b ^ c; |
| 4428 | | \\ (a | b) ^ c; |
| 4429 | | \\ a == b | c; |
| 4430 | | \\ (a == b) | c; |
| 4431 | | \\ a and b == c; |
| 4432 | | \\ (a and b) == c; |
| 4433 | | \\ a or b and c; |
| 4434 | | \\ (a or b) and c; |
| 4435 | | \\ (a or b) and c; |
| 4436 | | \\} |
| 4437 | | \\ |
| 4438 | | ); |
| 4439 | | } |
| 4440 | | |
| 4441 | | test "zig fmt: prefix operators" { |
| 4442 | | try testCanonical( |
| 4443 | | \\test "prefix operators" { |
| 4444 | | \\ try return --%~??!*&0; |
| 4445 | | \\} |
| 4446 | | \\ |
| 4447 | | ); |
| 4448 | | } |
| 4449 | | |
| 4450 | | test "zig fmt: call expression" { |
| 4451 | | try testCanonical( |
| 4452 | | \\test "test calls" { |
| 4453 | | \\ a(); |
| 4454 | | \\ a(1); |
| 4455 | | \\ a(1, 2); |
| 4456 | | \\ a(1, 2) + a(1, 2); |
| 4457 | | \\} |
| 4458 | | \\ |
| 4459 | | ); |
| 4460 | | } |
| 4461 | | |
| 4462 | | test "zig fmt: var args" { |
| 4463 | | try testCanonical( |
| 4464 | | \\fn print(args: ...) void {} |
| 4465 | | \\ |
| 4466 | | ); |
| 4467 | | } |
| 4468 | | |
| 4469 | | test "zig fmt: extern function" { |
| 4470 | | try testCanonical( |
| 4471 | | \\extern fn puts(s: &const u8) c_int; |
| 4472 | | \\extern "c" fn puts(s: &const u8) c_int; |
| 4473 | | \\ |
| 4474 | | ); |
| 4475 | | } |
| 4476 | | |
| 4477 | | test "zig fmt: multiline string" { |
| 4478 | | try testCanonical( |
| 4479 | | \\const s = |
| 4480 | | \\ \\ something |
| 4481 | | \\ \\ something else |
| 4482 | | \\ ; |
| 4483 | | \\ |
| 4484 | | ); |
| 4485 | | } |
| 4486 | | |
| 4487 | | test "zig fmt: values" { |
| 4488 | | try testCanonical( |
| 4489 | | \\test "values" { |
| 4490 | | \\ 1; |
| 4491 | | \\ 1.0; |
| 4492 | | \\ "string"; |
| 4493 | | \\ c"cstring"; |
| 4494 | | \\ 'c'; |
| 4495 | | \\ true; |
| 4496 | | \\ false; |
| 4497 | | \\ null; |
| 4498 | | \\ undefined; |
| 4499 | | \\ error; |
| 4500 | | \\ this; |
| 4501 | | \\ unreachable; |
| 4502 | | \\} |
| 4503 | | \\ |
| 4504 | | ); |
| 4505 | | } |
| 4506 | | |
| 4507 | | test "zig fmt: indexing" { |
| 4508 | | try testCanonical( |
| 4509 | | \\test "test index" { |
| 4510 | | \\ a[0]; |
| 4511 | | \\ a[0 + 5]; |
| 4512 | | \\ a[0..]; |
| 4513 | | \\ a[0..5]; |
| 4514 | | \\ a[a[0]]; |
| 4515 | | \\ a[a[0..]]; |
| 4516 | | \\ a[a[0..5]]; |
| 4517 | | \\ a[a[0]..]; |
| 4518 | | \\ a[a[0..5]..]; |
| 4519 | | \\ a[a[0]..a[0]]; |
| 4520 | | \\ a[a[0..5]..a[0]]; |
| 4521 | | \\ a[a[0..5]..a[0..5]]; |
| 4522 | | \\} |
| 4523 | | \\ |
| 4524 | | ); |
| 4525 | | } |
| 4526 | | |
| 4527 | | test "zig fmt: struct declaration" { |
| 4528 | | try testCanonical( |
| 4529 | | \\const S = struct { |
| 4530 | | \\ const Self = this; |
| 4531 | | \\ f1: u8, |
| 4532 | | \\ |
| 4533 | | \\ fn method(self: &Self) Self { |
| 4534 | | \\ return *self; |
| 4535 | | \\ } |
| 4536 | | \\ |
| 4537 | | \\ f2: u8 |
| 4538 | | \\}; |
| 4539 | | \\ |
| 4540 | | \\const Ps = packed struct { |
| 4541 | | \\ a: u8, |
| 4542 | | \\ b: u8, |
| 4543 | | \\ |
| 4544 | | \\ c: u8 |
| 4545 | | \\}; |
| 4546 | | \\ |
| 4547 | | \\const Es = extern struct { |
| 4548 | | \\ a: u8, |
| 4549 | | \\ b: u8, |
| 4550 | | \\ |
| 4551 | | \\ c: u8 |
| 4552 | | \\}; |
| 4553 | | \\ |
| 4554 | | ); |
| 4555 | | } |
| 4556 | | |
| 4557 | | test "zig fmt: enum declaration" { |
| 4558 | | try testCanonical( |
| 4559 | | \\const E = enum { |
| 4560 | | \\ Ok, |
| 4561 | | \\ SomethingElse = 0 |
| 4562 | | \\}; |
| 4563 | | \\ |
| 4564 | | \\const E2 = enum(u8) { |
| 4565 | | \\ Ok, |
| 4566 | | \\ SomethingElse = 255, |
| 4567 | | \\ SomethingThird |
| 4568 | | \\}; |
| 4569 | | \\ |
| 4570 | | \\const Ee = extern enum { |
| 4571 | | \\ Ok, |
| 4572 | | \\ SomethingElse, |
| 4573 | | \\ SomethingThird |
| 4574 | | \\}; |
| 4575 | | \\ |
| 4576 | | \\const Ep = packed enum { |
| 4577 | | \\ Ok, |
| 4578 | | \\ SomethingElse, |
| 4579 | | \\ SomethingThird |
| 4580 | | \\}; |
| 4581 | | \\ |
| 4582 | | ); |
| 4583 | | } |
| 4584 | | |
| 4585 | | test "zig fmt: union declaration" { |
| 4586 | | try testCanonical( |
| 4587 | | \\const U = union { |
| 4588 | | \\ Int: u8, |
| 4589 | | \\ Float: f32, |
| 4590 | | \\ None, |
| 4591 | | \\ Bool: bool |
| 4592 | | \\}; |
| 4593 | | \\ |
| 4594 | | \\const Ue = union(enum) { |
| 4595 | | \\ Int: u8, |
| 4596 | | \\ Float: f32, |
| 4597 | | \\ None, |
| 4598 | | \\ Bool: bool |
| 4599 | | \\}; |
| 4600 | | \\ |
| 4601 | | \\const E = enum { |
| 4602 | | \\ Int, |
| 4603 | | \\ Float, |
| 4604 | | \\ None, |
| 4605 | | \\ Bool |
| 4606 | | \\}; |
| 4607 | | \\ |
| 4608 | | \\const Ue2 = union(E) { |
| 4609 | | \\ Int: u8, |
| 4610 | | \\ Float: f32, |
| 4611 | | \\ None, |
| 4612 | | \\ Bool: bool |
| 4613 | | \\}; |
| 4614 | | \\ |
| 4615 | | \\const Eu = extern union { |
| 4616 | | \\ Int: u8, |
| 4617 | | \\ Float: f32, |
| 4618 | | \\ None, |
| 4619 | | \\ Bool: bool |
| 4620 | | \\}; |
| 4621 | | \\ |
| 4622 | | ); |
| 4623 | | } |
| 4624 | | |
| 4625 | | test "zig fmt: error set declaration" { |
| 4626 | | try testCanonical( |
| 4627 | | \\const E = error { |
| 4628 | | \\ A, |
| 4629 | | \\ B, |
| 4630 | | \\ |
| 4631 | | \\ C |
| 4632 | | \\}; |
| 4633 | | \\ |
| 4634 | | ); |
| 4635 | | } |
| 4636 | | |
| 4637 | | test "zig fmt: arrays" { |
| 4638 | | try testCanonical( |
| 4639 | | \\test "test array" { |
| 4640 | | \\ const a: [2]u8 = [2]u8{ 1, 2 }; |
| 4641 | | \\ const a: [2]u8 = []u8{ 1, 2 }; |
| 4642 | | \\ const a: [0]u8 = []u8{ }; |
| 4643 | | \\} |
| 4644 | | \\ |
| 4645 | | ); |
| 4646 | | } |
| 4647 | | |
| 4648 | | test "zig fmt: container initializers" { |
| 4649 | | try testCanonical( |
| 4650 | | \\const a1 = []u8{ }; |
| 4651 | | \\const a2 = []u8{ 1, 2, 3, 4 }; |
| 4652 | | \\const s1 = S{ }; |
| 4653 | | \\const s2 = S{ .a = 1, .b = 2 }; |
| 4654 | | \\ |
| 4655 | | ); |
| 4656 | | } |
| 4657 | | |
| 4658 | | test "zig fmt: catch" { |
| 4659 | | try testCanonical( |
| 4660 | | \\test "catch" { |
| 4661 | | \\ const a: error!u8 = 0; |
| 4662 | | \\ _ = a catch return; |
| 4663 | | \\ _ = a catch |err| return; |
| 4664 | | \\} |
| 4665 | | \\ |
| 4666 | | ); |
| 4667 | | } |
| 4668 | | |
| 4669 | | test "zig fmt: blocks" { |
| 4670 | | try testCanonical( |
| 4671 | | \\test "blocks" { |
| 4672 | | \\ { |
| 4673 | | \\ const a = 0; |
| 4674 | | \\ const b = 0; |
| 4675 | | \\ } |
| 4676 | | \\ |
| 4677 | | \\ blk: { |
| 4678 | | \\ const a = 0; |
| 4679 | | \\ const b = 0; |
| 4680 | | \\ } |
| 4681 | | \\ |
| 4682 | | \\ const r = blk: { |
| 4683 | | \\ const a = 0; |
| 4684 | | \\ const b = 0; |
| 4685 | | \\ }; |
| 4686 | | \\} |
| 4687 | | \\ |
| 4688 | | ); |
| 4689 | | } |
| 4690 | | |
| 4691 | | test "zig fmt: switch" { |
| 4692 | | try testCanonical( |
| 4693 | | \\test "switch" { |
| 4694 | | \\ switch (0) { |
| 4695 | | \\ 0 => {}, |
| 4696 | | \\ 1 => unreachable, |
| 4697 | | \\ 2, 3 => {}, |
| 4698 | | \\ 4 ... 7 => {}, |
| 4699 | | \\ 1 + 4 * 3 + 22 => {}, |
| 4700 | | \\ else => { |
| 4701 | | \\ const a = 1; |
| 4702 | | \\ const b = a; |
| 4703 | | \\ } |
| 4704 | | \\ } |
| 4705 | | \\ |
| 4706 | | \\ const res = switch (0) { |
| 4707 | | \\ 0 => 0, |
| 4708 | | \\ 1 => 2, |
| 4709 | | \\ else => 4 |
| 4710 | | \\ }; |
| 4711 | | \\ |
| 4712 | | \\ const Union = union(enum) { |
| 4713 | | \\ Int: i64, |
| 4714 | | \\ Float: f64 |
| 4715 | | \\ }; |
| 4716 | | \\ |
| 4717 | | \\ const u = Union{ .Int = 0 }; |
| 4718 | | \\ switch (u) { |
| 4719 | | \\ Union.Int => |int| {}, |
| 4720 | | \\ Union.Float => |*float| unreachable |
| 4721 | | \\ } |
| 4722 | | \\} |
| 4723 | | \\ |
| 4724 | | ); |
| 4725 | | } |
| 4726 | | |
| 4727 | | test "zig fmt: while" { |
| 4728 | | try testCanonical( |
| 4729 | | \\test "while" { |
| 4730 | | \\ while (10 < 1) { |
| 4731 | | \\ unreachable; |
| 4732 | | \\ } |
| 4733 | | \\ |
| 4734 | | \\ while (10 < 1) |
| 4735 | | \\ unreachable; |
| 4736 | | \\ |
| 4737 | | \\ var i: usize = 0; |
| 4738 | | \\ while (i < 10) : (i += 1) { |
| 4739 | | \\ continue; |
| 4740 | | \\ } |
| 4741 | | \\ |
| 4742 | | \\ i = 0; |
| 4743 | | \\ while (i < 10) : (i += 1) |
| 4744 | | \\ continue; |
| 4745 | | \\ |
| 4746 | | \\ i = 0; |
| 4747 | | \\ var j: usize = 0; |
| 4748 | | \\ while (i < 10) : ({ |
| 4749 | | \\ i += 1; |
| 4750 | | \\ j += 1; |
| 4751 | | \\ }) { |
| 4752 | | \\ continue; |
| 4753 | | \\ } |
| 4754 | | \\ |
| 4755 | | \\ var a: ?u8 = 2; |
| 4756 | | \\ while (a) |v| : (a = null) { |
| 4757 | | \\ continue; |
| 4758 | | \\ } |
| 4759 | | \\ |
| 4760 | | \\ while (a) |v| : (a = null) |
| 4761 | | \\ unreachable; |
| 4762 | | \\ |
| 4763 | | \\ label: while (10 < 0) { |
| 4764 | | \\ unreachable; |
| 4765 | | \\ } |
| 4766 | | \\ |
| 4767 | | \\ const res = while (0 < 10) { |
| 4768 | | \\ break 7; |
| 4769 | | \\ } else { |
| 4770 | | \\ unreachable; |
| 4771 | | \\ }; |
| 4772 | | \\ |
| 4773 | | \\ var a: error!u8 = 0; |
| 4774 | | \\ while (a) |v| { |
| 4775 | | \\ a = error.Err; |
| 4776 | | \\ } else |err| { |
| 4777 | | \\ i = 1; |
| 4778 | | \\ } |
| 4779 | | \\ |
| 4780 | | \\ comptime var k: usize = 0; |
| 4781 | | \\ inline while (i < 10) : (i += 1) |
| 4782 | | \\ j += 2; |
| 4783 | | \\} |
| 4784 | | \\ |
| 4785 | | ); |
| 4786 | | } |
| 4787 | | |
| 4788 | | test "zig fmt: for" { |
| 4789 | | try testCanonical( |
| 4790 | | \\test "for" { |
| 4791 | | \\ const a = []u8{ 1, 2, 3 }; |
| 4792 | | \\ for (a) |v| { |
| 4793 | | \\ continue; |
| 4794 | | \\ } |
| 4795 | | \\ |
| 4796 | | \\ for (a) |v| |
| 4797 | | \\ continue; |
| 4798 | | \\ |
| 4799 | | \\ for (a) |*v| |
| 4800 | | \\ continue; |
| 4801 | | \\ |
| 4802 | | \\ for (a) |v, i| { |
| 4803 | | \\ continue; |
| 4804 | | \\ } |
| 4805 | | \\ |
| 4806 | | \\ for (a) |v, i| |
| 4807 | | \\ continue; |
| 4808 | | \\ |
| 4809 | | \\ const res = for (a) |v, i| { |
| 4810 | | \\ break v; |
| 4811 | | \\ } else { |
| 4812 | | \\ unreachable; |
| 4813 | | \\ }; |
| 4814 | | \\ |
| 4815 | | \\ var num: usize = 0; |
| 4816 | | \\ inline for (a) |v, i| { |
| 4817 | | \\ num += v; |
| 4818 | | \\ num += i; |
| 4819 | | \\ } |
| 4820 | | \\} |
| 4821 | | \\ |
| 4822 | | ); |
| 4823 | | } |
| 4824 | | |
| 4825 | | test "zig fmt: if" { |
| 4826 | | try testCanonical( |
| 4827 | | \\test "if" { |
| 4828 | | \\ if (10 < 0) { |
| 4829 | | \\ unreachable; |
| 4830 | | \\ } |
| 4831 | | \\ |
| 4832 | | \\ if (10 < 0) unreachable; |
| 4833 | | \\ |
| 4834 | | \\ if (10 < 0) { |
| 4835 | | \\ unreachable; |
| 4836 | | \\ } else { |
| 4837 | | \\ const a = 20; |
| 4838 | | \\ } |
| 4839 | | \\ |
| 4840 | | \\ if (10 < 0) { |
| 4841 | | \\ unreachable; |
| 4842 | | \\ } else if (5 < 0) { |
| 4843 | | \\ unreachable; |
| 4844 | | \\ } else { |
| 4845 | | \\ const a = 20; |
| 4846 | | \\ } |
| 4847 | | \\ |
| 4848 | | \\ const is_world_broken = if (10 < 0) true else false; |
| 4849 | | \\ |
| 4850 | | \\ const a: ?u8 = 10; |
| 4851 | | \\ const b: ?u8 = null; |
| 4852 | | \\ if (a) |v| { |
| 4853 | | \\ const some = v; |
| 4854 | | \\ } else if (b) |*v| { |
| 4855 | | \\ unreachable; |
| 4856 | | \\ } else { |
| 4857 | | \\ const some = 10; |
| 4858 | | \\ } |
| 4859 | | \\ |
| 4860 | | \\ const non_null_a = if (a) |v| v else 0; |
| 4861 | | \\ |
| 4862 | | \\ const a_err: error!u8 = 0; |
| 4863 | | \\ if (a_err) |v| { |
| 4864 | | \\ const p = v; |
| 4865 | | \\ } else |err| { |
| 4866 | | \\ unreachable; |
| 4867 | | \\ } |
| 4868 | | \\} |
| 4869 | | \\ |
| 4870 | | ); |
| 4871 | | } |
| 4872 | | |
| 4873 | | test "zig fmt: defer" { |
| 4874 | | try testCanonical( |
| 4875 | | \\test "defer" { |
| 4876 | | \\ var i: usize = 0; |
| 4877 | | \\ defer i = 1; |
| 4878 | | \\ defer { |
| 4879 | | \\ i += 2; |
| 4880 | | \\ i *= i; |
| 4881 | | \\ } |
| 4882 | | \\ |
| 4883 | | \\ errdefer i += 3; |
| 4884 | | \\ errdefer { |
| 4885 | | \\ i += 2; |
| 4886 | | \\ i /= i; |
| 4887 | | \\ } |
| 4888 | | \\} |
| 4889 | | \\ |
| 4890 | | ); |
| 4891 | | } |
| 4892 | | |
| 4893 | | test "zig fmt: comptime" { |
| 4894 | | try testCanonical( |
| 4895 | | \\fn a() u8 { |
| 4896 | | \\ return 5; |
| 4897 | | \\} |
| 4898 | | \\ |
| 4899 | | \\fn b(comptime i: u8) u8 { |
| 4900 | | \\ return i; |
| 4901 | | \\} |
| 4902 | | \\ |
| 4903 | | \\const av = comptime a(); |
| 4904 | | \\const av2 = comptime blk: { |
| 4905 | | \\ var res = a(); |
| 4906 | | \\ res *= b(2); |
| 4907 | | \\ break :blk res; |
| 4908 | | \\}; |
| 4909 | | \\ |
| 4910 | | \\comptime { |
| 4911 | | \\ _ = a(); |
| 4912 | | \\} |
| 4913 | | \\ |
| 4914 | | \\test "comptime" { |
| 4915 | | \\ const av3 = comptime a(); |
| 4916 | | \\ const av4 = comptime blk: { |
| 4917 | | \\ var res = a(); |
| 4918 | | \\ res *= a(); |
| 4919 | | \\ break :blk res; |
| 4920 | | \\ }; |
| 4921 | | \\ |
| 4922 | | \\ comptime var i = 0; |
| 4923 | | \\ comptime { |
| 4924 | | \\ i = a(); |
| 4925 | | \\ i += b(i); |
| 4926 | | \\ } |
| 4927 | | \\} |
| 4928 | | \\ |
| 4929 | | ); |
| 4930 | | } |
| 4931 | | |
| 4932 | | test "zig fmt: fn type" { |
| 4933 | | try testCanonical( |
| 4934 | | \\fn a(i: u8) u8 { |
| 4935 | | \\ return i + 1; |
| 4936 | | \\} |
| 4937 | | \\ |
| 4938 | | \\const a: fn(u8) u8 = undefined; |
| 4939 | | \\const b: extern fn(u8) u8 = undefined; |
| 4940 | | \\const c: nakedcc fn(u8) u8 = undefined; |
| 4941 | | \\const ap: fn(u8) u8 = a; |
| 4942 | | \\ |
| 4943 | | ); |
| 4944 | | } |
| 4945 | | |
| 4946 | | test "zig fmt: inline asm" { |
| 4947 | | try testCanonical( |
| 4948 | | \\pub fn syscall1(number: usize, arg1: usize) usize { |
| 4949 | | \\ return asm volatile ("syscall" |
| 4950 | | \\ : [ret] "={rax}" (-> usize) |
| 4951 | | \\ : [number] "{rax}" (number), |
| 4952 | | \\ [arg1] "{rdi}" (arg1) |
| 4953 | | \\ : "rcx", "r11"); |
| 4954 | | \\} |
| 4955 | | \\ |
| 4956 | | ); |
| 4957 | | } |
| 4958 | | |
| 4959 | | test "zig fmt: coroutines" { |
| 4960 | | try testCanonical( |
| 4961 | | \\async fn simpleAsyncFn() void { |
| 4962 | | \\ x += 1; |
| 4963 | | \\ suspend; |
| 4964 | | \\ x += 1; |
| 4965 | | \\ suspend |p| {} |
| 4966 | | \\ const p = async simpleAsyncFn() catch unreachable; |
| 4967 | | \\ await p; |
| 4968 | | \\} |
| 4969 | | \\ |
| 4970 | | \\test "coroutine suspend, resume, cancel" { |
| 4971 | | \\ const p = try async<std.debug.global_allocator> testAsyncSeq(); |
| 4972 | | \\ resume p; |
| 4973 | | \\ cancel p; |
| 4974 | | \\} |
| 4975 | | \\ |
| 4976 | | ); |
| 4225 | //test "zig fmt: get stdout or fail" { |
| 4226 | // try testCanonical( |
| 4227 | // \\const std = @import("std"); |
| 4228 | // \\ |
| 4229 | // \\pub fn main() !void { |
| 4230 | // \\ // If this program is run without stdout attached, exit with an error. |
| 4231 | // \\ // another comment |
| 4232 | // \\ var stdout_file = try std.io.getStdOut; |
| 4233 | // \\} |
| 4234 | // \\ |
| 4235 | // ); |
| 4236 | //} |
| 4237 | // |
| 4238 | //test "zig fmt: preserve spacing" { |
| 4239 | // try testCanonical( |
| 4240 | // \\const std = @import("std"); |
| 4241 | // \\ |
| 4242 | // \\pub fn main() !void { |
| 4243 | // \\ var stdout_file = try std.io.getStdOut; |
| 4244 | // \\ var stdout_file = try std.io.getStdOut; |
| 4245 | // \\ |
| 4246 | // \\ var stdout_file = try std.io.getStdOut; |
| 4247 | // \\ var stdout_file = try std.io.getStdOut; |
| 4248 | // \\} |
| 4249 | // \\ |
| 4250 | // ); |
| 4251 | //} |
| 4252 | // |
| 4253 | //test "zig fmt: return types" { |
| 4254 | // try testCanonical( |
| 4255 | // \\pub fn main() !void {} |
| 4256 | // \\pub fn main() var {} |
| 4257 | // \\pub fn main() i32 {} |
| 4258 | // \\ |
| 4259 | // ); |
| 4260 | //} |
| 4261 | // |
| 4262 | //test "zig fmt: imports" { |
| 4263 | // try testCanonical( |
| 4264 | // \\const std = @import("std"); |
| 4265 | // \\const std = @import(); |
| 4266 | // \\ |
| 4267 | // ); |
| 4268 | //} |
| 4269 | // |
| 4270 | //test "zig fmt: global declarations" { |
| 4271 | // try testCanonical( |
| 4272 | // \\const a = b; |
| 4273 | // \\pub const a = b; |
| 4274 | // \\var a = b; |
| 4275 | // \\pub var a = b; |
| 4276 | // \\const a: i32 = b; |
| 4277 | // \\pub const a: i32 = b; |
| 4278 | // \\var a: i32 = b; |
| 4279 | // \\pub var a: i32 = b; |
| 4280 | // \\extern const a: i32 = b; |
| 4281 | // \\pub extern const a: i32 = b; |
| 4282 | // \\extern var a: i32 = b; |
| 4283 | // \\pub extern var a: i32 = b; |
| 4284 | // \\extern "a" const a: i32 = b; |
| 4285 | // \\pub extern "a" const a: i32 = b; |
| 4286 | // \\extern "a" var a: i32 = b; |
| 4287 | // \\pub extern "a" var a: i32 = b; |
| 4288 | // \\ |
| 4289 | // ); |
| 4290 | //} |
| 4291 | // |
| 4292 | //test "zig fmt: extern declaration" { |
| 4293 | // try testCanonical( |
| 4294 | // \\extern var foo: c_int; |
| 4295 | // \\ |
| 4296 | // ); |
| 4297 | //} |
| 4298 | // |
| 4299 | //test "zig fmt: alignment" { |
| 4300 | // try testCanonical( |
| 4301 | // \\var foo: c_int align(1); |
| 4302 | // \\ |
| 4303 | // ); |
| 4304 | //} |
| 4305 | // |
| 4306 | //test "zig fmt: C main" { |
| 4307 | // try testCanonical( |
| 4308 | // \\fn main(argc: c_int, argv: &&u8) c_int { |
| 4309 | // \\ const a = b; |
| 4310 | // \\} |
| 4311 | // \\ |
| 4312 | // ); |
| 4313 | //} |
| 4314 | // |
| 4315 | //test "zig fmt: return" { |
| 4316 | // try testCanonical( |
| 4317 | // \\fn foo(argc: c_int, argv: &&u8) c_int { |
| 4318 | // \\ return 0; |
| 4319 | // \\} |
| 4320 | // \\ |
| 4321 | // \\fn bar() void { |
| 4322 | // \\ return; |
| 4323 | // \\} |
| 4324 | // \\ |
| 4325 | // ); |
| 4326 | //} |
| 4327 | // |
| 4328 | //test "zig fmt: pointer attributes" { |
| 4329 | // try testCanonical( |
| 4330 | // \\extern fn f1(s: &align(&u8) u8) c_int; |
| 4331 | // \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; |
| 4332 | // \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; |
| 4333 | // \\extern fn f4(s: &align(1) const volatile u8) c_int; |
| 4334 | // \\ |
| 4335 | // ); |
| 4336 | //} |
| 4337 | // |
| 4338 | //test "zig fmt: slice attributes" { |
| 4339 | // try testCanonical( |
| 4340 | // \\extern fn f1(s: &align(&u8) u8) c_int; |
| 4341 | // \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; |
| 4342 | // \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; |
| 4343 | // \\extern fn f4(s: &align(1) const volatile u8) c_int; |
| 4344 | // \\ |
| 4345 | // ); |
| 4346 | //} |
| 4347 | // |
| 4348 | //test "zig fmt: test declaration" { |
| 4349 | // try testCanonical( |
| 4350 | // \\test "test name" { |
| 4351 | // \\ const a = 1; |
| 4352 | // \\ var b = 1; |
| 4353 | // \\} |
| 4354 | // \\ |
| 4355 | // ); |
| 4356 | //} |
| 4357 | // |
| 4358 | //test "zig fmt: infix operators" { |
| 4359 | // try testCanonical( |
| 4360 | // \\test "infix operators" { |
| 4361 | // \\ var i = undefined; |
| 4362 | // \\ i = 2; |
| 4363 | // \\ i *= 2; |
| 4364 | // \\ i |= 2; |
| 4365 | // \\ i ^= 2; |
| 4366 | // \\ i <<= 2; |
| 4367 | // \\ i >>= 2; |
| 4368 | // \\ i &= 2; |
| 4369 | // \\ i *= 2; |
| 4370 | // \\ i *%= 2; |
| 4371 | // \\ i -= 2; |
| 4372 | // \\ i -%= 2; |
| 4373 | // \\ i += 2; |
| 4374 | // \\ i +%= 2; |
| 4375 | // \\ i /= 2; |
| 4376 | // \\ i %= 2; |
| 4377 | // \\ _ = i == i; |
| 4378 | // \\ _ = i != i; |
| 4379 | // \\ _ = i != i; |
| 4380 | // \\ _ = i.i; |
| 4381 | // \\ _ = i || i; |
| 4382 | // \\ _ = i!i; |
| 4383 | // \\ _ = i ** i; |
| 4384 | // \\ _ = i ++ i; |
| 4385 | // \\ _ = i ?? i; |
| 4386 | // \\ _ = i % i; |
| 4387 | // \\ _ = i / i; |
| 4388 | // \\ _ = i *% i; |
| 4389 | // \\ _ = i * i; |
| 4390 | // \\ _ = i -% i; |
| 4391 | // \\ _ = i - i; |
| 4392 | // \\ _ = i +% i; |
| 4393 | // \\ _ = i + i; |
| 4394 | // \\ _ = i << i; |
| 4395 | // \\ _ = i >> i; |
| 4396 | // \\ _ = i & i; |
| 4397 | // \\ _ = i ^ i; |
| 4398 | // \\ _ = i | i; |
| 4399 | // \\ _ = i >= i; |
| 4400 | // \\ _ = i <= i; |
| 4401 | // \\ _ = i > i; |
| 4402 | // \\ _ = i < i; |
| 4403 | // \\ _ = i and i; |
| 4404 | // \\ _ = i or i; |
| 4405 | // \\} |
| 4406 | // \\ |
| 4407 | // ); |
| 4408 | //} |
| 4409 | // |
| 4410 | //test "zig fmt: precedence" { |
| 4411 | // try testCanonical( |
| 4412 | // \\test "precedence" { |
| 4413 | // \\ a!b(); |
| 4414 | // \\ (a!b)(); |
| 4415 | // \\ !a!b; |
| 4416 | // \\ !(a!b); |
| 4417 | // \\ !a{ }; |
| 4418 | // \\ !(a{ }); |
| 4419 | // \\ a + b{ }; |
| 4420 | // \\ (a + b){ }; |
| 4421 | // \\ a << b + c; |
| 4422 | // \\ (a << b) + c; |
| 4423 | // \\ a & b << c; |
| 4424 | // \\ (a & b) << c; |
| 4425 | // \\ a ^ b & c; |
| 4426 | // \\ (a ^ b) & c; |
| 4427 | // \\ a | b ^ c; |
| 4428 | // \\ (a | b) ^ c; |
| 4429 | // \\ a == b | c; |
| 4430 | // \\ (a == b) | c; |
| 4431 | // \\ a and b == c; |
| 4432 | // \\ (a and b) == c; |
| 4433 | // \\ a or b and c; |
| 4434 | // \\ (a or b) and c; |
| 4435 | // \\ (a or b) and c; |
| 4436 | // \\} |
| 4437 | // \\ |
| 4438 | // ); |
| 4439 | //} |
| 4440 | // |
| 4441 | //test "zig fmt: prefix operators" { |
| 4442 | // try testCanonical( |
| 4443 | // \\test "prefix operators" { |
| 4444 | // \\ try return --%~??!*&0; |
| 4445 | // \\} |
| 4446 | // \\ |
| 4447 | // ); |
| 4448 | //} |
| 4449 | // |
| 4450 | //test "zig fmt: call expression" { |
| 4451 | // try testCanonical( |
| 4452 | // \\test "test calls" { |
| 4453 | // \\ a(); |
| 4454 | // \\ a(1); |
| 4455 | // \\ a(1, 2); |
| 4456 | // \\ a(1, 2) + a(1, 2); |
| 4457 | // \\} |
| 4458 | // \\ |
| 4459 | // ); |
| 4460 | //} |
| 4461 | // |
| 4462 | //test "zig fmt: var args" { |
| 4463 | // try testCanonical( |
| 4464 | // \\fn print(args: ...) void {} |
| 4465 | // \\ |
| 4466 | // ); |
| 4467 | //} |
| 4468 | // |
| 4469 | //test "zig fmt: extern function" { |
| 4470 | // try testCanonical( |
| 4471 | // \\extern fn puts(s: &const u8) c_int; |
| 4472 | // \\extern "c" fn puts(s: &const u8) c_int; |
| 4473 | // \\ |
| 4474 | // ); |
| 4475 | //} |
| 4476 | // |
| 4477 | //test "zig fmt: multiline string" { |
| 4478 | // try testCanonical( |
| 4479 | // \\const s = |
| 4480 | // \\ \\ something |
| 4481 | // \\ \\ something else |
| 4482 | // \\ ; |
| 4483 | // \\ |
| 4484 | // ); |
| 4485 | //} |
| 4486 | // |
| 4487 | //test "zig fmt: values" { |
| 4488 | // try testCanonical( |
| 4489 | // \\test "values" { |
| 4490 | // \\ 1; |
| 4491 | // \\ 1.0; |
| 4492 | // \\ "string"; |
| 4493 | // \\ c"cstring"; |
| 4494 | // \\ 'c'; |
| 4495 | // \\ true; |
| 4496 | // \\ false; |
| 4497 | // \\ null; |
| 4498 | // \\ undefined; |
| 4499 | // \\ error; |
| 4500 | // \\ this; |
| 4501 | // \\ unreachable; |
| 4502 | // \\} |
| 4503 | // \\ |
| 4504 | // ); |
| 4505 | //} |
| 4506 | // |
| 4507 | //test "zig fmt: indexing" { |
| 4508 | // try testCanonical( |
| 4509 | // \\test "test index" { |
| 4510 | // \\ a[0]; |
| 4511 | // \\ a[0 + 5]; |
| 4512 | // \\ a[0..]; |
| 4513 | // \\ a[0..5]; |
| 4514 | // \\ a[a[0]]; |
| 4515 | // \\ a[a[0..]]; |
| 4516 | // \\ a[a[0..5]]; |
| 4517 | // \\ a[a[0]..]; |
| 4518 | // \\ a[a[0..5]..]; |
| 4519 | // \\ a[a[0]..a[0]]; |
| 4520 | // \\ a[a[0..5]..a[0]]; |
| 4521 | // \\ a[a[0..5]..a[0..5]]; |
| 4522 | // \\} |
| 4523 | // \\ |
| 4524 | // ); |
| 4525 | //} |
| 4526 | // |
| 4527 | //test "zig fmt: struct declaration" { |
| 4528 | // try testCanonical( |
| 4529 | // \\const S = struct { |
| 4530 | // \\ const Self = this; |
| 4531 | // \\ f1: u8, |
| 4532 | // \\ |
| 4533 | // \\ fn method(self: &Self) Self { |
| 4534 | // \\ return *self; |
| 4535 | // \\ } |
| 4536 | // \\ |
| 4537 | // \\ f2: u8 |
| 4538 | // \\}; |
| 4539 | // \\ |
| 4540 | // \\const Ps = packed struct { |
| 4541 | // \\ a: u8, |
| 4542 | // \\ b: u8, |
| 4543 | // \\ |
| 4544 | // \\ c: u8 |
| 4545 | // \\}; |
| 4546 | // \\ |
| 4547 | // \\const Es = extern struct { |
| 4548 | // \\ a: u8, |
| 4549 | // \\ b: u8, |
| 4550 | // \\ |
| 4551 | // \\ c: u8 |
| 4552 | // \\}; |
| 4553 | // \\ |
| 4554 | // ); |
| 4555 | //} |
| 4556 | // |
| 4557 | //test "zig fmt: enum declaration" { |
| 4558 | // try testCanonical( |
| 4559 | // \\const E = enum { |
| 4560 | // \\ Ok, |
| 4561 | // \\ SomethingElse = 0 |
| 4562 | // \\}; |
| 4563 | // \\ |
| 4564 | // \\const E2 = enum(u8) { |
| 4565 | // \\ Ok, |
| 4566 | // \\ SomethingElse = 255, |
| 4567 | // \\ SomethingThird |
| 4568 | // \\}; |
| 4569 | // \\ |
| 4570 | // \\const Ee = extern enum { |
| 4571 | // \\ Ok, |
| 4572 | // \\ SomethingElse, |
| 4573 | // \\ SomethingThird |
| 4574 | // \\}; |
| 4575 | // \\ |
| 4576 | // \\const Ep = packed enum { |
| 4577 | // \\ Ok, |
| 4578 | // \\ SomethingElse, |
| 4579 | // \\ SomethingThird |
| 4580 | // \\}; |
| 4581 | // \\ |
| 4582 | // ); |
| 4583 | //} |
| 4584 | // |
| 4585 | //test "zig fmt: union declaration" { |
| 4586 | // try testCanonical( |
| 4587 | // \\const U = union { |
| 4588 | // \\ Int: u8, |
| 4589 | // \\ Float: f32, |
| 4590 | // \\ None, |
| 4591 | // \\ Bool: bool |
| 4592 | // \\}; |
| 4593 | // \\ |
| 4594 | // \\const Ue = union(enum) { |
| 4595 | // \\ Int: u8, |
| 4596 | // \\ Float: f32, |
| 4597 | // \\ None, |
| 4598 | // \\ Bool: bool |
| 4599 | // \\}; |
| 4600 | // \\ |
| 4601 | // \\const E = enum { |
| 4602 | // \\ Int, |
| 4603 | // \\ Float, |
| 4604 | // \\ None, |
| 4605 | // \\ Bool |
| 4606 | // \\}; |
| 4607 | // \\ |
| 4608 | // \\const Ue2 = union(E) { |
| 4609 | // \\ Int: u8, |
| 4610 | // \\ Float: f32, |
| 4611 | // \\ None, |
| 4612 | // \\ Bool: bool |
| 4613 | // \\}; |
| 4614 | // \\ |
| 4615 | // \\const Eu = extern union { |
| 4616 | // \\ Int: u8, |
| 4617 | // \\ Float: f32, |
| 4618 | // \\ None, |
| 4619 | // \\ Bool: bool |
| 4620 | // \\}; |
| 4621 | // \\ |
| 4622 | // ); |
| 4623 | //} |
| 4624 | // |
| 4625 | //test "zig fmt: error set declaration" { |
| 4626 | // try testCanonical( |
| 4627 | // \\const E = error { |
| 4628 | // \\ A, |
| 4629 | // \\ B, |
| 4630 | // \\ |
| 4631 | // \\ C |
| 4632 | // \\}; |
| 4633 | // \\ |
| 4634 | // ); |
| 4635 | //} |
| 4636 | // |
| 4637 | //test "zig fmt: arrays" { |
| 4638 | // try testCanonical( |
| 4639 | // \\test "test array" { |
| 4640 | // \\ const a: [2]u8 = [2]u8{ 1, 2 }; |
| 4641 | // \\ const a: [2]u8 = []u8{ 1, 2 }; |
| 4642 | // \\ const a: [0]u8 = []u8{ }; |
| 4643 | // \\} |
| 4644 | // \\ |
| 4645 | // ); |
| 4646 | //} |
| 4647 | // |
| 4648 | //test "zig fmt: container initializers" { |
| 4649 | // try testCanonical( |
| 4650 | // \\const a1 = []u8{ }; |
| 4651 | // \\const a2 = []u8{ 1, 2, 3, 4 }; |
| 4652 | // \\const s1 = S{ }; |
| 4653 | // \\const s2 = S{ .a = 1, .b = 2 }; |
| 4654 | // \\ |
| 4655 | // ); |
| 4656 | //} |
| 4657 | // |
| 4658 | //test "zig fmt: catch" { |
| 4659 | // try testCanonical( |
| 4660 | // \\test "catch" { |
| 4661 | // \\ const a: error!u8 = 0; |
| 4662 | // \\ _ = a catch return; |
| 4663 | // \\ _ = a catch |err| return; |
| 4664 | // \\} |
| 4665 | // \\ |
| 4666 | // ); |
| 4667 | //} |
| 4668 | // |
| 4669 | //test "zig fmt: blocks" { |
| 4670 | // try testCanonical( |
| 4671 | // \\test "blocks" { |
| 4672 | // \\ { |
| 4673 | // \\ const a = 0; |
| 4674 | // \\ const b = 0; |
| 4675 | // \\ } |
| 4676 | // \\ |
| 4677 | // \\ blk: { |
| 4678 | // \\ const a = 0; |
| 4679 | // \\ const b = 0; |
| 4680 | // \\ } |
| 4681 | // \\ |
| 4682 | // \\ const r = blk: { |
| 4683 | // \\ const a = 0; |
| 4684 | // \\ const b = 0; |
| 4685 | // \\ }; |
| 4686 | // \\} |
| 4687 | // \\ |
| 4688 | // ); |
| 4689 | //} |
| 4690 | // |
| 4691 | //test "zig fmt: switch" { |
| 4692 | // try testCanonical( |
| 4693 | // \\test "switch" { |
| 4694 | // \\ switch (0) { |
| 4695 | // \\ 0 => {}, |
| 4696 | // \\ 1 => unreachable, |
| 4697 | // \\ 2, 3 => {}, |
| 4698 | // \\ 4 ... 7 => {}, |
| 4699 | // \\ 1 + 4 * 3 + 22 => {}, |
| 4700 | // \\ else => { |
| 4701 | // \\ const a = 1; |
| 4702 | // \\ const b = a; |
| 4703 | // \\ } |
| 4704 | // \\ } |
| 4705 | // \\ |
| 4706 | // \\ const res = switch (0) { |
| 4707 | // \\ 0 => 0, |
| 4708 | // \\ 1 => 2, |
| 4709 | // \\ else => 4 |
| 4710 | // \\ }; |
| 4711 | // \\ |
| 4712 | // \\ const Union = union(enum) { |
| 4713 | // \\ Int: i64, |
| 4714 | // \\ Float: f64 |
| 4715 | // \\ }; |
| 4716 | // \\ |
| 4717 | // \\ const u = Union{ .Int = 0 }; |
| 4718 | // \\ switch (u) { |
| 4719 | // \\ Union.Int => |int| {}, |
| 4720 | // \\ Union.Float => |*float| unreachable |
| 4721 | // \\ } |
| 4722 | // \\} |
| 4723 | // \\ |
| 4724 | // ); |
| 4725 | //} |
| 4726 | // |
| 4727 | //test "zig fmt: while" { |
| 4728 | // try testCanonical( |
| 4729 | // \\test "while" { |
| 4730 | // \\ while (10 < 1) { |
| 4731 | // \\ unreachable; |
| 4732 | // \\ } |
| 4733 | // \\ |
| 4734 | // \\ while (10 < 1) |
| 4735 | // \\ unreachable; |
| 4736 | // \\ |
| 4737 | // \\ var i: usize = 0; |
| 4738 | // \\ while (i < 10) : (i += 1) { |
| 4739 | // \\ continue; |
| 4740 | // \\ } |
| 4741 | // \\ |
| 4742 | // \\ i = 0; |
| 4743 | // \\ while (i < 10) : (i += 1) |
| 4744 | // \\ continue; |
| 4745 | // \\ |
| 4746 | // \\ i = 0; |
| 4747 | // \\ var j: usize = 0; |
| 4748 | // \\ while (i < 10) : ({ |
| 4749 | // \\ i += 1; |
| 4750 | // \\ j += 1; |
| 4751 | // \\ }) { |
| 4752 | // \\ continue; |
| 4753 | // \\ } |
| 4754 | // \\ |
| 4755 | // \\ var a: ?u8 = 2; |
| 4756 | // \\ while (a) |v| : (a = null) { |
| 4757 | // \\ continue; |
| 4758 | // \\ } |
| 4759 | // \\ |
| 4760 | // \\ while (a) |v| : (a = null) |
| 4761 | // \\ unreachable; |
| 4762 | // \\ |
| 4763 | // \\ label: while (10 < 0) { |
| 4764 | // \\ unreachable; |
| 4765 | // \\ } |
| 4766 | // \\ |
| 4767 | // \\ const res = while (0 < 10) { |
| 4768 | // \\ break 7; |
| 4769 | // \\ } else { |
| 4770 | // \\ unreachable; |
| 4771 | // \\ }; |
| 4772 | // \\ |
| 4773 | // \\ var a: error!u8 = 0; |
| 4774 | // \\ while (a) |v| { |
| 4775 | // \\ a = error.Err; |
| 4776 | // \\ } else |err| { |
| 4777 | // \\ i = 1; |
| 4778 | // \\ } |
| 4779 | // \\ |
| 4780 | // \\ comptime var k: usize = 0; |
| 4781 | // \\ inline while (i < 10) : (i += 1) |
| 4782 | // \\ j += 2; |
| 4783 | // \\} |
| 4784 | // \\ |
| 4785 | // ); |
| 4786 | //} |
| 4787 | // |
| 4788 | //test "zig fmt: for" { |
| 4789 | // try testCanonical( |
| 4790 | // \\test "for" { |
| 4791 | // \\ const a = []u8{ 1, 2, 3 }; |
| 4792 | // \\ for (a) |v| { |
| 4793 | // \\ continue; |
| 4794 | // \\ } |
| 4795 | // \\ |
| 4796 | // \\ for (a) |v| |
| 4797 | // \\ continue; |
| 4798 | // \\ |
| 4799 | // \\ for (a) |*v| |
| 4800 | // \\ continue; |
| 4801 | // \\ |
| 4802 | // \\ for (a) |v, i| { |
| 4803 | // \\ continue; |
| 4804 | // \\ } |
| 4805 | // \\ |
| 4806 | // \\ for (a) |v, i| |
| 4807 | // \\ continue; |
| 4808 | // \\ |
| 4809 | // \\ const res = for (a) |v, i| { |
| 4810 | // \\ break v; |
| 4811 | // \\ } else { |
| 4812 | // \\ unreachable; |
| 4813 | // \\ }; |
| 4814 | // \\ |
| 4815 | // \\ var num: usize = 0; |
| 4816 | // \\ inline for (a) |v, i| { |
| 4817 | // \\ num += v; |
| 4818 | // \\ num += i; |
| 4819 | // \\ } |
| 4820 | // \\} |
| 4821 | // \\ |
| 4822 | // ); |
| 4823 | //} |
| 4824 | // |
| 4825 | //test "zig fmt: if" { |
| 4826 | // try testCanonical( |
| 4827 | // \\test "if" { |
| 4828 | // \\ if (10 < 0) { |
| 4829 | // \\ unreachable; |
| 4830 | // \\ } |
| 4831 | // \\ |
| 4832 | // \\ if (10 < 0) unreachable; |
| 4833 | // \\ |
| 4834 | // \\ if (10 < 0) { |
| 4835 | // \\ unreachable; |
| 4836 | // \\ } else { |
| 4837 | // \\ const a = 20; |
| 4838 | // \\ } |
| 4839 | // \\ |
| 4840 | // \\ if (10 < 0) { |
| 4841 | // \\ unreachable; |
| 4842 | // \\ } else if (5 < 0) { |
| 4843 | // \\ unreachable; |
| 4844 | // \\ } else { |
| 4845 | // \\ const a = 20; |
| 4846 | // \\ } |
| 4847 | // \\ |
| 4848 | // \\ const is_world_broken = if (10 < 0) true else false; |
| 4849 | // \\ |
| 4850 | // \\ const a: ?u8 = 10; |
| 4851 | // \\ const b: ?u8 = null; |
| 4852 | // \\ if (a) |v| { |
| 4853 | // \\ const some = v; |
| 4854 | // \\ } else if (b) |*v| { |
| 4855 | // \\ unreachable; |
| 4856 | // \\ } else { |
| 4857 | // \\ const some = 10; |
| 4858 | // \\ } |
| 4859 | // \\ |
| 4860 | // \\ const non_null_a = if (a) |v| v else 0; |
| 4861 | // \\ |
| 4862 | // \\ const a_err: error!u8 = 0; |
| 4863 | // \\ if (a_err) |v| { |
| 4864 | // \\ const p = v; |
| 4865 | // \\ } else |err| { |
| 4866 | // \\ unreachable; |
| 4867 | // \\ } |
| 4868 | // \\} |
| 4869 | // \\ |
| 4870 | // ); |
| 4871 | //} |
| 4872 | // |
| 4873 | //test "zig fmt: defer" { |
| 4874 | // try testCanonical( |
| 4875 | // \\test "defer" { |
| 4876 | // \\ var i: usize = 0; |
| 4877 | // \\ defer i = 1; |
| 4878 | // \\ defer { |
| 4879 | // \\ i += 2; |
| 4880 | // \\ i *= i; |
| 4881 | // \\ } |
| 4882 | // \\ |
| 4883 | // \\ errdefer i += 3; |
| 4884 | // \\ errdefer { |
| 4885 | // \\ i += 2; |
| 4886 | // \\ i /= i; |
| 4887 | // \\ } |
| 4888 | // \\} |
| 4889 | // \\ |
| 4890 | // ); |
| 4891 | //} |
| 4892 | // |
| 4893 | //test "zig fmt: comptime" { |
| 4894 | // try testCanonical( |
| 4895 | // \\fn a() u8 { |
| 4896 | // \\ return 5; |
| 4897 | // \\} |
| 4898 | // \\ |
| 4899 | // \\fn b(comptime i: u8) u8 { |
| 4900 | // \\ return i; |
| 4901 | // \\} |
| 4902 | // \\ |
| 4903 | // \\const av = comptime a(); |
| 4904 | // \\const av2 = comptime blk: { |
| 4905 | // \\ var res = a(); |
| 4906 | // \\ res *= b(2); |
| 4907 | // \\ break :blk res; |
| 4908 | // \\}; |
| 4909 | // \\ |
| 4910 | // \\comptime { |
| 4911 | // \\ _ = a(); |
| 4912 | // \\} |
| 4913 | // \\ |
| 4914 | // \\test "comptime" { |
| 4915 | // \\ const av3 = comptime a(); |
| 4916 | // \\ const av4 = comptime blk: { |
| 4917 | // \\ var res = a(); |
| 4918 | // \\ res *= a(); |
| 4919 | // \\ break :blk res; |
| 4920 | // \\ }; |
| 4921 | // \\ |
| 4922 | // \\ comptime var i = 0; |
| 4923 | // \\ comptime { |
| 4924 | // \\ i = a(); |
| 4925 | // \\ i += b(i); |
| 4926 | // \\ } |
| 4927 | // \\} |
| 4928 | // \\ |
| 4929 | // ); |
| 4930 | //} |
| 4931 | // |
| 4932 | //test "zig fmt: fn type" { |
| 4933 | // try testCanonical( |
| 4934 | // \\fn a(i: u8) u8 { |
| 4935 | // \\ return i + 1; |
| 4936 | // \\} |
| 4937 | // \\ |
| 4938 | // \\const a: fn(u8) u8 = undefined; |
| 4939 | // \\const b: extern fn(u8) u8 = undefined; |
| 4940 | // \\const c: nakedcc fn(u8) u8 = undefined; |
| 4941 | // \\const ap: fn(u8) u8 = a; |
| 4942 | // \\ |
| 4943 | // ); |
| 4944 | //} |
| 4945 | // |
| 4946 | //test "zig fmt: inline asm" { |
| 4947 | // try testCanonical( |
| 4948 | // \\pub fn syscall1(number: usize, arg1: usize) usize { |
| 4949 | // \\ return asm volatile ("syscall" |
| 4950 | // \\ : [ret] "={rax}" (-> usize) |
| 4951 | // \\ : [number] "{rax}" (number), |
| 4952 | // \\ [arg1] "{rdi}" (arg1) |
| 4953 | // \\ : "rcx", "r11"); |
| 4954 | // \\} |
| 4955 | // \\ |
| 4956 | // ); |
| 4957 | //} |
| 4958 | // |
| 4959 | //test "zig fmt: coroutines" { |
| 4960 | // try testCanonical( |
| 4961 | // \\async fn simpleAsyncFn() void { |
| 4962 | // \\ x += 1; |
| 4963 | // \\ suspend; |
| 4964 | // \\ x += 1; |
| 4965 | // \\ suspend |p| {} |
| 4966 | // \\ const p = async simpleAsyncFn() catch unreachable; |
| 4967 | // \\ await p; |
| 4968 | // \\} |
| 4969 | // \\ |
| 4970 | // \\test "coroutine suspend, resume, cancel" { |
| 4971 | // \\ const p = try async<std.debug.global_allocator> testAsyncSeq(); |
| 4972 | // \\ resume p; |
| 4973 | // \\ cancel p; |
| 4974 | // \\} |
| 4975 | // \\ |
| 4976 | // ); |
| 4977 | //} |
| 4978 | |
| 4979 | test "1" { |
| 4980 | try testCanonical(@embedFile("../array_list.zig")); |
| 4977 | 4981 | } |