Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Result functions not unwrapped in ladder if / else if / else #22784

Closed
jorgeluismireles opened this issue Nov 7, 2024 · 1 comment · Fixed by #22786
Closed

Two Result functions not unwrapped in ladder if / else if / else #22784

jorgeluismireles opened this issue Nov 7, 2024 · 1 comment · Fixed by #22786
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Nov 7, 2024

Describe the bug

Code: https://play.vlang.io/p/6d4906bdaa

fn byte_str(num u32) !string {
	return if num > 0xff { error('larger than byte') } else { num.str() }
}

fn short_str(num int) !string {
	return if num > 0xffff { error('larger than short') } else { num.str() }
}

fn main() {
	num := u32(0xfffff)
	if bs := byte_str(num) {
		println('byte: ${bs}')
	} else if ss := short_str(num) {
		println('short: ${ss}')
	} else {
		println('err: ${err}') // err not found!
	}
}

Reproduction Steps

In the above code (playground) inside the final case else the variable err cannot be found and a C error is produced.

If we cancel one of the three cases leaving only two levels if / else, the err is unwrapped correctly:

fn main() {
	num := u32(0xfffff)
	if bs := byte_str(num) {
		println('byte: ${bs}')
	//} else if ss := short_str(num) {
		//println('short: ${ss}')
	} else {
		println('err: ${err}') // err found -> "larger than byte"
	}
}

Expected Behavior

Unwrap the error all the way down the ladder if / else if / else or notify it can't be done.

Current Behavior

Output:

/tmp/v_60000/../../../../../../box/code.v:16: error: 'err' undeclared
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 107167a

Environment details (OS name and version, etc.)

V full version: V 0.4.8 107167a
OS: linux, Debian GNU/Linux 12 (bookworm) (VM)
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz

getwd: /home/admin/playground
vexe: /home/admin/v/v
vexe mtime: 2024-11-07 02:25:29

vroot: OK, value: /home/admin/v
VMODULES: OK, value: .vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.39.5
Git vroot status: Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
.git/config present: true

CC version: cc (Debian 12.2.0-14) 12.2.0
emcc version: N/A
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

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.

Huly®: V_0.6-21230

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Nov 7, 2024
@felipensp felipensp self-assigned this Nov 7, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Nov 7, 2024
@felipensp
Copy link
Member

It seems was supposed to work on simple ones. But I've pushed a PR to expand to else if branches too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants