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

const expression in impl really is a constexpr #1847

Open
dank-openai opened this issue Jan 13, 2025 · 0 comments
Open

const expression in impl really is a constexpr #1847

dank-openai opened this issue Jan 13, 2025 · 0 comments
Labels
dslx DSLX (domain specific language) implementation / front-end

Comments

@dank-openai
Copy link

dank-openai commented Jan 13, 2025

Describe the bug

The compiler incorrectly says that an expression is not constexpr.

pub struct FixedPoint<NUM_BITS: u32, EXPONENT_IS_NEGATIVE:u32, BINARY_UEXPONENT: u32> {
    data: uN[NUM_BITS],  // concatenation of integer and fraction bits
}

impl FixedPoint<NUM_BITS, EXPONENT_IS_NEGATIVE, BINARY_UEXPONENT> {
    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<u32:8, u32:1, u32: 4> { data: u8:0 };
    assert_eq(FixedPoint<u32:8, u32:1, u32: 4>::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 <stuff> 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.

@cdleary cdleary added the dslx DSLX (domain specific language) implementation / front-end label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dslx DSLX (domain specific language) implementation / front-end
Projects
Status: No status
Development

No branches or pull requests

2 participants