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

False errors when initializing an embedded C struct that contains a reference. #21046

Closed
spiveeworks opened this issue Mar 17, 2024 · 0 comments · Fixed by #21137
Closed

False errors when initializing an embedded C struct that contains a reference. #21046

spiveeworks opened this issue Mar 17, 2024 · 0 comments · Fixed by #21137
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@spiveeworks
Copy link
Contributor

spiveeworks commented Mar 17, 2024

Describe the bug

When declaring a struct C.MyCStruct with any name, that has a reference inside it, and embedding that struct inside a wrapper, initializing the wrapper doesn't work unless both the MyCStruct field and the inner fields are initialized.

Reproduction Steps

struct C.MyCStruct {
    data &u8
}

struct MyWrapper {
    C.MyCStruct
}

fn (it C.MyCStruct)wrap() MyWrapper {
    return MyWrapper {
        MyCStruct: it
        data: it.data
    }
}

Expected Behavior

The above code should the following error:

error: duplicate field name in struct literal: data

Removing either field initializer should make it wrap the struct as expected.

Current Behavior

The incorrect code sample given actually compiles without error, generating the designated initializer ((main__MyWrapper){.MyCStruct = it,})

What's worse, though, is if you remove MyCStruct from the struct literal, leaving only the initializer for the data field you get this error message:

error: reference field MyWrapper.MyCStruct.data must be initialized (part of struct C.MyCStruct)

And if you remove the data field, leaving the MyCStruct: it field, you get two error messages:

error: reference field MyWrapper.data must be initialized
error: reference field C.MyCStruct.data must be initialized

If the inner type is not a C struct, e.g. replacing C.MyCStruct with MyNormalStruct then all three of these cases work as expected of course, but it is worth noting that when both fields are removed from the MyWrapper struct literal it also gives two errors rather than one, but I can't tell whether that is intentional or not. The errors:

error: reference field MyWrapper.data must be initialized
error: reference field MyNormalStruct.data must be initialized

Possible Solution

The check that all reference fields were initialized seems to have a bug in it, that only comes up when the embedded struct is a C struct.

Additional Information/Context

I was taking a C struct from my old codebase and giving it some iterator wrappers. The wrappers worked, but I had to write this seemingly incorrect struct literal to make it work!

V version

V 0.4.4 549654a

Environment details (OS name and version, etc.)

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

getwd: C:\Users\Owner
vexe: C:\Program Files\v\v.exe
vexe mtime: 2024-03-17 05:49:07

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.11-39-g549654ae
.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 Mar 17, 2024
@felipensp felipensp added the Unit: Checker Bugs/feature requests, that are related to the type checker. label Mar 29, 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: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants