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

fix: return error if branch not found in child order #364

Merged
merged 11 commits into from
Oct 23, 2024

Conversation

crodriguezvega
Copy link

No description provided.

go/proof.go Outdated
}
}
panic(fmt.Errorf("branch %d not found in order %v", branch, order))

return 0, fmt.Errorf("branch %d not found in order %v", branch, order)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically the change needed. The rest of changes are for bubbling the error up.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively I could have just swallowed the error in the call sites of getPosition and return in those places the default value (false, and 0s). I thought of bubbling the error up in case we ever do #29.

go/proof.go Outdated
if branch < 0 || int(branch) >= len(order) {
panic(fmt.Errorf("invalid branch: %d", branch))
return 0, fmt.Errorf("invalid branch: %d", branch)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also converted this one into a regular error instead of panicking.

idx, err := orderFromPadding(spec, op)
if err != nil {
return false
return false, err
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here an error that previously was swallowed can now be returned.

go/proof.go Outdated
idx, err := orderFromPadding(spec, op)
if err != nil {
return false
return false, nil
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one that previously was swallowed.

@colin-axner colin-axner self-assigned this Sep 26, 2024
@colin-axner colin-axner marked this pull request as draft September 26, 2024 08:34
go/proof.go Outdated
Comment on lines 303 to 309
leftBranchesAreEmpty, err := leftBranchesAreEmpty(spec, step)
if err != nil {
return false, err
}

if !hasPadding(step, minPrefix, maxPrefix, suffix) && !leftBranchesAreEmpty {
return false, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: did this short circuit before if hasPadding returned true? (ie would that have an effect if we now call left branches first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to change the code such that it would execute with the exact same code trace as before:
@crodriguezvega changed it to:

leftBranchesAreEmpty, err := leftBranchesAreEmpty(spec, step)
if err != nil {
	return false, err
}

if !hasPadding(step, minPrefix, maxPrefix, suffix) && !leftBranchesAreEmpty {
	return false, nil
}

I changed it to:

if !hasPadding(step, minPrefix, maxPrefix, suffix) {
        leftBranchesAreEmpty, err := leftBranchesAreEmpty(spec, step)
        if err != nil {
            return false, err 
        }   

        if !leftBranchesAreEmpty {
            return false, nil 
        }   
}

Because golang has short circuiting, !hasPadding would have previously resulted in rightBranchesAreEmpty() not being called, but in the code @crodriguezvega refactored, it would have been called on every execution. I tried to look at the correctness of such change, if rightBranchAreEmpty would function regardless of the determined padding, but it was too exhausting of a mental exercise to justify the minor differences in readability, so I opt'd to leave execution as it was before

cc @AdityaSripal

Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: add test case to cover change from panic to error in getPosition

@colin-axner colin-axner marked this pull request as ready for review September 26, 2024 14:37
@colin-axner colin-axner removed their assignment Sep 26, 2024
Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed again myself

@colin-axner colin-axner merged commit a6b11f1 into master Oct 23, 2024
6 checks passed
@colin-axner colin-axner deleted the carlos/return-error-getPosition branch October 23, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants