authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-29 22:02:11+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-31 09:55:03+00:00
log71bcbd59936a514843265756ae4ad03a8d656c3c
treef3d1aac6a545bf37ff3138777eadab2e7c87f4b9
parent9a70eeeac52f1526b9df45614b86912565e53e54
signature Commit is signed but in an unrecognized format.

AstGen: add missing comptimeExpr calls

Some sub-expressions should always be evaluated at comptime -- in particular, type expressions, e.g. `E` in `E!T`. However, bugs in this logic are easy to miss, because the parent scope is usually comptime anyway!

1 files changed, 19 insertions(+), 8 deletions(-)

lib/std/zig/AstGen.zig+19-8
...@@ -789,8 +789,17 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -789,8 +789,17 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
789 return rvalue(gz, ri, result, node);789 return rvalue(gz, ri, result, node);
790 },790 },
791791
792 .error_union => return simpleBinOp(gz, scope, ri, node, .error_union_type),792 .error_union, .merge_error_sets => |tag| {
793 .merge_error_sets => return simpleBinOp(gz, scope, ri, node, .merge_error_sets),793 const inst_tag: Zir.Inst.Tag = switch (tag) {
794 .error_union => .error_union_type,
795 .merge_error_sets => .merge_error_sets,
796 else => unreachable,
797 };
798 const lhs = try reachableTypeExpr(gz, scope, node_datas[node].lhs, node);
799 const rhs = try reachableTypeExpr(gz, scope, node_datas[node].rhs, node);
800 const result = try gz.addPlNode(inst_tag, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
801 return rvalue(gz, ri, result, node);
802 },
794803
795 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),804 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),
796 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),805 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),
...@@ -1366,6 +1375,7 @@ fn fnProtoExprInner(...@@ -1366,6 +1375,7 @@ fn fnProtoExprInner(
1366 assert(param_type_node != 0);1375 assert(param_type_node != 0);
1367 var param_gz = block_scope.makeSubBlock(scope);1376 var param_gz = block_scope.makeSubBlock(scope);
1368 defer param_gz.unstack();1377 defer param_gz.unstack();
1378 param_gz.is_comptime = true;
1369 const param_type = try fullBodyExpr(&param_gz, scope, coerced_type_ri, param_type_node, .normal);1379 const param_type = try fullBodyExpr(&param_gz, scope, coerced_type_ri, param_type_node, .normal);
1370 const param_inst_expected: Zir.Inst.Index = @enumFromInt(astgen.instructions.len + 1);1380 const param_inst_expected: Zir.Inst.Index = @enumFromInt(astgen.instructions.len + 1);
1371 _ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);1381 _ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);
...@@ -1382,18 +1392,19 @@ fn fnProtoExprInner(...@@ -1382,18 +1392,19 @@ fn fnProtoExprInner(
1382 };1392 };
13831393
1384 const cc: Zir.Inst.Ref = if (fn_proto.ast.callconv_expr != 0)1394 const cc: Zir.Inst.Ref = if (fn_proto.ast.callconv_expr != 0)
1385 try expr(1395 try comptimeExpr(
1386 &block_scope,1396 &block_scope,
1387 scope,1397 scope,
1388 .{ .rl = .{ .coerced_ty = try block_scope.addBuiltinValue(fn_proto.ast.callconv_expr, .calling_convention) } },1398 .{ .rl = .{ .coerced_ty = try block_scope.addBuiltinValue(fn_proto.ast.callconv_expr, .calling_convention) } },
1389 fn_proto.ast.callconv_expr,1399 fn_proto.ast.callconv_expr,
1400 .@"callconv",
1390 )1401 )
1391 else if (implicit_ccc)1402 else if (implicit_ccc)
1392 try block_scope.addBuiltinValue(node, .calling_convention_c)1403 try block_scope.addBuiltinValue(node, .calling_convention_c)
1393 else1404 else
1394 .none;1405 .none;
13951406
1396 const ret_ty = try expr(&block_scope, scope, coerced_type_ri, fn_proto.ast.return_type);1407 const ret_ty = try comptimeExpr(&block_scope, scope, coerced_type_ri, fn_proto.ast.return_type, .function_ret_ty);
13971408
1398 const result = try block_scope.addFunc(.{1409 const result = try block_scope.addFunc(.{
1399 .src_node = fn_proto.ast.proto_node,1410 .src_node = fn_proto.ast.proto_node,
...@@ -3916,7 +3927,7 @@ fn ptrType(...@@ -3916,7 +3927,7 @@ fn ptrType(
3916 gz.astgen.source_column = source_column;3927 gz.astgen.source_column = source_column;
39173928
3918 const addrspace_ty = try gz.addBuiltinValue(ptr_info.ast.addrspace_node, .address_space);3929 const addrspace_ty = try gz.addBuiltinValue(ptr_info.ast.addrspace_node, .address_space);
3919 addrspace_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, ptr_info.ast.addrspace_node);3930 addrspace_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, ptr_info.ast.addrspace_node, .@"addrspace");
3920 trailing_count += 1;3931 trailing_count += 1;
3921 }3932 }
3922 if (ptr_info.ast.align_node != 0) {3933 if (ptr_info.ast.align_node != 0) {
...@@ -3924,13 +3935,13 @@ fn ptrType(...@@ -3924,13 +3935,13 @@ fn ptrType(
3924 gz.astgen.source_line = source_line;3935 gz.astgen.source_line = source_line;
3925 gz.astgen.source_column = source_column;3936 gz.astgen.source_column = source_column;
39263937
3927 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);3938 align_ref = try comptimeExpr(gz, scope, coerced_align_ri, ptr_info.ast.align_node, .@"align");
3928 trailing_count += 1;3939 trailing_count += 1;
3929 }3940 }
3930 if (ptr_info.ast.bit_range_start != 0) {3941 if (ptr_info.ast.bit_range_start != 0) {
3931 assert(ptr_info.ast.bit_range_end != 0);3942 assert(ptr_info.ast.bit_range_end != 0);
3932 bit_start_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start);3943 bit_start_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start, .type);
3933 bit_end_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end);3944 bit_end_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end, .type);
3934 trailing_count += 2;3945 trailing_count += 2;
3935 }3946 }
39363947