From f4e051e35d8019c9a8d99ccae8f2e9d8f032629a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 17 Jan 2022 15:21:58 -0700 Subject: Sema: fix comptime break semantics Previously, breaking from an outer block at comptime would result in incorrect control flow. Now there is a mechanism, `error.ComptimeBreak`, similar to `error.ComptimeReturn`, to send comptime control flow further up the stack, to its matching block. This commit also introduces a new log scope. To use it, pass `--debug-log sema_zir` and you will see 1 line per ZIR instruction semantically analyzed. This is useful when you want to understand what comptime control flow is doing while debugging the compiler. One more `switch` test case is passing. --- src/Module.zig | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index a464ac83de..c66509f33a 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2486,6 +2486,9 @@ pub const CompileError = error{ /// In a comptime scope, a return instruction was encountered. This error is only seen when /// doing a comptime function call. ComptimeReturn, + /// In a comptime scope, a break instruction was encountered. This error is only seen when + /// evaluating a comptime block. + ComptimeBreak, }; pub fn deinit(mod: *Module) void { @@ -4446,6 +4449,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem error.NeededSourceLocation => unreachable, error.GenericPoison => unreachable, error.ComptimeReturn => unreachable, + error.ComptimeBreak => unreachable, else => |e| return e, }; if (opt_opv) |opv| { @@ -4478,6 +4482,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem error.NeededSourceLocation => @panic("zig compiler bug: NeededSourceLocation"), error.GenericPoison => @panic("zig compiler bug: GenericPoison"), error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"), + error.ComptimeBreak => @panic("zig compiler bug: ComptimeBreak"), else => |e| return e, }; -- cgit v1.2.3