Skip to content

Commit 3ceb27c

Browse files
committed
Sema: better explanation why opaque types require comptime
1 parent 3e2e6c1 commit 3ceb27c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Sema.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18098,10 +18098,12 @@ fn explainWhyTypeIsComptime(
1809818098
.NoReturn,
1809918099
.Undefined,
1810018100
.Null,
18101-
.Opaque,
18102-
.Optional,
1810318101
=> return,
1810418102

18103+
.Opaque => {
18104+
try mod.errNoteNonLazy(src_loc, msg, "opaque type '{}' has undefined size", .{ty.fmt(sema.mod)});
18105+
},
18106+
1810518107
.Array, .Vector => {
1810618108
try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.elemType());
1810718109
},
@@ -18124,6 +18126,10 @@ fn explainWhyTypeIsComptime(
1812418126
try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.elemType());
1812518127
},
1812618128

18129+
.Optional => {
18130+
var buf: Type.Payload.ElemType = undefined;
18131+
try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.optionalChild(&buf));
18132+
},
1812718133
.ErrorUnion => {
1812818134
try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.errorUnionPayload());
1812918135
},

test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ const Opaque = opaque {};
4040
// :14:8: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
4141
// :18:8: error: variable of type '@TypeOf(null)' must be const or comptime
4242
// :22:19: error: values of type 'tmp.Opaque' must be comptime known, but operand value is runtime known
43+
// :22:19: note: opaque type 'tmp.Opaque' has undefined size
4344
// :26:8: error: variable of type 'type' must be const or comptime
4445
// :26:8: note: types are not available at runtime

0 commit comments

Comments
 (0)