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 error: Lambda function does not compile #21591

Closed
elogir opened this issue May 29, 2024 · 2 comments · Fixed by #21592
Closed

C error: Lambda function does not compile #21591

elogir opened this issue May 29, 2024 · 2 comments · Fixed by #21592
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Result 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

@elogir
Copy link

elogir commented May 29, 2024

V doctor:

V full version: V 0.4.6 433f914.09eaae5
OS: linux, "Manjaro Linux"
Processor: 24 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i9-12900K

getwd: /home/rim/Documents/Git-Repos/superstream-server/src
vexe: /home/rim/Documents/Git-Repos/v/v
vexe mtime: 2024-05-28 17:57:06

vroot: OK, value: /home/rim/Documents/Git-Repos/v
VMODULES: OK, value: /home/rim/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.45.0
Git vroot status: weekly.2023.50-1096-g09eaae5f
.git/config present: true

CC version: cc (GCC) 13.2.1 20240417
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

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

module main

import encoding.xml

fn main() {
	get_text := fn (node xml.XMLNode, tag string) !string {
		return node.get_elements_by_tag(tag)[0]!.children[0]!.str()
	}
}

What did you expect to see?

compiles

What did you see instead?

bug.v:6:2: warning: unused variable: `get_text`
    4 | 
    5 | fn main() {
    6 |     get_text := fn (node xml.XMLNode, tag string) !string {
      |     ~~~~~~~~
    7 |         return node.get_elements_by_tag(tag)[0]!.children[0]!.str()
    8 |     }
==================
/tmp/v_1000/bug.01HZ1QH45SM2ZX2QGQQZBK3T5T.tmp.c:4031: error: lvalue expected
...
==================
(Use `v -cg` to print the entire error message)

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 .

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.

@spytheman
Copy link
Member

Splitting the return expression into several statements can be used as a workaround:

module main

import encoding.xml

fn main() {
	get_text := fn (node xml.XMLNode, tag string) !string {
		elements := node.get_elements_by_tag(tag)[0]!
		children := elements.children[0]!
		return children.str()
	}
	dump(get_text)
}

@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Result Type Bugs/feature requests, that are related to `!Type`. 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. Bug This tag is applied to issues which reports bugs. labels May 29, 2024
@spytheman
Copy link
Member

spytheman commented May 29, 2024

A simpler (less dependencies, and easier to isolate) reproduction of the same problem is:

struct Element {
    children []int
}

fn f() []Element {
    return [Element{
        children: [1, 2, 3]
    }]
}

a := f()[0]!.children[0]!.str()
dump(a)

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. Result 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