Skip to content

cgen: array assertion fails on MacBook Pro M2 #24529

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

Open
gchumillas opened this issue May 20, 2025 · 4 comments · May be fixed by #24576
Open

cgen: array assertion fails on MacBook Pro M2 #24529

gchumillas opened this issue May 20, 2025 · 4 comments · May be fixed by #24576
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

@gchumillas
Copy link
Contributor

gchumillas commented May 20, 2025

Describe the bug

The following code does not terminate properly on an MacBook Pro M2 (and M1 too):

// main_test.v
fn foo(i int) !int {
    m := [3, 4, 5]

    // This works
    // r := m[i] or {
    //     return error('bad boy!')
    // }

    // this does not work
    r := m[i]!

    return r
}

fn test_foo() {
    g := foo(3) or { 12 }
    assert g == 12
}

Test the file:

v test main_test.v

See the output:

fn test_foo
   > assert g == 12
     Left value (len: 7): `5119952` (this is a random value)
    Right value (len: 2): `12`

Reproduction Steps

Buy a Macbook Pro or ask a friend who already has one for a favor and run the code above.

Expected Behavior

It should complain.

Current Behavior

It doesn't complain.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 6be298d

Environment details (OS name and version, etc.)

V full version V 0.4.10 6be298d
OS macos, macOS, 15.5, 24F74
Processor 10 cpus, 64bit, little endian, Apple M4
Memory 0.14GB/16GB
V executable /Users/gonzalo/Documents/Projects/Personal/v/v
V last modified time 2025-05-20 11:21:19
V home dir OK, value: /Users/gonzalo/Documents/Projects/Personal/v
VMODULES OK, value: /Users/gonzalo/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/gonzalo/Documents/Projects/Personal/v
Git version git version 2.49.0
V git status 6be298d
.git/config present true
cc version Apple clang version 17.0.0 (clang-1700.0.13.3)
gcc version Apple clang version 17.0.0 (clang-1700.0.13.3)
clang version Apple clang version 17.0.0 (clang-1700.0.13.3)
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 e447816c
emcc version N/A
glibc version 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.

@gchumillas gchumillas added the Bug This tag is applied to issues which reports bugs. label May 20, 2025
Copy link

Connected to Huly®: V_0.6-22897

@gchumillas
Copy link
Contributor Author

It only fails on test files. For example:

// v run app.v OK!
fn foo(i int) !int {
    m := [3, 4, 5]
    return m[i]!
}

fn main() {
    g := foo(3) or { 12 }
    assert g == 12
}
// v app_test.v
//
// Output
// app_test.v:9: fn test_main
//    > assert g == 12
//      Left value (len: 8): `14917584` (random value)
//     Right value (len: 2): `12`
fn foo(i int) !int {
    m := [3, 4, 5]
    return m[i]!
}

fn test_main() {
    g := foo(3) or { 12 }
    assert g == 12
}

@gchumillas gchumillas changed the title cgen: array assertion fails on MacBook Pro M1 cgen: array assertion fails on MacBook Pro M1 and M2 May 20, 2025
@gchumillas gchumillas changed the title cgen: array assertion fails on MacBook Pro M1 and M2 cgen: array assertion fails on MacBook Pro M2 May 20, 2025
@gchumillas
Copy link
Contributor Author

Probably the problem is here: https://github.com/vlang/v/blob/master/vlib/v/gen/c/cgen.v#L7090

// maybe sizeof is incorrect
g.writeln2('\t${styp} ${err_obj};', '\tmemcpy(&${err_obj}, &${cvar_name}, sizeof(${result_name}));')

According to ChatGPT:

The problem is that `sizeof(_result)` (or `sizeof(_option)`) is **not sufficient** to copy the entire structure of a specific type like `_result_int` or `_option_int`.
Both types (`_result_int` and `_option_int`) have additional fields (such as the `data` array) whose size depends on the specific type (`int`, `string`, etc.).
Copying only the base size results in the `data` field of the destination structure being garbage or zeroed out, which is why the returned value is not what was expected.

@felipensp felipensp self-assigned this May 24, 2025
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label May 24, 2025
@felipensp
Copy link
Member

Can you try PR #24564 ?

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
2 participants