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

Get the element by the index or propagate an error to the caller #23452

Open
louis77 opened this issue Jan 13, 2025 · 1 comment
Open

Get the element by the index or propagate an error to the caller #23452

louis77 opened this issue Jan 13, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Comments

@louis77
Copy link
Contributor

louis77 commented Jan 13, 2025

Describe the bug

According to @spytheman , propagating the error of the array out-of-bounds access or getting the element should be possible to be put in parens, so unwrapping the element, in this case ?string, is possible in the same line. Currently it fails with a compilation error:

error: invalid expression: unexpected keyword `or`

Reproduction Steps

Code: https://play.vlang.io/p/856327f0f2

module main

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

Expected Behavior

This should return the element or propagate the error in case of OOB, then unwrap the optional.

Current Behavior

Output:

code.v:5:19: error: invalid expression: unexpected keyword `or`
    3 | fn main() {
    4 |     arr := [?string(none)]
    5 |     res := (arr[99]!) or { 'unknown' } // <- out of bounds access!
      |                      ~~
    6 |     println('... ${res}')
    7 | }
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

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.77GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-01-13 01:25:26
|                    |
|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
@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-21882

@spytheman spytheman added Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones). Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels 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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).
Projects
None yet
Development

No branches or pull requests

2 participants