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

immutable instance of a struct can be modified via interface #23509

Open
syobocat opened this issue Jan 18, 2025 · 2 comments
Open

immutable instance of a struct can be modified via interface #23509

syobocat opened this issue Jan 18, 2025 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@syobocat
Copy link
Contributor

syobocat commented Jan 18, 2025

Describe the bug

I don't know whether this is a bug or a feature, but the following syntax passes compilation, allowing you to modify an immutable instance of a struct:

foo := Struct{}
mut bar := Interface(foo)

Reproduction Steps

https://play.vlang.io/p/f79078e38a

interface Interface {
mut:
	foo string
}

struct Struct {
mut:
	foo string
}

// Immutable variable
bar := Struct{
	foo: 'hello'
}

// You can't do:
/*
mut foobar := &bar
foobar.foo = 'bye'
*/

// But you can do:
mut foobar := Interface(bar)
foobar.foo = 'bye'

// Both prints `bye`
println(foobar.foo)
println(bar.foo)

Expected Behavior

Compilation error?

Current Behavior

Compiles and runs normally

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 5dd28cf

Environment details (OS name and version, etc.)

V full version V 0.4.9 c4aaa2e.5dd28cf
OS macos, macOS, 15.2, 24C101
Processor 8 cpus, 64bit, little endian, Apple M3
Memory 0.59GB/16GB
V executable /Users/syobon/.v/v
V last modified time 2025-01-18 11:18:26
V home dir OK, value: /Users/syobon/.v
VMODULES OK, value: /Users/syobon/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/syobon
Git version git version 2.39.5 (Apple Git-154)
V git status weekly.2025.1-74-g5dd28cf9
.git/config present true
cc version Apple clang version 16.0.0 (clang-1600.0.26.6)
gcc version Apple clang version 16.0.0 (clang-1600.0.26.6)
clang version Apple clang version 16.0.0 (clang-1600.0.26.6)
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 713692d4
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.

@syobocat syobocat added the Bug This tag is applied to issues which reports bugs. label Jan 18, 2025
Copy link

Connected to Huly®: V_0.6-21938

@jorgeluismireles
Copy link

Let alone the interface, unsafe also permits to modify the mutable field of a non mutable object:
https://play.vlang.io/p/44c9430b7c

struct Struct {
mut:
	foo string
}
// Immutable variable
bar := Struct{ foo: 'hello' }
unsafe {
	bar.foo = 'bye'
}
println(bar.foo) // prints 'bye'

Also, I don't know if is a feature or bug.

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.
Projects
None yet
Development

No branches or pull requests

2 participants