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

Optional Function returns break everything #22278

Closed
shininglove opened this issue Sep 21, 2024 · 1 comment · Fixed by #22280
Closed

Optional Function returns break everything #22278

shininglove opened this issue Sep 21, 2024 · 1 comment · Fixed by #22280
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. 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

@shininglove
Copy link

shininglove commented Sep 21, 2024

V doctor:

V full version: V 0.4.7 63957f2.3307340
OS: linux, Ubuntu 24.04.1 LTS

getwd: ~/vlang/option_bug_maybe
vexe: ~/vlang/v/v
vexe mtime: 2024-09-21 23:21:32

vroot: OK, value: ~/vlang/v
VMODULES: OK, value: ~/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.43.0
Git vroot status: weekly.2024.37-36-g33073407
.git/config present: true

CC version: cc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

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

module main

type DataFn = fn (name string) string

fn which_lang(name string) string {
	return name
}

fn find_func(name string) ?DataFn {
	return match name {
		'vlang' { which_lang }
		else { none }
	}
}

fn main() {
	req := find_func('vlang')
	fun := req('options')
	println(fun)
}

What did you expect to see?

a print of 'options'

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/main.01J8BE9J6F2YCCRKS27WSRJ220.tmp.c:13346: error: lvalue expected
=================================================================
(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 .

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.

@felipensp felipensp self-assigned this Sep 22, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Sep 22, 2024
@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Option Type Bugs/feature requests, that are related to `?Type`. labels Sep 22, 2024
@spytheman
Copy link
Member

A workaround could be:

module main

type DataFn = fn (name string) string

struct Wrapper {
    f DataFn = unsafe { nil }
}

fn which_lang(name string) string {
    return name
}

fn find_func(name string) ?Wrapper {
    return match name {
        'vlang' { Wrapper{ f: which_lang } }
        else { none }
    }
}

fn main() {
    req := find_func('vlang')
    fun := req?.f('options')
    println(fun)
}

@felipensp felipensp added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Sep 22, 2024
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. 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