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 {...@@ -1821,8 +1821,8 @@ pub const SrcLoc = struct {
1821 return tree.firstToken(src_loc.parent_decl_node);1821 return tree.firstToken(src_loc.parent_decl_node);
1822 }1822 }
18231823
1824 pub fn declRelativeToNodeIndex(src_loc: SrcLoc, offset: i32) Ast.TokenIndex {1824 pub fn declRelativeToNodeIndex(src_loc: SrcLoc, offset: i32) Ast.Node.Index {
1825 return @as(Ast.Node.Index, @bitCast(offset + @as(i32, @bitCast(src_loc.parent_decl_node))));1825 return @bitCast(offset + @as(i32, @bitCast(src_loc.parent_decl_node)));
1826 }1826 }
18271827
1828 pub const Span = struct {1828 pub const Span = struct {
...@@ -2829,14 +2829,15 @@ pub const LazySrcLoc = union(enum) {...@@ -2829,14 +2829,15 @@ pub const LazySrcLoc = union(enum) {
2829 /// The Decl is determined contextually.2829 /// The Decl is determined contextually.
2830 for_capture_from_input: i32,2830 for_capture_from_input: i32,
2831 /// The source location points to the argument node of a function call.2831 /// The source location points to the argument node of a function call.
2832 /// The Decl is determined contextually.
2833 call_arg: struct {2832 call_arg: struct {
2833 decl: Decl.Index,
2834 /// Points to the function call AST node.2834 /// Points to the function call AST node.
2835 call_node_offset: i32,2835 call_node_offset: i32,
2836 /// The index of the argument the source location points to.2836 /// The index of the argument the source location points to.
2837 arg_index: u32,2837 arg_index: u32,
2838 },2838 },
2839 fn_proto_param: struct {2839 fn_proto_param: struct {
2840 decl: Decl.Index,
2840 /// Points to the function prototype AST node.2841 /// Points to the function prototype AST node.
2841 fn_proto_node_offset: i32,2842 fn_proto_node_offset: i32,
2842 /// The index of the parameter the source location points to.2843 /// The index of the parameter the source location points to.
...@@ -2931,13 +2932,18 @@ pub const LazySrcLoc = union(enum) {...@@ -2931,13 +2932,18 @@ pub const LazySrcLoc = union(enum) {
2931 .node_offset_store_operand,2932 .node_offset_store_operand,
2932 .for_input,2933 .for_input,
2933 .for_capture_from_input,2934 .for_capture_from_input,
2934 .call_arg,
2935 .fn_proto_param,
2936 => .{2935 => .{
2937 .file_scope = decl.getFileScope(mod),2936 .file_scope = decl.getFileScope(mod),
2938 .parent_decl_node = decl.src_node,2937 .parent_decl_node = decl.src_node,
2939 .lazy = lazy,2938 .lazy = lazy,
2940 },2939 },
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 },
2941 };2947 };
2942 }2948 }
2943};2949};
src/Sema.zig+15-24
...@@ -6997,10 +6997,14 @@ fn analyzeCall(...@@ -6997,10 +6997,14 @@ fn analyzeCall(
6997 var has_comptime_args = false;6997 var has_comptime_args = false;
6998 var arg_i: u32 = 0;6998 var arg_i: u32 = 0;
6999 for (fn_info.param_body) |inst| {6999 for (fn_info.param_body) |inst| {
7000 const arg_src: LazySrcLoc = .{ .call_arg = .{7000 const arg_src: LazySrcLoc = if (arg_i == 0 and bound_arg_src != null)
7001 .call_node_offset = call_src.node_offset.x,7001 bound_arg_src.?
7002 .arg_index = arg_i,7002 else
7003 } };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 } };
7004 try sema.analyzeInlineCallArg(7008 try sema.analyzeInlineCallArg(
7005 block,7009 block,
7006 &child_block,7010 &child_block,
...@@ -7356,7 +7360,7 @@ fn analyzeInlineCallArg(...@@ -7356,7 +7360,7 @@ fn analyzeInlineCallArg(
7356 }7360 }
7357 const casted_arg = sema.coerceExtra(arg_block, param_ty.toType(), uncasted_arg, arg_src, .{ .param_src = .{7361 const casted_arg = sema.coerceExtra(arg_block, param_ty.toType(), uncasted_arg, arg_src, .{ .param_src = .{
7358 .func_inst = func_inst,7362 .func_inst = func_inst,
7359 .param_i = @as(u32, @intCast(arg_i.*)),7363 .param_i = @intCast(arg_i.*),
7360 } }) catch |err| switch (err) {7364 } }) catch |err| switch (err) {
7361 error.NotCoercible => unreachable,7365 error.NotCoercible => unreachable,
7362 else => |e| return e,7366 else => |e| return e,
...@@ -7586,6 +7590,7 @@ fn instantiateGenericCall(...@@ -7586,6 +7590,7 @@ fn instantiateGenericCall(
7586 const arg_src: LazySrcLoc = if (total_i == 0 and bound_arg_src != null)7590 const arg_src: LazySrcLoc = if (total_i == 0 and bound_arg_src != null)
7587 bound_arg_src.?7591 bound_arg_src.?
7588 else if (call_src == .node_offset) .{ .call_arg = .{7592 else if (call_src == .node_offset) .{ .call_arg = .{
7593 .decl = block.src_decl,
7589 .call_node_offset = call_src.node_offset.x,7594 .call_node_offset = call_src.node_offset.x,
7590 .arg_index = @intCast(total_i),7595 .arg_index = @intCast(total_i),
7591 } } else .unneeded;7596 } } else .unneeded;
...@@ -8729,6 +8734,7 @@ fn funcCommon(...@@ -8729,6 +8734,7 @@ fn funcCommon(
8729 break :blk @as(u1, @truncate(noalias_bits >> index)) != 0;8734 break :blk @as(u1, @truncate(noalias_bits >> index)) != 0;
8730 };8735 };
8731 const param_src: LazySrcLoc = .{ .fn_proto_param = .{8736 const param_src: LazySrcLoc = .{ .fn_proto_param = .{
8737 .decl = block.src_decl,
8732 .fn_proto_node_offset = src_node_offset,8738 .fn_proto_node_offset = src_node_offset,
8733 .param_index = @intCast(i),8739 .param_index = @intCast(i),
8734 } };8740 } };
...@@ -9316,9 +9322,10 @@ fn zirParamAnytype(...@@ -9316,9 +9322,10 @@ fn zirParamAnytype(
9316 return;9322 return;
9317 }9323 }
9318 const arg_src: LazySrcLoc = if (sema.generic_call_src == .node_offset) .{ .call_arg = .{9324 const arg_src: LazySrcLoc = if (sema.generic_call_src == .node_offset) .{ .call_arg = .{
9325 .decl = sema.generic_call_decl.unwrap().?,
9319 .call_node_offset = sema.generic_call_src.node_offset.x,9326 .call_node_offset = sema.generic_call_src.node_offset.x,
9320 .arg_index = param_index,9327 .arg_index = param_index,
9321 } } else .unneeded;9328 } } else src;
93229329
9323 if (comptime_syntax) {9330 if (comptime_syntax) {
9324 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {9331 if (try sema.resolveMaybeUndefVal(air_ref)) |val| {
...@@ -9326,15 +9333,7 @@ fn zirParamAnytype(...@@ -9326,15 +9333,7 @@ fn zirParamAnytype(
9326 return;9333 return;
9327 }9334 }
9328 const msg = msg: {9335 const msg = msg: {
9329 const fallback_src = src.toSrcLoc(mod.declPtr(block.src_decl), mod);9336 const src_loc = arg_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
9338 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{9337 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9339 @as([]const u8, "runtime-known argument passed to comptime parameter"),9338 @as([]const u8, "runtime-known argument passed to comptime parameter"),
9340 });9339 });
...@@ -9354,15 +9353,7 @@ fn zirParamAnytype(...@@ -9354,15 +9353,7 @@ fn zirParamAnytype(
9354 return;9353 return;
9355 }9354 }
9356 const msg = msg: {9355 const msg = msg: {
9357 const fallback_src = src.toSrcLoc(mod.declPtr(block.src_decl), mod);9356 const src_loc = arg_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
9366 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{9357 const msg = try Module.ErrorMsg.create(gpa, src_loc, "{s}", .{
9367 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),9358 @as([]const u8, "runtime-known argument passed to comptime-only type parameter"),
9368 });9359 });
test/cases/compile_errors/array_in_c_exported_function.zig+2-2
...@@ -10,7 +10,7 @@ export fn zig_return_array() [10]u8 {...@@ -10,7 +10,7 @@ export fn zig_return_array() [10]u8 {
10// backend=stage210// backend=stage2
11// target=native11// target=native
12//12//
13// :1:21: error: parameter of type '[10]u8' not allowed in function with calling convention 'C'13// :1:24: 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 type14// :1:24: note: arrays are not allowed as a parameter type
15// :5:30: error: return type '[10]u8' not allowed in function with calling convention 'C'15// :5:30: error: return type '[10]u8' not allowed in function with calling convention 'C'
16// :5:30: note: arrays are not allowed as a return type16// :5:30: note: arrays are not allowed as a return type