diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-02-08 20:03:17 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-02-08 20:03:17 -0700 |
| commit | 7c1061784b8b126633cfe84f46280f7bf72beffc (patch) | |
| tree | eb99e5eb52d22b9f7d17cfd62dcfb19e3c26296a /test/behavior/null.zig | |
| parent | 210ee1067b06c14692432b7887077003e52b2137 (diff) | |
| download | zig-7c1061784b8b126633cfe84f46280f7bf72beffc.tar.gz zig-7c1061784b8b126633cfe84f46280f7bf72beffc.zip | |
stage2: fix inferred comptime constant locals
`const` declarations inside comptime blocks were not getting properly
evaluated at compile-time. To accomplish this there is a new ZIR
instruction, `alloc_inferred_comptime`. Actually we already had one
named that, but it got renamed to `alloc_inferred_comptime_mut` to match
the naming convention with the other similar instructions.
Diffstat (limited to 'test/behavior/null.zig')
| -rw-r--r-- | test/behavior/null.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/test/behavior/null.zig b/test/behavior/null.zig index 861921d39c..35ecafff80 100644 --- a/test/behavior/null.zig +++ b/test/behavior/null.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const expect = std.testing.expect; @@ -140,3 +141,50 @@ const Particle = struct { c: u64, d: u64, }; + +test "null literal outside function" { + const is_null = here_is_a_null_literal.context == null; + try expect(is_null); + + const is_non_null = here_is_a_null_literal.context != null; + try expect(!is_non_null); +} + +const SillyStruct = struct { + context: ?i32, +}; + +const here_is_a_null_literal = SillyStruct{ .context = null }; + +test "unwrap optional which is field of global var" { + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + struct_with_optional.field = null; + if (struct_with_optional.field) |payload| { + _ = payload; + unreachable; + } + struct_with_optional.field = 1234; + if (struct_with_optional.field) |payload| { + try expect(payload == 1234); + } else { + unreachable; + } +} +const StructWithOptional = struct { + field: ?i32, +}; + +var struct_with_optional: StructWithOptional = undefined; + +test "optional types" { + comptime { + const opt_type_struct = StructWithOptionalType{ .t = u8 }; + try expect(opt_type_struct.t != null and opt_type_struct.t.? == u8); + } +} + +const StructWithOptionalType = struct { + t: ?type, +}; |
