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

performance, robustness: don't evaluate expressions in conditions unnecessarily #21590

Open
ttytm opened this issue May 28, 2024 · 3 comments
Open
Labels
Feature Request This issue is made to request a feature.

Comments

@ttytm
Copy link
Member

ttytm commented May 28, 2024

Describe the bug

A logical AND (&&) with an intentional order of conditional statements could behave the same creating deeper nesting levels.

Reproduction Steps

import semver

version := 'nightly'
if version != 'nightly' && semver.from(version)! < semver.from('2.4.1')! {
} else {
	// condition
}

Result:
version != 'nightly is checked, still getting the error:

V panic: result not set (Invalid version format for input "nightly")

Expected Behavior

Same as using a nested conditon:

import semver

version := 'nightly'
if version != 'nightly' {
	if semver.from(version)! < semver.from('2.4.1')! {
	} else {
		// condition
	}
} else {
	// same else condition
}

Result:
Won't error

Current Behavior

V panic: result not set (Invalid version format for input "nightly")

Possible Solution

No response

Additional Information/Context

No response

V version

👇

Environment details (OS name and version, etc.)

V full version: V 0.4.6 09eaae5
OS: linux, "EndeavourOS Linux"
Processor: 16 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz

getwd: /home/t/Nextcloud/Dev/vlang/playground
vexe: /home/t/Nextcloud/Dev/vlang/v/v
vexe mtime: 2024-05-28 18:24:11

vroot: OK, value: /home/t/Nextcloud/Dev/vlang/v
VMODULES: OK, value: /home/t/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.45.1
Git vroot status: weekly.2024.22-4-g09eaae5f
.git/config present: true

CC version: cc (GCC) 14.1.1 20240522
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

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.

@ttytm ttytm added the Bug This tag is applied to issues which reports bugs. label May 28, 2024
@spytheman spytheman added Feature Request This issue is made to request a feature. and removed Bug This tag is applied to issues which reports bugs. labels May 29, 2024
@spytheman
Copy link
Member

The evaluation order is not defined currently.

@spytheman
Copy link
Member

The same problem is demonstrated by this program, that does not need semver:

fn f(s string) !int {
    if s == '' {
        return error('invalid s')
    }
    return s.len
}

v := ''
x := v != 'xyz' && f(v)! < f('abc')!
dump(x)

@ttytm
Copy link
Member Author

ttytm commented May 29, 2024

It might work to just cgen the nesting if there is a function expression in a condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

2 participants