authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-25 13:48:21+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-25 22:04:08+03:00
log2f34d06d01189ae6349e9c6341ba85ec50b92bb0
treeecf6ce792234a32bca10b0a1e145595f401b270d
parent370793a36b3de34ead8456553a2aa2c56f0d3de8

Sema: `analyzeInlineCallArg` needs a block for the arg and the param


3 files changed, 58 insertions(+), 22 deletions(-)

src/Module.zig+2
......@@ -5940,7 +5940,9 @@ pub fn argSrc(
59405940 gpa: Allocator,
59415941 decl: *Decl,
59425942 arg_i: usize,
5943 bound_arg_src: ?LazySrcLoc,
59435944) LazySrcLoc {
5945 if (arg_i == 0 and bound_arg_src != null) return bound_arg_src.?;
59445946 @setCold(true);
59455947 const tree = decl.getFileScope().getTree(gpa) catch |err| {
59465948 // In this case we emit a warning + a less precise source location.
src/Sema.zig+32-22
......@@ -5353,7 +5353,9 @@ fn zirCall(
53535353 const func_type = sema.typeOf(func);
53545354
53555355 // Desugar bound functions here
5356 var bound_arg_src: ?LazySrcLoc = null;
53565357 if (func_type.tag() == .bound_fn) {
5358 bound_arg_src = func_src;
53575359 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
53585360 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
53595361 func = bound_data.func_inst;
......@@ -5369,7 +5371,7 @@ fn zirCall(
53695371 }
53705372 }
53715373
5372 return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args);
5374 return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args, bound_arg_src);
53735375}
53745376
53755377const GenericCallAdapter = struct {
......@@ -5438,6 +5440,7 @@ fn analyzeCall(
54385440 modifier: std.builtin.CallOptions.Modifier,
54395441 ensure_result_used: bool,
54405442 uncasted_args: []const Air.Inst.Ref,
5443 bound_arg_src: ?LazySrcLoc,
54415444) CompileError!Air.Inst.Ref {
54425445 const mod = sema.mod;
54435446
......@@ -5532,6 +5535,7 @@ fn analyzeCall(
55325535 ensure_result_used,
55335536 uncasted_args,
55345537 call_tag,
5538 bound_arg_src,
55355539 )) |some| {
55365540 return some;
55375541 } else |err| switch (err) {
......@@ -5654,6 +5658,7 @@ fn analyzeCall(
56545658 var arg_i: usize = 0;
56555659 for (fn_info.param_body) |inst| {
56565660 sema.analyzeInlineCallArg(
5661 block,
56575662 &child_block,
56585663 .unneeded,
56595664 inst,
......@@ -5665,12 +5670,13 @@ fn analyzeCall(
56655670 memoized_call_key,
56665671 ) catch |err| switch (err) {
56675672 error.NeededSourceLocation => {
5673 sema.inst_map.clearRetainingCapacity();
56685674 const decl = sema.mod.declPtr(block.src_decl);
5675 child_block.src_decl = block.src_decl;
56695676 try sema.analyzeInlineCallArg(
5670 // Intentionally use the wrong block here since we know it's
5671 // going to fail and `argSrc` is relative to `block.src_decl`.
56725677 block,
5673 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, arg_i),
5678 &child_block,
5679 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, arg_i, bound_arg_src),
56745680 inst,
56755681 new_fn_info,
56765682 &arg_i,
......@@ -5832,7 +5838,7 @@ fn analyzeCall(
58325838 const decl = sema.mod.declPtr(block.src_decl);
58335839 _ = try sema.analyzeCallArg(
58345840 block,
5835 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, i),
5841 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, i, bound_arg_src),
58365842 param_ty,
58375843 uncasted_arg,
58385844 );
......@@ -5873,7 +5879,8 @@ fn analyzeCall(
58735879
58745880fn analyzeInlineCallArg(
58755881 sema: *Sema,
5876 block: *Block,
5882 arg_block: *Block,
5883 param_block: *Block,
58775884 arg_src: LazySrcLoc,
58785885 inst: Zir.Inst.Index,
58795886 new_fn_info: Type.Payload.Function.Data,
......@@ -5892,19 +5899,19 @@ fn analyzeInlineCallArg(
58925899 const param_src = pl_tok.src();
58935900 const extra = sema.code.extraData(Zir.Inst.Param, pl_tok.payload_index);
58945901 const param_body = sema.code.extra[extra.end..][0..extra.data.body_len];
5895 const param_ty_inst = try sema.resolveBody(block, param_body, inst);
5896 const param_ty = try sema.analyzeAsType(block, param_src, param_ty_inst);
5902 const param_ty_inst = try sema.resolveBody(param_block, param_body, inst);
5903 const param_ty = try sema.analyzeAsType(param_block, param_src, param_ty_inst);
58975904 new_fn_info.param_types[arg_i.*] = param_ty;
58985905 const uncasted_arg = uncasted_args[arg_i.*];
5899 if (try sema.typeRequiresComptime(block, arg_src, param_ty)) {
5900 _ = try sema.resolveConstMaybeUndefVal(block, arg_src, uncasted_arg, "argument to parameter with comptime only type must be comptime known");
5906 if (try sema.typeRequiresComptime(arg_block, arg_src, param_ty)) {
5907 _ = try sema.resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, "argument to parameter with comptime only type must be comptime known");
59015908 }
5902 const casted_arg = try sema.coerce(block, param_ty, uncasted_arg, arg_src);
5909 const casted_arg = try sema.coerce(arg_block, param_ty, uncasted_arg, arg_src);
59035910 try sema.inst_map.putNoClobber(sema.gpa, inst, casted_arg);
59045911
59055912 if (is_comptime_call) {
59065913 // TODO explain why function is being called at comptime
5907 const arg_val = try sema.resolveConstMaybeUndefVal(block, arg_src, casted_arg, "argument to function being called at comptime must be comptime known");
5914 const arg_val = try sema.resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, "argument to function being called at comptime must be comptime known");
59085915 switch (arg_val.tag()) {
59095916 .generic_poison, .generic_poison_type => {
59105917 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -5915,7 +5922,7 @@ fn analyzeInlineCallArg(
59155922 // Needed so that lazy values do not trigger
59165923 // assertion due to type not being resolved
59175924 // when the hash function is called.
5918 try sema.resolveLazyValue(block, arg_src, arg_val);
5925 try sema.resolveLazyValue(arg_block, arg_src, arg_val);
59195926 },
59205927 }
59215928 should_memoize.* = should_memoize.* and !arg_val.canMutateComptimeVarState();
......@@ -5935,7 +5942,7 @@ fn analyzeInlineCallArg(
59355942
59365943 if (is_comptime_call) {
59375944 // TODO explain why function is being called at comptime
5938 const arg_val = try sema.resolveConstMaybeUndefVal(block, arg_src, uncasted_arg, "argument to function being called at comptime must be comptime known");
5945 const arg_val = try sema.resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, "argument to function being called at comptime must be comptime known");
59395946 switch (arg_val.tag()) {
59405947 .generic_poison, .generic_poison_type => {
59415948 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -5946,7 +5953,7 @@ fn analyzeInlineCallArg(
59465953 // Needed so that lazy values do not trigger
59475954 // assertion due to type not being resolved
59485955 // when the hash function is called.
5949 try sema.resolveLazyValue(block, arg_src, arg_val);
5956 try sema.resolveLazyValue(arg_block, arg_src, arg_val);
59505957 },
59515958 }
59525959 should_memoize.* = should_memoize.* and !arg_val.canMutateComptimeVarState();
......@@ -6011,6 +6018,7 @@ fn instantiateGenericCall(
60116018 ensure_result_used: bool,
60126019 uncasted_args: []const Air.Inst.Ref,
60136020 call_tag: Air.Inst.Tag,
6021 bound_arg_src: ?LazySrcLoc,
60146022) CompileError!Air.Inst.Ref {
60156023 const mod = sema.mod;
60166024 const gpa = sema.gpa;
......@@ -6070,7 +6078,7 @@ fn instantiateGenericCall(
60706078 const arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, uncasted_args[i]) catch |err| switch (err) {
60716079 error.NeededSourceLocation => {
60726080 const decl = sema.mod.declPtr(block.src_decl);
6073 const arg_src = Module.argSrc(call_src.node_offset.x, sema.gpa, decl, i);
6081 const arg_src = Module.argSrc(call_src.node_offset.x, sema.gpa, decl, i, bound_arg_src);
60746082 _ = try sema.analyzeGenericCallArgVal(block, arg_src, uncasted_args[i]);
60756083 return error.AnalysisFail;
60766084 },
......@@ -6392,7 +6400,7 @@ fn instantiateGenericCall(
63926400 const decl = sema.mod.declPtr(block.src_decl);
63936401 _ = try sema.analyzeGenericCallArg(
63946402 block,
6395 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, total_i),
6403 Module.argSrc(call_src.node_offset.x, sema.gpa, decl, total_i, bound_arg_src),
63966404 uncasted_args[total_i],
63976405 comptime_args[total_i],
63986406 runtime_args,
......@@ -14263,7 +14271,7 @@ fn analyzeRet(
1426314271 const ptr_stack_trace_ty = try Type.Tag.optional_single_mut_pointer.create(sema.arena, stack_trace_ty);
1426414272 const err_return_trace = try block.addTy(.err_return_trace, ptr_stack_trace_ty);
1426514273 const args: [1]Air.Inst.Ref = .{err_return_trace};
14266 _ = try sema.analyzeCall(block, return_err_fn, src, src, .never_inline, false, &args);
14274 _ = try sema.analyzeCall(block, return_err_fn, src, src, .never_inline, false, &args, null);
1426714275 }
1426814276
1426914277 try sema.resolveTypeLayout(block, src, sema.fn_ret_ty);
......@@ -17880,7 +17888,9 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1788017888 var resolved_args: []Air.Inst.Ref = undefined;
1788117889
1788217890 // Desugar bound functions here
17891 var bound_arg_src: ?LazySrcLoc = null;
1788317892 if (sema.typeOf(func).tag() == .bound_fn) {
17893 bound_arg_src = func_src;
1788417894 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
1788517895 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
1788617896 func = bound_data.func_inst;
......@@ -17896,7 +17906,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1789617906 }
1789717907 }
1789817908 const ensure_result_used = extra.flags.ensure_result_used;
17899 return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args);
17909 return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args, bound_arg_src);
1790017910}
1790117911
1790217912fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -19182,7 +19192,7 @@ fn panicWithMsg(
1918219192 Value.@"null",
1918319193 );
1918419194 const args: [2]Air.Inst.Ref = .{ msg_inst, null_stack_trace };
19185 _ = try sema.analyzeCall(block, panic_fn, src, src, .auto, false, &args);
19195 _ = try sema.analyzeCall(block, panic_fn, src, src, .auto, false, &args, null);
1918619196 return always_noreturn;
1918719197}
1918819198
......@@ -19223,7 +19233,7 @@ fn panicUnwrapError(
1922319233 const err = try fail_block.addTyOp(unwrap_err_tag, Type.anyerror, operand);
1922419234 const err_return_trace = try sema.getErrorReturnTrace(&fail_block, src);
1922519235 const args: [2]Air.Inst.Ref = .{ err_return_trace, err };
19226 _ = try sema.analyzeCall(&fail_block, panic_fn, src, src, .auto, false, &args);
19236 _ = try sema.analyzeCall(&fail_block, panic_fn, src, src, .auto, false, &args, null);
1922719237 }
1922819238 }
1922919239 try sema.addSafetyCheckExtra(parent_block, ok, &fail_block);
......@@ -19264,7 +19274,7 @@ fn panicIndexOutOfBounds(
1926419274 } else {
1926519275 const panic_fn = try sema.getBuiltin(&fail_block, src, "panicOutOfBounds");
1926619276 const args: [2]Air.Inst.Ref = .{ index, len };
19267 _ = try sema.analyzeCall(&fail_block, panic_fn, src, src, .auto, false, &args);
19277 _ = try sema.analyzeCall(&fail_block, panic_fn, src, src, .auto, false, &args, null);
1926819278 }
1926919279 }
1927019280 try sema.addSafetyCheckExtra(parent_block, ok, &fail_block);
test/compile_errors.zig+24
......@@ -183,6 +183,30 @@ pub fn addCases(ctx: *TestContext) !void {
183183 });
184184 }
185185
186 {
187 const case = ctx.obj("argument causes error ", .{});
188 case.backend = .stage2;
189
190 case.addSourceFile("b.zig",
191 \\pub const ElfDynLib = struct {
192 \\ pub fn lookup(self: *ElfDynLib, comptime T: type) ?T {
193 \\ _ = self;
194 \\ return undefined;
195 \\ }
196 \\};
197 );
198
199 case.addError(
200 \\pub export fn entry() void {
201 \\ var lib: @import("b.zig").ElfDynLib = undefined;
202 \\ _ = lib.lookup(fn () void);
203 \\}
204 , &[_][]const u8{
205 ":3:12: error: unable to resolve comptime value",
206 ":3:12: note: argument to function being called at comptime must be comptime known",
207 });
208 }
209
186210 // TODO test this in stage2, but we won't even try in stage1
187211 //ctx.objErrStage1("inline fn calls itself indirectly",
188212 // \\export fn foo() void {