Skip to content

if expression with multi-statements in branch does not compile unless ';' present #24297

Description

@pd-giz-dave

Describe the bug

if expression with multi-statement branch fails to compile when the penultimate statement has a variable reference unless a statement terminator ';' is present. vfmt removes the ';'

Reproduction Steps

compile this:

module main
fn if_expt(this int) (string, int) {
	inc := 1
	mut count := 0
	thing := if this in [0,1,2] {
		count += 1
		'0..2'
	} else if this in [3,4,5] {
		count += inc;   // fails when no end-of-statement ';' which is removed by vfmt
		'3..5'
	} else {
		'not 0..5'
	}
	return thing, count
}
fn main() {
	println(if_expt(1))  // expect ('0..2', 1)
	println(if_expt(4))  // expect ('3..5', 1)
	println(if_expt(7))  // expect ('not 0..5', 0)
}

then run it through vmt and compile again, vfmt removes the ';' and the compiler complains:

compiler_bugs/main.v:9:12: error: only `c`, `r`, `js` are recognized string prefixes, but you tried to use `inc`
    7 |         '0..2'
    8 |     } else if this in [3,4,5] {
    9 |         count += inc   // fails when no end-of-statement ';' which is removed by vfmt
      |                  ~~~
   10 |         '3..5'
   11 |     } else {

Expected Behavior

It should either compile or vfmt should not remove ';'

Current Behavior

get compiler error:

compiler_bugs/main.v:9:12: error: only `c`, `r`, `js` are recognized string prefixes, but you tried to use `inc`
    7 |         '0..2'
    8 |     } else if this in [3,4,5] {
    9 |         count += inc   // fails when no end-of-statement ';' which is removed by vfmt
      |                  ~~~
   10 |         '3..5'
   11 |     } else {

Possible Solution

wrap the statement with vfmt off/on

Additional Information/Context

No response

V version

V 0.4.10 b76b1ee

Environment details (OS name and version, etc.)

v doctor
|V full version      |V 0.4.10 537605a.b76b1ee
|:-------------------|:-------------------
|OS                  |linux, "EndeavourOS Linux"
|Processor           |16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics
|Memory              |13.09GB/30.75GB
|                    |
|V executable        |/home/dave/v/v
|V last modified time|2025-04-22 17:08:20
|                    |
|V home dir          |OK, value: /home/dave/v
|VMODULES            |OK, value: /home/dave/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /home/dave/precious/fellsafe/v
|                    |
|Git version         |git version 2.48.1
|V git status        |weekly.2025.16-33-gb76b1ee7
|.git/config present |true
|                    |
|cc version          |cc (GCC) 14.2.1 20250207
|gcc version         |gcc (GCC) 14.2.1 20250207
|clang version       |clang version 19.1.7
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 0134e9b9
|emcc version        |N/A
|glibc version       |ldd (GNU libc) 2.41

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

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: ParserBugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions