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

Out-of-bounds access of array with or block doesn't return the value of the or block #23451

Closed
louis77 opened this issue Jan 13, 2025 · 1 comment · Fixed by #23455
Closed
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@louis77
Copy link
Contributor

louis77 commented Jan 13, 2025

Describe the bug

With an array with optional values (as used in the standard lib, db.pg for example), trying to access an item out-of-bounds with an or block, should return the value from the or block. But that doesn't happen.

Reproduction Steps

https://play.vlang.io/p/9326094f30

module main

fn make_option() ?string {
	return none
}

fn main() {
	arr := [make_option()]
	unwrapped := arr[99] or { 'unknown' } // <- out of bounds access!
	println('... ${unwrapped}')
}

Expected Behavior

should print: ... Option('unknown')

Current Behavior

prints: ... Option(none)

Possible Solution

No response

Additional Information/Context

The out-of-bounds access logic with or {} blocks for arrays and maps seems to collide with the Option/Result logic. Genereally it should be possible to have a shortcut for accessing an element in an array and unwrapping the element, if it is an Optional. Otherwise, the element must be assigned to a new variable first and then unwrapped.

V version

V 0.4.9 d2b30df

Environment details (OS name and version, etc.)

|V full version      |V 0.4.9 d2b30df
|:-------------------|:-------------------
|OS                  |linux, Debian GNU/Linux 12 (bookworm) (VM)
|Processor           |2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
|Memory              |0.79GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-01-13 00:25:27
|                    |
|V home dir          |OK, value: /home/admin/v
|VMODULES            |OK, value: .vmodules
|VTMP                |OK, value: /tmp/v_0
|Current working dir |OK, value: /home/admin/playground
|                    |
|Git version         |git version 2.39.5
|V git 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
|gcc version         |gcc (Debian 12.2.0-14) 12.2.0
|clang version       |Debian clang version 14.0.6
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git 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

|emcc version        |N/A
|glibc version       |ldd (Debian GLIBC 2.36-9+deb12u8) 2.36

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.

@louis77 louis77 added the Bug This tag is applied to issues which reports bugs. label Jan 13, 2025
Copy link

Connected to Huly®: V_0.6-21881

@spytheman spytheman added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Option Type Bugs/feature requests, that are related to `?Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 13, 2025
@yuyi98 yuyi98 self-assigned this Jan 13, 2025
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. Option Type Bugs/feature requests, that are related to `?Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants