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

Early exit from function with specific for loop construct using MinHeap #14042

Open
ChewingBever opened this issue Apr 14, 2022 · 1 comment
Open
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

@ChewingBever
Copy link

V version: V 0.2.4 67e33bf.1be28c5
OS: EndeavourOS Linux

What did you do?

import datatypes { MinHeap }

struct Test {
mut:
    queue MinHeap<string>
    v int
}

fn (mut t Test) buggy() ? {
    for t.queue.len() > 0 && t.queue.peek() ? != '' {
        continue
    }

    t.v = 1
}

fn main() {
    mut t := Test{}

    t.buggy() or {
        eprintln('Error')
        exit(1)
    }

    println(t)
}

I wrote this for loop construct where I both check for the length of the queue & inspect the value of the peek() function.

What did you expect to see?
The for loop should either return an error or properly set t.v = 1. In this specific case, the loop would have to loop zero times.

What did you see instead?
The function exits at the point of the for loop, without returning an error. The result is that the final println call outputs

Test{
    queue: datatypes.MinHeap<string>{
        data: []
    }
    v: 0
}

showing that the function didn't return an error, yet did not execute the final line which sets the value of v.t.
If we replace the for loop with

    for t.queue.len() > 0 {
        if t.queue.peek() ? != '' {
            continue
        }else{
            break
        }
    }

the function does execute as expected, and the value of t.v is set to 1.

@ChewingBever ChewingBever added the Bug This tag is applied to issues which reports bugs. label Apr 14, 2022
@vincenzopalazzo
Copy link
Contributor

Yeah looks like the the for loop works badly with the valuation of the && condition.

This is another example

import datatypes { BSTree }

struct Test {
mut:
    tree BSTree<string>
    v int
}

fn (mut t Test) buggy() ? {
	assert t.tree.is_empty() == true
	for !t.tree.is_empty() && t.tree.min() ? != '' {
		continue
	}

    t.v = 1
}

fn main() {
    mut t := Test{}

    t.buggy() or {
        eprintln('Error')
        exit(1)
    }

    println(t)
}

The following condition !t.tree.is_empty() && t.tree.min() ? != '' should exit when is_empty() is false without evaluating the other side of the end.

Good catch!

@vincenzopalazzo vincenzopalazzo added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Apr 17, 2022
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

No branches or pull requests

2 participants