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

constant arrays shouldn't be allowed to be put under mut: struct fields #22862

Closed
zeozeozeo opened this issue Nov 14, 2024 · 2 comments · Fixed by #22863
Closed

constant arrays shouldn't be allowed to be put under mut: struct fields #22862

zeozeozeo opened this issue Nov 14, 2024 · 2 comments · Fixed by #22863
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@zeozeozeo
Copy link
Contributor

zeozeozeo commented Nov 14, 2024

V doctor:

V full version: V 0.4.8 5c65e58.df51e84
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 10 cpus, 64bit, little endian, 

getwd: C:\Users\user\Downloads
vexe: C:\Users\user\Desktop\Code\v\v.exe
vexe mtime: 2024-11-14 18:10:30

vroot: OK, value: C:\Users\user\Desktop\Code\v
VMODULES: OK, value: C:\Users\user\.vmodules
VTMP: OK, value: C:\Users\user\AppData\Local\Temp\v_0

Git version: git version 2.42.0.windows.1
Git vroot status: weekly.2024.45-99-gdf51e840
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,

operable program or batch file.


emcc version: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.71 (4171ae200b77a6c266b0e1ebb507d61d1ade3501)
thirdparty/tcc status: thirdparty-windows-amd64 b425ac82

What did you do?
./v -g -o vdbg cmd/v && ./vdbg vvv.v

const arr = [1, 2, 3]

struct Muta {
mut:
	a []int
}

fn main() {
	mut muta := Muta{a: arr}
	muta.a[0] = 52
	println(arr) // [52, 2, 3]
}

What did you expect to see?

compilation error or [1, 2, 3] in stdout

What did you see instead?

[52, 2, 3]

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.

Huly®: V_0.6-21305

@Delta456 Delta456 self-assigned this Nov 14, 2024
@JalonSolov
Copy link
Contributor

Agreed. It should be copying the const into the field, rather than just pointing to it. The current C code being generated is

VV_LOCAL_SYMBOL void main__main(void) {
        main__Muta muta = ((main__Muta){.a = _const_main__arr,});
        array_set(&muta.a, 0, &(int[]) { 52 });
        println(Array_int_str(_const_main__arr));
}

@JalonSolov JalonSolov added Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Nov 14, 2024
@Delta456
Copy link
Member

The checker needs to raise an error and recommend them to use arr.copy()

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. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants