v icon indicating copy to clipboard operation
v copied to clipboard

Ternary expression cannot use nested Ifs

Open csfore opened this issue 2 years ago • 12 comments

Describe the bug

The ternary expression cannot currently use nested if statements.

Expected Behavior

The value of the nested if statement to be set to the variable.

Current Behavior

error: `if` expression requires an expression as the last statement of every branch
    7 |         if true {
    8 |             1
    9 |         } else {
      |           ~~~~
   10 |             eprintln('Error')
   11 |             return

Reproduction Steps

var := if true  {
	0
} else {
	if true {
		1
	} else {
		0
	}
}

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 6b20c57.2d51379

Environment details (OS name and version, etc.)

OS: linux, "Arch Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 9 5900HX with Radeon Graphics
CC version: cc (GCC) 12.2.1 20230111

getwd: /home/user/Workspaces/V/bug-testing
vmodules: /home/user/.vmodules
vroot: /home/user/v
vexe: /home/user/v/v
vexe mtime: 2023-01-30 21:57:54
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.3 6b20c57.2d51379

Git version: git version 2.39.1
Git vroot status: 0.3.3-3-g2d51379f
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

csfore avatar Jan 31 '23 03:01 csfore

No problem on M1 Mac

OS: macos, macOS, 13.2, 22D49
Processor: 8 cpus, 64bit, little endian, Apple M1
CC version: Apple clang version 14.0.0 (clang-1400.0.29.202)

V full version: V 0.3.3 4747e70.2d51379

thirdparty/tcc status: thirdparty-macos-arm64 173c526e

DevrosTheOne avatar Jan 31 '23 04:01 DevrosTheOne

Your reproduction step has not the same content as the error

danieldaeschle avatar Jan 31 '23 10:01 danieldaeschle

Your reproduction step has not the same content as the error

Sorry, I skimmed the code and missed the intent of the post.

DevrosTheOne avatar Jan 31 '23 10:01 DevrosTheOne

I meant @csfore

danieldaeschle avatar Jan 31 '23 14:01 danieldaeschle

Right. The code in the Reproduction Steps section will not produce the error shown.

JalonSolov avatar Jan 31 '23 14:01 JalonSolov

@danieldaeschle

This should reproduce it

module main

fn main() {
	var := if true  {
		0
	} else if false {
		1
	} else {
		eprintln('Error')
		return
	}
}

csfore avatar Feb 01 '23 01:02 csfore

It does for me.

JalonSolov avatar Feb 01 '23 01:02 JalonSolov

I also think this should work. Similar thing was fixed in match recently... basically the same issue.

JalonSolov avatar Feb 01 '23 01:02 JalonSolov

It seems like it shouldn't work, because it's trying to assign nothing to a variable essentially but the error message seems like it's erroring for the wrong reason. The variable doesn't technically exist yet though so I'm not sure what it should do.

csfore avatar Feb 01 '23 15:02 csfore

It should work because it is short-circuiting the program flow such that var doesn't need to be defined - it is exiting that code flow and returning to the caller.

JalonSolov avatar Feb 01 '23 16:02 JalonSolov

Ah, alright. Should I rename the issue to something more appropriate then? As nested If statements do technically work. It's just the short-circuiting that doesn't work.

csfore avatar Feb 01 '23 17:02 csfore

:man_shrugging:

JalonSolov avatar Feb 01 '23 20:02 JalonSolov

I will be closing the issue because the program compiles successfully for me.

Feel free to open it again if the problem still exists.

Delta456 avatar Jun 28 '24 18:06 Delta456