Skip to content

assigning to mutable optional give v error: next is immutable, declare it with mut to make it mutable #24351

Description

@daansystems
V version: V 0.4.10 880f216, press to see full `v doctor` output
V full version V 0.4.10 5c65e58.880f216
OS windows, Microsoft Windows 11 Pro 26100 64-bit
Processor 24 cpus, 64bit, little endian, AMD Ryzen 9 3900X 12-Core Processor
Memory 18.12GB/31.9GB
V executable C:\v\v.exe
V last modified time 2025-04-28 07:34:43
V home dir OK, value: C:\v
VMODULES OK, value: C:\Users\info.vmodules
VTMP OK, value: C:\Users\info\AppData\Local\Temp\v_0
Current working dir OK, value: D:\vbug2
Git version git version 2.42.0.windows.2
V git status weekly.2025.17-12-g880f2169-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
	next       ?&IGameObject
	child      ?&IGameObject
	last_child ?&IGameObject
}

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

fn main() {
	mut v1 := &GameObject{
		name: 'v1'
	}

	mut next := v1.next
	for {
		if next != none {
			eprintln(next.name)
			next = next.next
		} else {
			break
		}
	}
}

What did you see?

main.v:32:4: error: `next` is immutable, declare it with `mut` to make it mutable
   30 |         if next != none {
   31 |             eprintln(next.name)
   32 |             next = next.next
      |             ~~~~
   33 |         } else {
   34 |             break

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.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: CheckerBugs/feature requests, that are related to the type checker.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions