Skip to content

Commit 7123214

Browse files
committed
Sema: fix crash when coercing error/optional int type at comptime
1 parent dbf9c7b commit 7123214

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Sema.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29153,15 +29153,15 @@ fn coerceExtra(
2915329153

2915429154
// E!T to T
2915529155
if (inst_ty.zigTypeTag(zcu) == .error_union and
29156-
(try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)
29156+
(try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, null)) == .ok)
2915729157
{
2915829158
try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{});
2915929159
try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{});
2916029160
}
2916129161

2916229162
// ?T to T
2916329163
if (inst_ty.zigTypeTag(zcu) == .optional and
29164-
(try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)
29164+
(try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src, null)) == .ok)
2916529165
{
2916629166
try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{});
2916729167
try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const std = @import("std");
2+
pub fn main() !void {
3+
const x = 0;
4+
comptime {
5+
x += std.math.add(usize, x, 2);
6+
}
7+
}
8+
9+
// error
10+
//
11+
// :5:26: error: expected type 'comptime_int', found 'error{Overflow}!usize'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const std = @import("std");
2+
pub fn main() !void {
3+
const x = 0;
4+
const y: ?usize = null;
5+
comptime {
6+
x += y;
7+
}
8+
}
9+
10+
// error
11+
//
12+
// :6:14: error: expected type 'comptime_int', found '?usize'

0 commit comments

Comments
 (0)