Bug when returning booleans from functions #41

Closed
opened 2026-06-15 15:18:46 +00:00 by Furkan · 5 comments

Platform: windows x86_64

Code:

import io

foo() -> bool {
	return -1 > 0
}

main() {
	if foo() {
		io.writeLine("bug")
	} else {
		io.writeLine("fine")
	}
}
Platform: windows x86_64 Code: ``` import io foo() -> bool { return -1 > 0 } main() { if foo() { io.writeLine("bug") } else { io.writeLine("fine") } } ```
ed self-assigned this 2026-06-15 15:27:51 +00:00
Owner

Nice find, thank you for the bug report!
This should be fixed in c64bce3eb8.

Nice find, thank you for the bug report! This should be fixed in https://git.urbach.dev/cli/q/commit/c64bce3eb8a6f5e9d4873f3ce3b3e16ad5e614cb.
ed added spent time 2026-06-15 16:53:57 +00:00
1 hour
Author

I think this is caused by the same issue

import io

foo(first string, second string) -> bool {
	count := 0
	loop j := 1..2 {
		if (second[0] == second[j]) && (second[0] == first[j]) {
			count -= 1
		}
	}

	// When this line is present everything goes normally
	// io.write(count)

	return count > 0
}

main() {
	word := "aa"
	second := "ss"

	if foo(word, second) {
		io.write("bug")
	}
}
I think this is caused by the same issue ``` import io foo(first string, second string) -> bool { count := 0 loop j := 1..2 { if (second[0] == second[j]) && (second[0] == first[j]) { count -= 1 } } // When this line is present everything goes normally // io.write(count) return count > 0 } main() { word := "aa" second := "ss" if foo(word, second) { io.write("bug") } } ```
Owner

I did a diff between the io.write version and the one without:

$ q ssa tests/a.q -func foo > foo1.txt
$ q ssa tests/a.q -func foo > foo2.txt
$ diff foo1.txt foo2.txt 
2c2
<   α0  = args[0] *uint8 r0 id: first.ptr live: α1 α0 
---
>   α0  = args[0] *uint8 r9 id: first.ptr live: α1 α0 
4c4
<   α2  = 0 int r2 id: count live: α2 α1 α0 
---
>   α2  = 0 int r0 id: count live: α2 α1 α0 
10c10
<   α7  = phi(α2, α28) int r9 id: count live: α7 α6 α1 α0 
---
>   α7  = phi(α2, α28) int r3 id: count live: α7 α6 α1 α0 
31c31
<   α25 = α7 - α24 int r7 id: count live: α28 α25 α7 α6 α1 α0 
---
>   α25 = α7 - α24 int r2 id: count live: α28 α25 α7 α6 α1 α0 
35c35
<   α28 = phi(α7, α25) int r2 id: count live: α28 α7 α6 α1 α0 
---
>   α28 = phi(α7, α25) int r0 id: count live: α28 α7 α6 α1 α0 
41,43c41,44
<   α33 = 0 int r? live: α33 α7 
<   α34 = α7 > α33 bool r0 live: α34 
<   α35 = return α34 void r? 
---
>   α33 = io.write[int64](α7) void r? live: α7 
>   α34 = 0 int r? live: α34 α7 
>   α35 = α7 > α34 bool r0 live: α35 
>   α36 = return α35 void r?

The last part is basically equal.
The only difference is the registers and it indeed seems like a register allocation issue.

I did a diff between the `io.write` version and the one without: ``` $ q ssa tests/a.q -func foo > foo1.txt $ q ssa tests/a.q -func foo > foo2.txt $ diff foo1.txt foo2.txt 2c2 < α0 = args[0] *uint8 r0 id: first.ptr live: α1 α0 --- > α0 = args[0] *uint8 r9 id: first.ptr live: α1 α0 4c4 < α2 = 0 int r2 id: count live: α2 α1 α0 --- > α2 = 0 int r0 id: count live: α2 α1 α0 10c10 < α7 = phi(α2, α28) int r9 id: count live: α7 α6 α1 α0 --- > α7 = phi(α2, α28) int r3 id: count live: α7 α6 α1 α0 31c31 < α25 = α7 - α24 int r7 id: count live: α28 α25 α7 α6 α1 α0 --- > α25 = α7 - α24 int r2 id: count live: α28 α25 α7 α6 α1 α0 35c35 < α28 = phi(α7, α25) int r2 id: count live: α28 α7 α6 α1 α0 --- > α28 = phi(α7, α25) int r0 id: count live: α28 α7 α6 α1 α0 41,43c41,44 < α33 = 0 int r? live: α33 α7 < α34 = α7 > α33 bool r0 live: α34 < α35 = return α34 void r? --- > α33 = io.write[int64](α7) void r? live: α7 > α34 = 0 int r? live: α34 α7 > α35 = α7 > α34 bool r0 live: α35 > α36 = return α35 void r? ``` The last part is basically equal. The only difference is the registers and it indeed seems like a register allocation issue.
Owner

@Furkan wrote in #41 (comment):

I think this is caused by the same issue

This was caused by the logical and / logical or operators and it should be fixed in e784e89e0d.

@Furkan wrote in https://git.urbach.dev/cli/q/issues/41#issuecomment-416: > I think this is caused by the same issue This was caused by the logical and / logical or operators and it should be fixed in https://git.urbach.dev/cli/q/commit/e784e89e0d7582d47d0a3afbab8009d3a7eca138.
Owner

Closing.
Feel free to create a new issue if you still see problems regarding return -1 > 0.

Closing. Feel free to create a new issue if you still see problems regarding `return -1 > 0`.
ed closed this issue 2026-06-17 16:50:45 +00:00
ed added spent time 2026-06-17 16:51:59 +00:00
2 hours
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 3 hours
ed
3 hours
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#41
No description provided.