google / google/xls

`const` expression in `impl` really is a constexpr

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

Description

**Describe the bug**

The compiler incorrectly says that an expression is not constexpr.

```
pub struct FixedPoint {
data: uN[NUM_BITS], // concatenation of integer and fraction bits
}

impl FixedPoint {
const BINARY_EXPONENT : s32 = if EXPONENT_IS_NEGATIVE>u32:0 { -BINARY_UEXPONENT as s32 } else { BINARY_UEXPONENT as s32 };
}

#[test]
fn test_impl() {
let a = FixedPoint { data: u8:0 };
assert_eq(FixedPoint::BINARY_EXPONENT, s32:-4);
}
```

Now you have to **test** (not build! there's no problem when I bazel build the dslx library):

```
Error: INVALID_ARGUMENT: Expression was not constexpr: `if EXPONENT_IS_NEGATIVE > u32:0 {
-BINARY_UEXPONENT as s32
} else {
BINARY_UEXPONENT as s32
}`
```

**To Reproduce**

Copy the code above and test it (again, no problem when building, which is (arguably) a separate bug).

**Expected behavior**

It to compile and test without error.

**Other**

The fix might be as simple as lifting the restriction that all parametric types for structs are `u32`.

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.