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

generic code struggles with optional function args #21215

Closed
spiveeworks opened this issue Apr 7, 2024 · 0 comments · Fixed by #21276
Closed

generic code struggles with optional function args #21215

spiveeworks opened this issue Apr 7, 2024 · 0 comments · Fixed by #21276
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@spiveeworks
Copy link
Contributor

spiveeworks commented Apr 7, 2024

Describe the bug

Calling generic higher order functions with optional callbacks leads to all kinds of C compile and runtime errors. I have tried to provide a concrete example that is easy to understand, but this gives issues completely different to the issues I was originally running into. This case still seem like a simpler place to start, before I try and build SSCCEs for the other issues I found.

Reproduction Steps

pub type MyFn[T] = fn (mut my_state T)

fn higher_order_fn[T](initial_state T, maybe_callback ?MyFn[T]) State {
    mut my_state := initial_state
    if callback := maybe_callback {
        callback[T](mut my_state)
        callback[T](mut my_state)
    }
    return my_state
}

pub struct State {
mut:
    x int
}

fn handler(mut my_state State) {
    my_state.x += 1
}

fn main() {
    mut state := State { }
    state = higher_order_fn[State](state, ?MyFn[State](handler))
    state = higher_order_fn[State](state, ?MyFn[State](none))
    state = higher_order_fn[State](state, handler)
    state = higher_order_fn[State](state, none)
    println("${state}")
}

Expected Behavior

I expect the above code to work, and to print State { x: 4 }. I also expect any subset of those four lines to work.

Current Behavior

Many things go wrong.

The first thing seems intentional:

higher_order_fn_error2.v:26:43: error: cannot use `none` as generic argument
   24 |     state = higher_order_fn[State](state, ?MyFn[State](none))
   25 |     state = higher_order_fn[State](state, handler)
   26 |     state = higher_order_fn[State](state, none)
      |                                           ~~~~
   27 |     println("${state}")
   28 | }

Fine, so let's remove line 26.

Now it compiles, but crashes:

Unhandled Exception 0xC0000005
C.CaptureStackBackTrace returned less than 2 frames
signal 11: segmentation fault
6 : print_backtrace_skipping_top_frames  ?? : address = 0x2a1510514d0
5 : seh_filter_exe             ?? : address = 0x2a1510515a0
4 : `__scrt_common_main_seh'::`1'::filt$0  D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:304
3 : _C_specific_handler        ?? : address = 0x2a151051690
2 : _chkstk                    ?? : address = 0x2a151051510
1 : RtlRaiseException          ?? : address = 0x2a1510516a0
0 : KiUserExceptionDispatcher  ?? : address = 0x2a151051580

so remove line 24, and run again:

State{
    x: 4
}

It works. Okay, let's remove line 23 now, and just call the function once.

higher_order_fn_error2.01HTVR8KKCEQBVVH81E32QQZ4E.tmp.c
C:\Users\Owner\AppData\Local\Temp\v_0\higher_order_fn_error2.01HTVR8KKCEQBVVH81E32QQZ4E.tmp.c(12786): error C2440: '=': cannot convert from '_option_anon_fn_mut_main__state' to '_option_anon_fn_mut_t'

builder error: msvc error

cgen seems to have screwed something up now. weird!

This may turn out to be one bug or two, but these were just the things I ran into this time. Other times I find these higher order functions work fine, and other times I think I run into other issues. I can experiment more and give more specific bug reports once the general chaos has been fixed a little.

Possible Solution

Good luck.

Additional Information/Context

Similar to #21214 I encountered these issues trying to make a wrapper for gg that handled streaming images automatically. Some apps provide event callbacks, other apps just provide do_frame callbacks with no event callbacks. I got it working, with lots of [T] annotations everywhere, but it was a minefield.

V version

V 0.4.5 ad17be5

Environment details (OS name and version, etc.)

V full version: V 0.4.5 386bd77.ad17be5
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 4 cpus, 64bit, little endian,

getwd: C:\Users\Owner\Code\v-experiments
vexe: C:\Program Files\v\v.exe
vexe mtime: 2024-04-07 05:22:56

vroot: contains spaces, value: C:\Program Files\v
VMODULES: OK, value: C:\Users\Owner.vmodules
VTMP: OK, value: C:\Users\Owner\AppData\Local\Temp\v_0

Git version: git version 2.37.2.windows.2
Git vroot status: weekly.2024.14-26-gad17be5d (6 commit(s) behind V master)
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc: N/A

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.

@spiveeworks spiveeworks added the Bug This tag is applied to issues which reports bugs. label Apr 7, 2024
@felipensp felipensp self-assigned this Apr 13, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Apr 13, 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. 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.

2 participants