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

Cannot use optional in closure #19748

Closed
elliotchance opened this issue Nov 3, 2023 · 0 comments · Fixed by #19750
Closed

Cannot use optional in closure #19748

elliotchance opened this issue Nov 3, 2023 · 0 comments · Fixed by #19750
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@elliotchance
Copy link
Contributor

elliotchance commented Nov 3, 2023

Describe the bug

Using an optional in the closure bound variables results in a compiler error.

Reproduction Steps

module main

struct Foo {
	optional ?int
}

fn main() {
	f := Foo{45}
	if v := f.optional {
		func := fn [v] () {
			println(v)
		}
		func()
	}
}

Expected Behavior

No compiler error.

Current Behavior

V panic: sym: invalid type (typ=ast.Type(0x0 = 0) idx=0). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.

v hash: 48a1d6c
0   v                                   0x0000000100382d71 v__ast__default_table_panic_handler + 33
1   v                                   0x0000000100382dc8 v__ast__Table_panic + 72
2   v                                   0x00000001002c9532 v__ast__Table_sym + 354
3   v                                   0x00000001003d4214 v__gen__c__Gen_gen_anon_fn_decl + 820
4   v                                   0x00000001003d0a8a v__gen__c__Gen_gen_anon_fn + 58
5   v                                   0x00000001003c7f14 v__gen__c__Gen_expr + 500
6   v                                   0x00000001003ea3d8 v__gen__c__Gen_assign_stmt + 10104
7   v                                   0x000000010043f6c3 v__gen__c__Gen_stmt + 467
8   v                                   0x000000010043f0d3 v__gen__c__Gen_stmts_with_tmp_var + 4355
9   v                                   0x00000001003e60dc v__gen__c__Gen_stmts + 92
10  v                                   0x00000001004760eb v__gen__c__Gen_if_expr + 11867
11  v                                   0x00000001003c9113 v__gen__c__Gen_expr + 5107
12  v                                   0x000000010043fc97 v__gen__c__Gen_stmt + 1959
13  v                                   0x000000010043f0d3 v__gen__c__Gen_stmts_with_tmp_var + 4355
14  v                                   0x00000001003e60dc v__gen__c__Gen_stmts + 92
15  v                                   0x00000001004a367a v__gen__c__Gen_gen_fn_decl + 14746
16  v                                   0x0000000100449d1b v__gen__c__Gen_fn_decl + 2683
17  v                                   0x000000010043fe1a v__gen__c__Gen_stmt + 2346
18  v                                   0x000000010043f0d3 v__gen__c__Gen_stmts_with_tmp_var + 4355
19  v                                   0x00000001003e60dc v__gen__c__Gen_stmts + 92
20  v                                   0x00000001004231f8 v__gen__c__Gen_gen_file + 504
21  v                                   0x0000000100422c87 v__gen__c__cgen_process_one_file_cb + 5543
22  v                                   0x0000000100366c4a sync__pool__process_in_thread + 122
23  v                                   0x00000001002c7554 sync__pool__process_in_thread_thread_wrapper + 36
24  libsystem_pthread.dylib             0x00007ff8144c14e1 _pthread_start + 125
25  libsystem_pthread.dylib             0x00007ff8144bcf6b thread_start + 15

Possible Solution

I can use a new variables to avoid this issue:

	if v := f.optional {
		v2 := v
		func := fn [v2] () {
			println(v2)
		}
		func()
	}

Additional Information/Context

No response

V version

V 0.4.2 48a1d6c

Environment details (OS name and version, etc.)

V full version: V 0.4.2 4bc9a8f.48a1d6c
OS: macos, macOS, 12.6, 21G115
Processor: 10 cpus, 64bit, little endian, Apple M1 Max

getwd: /Users/elliot
vexe: /Users/elliot/Downloads/v-5/v
vexe mtime: 2023-10-21 03:59:08

vroot: OK, value: /Users/elliot/Downloads/v-5
VMODULES: OK, value: /Users/elliot/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.37.0 (Apple Git-136)
Git vroot status: weekly.2023.42-19-g48a1d6cc (100 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 14.0.0 (clang-1400.0.29.102)
thirdparty/tcc status: thirdparty-macos-amd64 46662e20

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.

@elliotchance elliotchance added the Bug This tag is applied to issues which reports bugs. label Nov 3, 2023
@ArtemkaKun ArtemkaKun added Unit: Type System Bugs/feature requests, that are related to the V types system. Option Type Bugs/feature requests, that are related to `?Type`. labels Nov 3, 2023
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`. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants