Skip to content

Commit 0958d5d

Browse files
committed
Sema: fix crash when generating anon name on invalid code
Closes #15615
1 parent c0102ac commit 0958d5d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/Sema.zig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,9 +2730,13 @@ fn createAnonymousDeclTypeNamed(
27302730
for (fn_info.param_body) |zir_inst| switch (zir_tags[zir_inst]) {
27312731
.param, .param_comptime, .param_anytype, .param_anytype_comptime => {
27322732
const arg = sema.inst_map.get(zir_inst).?;
2733-
// The comptime call code in analyzeCall already did this, so we're
2734-
// just repeating it here and it's guaranteed to work.
2735-
const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, "") catch unreachable;
2733+
// If this is being called in a generic function then analyzeCall will
2734+
// have already resolved the args and this will work.
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

27372741
if (arg_i != 0) try buf.appendSlice(",");
27382742
try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
@@ -6562,7 +6566,6 @@ fn analyzeCall(
65626566
) CompileError!Air.Inst.Ref {
65636567
const mod = sema.mod;
65646568

6565-
65666569
const callee_ty = sema.typeOf(func);
65676570
const func_ty_info = func_ty.fnInfo();
65686571
const fn_params_len = func_ty_info.param_types.len;

src/print_air.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,6 @@ const Writer = struct {
917917

918918
try s.writeAll("\n");
919919
try s.writeByteNTimes(' ', old_indent);
920-
try s.writeAll("}");
921920
}
922921

923922
fn writeWasmMemorySize(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub 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

0 commit comments

Comments
 (0)