google / google/xls

constexpr rollover error needs more context information printed

Open
#2,355 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

**Describe the bug**
A contexpr-calculated template parameter based on another parameter produces a rollover in the calculation, but the involved numbers are so small that the error printout warrants more information to be printed. Also, given the numbers, it should actually not happen.

**To Reproduce**

Consider the following file

```rust
import std;

fn foo() -> u32 { // B should evaluate to u32:8
trace_fmt!("A={}, B={}", A, B);
B
}

#[test]
fn foo_test() {
assert_eq(u32:8, foo());
}
```

Run with

```
interpreter_main foo.x
```

Observe error, which actually reports an overflow in `std::upow()` which is used in `std::next_pow2()`

```
xls/dslx/stdlib/std.x:825:18-825:23
0823: let result = if (n & uN[N]:1) == uN[N]:1 { result * p } else { result };
0824:
0825: (n >> 1, p * p, result)
~~~~~~~~~~~~~~~~~~~~~~~^---^ constexpr evaluation detected rollover in operation
0826: }((n, p, result));
0827: work.2
```

**Expected behavior**

If there is an overflow/rollover, it would be good to have the values and types involved printed. Since this is a `constexpr` evaluation, all these should be available.

Besides: `std::next_pow2()` doesn't look like it should overflow with `u32:6` as parameter.

In fact, if we calculate it manually when instantiating: it works

```rust
#[test]
fn foo_test() {
const B = std::next_pow2(u32:6); // calculate as const expr
assert_eq(u32:8, foo()); // and use instead of the {}-calculated parameteer
}
```

So there is probably something else broken.

**Environment (this can be helpful for troubleshooting):**
Version: `v0.0.0-7829-gc605fae2d`

**Additional context**
While this bug report laments the diagnostics printed, we should not encounter this in the first place.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.