Skip to content

Commit 71bcbd5

Browse files
committed
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 parent 9a70eee commit 71bcbd5

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/std/zig/AstGen.zig

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,17 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
789789
return rvalue(gz, ri, result, node);
790790
},
791791

792-
.error_union => return simpleBinOp(gz, scope, ri, node, .error_union_type),
793-
.merge_error_sets => return simpleBinOp(gz, scope, ri, node, .merge_error_sets),
792+
.error_union, .merge_error_sets => |tag| {
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

795804
.bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),
796805
.bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),
@@ -1366,6 +1375,7 @@ fn fnProtoExprInner(
13661375
assert(param_type_node != 0);
13671376
var param_gz = block_scope.makeSubBlock(scope);
13681377
defer param_gz.unstack();
1378+
param_gz.is_comptime = true;
13691379
const param_type = try fullBodyExpr(&param_gz, scope, coerced_type_ri, param_type_node, .normal);
13701380
const param_inst_expected: Zir.Inst.Index = @enumFromInt(astgen.instructions.len + 1);
13711381
_ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);
@@ -1382,18 +1392,19 @@ fn fnProtoExprInner(
13821392
};
13831393

13841394
const cc: Zir.Inst.Ref = if (fn_proto.ast.callconv_expr != 0)
1385-
try expr(
1395+
try comptimeExpr(
13861396
&block_scope,
13871397
scope,
13881398
.{ .rl = .{ .coerced_ty = try block_scope.addBuiltinValue(fn_proto.ast.callconv_expr, .calling_convention) } },
13891399
fn_proto.ast.callconv_expr,
1400+
.@"callconv",
13901401
)
13911402
else if (implicit_ccc)
13921403
try block_scope.addBuiltinValue(node, .calling_convention_c)
13931404
else
13941405
.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

13981409
const result = try block_scope.addFunc(.{
13991410
.src_node = fn_proto.ast.proto_node,
@@ -3916,21 +3927,21 @@ fn ptrType(
39163927
gz.astgen.source_column = source_column;
39173928

39183929
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");
39203931
trailing_count += 1;
39213932
}
39223933
if (ptr_info.ast.align_node != 0) {
39233934
gz.astgen.source_offset = source_offset;
39243935
gz.astgen.source_line = source_line;
39253936
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");
39283939
trailing_count += 1;
39293940
}
39303941
if (ptr_info.ast.bit_range_start != 0) {
39313942
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);
3933-
bit_end_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end);
3943+
bit_start_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start, .type);
3944+
bit_end_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end, .type);
39343945
trailing_count += 2;
39353946
}
39363947

0 commit comments

Comments
 (0)