authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-18 17:12:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-18 19:02:06-07:00
log0153f3a8f9b93ebef7b5cd70db8560fcac658ce7
tree7d4b799c06a0fd3470667051f3271abbde096e8b
parent47499bf47ba73ab49503298a87123b9e873e3693

Sema: fix crash: array_in_c_exported_function

Fuck it, we're storing decl indexes in LazySrcLoc now.

3 files changed, 28 insertions(+), 31 deletions(-)

src/Module.zig+11-5
......@@ -1821,8 +1821,8 @@ pub const SrcLoc = struct {
18211821 return tree.firstToken(src_loc.parent_decl_node);
18221822 }
18231823
1824 pub fn declRelativeToNodeIndex(src_loc: SrcLoc, offset: i32) Ast.TokenIndex {
1825 return @as(Ast.Node.Index, @bitCast(offset + @as(i32, @bitCast(src_loc.parent_decl_node))));
1824 pub fn declRelativeToNodeIndex(src_loc: SrcLoc, offset: i32) Ast.Node.Index {
1825 return @bitCast(offset + @as(i32, @bitCast(src_loc.parent_decl_node)));
18261826 }
18271827
18281828 pub const Span = struct {
......@@ -2829,14 +2829,15 @@ pub const LazySrcLoc = union(enum) {
28292829 /// The Decl is determined contextually.
28302830 for_capture_from_input: i32,
28312831 /// The source location points to the argument node of a function call.
2832 /// The Decl is determined contextually.
28332832 call_arg: struct {
2833 decl: Decl.Index,
28342834 /// Points to the function call AST node.
28352835 call_node_offset: i32,
28362836 /// The index of the argument the source location points to.
28372837 arg_index: u32,
28382838 },
28392839 fn_proto_param: struct {
2840 decl: Decl.Index,
28402841 /// Points to the function prototype AST node.
28412842 fn_proto_node_offset: i32,
28422843 /// The index of the parameter the source location points to.
......@@ -2931,13 +2932,18 @@ pub const LazySrcLoc = union(enum) {
29312932 .node_offset_store_operand,
29322933 .for_input,
29332934 .for_capture_from_input,
2934 .call_arg,
2935 .fn_proto_param,
29362935 => .{
29372936 .file_scope = decl.getFileScope(mod),
29382937 .parent_decl_node = decl.src_node,
29392938 .lazy = lazy,
29402939 },
2940 inline .call_arg,
2941 .fn_proto_param,
2942 => |x| .{
2943 .file_scope = decl.getFileScope(mod),
2944 .parent_decl_node = mod.declPtr(x.decl).src_node,
2945 .lazy = lazy,
2946 },
29412947 };
29422948 }
29432949};
src/Sema.zig+15-24
......@@ -6997,10 +6997,14 @@ fn analyzeCall(
69976997 var has_comptime_args = false;
69986998 var arg_i: u32 = 0;
69996999 for (fn_info.param_body) |inst| {
7000 const arg_src: LazySrcLoc = .{ .call_arg = .{
7001 .call_node_offset = call_src.node_offset.x,
7002 .arg_index = arg_i,
7003 } };
7000 const arg_src: LazySrcLoc = if (arg_i == 0 and bound_arg_src != null)
7001 bound_arg_src.?
7002 else
7003 .{ .call_arg = .{
7004 .decl = block.src_decl,
7005 .call_node_offset = call_src.node_offset.x,
7006 .arg_index = arg_i - @intFromBool(bound_arg_src != null),
7007 } };
70047008 try sema.analyzeInlineCallArg(
70057009 block,
70067010 &child_block,
......@@ -7356,7 +7360,7 @@ fn analyzeInlineCallArg(
73567360 }
73577361 const casted_arg = sema.coerceExtra(arg_block, param_ty.toType(), uncasted_arg, arg_src, .{ .param_src = .{
73587362 .func_inst = func_inst,
7359 .param_i = @as(u32, @intCast(arg_i.*)),
7363 .param_i = @intCast(arg_i.*),
73607364 } }) catch |err| switch (err) {
73617365 error.NotCoercible => unreachable,
73627366 else => |e| return e,
......@@ -7586,6 +7590,7 @@ fn instantiateGenericCall(
75867590 const arg_src: LazySrcLoc = if (total_i == 0 and bound_arg_src != null)
75877591 bound_arg_src.?
75887592 else if (call_src == .node_offset) .{ .call_arg = .{
7593 .decl = block.src_decl,
75897594 .call_node_offset = call_src.node_offset.x,
75907595 .arg_index = @intCast(total_i),
75917596 } } else .unneeded;
......@@ -8729,6 +8734,7 @@ fn funcCommon(
87298734 break :blk @as(u1, @truncate(noalias_bits >> index)) != 0;
87308735 };
87318736 const param_src: LazySrcLoc = .{ .fn_proto_param = .{
8737 .decl = block.src_decl,
87328738 .fn_proto_node_offset = src_node_offset,
87338739 .param_index = @intCast(i),
87348740 } };
......@@ -9316,9 +9322,10 @@ fn zirParamAnytype(
93169322 return;
93179323 }
93189324 const arg_src: LazySrcLoc = if (sema.generic_call_src == .node_offset) .{ .call_arg = .{
9325 .decl = sema.generic_call_decl.unwrap().?,
93199326 .call_node_offset = sema.generic_call_src.node_offset.x,
93209327 .arg_index = param_index,
9321 } } else .unneeded;
9328 } } else src;
93229329
93239330 if (comptime_syntax) {
93249331 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {
......@@ -9326,15 +9333,7 @@ fn zirParamAnytype(
93269333 return;
93279334 }
93289335 const msg = msg: {
9329 const fallback_src = src.toSrcLoc(mod.declPtr(block.src_decl), mod);
9330 const src_loc = if (sema.generic_call_decl.unwrap()) |decl|
9331 if (arg_src != .unneeded)
9332 arg_src.toSrcLoc(mod.declPtr(decl), mod)
9333 else
9334 fallback_src
9335 else
9336 fallback_src;
9337
9336 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);
93389337 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
93399338 @as([]const u8, "runtime-known argument passed to comptime parameter"),
93409339 });
......@@ -9354,15 +9353,7 @@ fn zirParamAnytype(
93549353 return;
93559354 }
93569355 const msg = msg: {
9357 const fallback_src = src.toSrcLoc(mod.declPtr(block.src_decl), mod);
9358 const src_loc = if (sema.generic_call_decl.unwrap()) |decl|
9359 if (arg_src != .unneeded)
9360 arg_src.toSrcLoc(mod.declPtr(decl), mod)
9361 else
9362 fallback_src
9363 else
9364 fallback_src;
9365
9356 const src_loc = arg_src.toSrcLoc(mod.declPtr(block.src_decl), mod);
93669357 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
93679358 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),
93689359 });
test/cases/compile_errors/array_in_c_exported_function.zig+2-2
......@@ -10,7 +10,7 @@ export fn zig_return_array() [10]u8 {
1010// backend=stage2
1111// target=native
1212//
13// :1:21: error: parameter of type '[10]u8' not allowed in function with calling convention 'C'
14// :1:21: note: arrays are not allowed as a parameter type
13// :1:24: error: parameter of type '[10]u8' not allowed in function with calling convention 'C'
14// :1:24: note: arrays are not allowed as a parameter type
1515// :5:30: error: return type '[10]u8' not allowed in function with calling convention 'C'
1616// :5:30: note: arrays are not allowed as a return type