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

Swap assignment not working in closure #19734

Closed
elliotchance opened this issue Nov 2, 2023 · 0 comments · Fixed by #19736
Closed

Swap assignment not working in closure #19734

elliotchance opened this issue Nov 2, 2023 · 0 comments · Fixed by #19736
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@elliotchance
Copy link
Contributor

elliotchance commented Nov 2, 2023

Describe the bug

Cannot use a, b = b, a in a closure.

Reproduction Steps

module main

fn main() {
	f := foo()
	f()
}

fn foo() fn () {
	return fn () {
		mut left := 1
		mut right := 2

		left, right = right, left

		println('${left} ${right}')
	}
}

Expected Behavior

No compiler error.

Current Behavior

Produces a C compiler error:

==================
/tmp/v_501/bug.3024684967219090772.tmp.c:2081:17: error: use of undeclared identifier '_V_closure_ctx'
        int _var_114 = _V_closure_ctx->left;
                       ^
/tmp/v_501/bug.3024684967219090772.tmp.c:2082:17: error: use of undeclared identifier '_V_closure_ctx'
        int _var_120 = _V_closure_ctx->right;
                       ^
2 errors generated.
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

Possible Solution

You can work around this with a temporary variable:

tmp := left
left = right
right = left

Additional Information/Context

No response

V version

V 0.4.2 4bc9a8f.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 (94 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.

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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant