aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-03-10 17:09:23 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-03-10 17:52:18 -0700
commit273da9efd9238e20da1299789537b00de2f0ebd8 (patch)
tree54905594f55be9fd19a179ab1bcaeb1900d02bc1 /src/AstGen.zig
parenta30d283981ae31ed0212ef153f086e07daeffe65 (diff)
downloadzig-273da9efd9238e20da1299789537b00de2f0ebd8.tar.gz
zig-273da9efd9238e20da1299789537b00de2f0ebd8.zip
AstGen: structInitExpr and arrayInitExpr avoid crash
when an inferred alloc is passed as the result pointer of a block.
Diffstat (limited to 'src/AstGen.zig')
-rw-r--r--src/AstGen.zig31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index eb724934c9..97ca9f0a12 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -1360,6 +1360,12 @@ fn arrayInitExpr(
}
},
.block_ptr => |block_gz| {
+ // This condition is here for the same reason as the above condition in `inferred_ptr`.
+ // See corresponding logic in structInitExpr.
+ if (types.array == .none and astgen.isInferred(block_gz.rl_ptr)) {
+ const result = try arrayInitExprRlNone(gz, scope, node, array_init.ast.elements, .array_init_anon);
+ return rvalue(gz, rl, result, node);
+ }
return arrayInitExprRlPtr(gz, scope, rl, node, block_gz.rl_ptr, array_init.ast.elements, types.array);
},
}
@@ -1604,7 +1610,16 @@ fn structInitExpr(
return structInitExprRlPtr(gz, scope, rl, node, struct_init, ptr_inst);
}
},
- .block_ptr => |block_gz| return structInitExprRlPtr(gz, scope, rl, node, struct_init, block_gz.rl_ptr),
+ .block_ptr => |block_gz| {
+ // This condition is here for the same reason as the above condition in `inferred_ptr`.
+ // See corresponding logic in arrayInitExpr.
+ if (struct_init.ast.type_expr == 0 and astgen.isInferred(block_gz.rl_ptr)) {
+ const result = try structInitExprRlNone(gz, scope, node, struct_init, .struct_init_anon);
+ return rvalue(gz, rl, result, node);
+ }
+
+ return structInitExprRlPtr(gz, scope, rl, node, struct_init, block_gz.rl_ptr);
+ },
}
}
@@ -10938,3 +10953,17 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
}
return decl_count;
}
+
+fn isInferred(astgen: *AstGen, ref: Zir.Inst.Ref) bool {
+ const inst = refToIndex(ref) orelse return false;
+ const zir_tags = astgen.instructions.items(.tag);
+ return switch (zir_tags[inst]) {
+ .alloc_inferred,
+ .alloc_inferred_mut,
+ .alloc_inferred_comptime,
+ .alloc_inferred_comptime_mut,
+ => true,
+
+ else => false,
+ };
+}