authorgravatar for martinhath@users.noreply.github.commartinhath <martinhath@users.noreply.github.com> 2022-08-26 10:37:17+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-26 11:37:17+03:00
log3fa5415253695001149ad65ae6f2ca8d0fa63565
tree0dfb0d0db54dd2d7c00490daf7e41aef4107fdaf
parentbcaa9df5b42747577dcb529a99f6da6d69e09309
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Sema: ensure resolveTypeFields is called for optional and error union types

We call `sema.resolveTypeFields` in order to get the fields of structs and unions inserted into their data structures. If it isn't called, it can happen that the fields of a type is queried before those fields are inserted into (for instance) `Module.Union.fields`, which would result in a wrong 'no field named' error. Fixes: #12486

3 files changed, 53 insertions(+), 2 deletions(-)

src/Sema.zig+3-2
...@@ -16190,9 +16190,10 @@ fn fieldType(...@@ -16190,9 +16190,10 @@ fn fieldType(
16190 field_src: LazySrcLoc,16190 field_src: LazySrcLoc,
16191 ty_src: LazySrcLoc,16191 ty_src: LazySrcLoc,
16192) CompileError!Air.Inst.Ref {16192) CompileError!Air.Inst.Ref {
16193 const resolved_ty = try sema.resolveTypeFields(block, ty_src, aggregate_ty);16193 var cur_ty = aggregate_ty;
16194 var cur_ty = resolved_ty;
16195 while (true) {16194 while (true) {
16195 const resolved_ty = try sema.resolveTypeFields(block, ty_src, cur_ty);
16196 cur_ty = resolved_ty;
16196 switch (cur_ty.zigTypeTag()) {16197 switch (cur_ty.zigTypeTag()) {
16197 .Struct => {16198 .Struct => {
16198 if (cur_ty.isAnonStruct()) {16199 if (cur_ty.isAnonStruct()) {
test/behavior.zig+1
...@@ -84,6 +84,7 @@ test {...@@ -84,6 +84,7 @@ test {
84 _ = @import("behavior/bugs/12003.zig");84 _ = @import("behavior/bugs/12003.zig");
85 _ = @import("behavior/bugs/12033.zig");85 _ = @import("behavior/bugs/12033.zig");
86 _ = @import("behavior/bugs/12430.zig");86 _ = @import("behavior/bugs/12430.zig");
87 _ = @import("behavior/bugs/12486.zig");
87 _ = @import("behavior/byteswap.zig");88 _ = @import("behavior/byteswap.zig");
88 _ = @import("behavior/byval_arg_var.zig");89 _ = @import("behavior/byval_arg_var.zig");
89 _ = @import("behavior/call.zig");90 _ = @import("behavior/call.zig");
test/behavior/bugs/12486.zig created+49
...@@ -0,0 +1,49 @@
1const SomeEnum = union(enum) {
2 EnumVariant: u8,
3};
4
5const SomeStruct = struct {
6 struct_field: u8,
7};
8
9const OptEnum = struct {
10 opt_enum: ?SomeEnum,
11};
12
13const ErrEnum = struct {
14 err_enum: anyerror!SomeEnum,
15};
16
17const OptStruct = struct {
18 opt_struct: ?SomeStruct,
19};
20
21const ErrStruct = struct {
22 err_struct: anyerror!SomeStruct,
23};
24
25test {
26 _ = OptEnum{
27 .opt_enum = .{
28 .EnumVariant = 1,
29 },
30 };
31
32 _ = ErrEnum{
33 .err_enum = .{
34 .EnumVariant = 1,
35 },
36 };
37
38 _ = OptStruct{
39 .opt_struct = .{
40 .struct_field = 1,
41 },
42 };
43
44 _ = ErrStruct{
45 .err_struct = .{
46 .struct_field = 1,
47 },
48 };
49}