question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Operator precedence of `<=` misleading when using `if-then`

See original GitHub issue

In Python <= has higher precedence than not, or, and, if-then, and lambda. In cocotb <= is reused as an assignment operator, which leads to some highly surprising behavior when compared to the “normal” assignment operator =.

Example Expected Actual
1 a <= b or c a <= (b or c) (a <= b) or c Edit: Mitigation implemented, see below
2 a <= b and c a <= (b and c) (a <= b) and c Edit: Mitigation implemented, see below
3 a <= b if c else d a <= (b if c else d) (a <= b) if c else d

I recently had my third encounter with this behavior. Although I had seen it twice before, it still took me some time to realize it was an operator precedence issue thanks to the pervasiveness of “normal” assignments.

Partial solution

Edit: this has already been implemented. With a recent version of cocotb only `if-then` is still an issue. Click to expand original text.

To prevent (1) and (2) <= could return a singleton DontUse like:

class DontUse:
    def __bool__(self):
        raise ValueError("Don't use me like this dummy")

DontUse = DontUse()

This would make Python throw the error when trying to evaluate or and and. There’s no such workaround available for if-then AFAIK.

Full solution

I don’t think there’s much we can do. I think using <= should be deprecated and eventually removed entirely. There’s very little benefit, but it can cause some pretty annoying debug sessions. Assignment on simulator objects should probably replace <='s current function.

Old New
dut.a <= b if c else d dut.a = b if c else d
a <= b if c else d a.value = b if c else d

The good news is that there’s a good upgrade path:

  1. Deprecation warning on <=
  2. Hard error in __le__
  3. Removal of __le__ (?)

Workarounds

  1. Use parentheses: a <= (b if c else d)
  2. Use .value: a.value = b if c else d

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

0reactions
martijnbastiaancommented, Aug 10, 2021

Works for me. It’s non-surprising, performant, and solves this issue. It’s slightly annoying that it makes testbenches more verbose, but I 💯% prefer that over the status quo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IfThen, If - Oracle Help Center
IfThen is a conditional function that returns a value when the condition equals True, and another value when the condition equals False. ......
Read more >
Using IF with AND, OR and NOT functions - Microsoft Support
How to use the IF function (combined with the AND, OR, and NOT functions) in Excel to make logical comparisons between given values....
Read more >
Conditional - IF...THEN Statements - FORTH, Inc.
Forth comparisons are true or false, so a comparison operator precedes IF... Here is a partial list of comparison operators that you can...
Read more >
Lesson 5: If-Then-Else Statements - STAT ONLINE
In this lesson, we will learn how to use if-then-else statements to add some information to some but not all of the observations...
Read more >
Operators - isee systems
The higher precedence of the comparison operators means that this can be written with no parentheses. It evaluates to 1 for true and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found