Skip to content

cgen: match with !-propagating arms used as an if-expression value emits _t4 = ; (expected expression) #28000

Description

@medvednikov

V version: V 0.5.2 (master)
OS: Linux (reported via bugs.vlang.io)

Description

A match whose arms use ! error-propagation (f(value)!), when the match is used as the value of an if-expression (here an if value := opt { … } else { … } option-guard assigned to a variable), emits invalid C: the result temp is assigned an empty expression — _t4 = ; — giving expected expression. Replacing the !-propagating arms with plain expressions compiles fine, so the trigger is !-propagation inside a match-as-if-expr-value.

Reproduction

module main

struct First {}
struct Second {}

type Node = First | Second

fn lower_first(_ First) !int {
	return 1
}

fn lower_second(_ Second) !int {
	return 2
}

fn select_value(node ?Node) !int {
	result := if value := node {
		match value {
			First { lower_first(value)! }
			Second { lower_second(value)! }
		}
	} else {
		0
	}
	return result
}

fn main() {
	println(select_value(First{})!)
}

Actual (generated C fails to compile)

error: expected expression
    _t4 = ;

The result temp for the if/match expression is never assigned the propagated value.

Expected

Compiles; the match arms' !-propagated results are assigned to the if-expression's result temp.

Note

The identical code with non-! arms (First { 1 } Second { 2 }, and a non-! return type) compiles and runs correctly.

Reported via bugs.vlang.io

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

    bugs.vlang.ioReported via the bugs.vlang.io crash reporter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions