authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-11 12:23:39+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-11 17:23:06+03:00
log0958d5d7db24222bdcc0fb8e8cdc9838953b9857
tree21569090326a60c510deaa3fc549237d8b080ffc
parentc0102ac1c89db1727d7c58d566376a6a79af4e98

Sema: fix crash when generating anon name on invalid code

Closes #15615

3 files changed, 17 insertions(+), 5 deletions(-)

src/Sema.zig+7-4
...@@ -2730,9 +2730,13 @@ fn createAnonymousDeclTypeNamed(...@@ -2730,9 +2730,13 @@ fn createAnonymousDeclTypeNamed(
2730 for (fn_info.param_body) |zir_inst| switch (zir_tags[zir_inst]) {2730 for (fn_info.param_body) |zir_inst| switch (zir_tags[zir_inst]) {
2731 .param, .param_comptime, .param_anytype, .param_anytype_comptime => {2731 .param, .param_comptime, .param_anytype, .param_anytype_comptime => {
2732 const arg = sema.inst_map.get(zir_inst).?;2732 const arg = sema.inst_map.get(zir_inst).?;
2733 // The comptime call code in analyzeCall already did this, so we're2733 // If this is being called in a generic function then analyzeCall will
2734 // just repeating it here and it's guaranteed to work.2734 // have already resolved the args and this will work.
2735 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, "") catch unreachable;2735 // If not then this is a struct type being returned from a non-generic
2736 // function and the name doesn't matter since it will later
2737 // result in a compile error.
2738 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, "") catch
2739 return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null);
27362740
2737 if (arg_i != 0) try buf.appendSlice(",");2741 if (arg_i != 0) try buf.appendSlice(",");
2738 try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});2742 try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
...@@ -6562,7 +6566,6 @@ fn analyzeCall(...@@ -6562,7 +6566,6 @@ fn analyzeCall(
6562) CompileError!Air.Inst.Ref {6566) CompileError!Air.Inst.Ref {
6563 const mod = sema.mod;6567 const mod = sema.mod;
65646568
6565
6566 const callee_ty = sema.typeOf(func);6569 const callee_ty = sema.typeOf(func);
6567 const func_ty_info = func_ty.fnInfo();6570 const func_ty_info = func_ty.fnInfo();
6568 const fn_params_len = func_ty_info.param_types.len;6571 const fn_params_len = func_ty_info.param_types.len;
src/print_air.zig-1
...@@ -917,7 +917,6 @@ const Writer = struct {...@@ -917,7 +917,6 @@ const Writer = struct {
917917
918 try s.writeAll("\n");918 try s.writeAll("\n");
919 try s.writeByteNTimes(' ', old_indent);919 try s.writeByteNTimes(' ', old_indent);
920 try s.writeAll("}");
921 }920 }
922921
923 fn writeWasmMemorySize(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {922 fn writeWasmMemorySize(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
test/cases/compile_errors/struct_type_returned_from_non-generic_function.zig created+10
...@@ -0,0 +1,10 @@
1pub export fn entry(param: usize) usize {
2 return struct{ param };
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:12: error: expected type 'usize', found 'type'
10// :1:35: note: function return type declared here