From 3ceb27c8401df17e5a7f522d68104da79501b5b6 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 11 Jul 2022 16:33:37 +0300 Subject: [PATCH] Sema: better explanation why opaque types require comptime --- src/Sema.zig | 10 ++++++++-- ...ariables_of_things_that_require_const_variables.zig | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 0e7c1c5937..953336ee39 100644 --- a/src/Sema.zig +++ b/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()); }, diff --git a/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig b/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig index c6713b755a..b6edbc265a 100644 --- a/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig +++ b/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