Files
Nim/compiler
Corey Leavitt 73986c03a1 fixes #25857; don't treat typeof(result) as a use-before-init of result (#25858)
fixes #25857

## Bug

`typeof(result)` inside the expression that builds `result` gets counted
as a read
of `result` before it's set. On a `{.requiresInit.}` return type that's
a hard error
("'result' requires explicit initialization"). `typeof` never evaluates
its operand,
so it's a false positive. On 2.2.4 it compiles, but the same line still
emits a bogus
`ProveInit` warning, so no released version gets it right.

Regression from #25151. That PR made a used-before-init `requiresInit`
result a hard
error instead of a warning, which is correct on its own. The side effect
was that
this old false-positive warning became a build error.

## Root cause

`track` in `compiler/sempass2.nim` has no arm for `nkTypeOfExpr`, so it
hits the
default that recurses into every child, reaches the `result` `nkSym`
inside the
`typeof`, and calls `useVar`. `sizeof`/`compiles`/`declared` don't hit
this because
they fold to a constant before `track` runs. A `typeof(result)` typedesc
argument
survives into `track`.

## Fix

Skip `nkTypeOfExpr` in `track`. Its operand is never evaluated, so it
isn't a
definite-assignment use. After the patch there's no error and no warning
here, even
with `--warnings:on`. The #25151 check is untouched: a real use of
`result` before
init is a plain `nkSym`, not inside a `typeof`, so it still reaches
`useVar`.

## Test

`tests/init/t25857.nim`, a positive test that compiles and prints `1`.

## Checks

- Repro compiles and runs on patched 2.2.6 and patched devel.
- `tests/errmsgs/t25117.nim` still fails as expected. A real
`xxx(result)` before
  init still errors.
- `testament cat init` and `testament cat errmsgs` green on patched
devel (55 tests,
  0 failures), including the `--warningAsError:ProveInit` tests.
- Bisect: parent `1ab68797` good, `576c4018` (#25151) bad.
2026-06-02 07:07:44 +02:00
..
2023-12-15 10:20:57 +01:00
2025-12-01 22:59:12 +01:00
2026-02-10 13:21:35 +01:00
2026-04-02 07:19:43 +02:00
2025-12-11 18:22:38 +01:00
2026-03-16 16:56:18 +01:00
2025-12-31 13:33:57 +01:00
2017-01-07 22:35:09 +01:00
2026-02-10 13:21:35 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2026-03-07 15:10:01 +01:00
2025-11-25 12:49:23 +01:00
2026-04-02 07:19:43 +02:00
2024-12-27 19:42:18 +01:00
2026-04-02 07:19:43 +02:00
2025-12-11 18:22:38 +01:00
2026-04-02 07:19:43 +02:00
2025-12-11 18:22:38 +01:00
2021-01-12 09:36:51 +01:00
2026-01-09 13:10:04 +01:00
2026-02-10 13:21:35 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2026-01-24 06:07:41 +01:00
2026-02-10 13:21:35 +01:00
2023-07-02 22:36:05 +02:00
2023-11-06 18:33:28 +01:00
2026-02-10 13:21:35 +01:00
2026-05-12 23:20:10 +02:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2026-05-29 08:08:42 +02:00
2025-12-11 18:22:38 +01:00
2024-03-16 08:35:18 +08:00
2025-12-29 13:52:22 +01:00
2025-12-31 13:33:57 +01:00
2026-02-10 13:21:35 +01:00
2023-12-25 07:12:54 +01:00

Nim Compiler

  • This directory contains the Nim compiler written in Nim.
  • Note that this code has been translated from a bootstrapping version written in Pascal.
  • So the code is not a poster child of good Nim code.

See Internals of the Nim Compiler for more information.