| author | |
| committer | |
| log | fa46bcb36864e6616ce4449965063f3b8720f8e1 |
| tree | d0f486600d651e47609ac57bed117422791930bc |
| parent | 83d27f71ef92d555cc15645bf9d948203ba8af1e |
| signature |
Now it will emit a compile error rather than crashing when the child
type has not been resolved properly.
Introduces `get_optional_type2` which should be used generally inside
ir.cpp.
Fix some std lib compile errors noticed by the provided test case.
Thanks @LemonBoy for the test case. Closes #4377.
Fixes #4374.5 files changed, 24 insertions(+), 6 deletions(-)
lib/std/builtin.zig+4-4| ... | ... | @@ -27,7 +27,7 @@ pub const Cpu = std.Target.Cpu; |
| 27 | 27 | /// On non-Windows targets, this is `null`. |
| 28 | 28 | pub const subsystem: ?SubSystem = blk: { |
| 29 | 29 | if (@hasDecl(@This(), "explicit_subsystem")) break :blk explicit_subsystem; |
| 30 | switch (os) { | |
| 30 | switch (os.tag) { | |
| 31 | 31 | .windows => { |
| 32 | 32 | if (is_test) { |
| 33 | 33 | break :blk SubSystem.Console; |
| ... | ... | @@ -406,9 +406,9 @@ pub const Version = struct { |
| 406 | 406 | min: Version, |
| 407 | 407 | max: Version, |
| 408 | 408 | |
| 409 | pub fn includesVersion(self: LinuxVersionRange, ver: Version) bool { | |
| 410 | if (self.min.compare(ver) == .gt) return false; | |
| 411 | if (self.max.compare(ver) == .lt) return false; | |
| 409 | pub fn includesVersion(self: Range, ver: Version) bool { | |
| 410 | if (self.min.order(ver) == .gt) return false; | |
| 411 | if (self.max.order(ver) == .lt) return false; | |
| 412 | 412 | return true; |
| 413 | 413 | } |
| 414 | 414 | }; |
src/analyze.cpp+12-1| ... | ... | @@ -649,11 +649,22 @@ ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const) { |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | ZigType *get_optional_type(CodeGen *g, ZigType *child_type) { |
| 652 | ZigType *result = get_optional_type2(g, child_type); | |
| 653 | if (result == nullptr) { | |
| 654 | codegen_report_errors_and_exit(g); | |
| 655 | } | |
| 656 | return result; | |
| 657 | } | |
| 658 | ||
| 659 | ZigType *get_optional_type2(CodeGen *g, ZigType *child_type) { | |
| 652 | 660 | if (child_type->optional_parent != nullptr) { |
| 653 | 661 | return child_type->optional_parent; |
| 654 | 662 | } |
| 655 | 663 | |
| 656 | assert(type_is_resolved(child_type, ResolveStatusSizeKnown)); | |
| 664 | Error err; | |
| 665 | if ((err = type_resolve(g, child_type, ResolveStatusSizeKnown))) { | |
| 666 | return nullptr; | |
| 667 | } | |
| 657 | 668 | |
| 658 | 669 | ZigType *entry = new_type_table_entry(ZigTypeIdOptional); |
| 659 | 670 |
src/analyze.hpp+1| ... | ... | @@ -34,6 +34,7 @@ ZigType **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type); |
| 34 | 34 | ZigType *get_c_int_type(CodeGen *g, CIntType c_int_type); |
| 35 | 35 | ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id); |
| 36 | 36 | ZigType *get_optional_type(CodeGen *g, ZigType *child_type); |
| 37 | ZigType *get_optional_type2(CodeGen *g, ZigType *child_type); | |
| 37 | 38 | ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, ZigValue *sentinel); |
| 38 | 39 | ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type); |
| 39 | 40 | ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, |
src/ir.cpp+2-1| ... | ... | @@ -24339,7 +24339,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy |
| 24339 | 24339 | |
| 24340 | 24340 | // default_value: var |
| 24341 | 24341 | inner_fields[3]->special = ConstValSpecialStatic; |
| 24342 | inner_fields[3]->type = get_optional_type(ira->codegen, struct_field->type_entry); | |
| 24342 | inner_fields[3]->type = get_optional_type2(ira->codegen, struct_field->type_entry); | |
| 24343 | if (inner_fields[3]->type == nullptr) return ErrorSemanticAnalyzeFail; | |
| 24343 | 24344 | memoize_field_init_val(ira->codegen, type_entry, struct_field); |
| 24344 | 24345 | set_optional_payload(inner_fields[3], struct_field->init_val); |
| 24345 | 24346 |
test/stage1/behavior/type_info.zig+5| ... | ... | @@ -386,3 +386,8 @@ test "@typeInfo does not force declarations into existence" { |
| 386 | 386 | }; |
| 387 | 387 | comptime expect(@typeInfo(S).Struct.fields.len == 1); |
| 388 | 388 | } |
| 389 | ||
| 390 | test "defaut value for a var-typed field" { | |
| 391 | const S = struct { x: var }; | |
| 392 | expect(@typeInfo(S).Struct.fields[0].default_value == null); | |
| 393 | } |