Skip to content

checker: array.insert() does not work if the array contains fixed arrays #23090

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

Closed
Le0Developer opened this issue Dec 7, 2024 · 6 comments · Fixed by #23100
Closed

checker: array.insert() does not work if the array contains fixed arrays #23090

Le0Developer opened this issue Dec 7, 2024 · 6 comments · Fixed by #23100
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. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@Le0Developer
Copy link
Member

Le0Developer commented Dec 7, 2024

V doctor:

V full version: V 0.4.8 602b097.e32e9f7
OS: macos, macOS, 15.1.1, 24B2091
Processor: 10 cpus, 64bit, little endian, Apple M4

getwd: /Users/leodev/p/v/fast2
vexe: /Users/leodev/p/v/v/v
vexe mtime: 2024-12-07 13:12:52

vroot: OK, value: /Users/leodev/p/v/v
VMODULES: OK, value: /Users/leodev/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.47.0
Git vroot status: weekly.2024.49-33-ge32e9f70 (1 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

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

fn main() {
	mut arr := [][2]int{}
	arr.insert(0, [1, 2]!)
	println(arr)
}

What did you expect to see?

Successful compilation

What did you see instead?

repro.v:3:16: error: cannot use `[2]int` as `voidptr` in argument 2 to `[][2]int.insert`
    1 | fn main() {
    2 |     mut arr := [][2]int{}
    3 |     arr.insert(0, [1, 2]!)
      |                   ~~~~~~~
    4 |     println(arr)
    5 | }

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-21528

@Le0Developer Le0Developer added the Bug This tag is applied to issues which reports bugs. label Dec 7, 2024
@felipensp
Copy link
Member

The correct would be:

fn main() {
	mut arr := [][2]int{}
	arr.insert(0, [[1, 2]!])
	println(arr)
}

@felipensp felipensp removed the Bug This tag is applied to issues which reports bugs. label Dec 7, 2024
@Le0Developer
Copy link
Member Author

The correct would be:

fn main() {
	mut arr := [][2]int{}
	arr.insert(0, [[1, 2]!])
	println(arr)
}

no? this is insert, not the private insert_many or arr <<, this function takes a single item.

@jorgeluismireles
Copy link

I think LeoDeveloper is right. I think the problem could be related to the fixed array. For normal array elements below 7,8 are appended ok.

fn main() {
	mut arr := [][]int{}
	arr << [1, 2]
	arr << [[3, 4], [5, 6]]
	arr.insert(arr.len, [7, 8]) // <- works as expected
	arr.insert(arr.len, [[9,10]]) // <- not rejected but C error
	println(arr)
}

But I was expecting the compiler will reject elements 9 and 10, but not, generates a C error :-(

@JalonSolov
Copy link
Contributor

The last one should have been a V error. You tried to insert an array of arrays as an array in the array.

It would be the same as trying

mut arr := []int{}
arr.insert(arr.len, [9,10])

In other words, the thing being inserted must have one fewer set of [] than what it is being inserted into. That makes this a separate issue from the original, as it has the correct number of sets of [], but it is failing.

@felipensp felipensp added the Bug This tag is applied to issues which reports bugs. label Dec 7, 2024
@felipensp
Copy link
Member

I think we should improve the error message.

bug.v:3:16: error: cannot insert `[]int` to `[][2]int`
    1 | fn main() {
    2 |     mut arr := [][2]int{}
    3 |     arr.insert(0, [1, 2])
      |                   ~~~~~~
    4 |     println(arr)
    5 | }

@felipensp felipensp added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Dec 7, 2024
@felipensp felipensp self-assigned this Dec 7, 2024
@JalonSolov
Copy link
Contributor

I agree that the error message could be better, but remember the original code has

arr.insert(0, [1, 2]!)

which should work as it is trying to insert a fixed array, while your example has

arr.insert(0, [1, 2])

which should not, since it isn't a fixed array.

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. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants