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

C compiler error when unwrapping Optional in an array #23438

Closed
louis77 opened this issue Jan 11, 2025 · 5 comments · Fixed by #23439
Closed

C compiler error when unwrapping Optional in an array #23438

louis77 opened this issue Jan 11, 2025 · 5 comments · Fixed by #23439
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. Testing Bugs/feature requests, that are related to testing framework, `test_` functions and `_test.v` files. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@louis77
Copy link
Contributor

louis77 commented Jan 11, 2025

V version: V 0.4.9 63f5f4a, press to see full `v doctor` output
V full version V 0.4.9 7af8faf.63f5f4a
OS macos, macOS, 15.2, 24C101
Processor 8 cpus, 64bit, little endian, Apple M2
Memory 0.06GB/16GB
V executable /Users/louis/Git/nv/v
V last modified time 2025-01-10 15:29:47
V home dir OK, value: /Users/louis/Git/nv
VMODULES OK, value: /Users/louis/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/louis/Git/v_staleworker
Git version git version 2.39.5 (Apple Git-154)
V git status weekly.2025.1-29-g63f5f4a6-dirty (8 commit(s) behind V master)
.git/config present true
cc version Apple clang version 16.0.0 (clang-1600.0.26.4)
gcc version Apple clang version 16.0.0 (clang-1600.0.26.4)
clang version Homebrew clang version 18.1.8
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 713692d4
emcc version N/A
glibc version N/A

What did you do?
./v -g -o vdbg cmd/v && ./vdbg src/main_test.v && src/main_test

module main

fn make_option() ?string {
	return "abc"
}

fn test_rows() {
	cols := [make_option()]
	if col := cols[0] {
		println("I made it")
	}
}

What did you see?

================== C compilation error (from cc): ==============
cc: /tmp/v_501/main_test.01JHB9CM93FXXWM4QG45C2FYHY.tmp.c:13055:6: error: use of undeclared identifier '_t1'
cc:  13055 |         if (_t1.state == 0) {
cc:        |             ^
cc: /tmp/v_501/main_test.01JHB9CM93FXXWM4QG45C2FYHY.tmp.c:13056:10: error: initializing 'string' (aka 'struct string') with an expression of incompatible type '_option_string' (aka 'struct _option_string')
cc:  13056 |                 string col = (*(_option_string*)array_get(cols, 0));
cc:        |                        ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc: 2 errors generated.
================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
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 .

What did you expect to see?

I expected this code to compile. cols[0] is a ?string and it should be unwrapped by if val := col[0]. I stumbled across this bug while using the Row type from db.pg library.

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.

@JalonSolov
Copy link
Contributor

JalonSolov commented Jan 11, 2025

Using latest V, V 0.4.9 81b421b, I put your example in a file named x.v and tried it with v -g run x.v. I got this:

x.v:9:5: warning: unused variable: `col`
    7 | fn test_rows() {
    8 |     cols := [make_option()]
    9 |     if col := cols[0] {
      |        ~~~
   10 |         println("I made it")
   11 |     }

No failures, nothing else. You need to do v up and try again.

@louis77
Copy link
Contributor Author

louis77 commented Jan 11, 2025

Ok, I pulled the latest commit but still get the same error:

% ~/Git/nv/v test src/main_test.v
---- Testing... ----------------------------------------------------------------------------------------------------------------------------
 FAIL     0.000 ms /Users/louis/Git/v_staleworker/src/main_test.v
>> compilation failed:
================== C compilation error (from cc): ==============
cc: /tmp/v_501/tsession_1ed3b0240_01JHBAFTBGQ8X3YGM1KJQY8JRH/main_test.01JHBAFV4X475V943FF2SXJGZW.tmp.c:12973:6: error: use of undeclared identifier '_t1'
cc:  12973 |         if (_t1.state == 0) {
cc:        |             ^
cc: /tmp/v_501/tsession_1ed3b0240_01JHBAFTBGQ8X3YGM1KJQY8JRH/main_test.01JHBAFV4X475V943FF2SXJGZW.tmp.c:12974:10: error: initializing 'string' (aka 'struct string') with an expression of incompatible type '_option_string' (aka 'struct _option_string')
cc:  12974 |                 string col = (*(_option_string*)array_get(cols, 0));
cc:        |                        ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc: 2 errors generated.
================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
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 .

V doctor:

% ~/Git/nv/v doctor              
|V full version      |V 0.4.9 7af8faf.81b421b
|:-------------------|:-------------------
|OS                  |macos, macOS, 15.2, 24C101
|Processor           |8 cpus, 64bit, little endian, Apple M2
|Memory              |0.14GB/16GB
|                    |
|V executable        |/Users/louis/Git/nv/v
|V last modified time|2025-01-11 18:35:28
|                    |
|V home dir          |OK, value: /Users/louis/Git/nv
|VMODULES            |OK, value: /Users/louis/.vmodules
|VTMP                |OK, value: /tmp/v_501
|Current working dir |OK, value: /Users/louis/Git/v_staleworker
|                    |
|Git version         |git version 2.39.5 (Apple Git-154)
|V git status        |weekly.2025.1-35-g81b421bb-dirty
|.git/config present |true
|                    |
|cc version          |Apple clang version 16.0.0 (clang-1600.0.26.4)
|gcc version         |Apple clang version 16.0.0 (clang-1600.0.26.4)
|clang version       |Homebrew clang version 18.1.8
|tcc version         |tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
|tcc git status      |thirdparty-macos-arm64 713692d4
|emcc version        |N/A
|glibc version       |N/A

@JalonSolov
Copy link
Contributor

Ah... I missed that it was a test file... renaming to x_test.v, and trying with v -g test x_test.v, I see this:

---- Testing... ----------------------------------------------------------------------------------------------------------------------------
 FAIL     0.000 ms /tmp/x_test.v
>> compilation failed:
/tmp/v_1000/tsession_7d6955be5740_01JHBB0KDSGJRY0ERV68ZVEW82/../../../../../../tmp/x_test.v:9: error: '_t1' 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 .

--------------------------------------------------------------------------------------------------------------------------------------------
To reproduce just failure 1 run:    '/home/jalon/git/v/v' -g '/tmp/x_test.v'
Summary for all V _test.v files: 1 failed, 1 total. Elapsed time: 826 ms, on 1 job. Comptime: 750 ms. Runtime: 0 ms.

@JalonSolov
Copy link
Contributor

So the bug only happens when the test option is used. Exact same source only gives the unused variable warning if compiled.

@JalonSolov JalonSolov added Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Testing Bugs/feature requests, that are related to testing framework, `test_` functions and `_test.v` files. labels Jan 11, 2025
@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Option Type Bugs/feature requests, that are related to `?Type`. labels Jan 11, 2025
Copy link

Connected to Huly®: V_0.6-21869

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. Testing Bugs/feature requests, that are related to testing framework, `test_` functions and `_test.v` files. 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.

4 participants