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: correct inconsistent runtime typing for logicalVectorEvaluator #5160

Merged
merged 2 commits into from
Sep 1, 2022

Conversation

onelson
Copy link
Contributor

@onelson onelson commented Sep 1, 2022

Fixes #5155

...which shows a runtime error when nesting a logical expression inside a conditional expression.

When in the test position, we'd get:

cannot use test of type vector in conditional expression; expected boolean

Originally, logicalVectorEvaluator advertised a runtime type of bool, which is inconsistent with reality since it is actually a vector of bool.

The error message we saw in this situation stemmed from the fact we choose either the vectorized or row-based conditional evaluator with this check here:

flux/compiler/compiler.go

Lines 657 to 669 in df2b4c4

if test.Type().Nature() == semantic.Vector && c.Type().Nature() == semantic.Vector && a.Type().Nature() == semantic.Vector {
return &conditionalVectorEvaluator{
test: test,
consequent: c,
alternate: a,
}, nil
}
return &conditionalEvaluator{
test: test,
consequent: c,
alternate: a,
}, nil

Since test in this case was a logicalVectorEvaluator with incorrect (bool) typing, this check failed and we fall back to the non-vectorized version for conditional.
This is why the error message states "expected boolean" -- we were calling the non-vectorized version with a vector as input.

This diff updates the impl for logicalVectorEvaluator.Type() such that it reports a vector of bool instead of bool.

Checklist

Dear Author 👋, the following checks should be completed (or explicitly dismissed) before merging.

  • ✏️ Write a PR description, regardless of triviality, to include the value of this PR
  • 🔗 Reference related issues
  • 🏃 Test cases are included to exercise the new code
  • 🧪 If new packages are being introduced to stdlib, link to Working Group discussion notes and ensure it lands under experimental/ N/A
  • 📖 If language features are changing, ensure docs/Spec.md has been updated N/A

Dear Reviewer(s) 👋, you are responsible (among others) for ensuring the completeness and quality of the above before approval.

@onelson onelson force-pushed the fix/vec-conditionals-with-logical-test-bug branch from f84f478 to 55573a8 Compare September 1, 2022 18:09
Comment on lines +478 to +480
(r) => {
return {x: (if r:{#O with a: v[bool], b: v[bool], x: v[#K], y: v[#K]}.a:v[bool] and:bool r:{#O with a: v[bool], b: v[bool], x: v[#K], y: v[#K]}.b:v[bool] then r:{#O with a: v[bool], b: v[bool], x: v[#K], y: v[#K]}.x:v[#K] else r:{#O with a: v[bool], b: v[bool], x: v[#K], y: v[#K]}.y:v[#K]):v[#G]}:{x: v[#G]}
}:(r: {#O with a: v[bool], b: v[bool], x: v[#K], y: v[#K]}) => {x: v[#G]}"##]].assert_eq(&crate::semantic::formatter::format_node(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The typing for the logical and conditional exprs here on the rust side look good. The runtime doesn't appear to rely on this info in this case, it seems.

Comment on lines +250 to +254
// Using a logical expr in the test of the conditional expr produced a
// runtime error:
// ```
// cannot use test of type vector in conditional expression; expected boolean
// ```
Copy link
Contributor Author

@onelson onelson Sep 1, 2022

Choose a reason for hiding this comment

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

In theory, I think we might have seen this for any nested expression where logical ops were children of a larger expression. I'm trying to think of why we didn't see this in the test suite earlier...

Owen Nelson added 2 commits September 1, 2022 11:37
When a logical op appears in the `test` position for a conditional,
we see:

```
cannot use test of type vector in conditional expression; expected boolean
```

The runtime typing for `logicalVectorEvaluator` was bool
rather than a vector of bool which would cause the conditional evaluator
to incorrectly select the non-vectorized implementation (it expects
test, consequent and alternate to all be vectors in order to use the
vectorized version).

By updating the `Type()` method for `logicalVectorEvaluator` to be a
vector of bool, the `conditionalVectorEvaluator` is able to be used.
@onelson onelson force-pushed the fix/vec-conditionals-with-logical-test-bug branch from 55573a8 to 2e4f4ec Compare September 1, 2022 18:37
@onelson onelson changed the title fix: vectorized conditionals with logical expression in test fix: correct inconsistent runtime typing for logicalVectorEvaluator Sep 1, 2022
@onelson onelson marked this pull request as ready for review September 1, 2022 18:55
@onelson onelson requested a review from a team as a code owner September 1, 2022 18:55
@onelson onelson requested review from nathanielc and removed request for a team September 1, 2022 18:55
@onelson onelson merged commit 8e8e19b into master Sep 1, 2022
@onelson onelson deleted the fix/vec-conditionals-with-logical-test-bug branch September 1, 2022 19:09
onelson pushed a commit that referenced this pull request Sep 8, 2022
This is mostly just to check for regressions (the fix was already made
in #5160).

Prior to the fix in #5160, this test would fail with:

```
runtime error: cannot use operand of type vector with logical or; expected boolean
```

Today, the test passes and all is well.
@onelson onelson mentioned this pull request Sep 8, 2022
5 tasks
onelson pushed a commit that referenced this pull request Sep 8, 2022
* test: vectorized nested logical ops

This is mostly just to check for regressions (the fix was already made
in #5160).

Prior to the fix in #5160, this test would fail with:

```
runtime error: cannot use operand of type vector with logical or; expected boolean
```

Today, the test passes and all is well.

* chore: make generate
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.

v 0.181.0 compound logical expressions in map failing
2 participants