Skip to content

Commit 5b130ec

Browse files
committed
Sema: rewrite semantic analysis of function calls
This rewrite improves some error messages, hugely simplifies the logic, and fixes several bugs. One of these bugs is technically a new rule which Andrew and I agreed on: if a parameter has a comptime-only type but is not declared `comptime`, then the corresponding call argument should not be *evaluated* at comptime; only resolved. Implementing this required changing how function types work a little, which in turn required allowing a new kind of function coercion for some generic use cases: function coercions are now allowed to implicitly *remove* `comptime` annotations from parameters with comptime-only types. This is okay because removing the annotation affects only the call site. Resolves: #22262
1 parent 136c5a9 commit 5b130ec

32 files changed

+795
-1176
lines changed

lib/compiler/aro_translate_c.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub fn translate(
168168
context.pattern_list.deinit(gpa);
169169
}
170170

171+
@setEvalBranchQuota(2000);
171172
inline for (@typeInfo(std.zig.c_builtins).@"struct".decls) |decl| {
172173
const builtin_fn = try ZigTag.pub_var_simple.create(arena, .{
173174
.name = decl.name,

lib/std/zig.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,8 @@ pub const SimpleComptimeReason = enum(u32) {
749749
array_mul_factor,
750750
slice_cat_operand,
751751
comptime_call_target,
752+
inline_call_target,
753+
generic_call_target,
752754
wasm_memory_index,
753755
work_group_dim_index,
754756

@@ -791,7 +793,6 @@ pub const SimpleComptimeReason = enum(u32) {
791793
struct_field_default_value,
792794
enum_field_tag_value,
793795
slice_single_item_ptr_bounds,
794-
comptime_param_arg,
795796
stored_to_comptime_field,
796797
stored_to_comptime_var,
797798
casted_to_comptime_enum,
@@ -828,6 +829,8 @@ pub const SimpleComptimeReason = enum(u32) {
828829
.array_mul_factor => "array multiplication factor must be comptime-known",
829830
.slice_cat_operand => "slice being concatenated must be comptime-known",
830831
.comptime_call_target => "function being called at comptime must be comptime-known",
832+
.inline_call_target => "function being called inline must be comptime-known",
833+
.generic_call_target => "generic function being called must be comptime-known",
831834
.wasm_memory_index => "wasm memory index must be comptime-known",
832835
.work_group_dim_index => "work group dimension index must be comptime-known",
833836

@@ -865,7 +868,6 @@ pub const SimpleComptimeReason = enum(u32) {
865868
.struct_field_default_value => "struct field default value must be comptime-known",
866869
.enum_field_tag_value => "enum field tag value must be comptime-known",
867870
.slice_single_item_ptr_bounds => "slice of single-item pointer must have comptime-known bounds",
868-
.comptime_param_arg => "argument to comptime parameter must be comptime-known",
869871
.stored_to_comptime_field => "value stored to a comptime field must be comptime-known",
870872
.stored_to_comptime_var => "value stored to a comptime variable must be comptime-known",
871873
.casted_to_comptime_enum => "value casted to enum with 'comptime_int' tag type must be comptime-known",

lib/std/zig/AstGen.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10209,9 +10209,6 @@ fn callExpr(
1020910209

1021010210
const callee = try calleeExpr(gz, scope, ri.rl, call.ast.fn_expr);
1021110211
const modifier: std.builtin.CallModifier = blk: {
10212-
if (gz.is_comptime) {
10213-
break :blk .compile_time;
10214-
}
1021510212
if (call.async_token != null) {
1021610213
break :blk .async_kw;
1021710214
}

lib/std/zig/Zir.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4735,6 +4735,7 @@ pub const FnInfo = struct {
47354735
body: []const Inst.Index,
47364736
ret_ty_ref: Zir.Inst.Ref,
47374737
total_params_len: u32,
4738+
inferred_error_set: bool,
47384739
};
47394740

47404741
pub fn getParamBody(zir: Zir, fn_inst: Inst.Index) []const Zir.Inst.Index {
@@ -4774,8 +4775,9 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
47744775
body: []const Inst.Index,
47754776
ret_ty_ref: Inst.Ref,
47764777
ret_ty_body: []const Inst.Index,
4778+
ies: bool,
47774779
} = switch (tags[@intFromEnum(fn_inst)]) {
4778-
.func, .func_inferred => blk: {
4780+
.func, .func_inferred => |tag| blk: {
47794781
const inst_data = datas[@intFromEnum(fn_inst)].pl_node;
47804782
const extra = zir.extraData(Inst.Func, inst_data.payload_index);
47814783

@@ -4805,14 +4807,15 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
48054807
.ret_ty_ref = ret_ty_ref,
48064808
.ret_ty_body = ret_ty_body,
48074809
.body = body,
4810+
.ies = tag == .func_inferred,
48084811
};
48094812
},
48104813
.func_fancy => blk: {
48114814
const inst_data = datas[@intFromEnum(fn_inst)].pl_node;
48124815
const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index);
48134816

48144817
var extra_index: usize = extra.end;
4815-
var ret_ty_ref: Inst.Ref = .void_type;
4818+
var ret_ty_ref: Inst.Ref = .none;
48164819
var ret_ty_body: []const Inst.Index = &.{};
48174820

48184821
if (extra.data.bits.has_cc_body) {
@@ -4828,6 +4831,8 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
48284831
} else if (extra.data.bits.has_ret_ty_ref) {
48294832
ret_ty_ref = @enumFromInt(zir.extra[extra_index]);
48304833
extra_index += 1;
4834+
} else {
4835+
ret_ty_ref = .void_type;
48314836
}
48324837

48334838
extra_index += @intFromBool(extra.data.bits.has_any_noalias);
@@ -4839,6 +4844,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
48394844
.ret_ty_ref = ret_ty_ref,
48404845
.ret_ty_body = ret_ty_body,
48414846
.body = body,
4847+
.ies = extra.data.bits.is_inferred_error,
48424848
};
48434849
},
48444850
else => unreachable,
@@ -4860,6 +4866,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
48604866
.ret_ty_ref = info.ret_ty_ref,
48614867
.body = info.body,
48624868
.total_params_len = total_params_len,
4869+
.inferred_error_set = info.ies,
48634870
};
48644871
}
48654872

0 commit comments

Comments
 (0)