Commit 3ceb27c840

Veikka Tuominen <git@vexu.eu>
2022-07-11 15:33:37
Sema: better explanation why opaque types require comptime
1 parent 3e2e6c1
Changed files (2)
src/Sema.zig
@@ -18098,10 +18098,12 @@ fn explainWhyTypeIsComptime(
         .NoReturn,
         .Undefined,
         .Null,
-        .Opaque,
-        .Optional,
         => return,
 
+        .Opaque => {
+            try mod.errNoteNonLazy(src_loc, msg, "opaque type '{}' has undefined size", .{ty.fmt(sema.mod)});
+        },
+
         .Array, .Vector => {
             try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.elemType());
         },
@@ -18124,6 +18126,10 @@ fn explainWhyTypeIsComptime(
             try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.elemType());
         },
 
+        .Optional => {
+            var buf: Type.Payload.ElemType = undefined;
+            try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.optionalChild(&buf));
+        },
         .ErrorUnion => {
             try sema.explainWhyTypeIsComptime(block, src, msg, src_loc, ty.errorUnionPayload());
         },
test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig
@@ -40,5 +40,6 @@ const Opaque = opaque {};
 // :14:8: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
 // :18:8: error: variable of type '@TypeOf(null)' must be const or comptime
 // :22:19: error: values of type 'tmp.Opaque' must be comptime known, but operand value is runtime known
+// :22:19: note: opaque type 'tmp.Opaque' has undefined size
 // :26:8: error: variable of type 'type' must be const or comptime
 // :26:8: note: types are not available at runtime