Skip to content

Why adding to a nested if expression causes a compilation error? #26865

@JalonSolov

Description

@JalonSolov

Discussed in #26864

Originally posted by programandala-net April 12, 2026
i have found out a curious issue about nested if expressions; i have isolated it in this example code:

import rand

fn chance() bool {
	return rand.i8() >= 0
}

fn main() {
	// these nested `if` expressions are compiled fine
	// (note a literal is added to the *outer* `if` expression):

	_ := if chance() {  0  } else { if chance() {  2  } else {  3  } } + 5
	_ := if chance() { 'a' } else { if chance() { 'c' } else { 'd' } } + 'f'

	// but when something is added to the *inner* `if` expression, the compilation
	// fails:

	// XXX compilation error: mismatched types `void` and `int literal`:
	// _ := if chance() {  0  } else { if chance() {  2  } else {  3  } +  4 } + 5

	// XXX compilation error: mismatched types `void` and `string`:
	// _ := if chance() { 'a' } else { if chance() { 'c' } else { 'd' } + 'e'} + 'f'

	// and when the inner `if` expression is added to something, the
	// compilation works:

	_ := if chance() {  0  } else {  1  + if chance() {  2  } else {  3  } +  4 } + 5
	_ := if chance() { 'a' } else { 'b' + if chance() { 'c' } else { 'd' } + 'e'} + 'f'

}

i have searched the documentation, the github issues and the github discussions but found nothing related.

it seems a bug, but perhaps i am neglecting something, so i prefer to ask here first, before opening an issue.

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions