From 561565fa81e4107f50b8cbdcc00ec0fa2ee4de15 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 13 Dec 2020 20:27:04 +0100 Subject: [PATCH 1/2] stage1: Fix crash in can_mutate_comptime_var_state No lazy value can mutate global state, no need to resolve them. Closes #7426 --- src/stage1/analyze.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index ed0a033592..35405518eb 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -5749,6 +5749,28 @@ static bool can_mutate_comptime_var_state(ZigValue *value) { assert(value != nullptr); if (value->special == ConstValSpecialUndef) return false; + + if (value->special == ConstValSpecialLazy) { + // No lazy value has side effects. + // Use a switch here to get a compile error whenever a new kind of lazy + // value is added. + switch (value->data.x_lazy->id) { + case LazyValueIdInvalid: + zig_unreachable(); + + case LazyValueIdAlignOf: + case LazyValueIdSizeOf: + case LazyValueIdPtrType: + case LazyValueIdOptType: + case LazyValueIdSliceType: + case LazyValueIdFnType: + case LazyValueIdErrUnionType: + case LazyValueIdArrayType: + case LazyValueIdTypeInfoDecls: + return false; + } + } + switch (value->type->id) { case ZigTypeIdInvalid: zig_unreachable(); From bbfa3550a02e82df91f5e2fafeab564906ae943c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 14 Dec 2020 17:39:35 +0100 Subject: [PATCH 2/2] Add a test case Co-authored-with: Vexu --- test/stage1/behavior/misc.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index 269db18579..850622c5ba 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -752,3 +752,10 @@ test "extern variable with non-pointer opaque type" { @export(var_to_export, .{ .name = "opaque_extern_var" }); expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42); } + +test "lazy typeInfo value as generic parameter" { + const S = struct { + fn foo(args: anytype) void {} + }; + S.foo(@typeInfo(@TypeOf(.{}))); +}