Skip to content

Conversation

@mlugg
Copy link
Member

@mlugg mlugg commented Dec 29, 2024

Also fixes a bug where a const in a comptime scope was allowed to contain a runtime value depending on a seemingly arbitrary factor, exposing a compiler implementation detail.


Sema.ComptimeReason is 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 -- a std.zig.SimpleComptimeReason (which is a simple u32-backed enum).

Sema.BlockComptimeReason represents the reason a Block is being evaluated at comptime. It is either a Sema.ComptimeReason with an attached LazySrcLoc, or it is the value .inlining_parent, meaning that this Block is comptime-evaluated because block.inlining.?.call_block is comptime-evaluated, and that block should therefore be consulted. This value allows us to change the note: called from here notes on the error message, moving the ones before the "cause" of comptime evaluation to their "correct" location.

ZIR block_comptime now embeds a std.zig.SimpleComptimeReason in its extra data, representing the reason for comptime evaluation in this case. This hugely improves the error messages in some cases.

@mlugg mlugg force-pushed the zir-comptime-reason branch 3 times, most recently from 49d8866 to a2c14c6 Compare December 30, 2024 03:23
@nickelca
Copy link

You're so cool mlugg

@mlugg mlugg requested a review from andrewrk December 31, 2024 05:21
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful error messages

@mlugg
Copy link
Member Author

mlugg commented Dec 31, 2024

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 runtime value contains reference to comptime var. Wouldn't it be crazy if it actually gave you any indication whatsoever as to where that reference is?

@andrewrk andrewrk added the release notes This PR should be mentioned in the release notes. label Dec 31, 2024
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
@mlugg mlugg force-pushed the zir-comptime-reason branch from a2c14c6 to 7d26b52 Compare December 31, 2024 09:55
@mlugg mlugg enabled auto-merge December 31, 2024 09:55
@mlugg mlugg force-pushed the zir-comptime-reason branch from 7d26b52 to 106df88 Compare December 31, 2024 09:56
@mlugg mlugg merged commit 0df1f3d into ziglang:master Dec 31, 2024
10 checks passed
@andrewrk
Copy link
Member

andrewrk commented Mar 5, 2025

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.

@mlugg mlugg deleted the zir-comptime-reason branch May 18, 2025 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes This PR should be mentioned in the release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants