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

compiler panic: string.repeat: count is negative: -1 #20734

Closed
pd-giz-dave opened this issue Feb 5, 2024 · 3 comments · Fixed by #20741
Closed

compiler panic: string.repeat: count is negative: -1 #20734

pd-giz-dave opened this issue Feb 5, 2024 · 3 comments · Fixed by #20741
Assignees
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@pd-giz-dave
Copy link

pd-giz-dave commented Feb 5, 2024

Describe the bug

Compile this code:

module main

struct Blob {
	label int
}

// this breaks the compiler - comment it out and everything runs OK
// with it in get:
// V panic: string.repeat: count is negative: -1
// v hash: ac2dcc2
//                                                        | 0x561fbf1fa1a3 | /usr/lib/vlang/v(+0x1681a3)
//                                                        | 0x561fbf1fd7c3 | /usr/lib/vlang/v(+0x16b7c3)
//                                                        | 0x561fbf306e23 | /usr/lib/vlang/v(+0x274e23)
//                                                        | 0x561fbf2a0737 | /usr/lib/vlang/v(+0x20e737)
//                                                        | 0x561fbf2a3879 | /usr/lib/vlang/v(+0x211879)
//                                                        | 0x561fbf2a68b0 | /usr/lib/vlang/v(+0x2148b0)
//                                                        | 0x561fbf09571a | /usr/lib/vlang/v(+0x371a)
//                                                        | 0x7f83ce258cd0 | /usr/lib/libc.so.6(+0x27cd0)
//                                                        | 0x7f83ce258d8a | /usr/lib/libc.so.6(__libc_start_main+0x8a)
//                                                        | 0x561fbf095755 | /usr/lib/vlang/v(+0x3755)
// remove the mut and it works, put it in and get the error above
fn (mut b Blob) str() string {
	return ''
}

struct Labels {
	blobs map[int]Blob
}

fn break_it() Labels {
	mut a := Labels{}
	return a
}

fn main() {
	mut a := break_it()
	println(a)
}

Reproduction Steps

Just attempt to compile the above code

Expected Behavior

Compiles OK

Current Behavior

Compiler reports:

V panic: string.repeat: count is negative: -1
v hash: ac2dcc2
                                                        | 0x64f0576211a3 | /usr/lib/vlang/v(+0x1681a3) 
                                                        | 0x64f0576247c3 | /usr/lib/vlang/v(+0x16b7c3) 
                                                        | 0x64f05772de23 | /usr/lib/vlang/v(+0x274e23) 
                                                        | 0x64f0576c7737 | /usr/lib/vlang/v(+0x20e737) 
                                                        | 0x64f0576ca879 | /usr/lib/vlang/v(+0x211879) 
                                                        | 0x64f0576cd8b0 | /usr/lib/vlang/v(+0x2148b0) 
                                                        | 0x64f0574bc71a | /usr/lib/vlang/v(+0x371a) 
                                                        | 0x70499631dcd0 | /usr/lib/libc.so.6(+0x27cd0) 
                                                        | 0x70499631dd8a | /usr/lib/libc.so.6(__libc_start_main+0x8a) 
                                                        | 0x64f0574bc755 | /usr/lib/vlang/v(+0x3755) 

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 ac2dcc2

Environment details (OS name and version, etc.)

EndeavourOS

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.

@pd-giz-dave pd-giz-dave added the Bug This tag is applied to issues which reports bugs. label Feb 5, 2024
@pd-giz-dave
Copy link
Author

After doing a v up to "V 0.4.4 a374d25, timestamp: 2024-02-05 17:14:27 +0200" stack trace slightly more informative:

V panic: string.repeat: count is negative: -1
v hash: a374d25
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:21494: at _v_panic: Backtrace
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:25928: by string_repeat
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:29478: by v__gen__c__Gen_gen_str_for_map
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:28841: by v__gen__c__Gen_final_gen_str
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:30571: by v__gen__c__gen
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53223: by v__builder__cbuilder__gen_c
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53192: by v__builder__cbuilder__build_c
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53182: by v__builder__cbuilder__compile_c
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53088: by v__builder__Builder_rebuild
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:52193: by v__builder__compile
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53523: by main__rebuild
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:53471: by main__main
/tmp/v_1000/v2.01HNWY079Z5K8D4Z9M7VDKMQ8R.tmp.c:54180: by main

@viniciusfdasilva
Copy link
Contributor

To contribute with this issue, I executed several tests and managed to find a solution by passing & to reference Blob and successfully executed with mut in the str() function. The problem lies in using mut with the value of a map being of type Blob (struct) as in the code below:

module main

struct Blob {
	label int
}

// remove the mut and it works, put it in and get the error above
module main

struct Blob {
	label int
}

// remove the mut and it works, put it in and get the error above
fn (mut b Blob) str() string {
	return 'blob'
}

fn main() {

	// Works
	mut a1 := map[int]&Blob
	a1[1] = &Blob{label: 4}
	println('First execution ${a1}')


	// Not works
	//mut a2 := map[int]Blob
	//a2[1] = Blob{label: 4}
	//println('Second execution ${a2}')

	
}

@pd-giz-dave
Copy link
Author

pd-giz-dave commented Feb 6, 2024 via email

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

Successfully merging a pull request may close this issue.

3 participants