google / google/xls

DSLX type checker should not error on width slice of 0-bit value

Open
#1,969 0 comments 0 reactions 1 assignee Claimed by @richmckeever View on GitHub
bug dslx 🧦 sox
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

**Describe the bug**
The following code does not compile, even though we'd expect to "skip the loop" for `N == 0`.

```
fn foo(x: uN[N]) {
for (idx, _): (u32, ()) in range(u32:0, N) {
trace_fmt!("{}", x[idx+:u1]);
}(())
}

fn main() { foo(uN[0]:0); }
```

```
0020: trace_fmt!("{}", x[idx+:u1]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^-^ XlsTypeError: uN[0] vs uN[1]: Slice type must have <= original number of bits; attempted slice from 0 to 1 bits.
```

You cannot get around this with `x[idx:idx + 1]` or special casing `if N == u32:0 ...` inside the function (inside or outside the loop).

Note that the array version does work (though runs into IR conversion issues - https://github.com/google/xls/issues/917).

```
fn foo(x: u1[N]) {
for (idx, _): (u32, ()) in range(u32:0, N) {
trace_fmt!("{}", x[idx]);
}(())
}

fn main() { foo(u1[0]:[]); }
```

**To Reproduce**
Compile the code above.

**Expected behavior**
There should be a way for this to type check since other parts of XLS allow the 0-bit type.

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.