Skip to content

checker: explain smartcast narrowing in or {} / ? error on unwrapped option (fix #27130) - #27238

Merged
medvednikov merged 1 commit into
masterfrom
bug-27130-smartcast-error
May 24, 2026
Merged

medvednikov merged 1 commit into
masterfrom
bug-27130-smartcast-error

Conversation

@medvednikov

Copy link
Copy Markdown
Member

Summary

Improves the error message for or {} / ? used on an option variable that has already been smartcast-narrowed to its unwrapped type by an earlier if x == none { return } (or != none) check.

Before: cannot use `or {}` block on non-option variable — misleading, since the variable's declared type is ?string.

After: cannot use `or {}` block on non-option variable `name` (it was already unwrapped to `string` by an earlier none check; use `name` directly).

The new wording matches the "Alternative" suggested in the issue: it tells the user the variable was narrowed and what to do instead, instead of pretending the variable was never an option.

Fixes #27130.

Test plan

  • New regression test vlib/v/checker/tests/or_block_smartcast_unwrapped_err.vv covers both or {} and ? on a smartcast-narrowed option variable
  • ./v -silent test vlib/v/compiler_errors_test.v passes the new case
  • ./v self rebuilds cleanly with the patched checker

…d option (fix #27130)

When an option variable was narrowed to its unwrapped type by an earlier
`if x == none { return }` check, using `or {}` or `?` on it produced a
misleading "non-option variable" error. Mention the prior smartcast and
suggest using the variable directly.
@medvednikov
medvednikov merged commit a22727d into master May 24, 2026
86 of 181 checks passed
@JalonSolov
JalonSolov deleted the bug-27130-smartcast-error branch July 28, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

checker: or-block rejected on ?T variable after == none guard smartcast

1 participant