-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Zir: attach reason to block_comptime and improve corresponding error reporting
#22352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
49d8866 to
a2c14c6
Compare
|
You're so cool mlugg |
andrewrk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful error messages
|
Glad you like them! I'll push some doc comments shortly and auto-merge. My next target in the Land of Complicated Error Messages will be |
To avoid this PR regressing error messages, most of the work here has gone towards improving error notes for why code was comptime-evaluated. ZIR `block_comptime` now stores a "comptime reason", the enum for which is also used by Sema. There are two types in Sema: * `ComptimeReason` represents the reason we started evaluating something at comptime. * `BlockComptimeReason` represents the reason a given block is evaluated at comptime; it's either a `ComptimeReason` with an attached source location, or it's because we're in a function which was called at comptime (and that function's `Block` should be consulted for the "parent" reason). Every `Block` stores a `?BlockComptimeReason`. The old `is_comptime` field is replaced with a trivial `isComptime()` method which returns whether that reason is non-`null`. Lastly, the handling for `block_comptime` has been simplified. It was previously going through an unnecessary runtime-handling path; now, it is a trivial sub block exited through a `break_inline` instruction. Resolves: ziglang#22296
This fixes a bug which exposed a compiler implementation detail (ZIR alloc elision). Previously, `const` declarations with a runtime-known value in a comptime scope were permitted only if AstGen was able to elide the alloc in ZIR, since the error was reported by storing to the comptime alloc. This just adds a new instruction to also emit this error when the alloc is elided.
Some sub-expressions should always be evaluated at comptime -- in particular, type expressions, e.g. `E` in `E!T`. However, bugs in this logic are easy to miss, because the parent scope is usually comptime anyway!
Most calls to `requireRuntimeBlock` in Sema are not correct. This function doesn't deal with all of them, but it does deal with ones which have, in combination with the past few commits, introduced real-world regressions. Related: ziglang#22353
a2c14c6 to
7d26b52
Compare
7d26b52 to
106df88
Compare
|
I'm skipping adding release notes for this to save time, feel free to add them if you want by pushing directly to www.ziglang.org repo in the release notes branch. |
Also fixes a bug where a
constin a comptime scope was allowed to contain a runtime value depending on a seemingly arbitrary factor, exposing a compiler implementation detail.Sema.ComptimeReasonis a type which represents a possible reason to be evaluating or resolving a value at comptime. This is either something to do with a comptime-only type, or -- most commonly -- astd.zig.SimpleComptimeReason(which is a simpleu32-backed enum).Sema.BlockComptimeReasonrepresents the reason aBlockis being evaluated at comptime. It is either aSema.ComptimeReasonwith an attachedLazySrcLoc, or it is the value.inlining_parent, meaning that thisBlockis comptime-evaluated becauseblock.inlining.?.call_blockis comptime-evaluated, and that block should therefore be consulted. This value allows us to change thenote: called from herenotes on the error message, moving the ones before the "cause" of comptime evaluation to their "correct" location.ZIR
block_comptimenow embeds astd.zig.SimpleComptimeReasonin its extra data, representing the reason for comptime evaluation in this case. This hugely improves the error messages in some cases.