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

structs containing references cannot be used in maps #20245

Closed
elliotchance opened this issue Dec 21, 2023 · 3 comments · Fixed by #20251
Closed

structs containing references cannot be used in maps #20245

elliotchance opened this issue Dec 21, 2023 · 3 comments · Fixed by #20251
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Nicer V Errors Bugs/feature requests, related to improving V error messages. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@elliotchance
Copy link
Contributor

elliotchance commented Dec 21, 2023

Describe the bug

If a struct contains a reference and is used in a map the compiler always reports:

bug.v:7:13: notice: reference field `MyStruct.a` must be initialized (part of struct `MyStruct`)
    5 | 
    6 | fn main() {
    7 |     mut foo := map[string]MyStruct{}
      |                ~~~~~~~~~~~~~~~~~~~~~
    8 |     println(foo)
    9 | }

Reproduction Steps

struct MyStruct {
	a &int
	b int
}

fn main() {
	mut foo := map[string]MyStruct{}
	println(foo)
}

Expected Behavior

The compiler should not raise a notice.

Current Behavior

An notice is raised.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.3 ed754cf

Environment details (OS name and version, etc.)

V full version: V 0.4.3 4bc9a8f.ed754cf
OS: macos, macOS, 12.6, 21G115
Processor: 10 cpus, 64bit, little endian, Apple M1 Max

getwd: /Users/elliot/Library/Mobile Documents/com~apple~CloudDocs/Development/github.com/elliotchance/vsql/tests
vexe: /Users/elliot/Downloads/v-5/v
vexe mtime: 2023-12-21 14:58:49

vroot: OK, value: /Users/elliot/Downloads/v-5
VMODULES: OK, value: /Users/elliot/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.37.0 (Apple Git-136)
Git vroot status: weekly.2023.44-420-ged754cfc
.git/config present: true

CC version: Apple clang version 14.0.0 (clang-1400.0.29.102)
thirdparty/tcc status: thirdparty-macos-amd64 46662e20

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.

@elliotchance elliotchance added the Bug This tag is applied to issues which reports bugs. label Dec 21, 2023
@JalonSolov
Copy link
Contributor

Note that this works if you give a default value for the a field, as in

    a &int = unsafe { nil }

@elliotchance
Copy link
Contributor Author

@JalonSolov that solution is not acceptable for two reasons:

  1. It's not the fault of MyStruct to have to allow an invalid state to satisfy the compiler when it can (and should) be guaranteed to have a set.
  2. What if MyStruct is actually an external type that you cannot modify? You would be stuck with the compiler warning forever, or have to create duplicate types and mappers to get around it.

@spytheman spytheman added Unit: Checker Bugs/feature requests, that are related to the type checker. Nicer V Errors Bugs/feature requests, related to improving V error messages. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Dec 21, 2023
@shove70
Copy link
Contributor

shove70 commented Dec 22, 2023

This is intentional, to prevent inadvertency segmentation fault such as:

struct MyStruct {
	a &int
	b int
}

fn main() {
	mut foo := map[string]MyStruct{}
	println(*(foo[''].a))
}

outputs:

signal 11: segmentation fault
0   libsystem_platform.dylib            0x00007ff8134f037d _sigtramp + 29
1   ???                                 0x0000000101700030 0x0 + 4319084592
2   a                                   0x00000001011339f7 main + 71
3   dyld                                0x00007ff813137386 start + 1942

Considering the reason given by @elliotchance, which is also sufficient, I wonder whether it is possible to add the notification of use unsafe {}

struct MyStruct {
	a &int
	b int
}

fn main() {
	mut foo := unsafe { map[string]MyStruct{} }
}

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. Nicer V Errors Bugs/feature requests, related to improving V error messages. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
4 participants