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

type OneD = []Thing causes unexpected behaviour #22170

Closed
pd-giz-dave opened this issue Sep 6, 2024 · 1 comment · Fixed by #22175
Closed

type OneD = []Thing causes unexpected behaviour #22170

pd-giz-dave opened this issue Sep 6, 2024 · 1 comment · Fixed by #22175
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@pd-giz-dave
Copy link

pd-giz-dave commented Sep 6, 2024

Describe the bug

Identical functions except one uses a type alias for an array and the other does not behave differently.
The one using an alias also generates bad code (as seen in the debugger).
See example below.

Reproduction Steps

Compile and run this:

module main
type OneD = []Thing
struct Thing {
	item int
}
fn good() [][]Thing {
	println('start good()')
	mut two_d := [][]Thing{}
	mut one_d := []Thing{}
	one_d << Thing{1}
	one_d << Thing{2}
	assert one_d.len == 2, 'one_d length not 2'
	assert one_d[0] == Thing{1}, 'one_d[0] not 1'
	assert one_d[1] == Thing{2}, 'one_d[1] not 2'
	two_d << one_d
	assert two_d.len == 1, 'two_d length not 1'
	for i, one in two_d {
		for j, item in one {
			println('two_d[${i}][${j}]=${item}')
			assert item == Thing{j+1}
		}
	}
	println('end good()')
	return two_d
}
fn bad() []OneD {
	println('start bad()')
	mut two_d := []OneD{}
	mut one_d := OneD{}
	one_d << Thing{1}
	one_d << Thing{2}
	assert one_d.len == 2, 'one_d length not 2'
	assert one_d[0] == Thing{1}, 'one_d[0] not 1'
	assert one_d[1] == Thing{2}, 'one_d[1] not 2'
	two_d << one_d
	assert two_d.len == 1, 'two_d length not 1'
	for i, one in two_d {
		for j, item in one {
			println('two_d[${i}][${j}]=${item}')
			assert item == Thing{j+1}
		}
	}
	println('end bad()')
	return two_d
}
fn main() {
	println('start main()')
	good_two_d := good()
	assert good_two_d.len == 1, 'good_two_d length not 1'
	for i, one_d in good_two_d {
		for j, item in one_d {
			println('good_two_d[${i}][${j}]=${item}')
			assert item == Thing{j+1}
		}
	}
	bad_two_d := bad()
	assert bad_two_d.len == 1, 'bad_two_d length not 1'
	for i, one_d in bad_two_d {
		for j, item in one_d {
			println('bad_two_d[${i}][${j}]=${item}')
			assert item == Thing{j+1}
		}
	}
	println('end main()')
}

Expected Behavior

I expect good() and bad() to behave the same.

Current Behavior

bad() produces a rubbish two_d array

Possible Solution

do not use a type alias

Additional Information/Context

No response

V version

V 0.4.7 294f7e4.ae9456a

Environment details (OS name and version, etc.)

dave@dave-mini-pc~/.../fellsafe/v >>> v doctor
V full version: V 0.4.7 294f7e4.ae9456a
OS: linux, "EndeavourOS Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics

getwd: /home/dave/precious/fellsafe/v
vexe: /home/dave/v/v
vexe mtime: 2024-09-06 15:35:32

vroot: OK, value: /home/dave/v
VMODULES: OK, value: /home/dave/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.27-292-gae9456ad (3 commit(s) behind V master)
.git/config present: true

CC version: cc (GCC) 14.2.1 20240805
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5
dave@dave-mini-pc~/.../fellsafe/v >>>  
@pd-giz-dave pd-giz-dave added the Bug This tag is applied to issues which reports bugs. label Sep 6, 2024
@felipensp felipensp self-assigned this Sep 7, 2024
@felipensp felipensp added the Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. label Sep 7, 2024
@pd-giz-dave
Copy link
Author

I continue to be impressed by your responsiveness, great work. Thanks.

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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants