Skip to content

Cannot assign interface pointer to struct initialization: error: cannot assign a pointer to option struct field #23879

Closed
@daansystems

Description

@daansystems
V version: V 0.4.9 e66e996, press to see full `v doctor` output
V full version V 0.4.9 5c65e58.e66e996
OS windows, Microsoft Windows 11 Pro 26100 64-bit
Processor 24 cpus, 64bit, little endian, AMD Ryzen 9 3900X 12-Core Processor
Memory 18.64GB/31.9GB
V executable C:\v\v.exe
V last modified time 2025-03-08 10:49:50
V home dir OK, value: C:\v
VMODULES OK, value: C:\Users\info.vmodules
VTMP OK, value: C:\Users\info\Documents\MobaXterm\slash\mx86_64b\var\log\xwin\v_0
Current working dir OK, value: D:\vbug2
Git version git version 2.42.0.windows.2
V git status weekly.2025.08-112-ge66e9960-dirty
.git/config present true
cc version N/A
gcc version N/A
clang version clang version 17.0.1
msvc version N/A
tcc version tcc version 0.9.27 (x86_64 Windows)
tcc git status thirdparty-windows-amd64 b425ac82
emcc version N/A
glibc version ldd (cygwin) 3.4.7

What did you do?
./v -g -o vdbg cmd/v && ./vdbg main.v && D:\vbug2\main.exe

module main

@[heap]
interface IGameObject {
mut:
	name     string
	parent   ?&IGameObject
}

@[heap]
struct GameObject implements IGameObject {
mut:
	name     string
	parent   ?&IGameObject
}

struct Ship implements IGameObject {
	GameObject
	speed f32
}

fn main() {
	mut world := &GameObject{
		name: 'world'
	}
	mut ship := &Ship{
		name: 'ship',
		parent: world
	}
	eprintln("ship: ${ship}")
}

What did you see?

main.v:28:3: error: cannot assign a pointer to option struct field
   26 |     mut ship := &Ship{
   27 |         name: 'ship',
   28 |         parent: world
      |         ~~~~~~~~~~~~~
   29 |     }
   30 |     eprintln("ship: ${ship}")

What did you expect to see?

no v error

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Unit: CheckerBugs/feature requests, that are related to the type checker.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions