Skip to content

template parameters with same value binding in different functions can loose constexpr status. #4895

Description

@hzeller

Yeah, bug description is confusing. Here a reproducible example:

Describe the bug

Consider the following code. There are two functions with a template parameter P. They are called in the test with the same parameter value:

struct Foo<U:u32> {}
impl Foo<U> {
    fn get_u(self) -> u32 { U }
}

fn fun_1<P:u32>() -> u32 {
    let x = Foo<P>{};
    x.get_u()
}

fn fun_2<P:u32>() -> u32 {
    let x = Foo<P>{};
    x.get_u()
}

#[test]
fn does_not_work_test() {
    assert_eq(fun_1<u32:8>(), 8);
    assert_eq(fun_2<u32:8>(), 8);  // Same P = 8 binding.
}

This results in a strange error:

$ bazel-bin/xls/dslx/interpreter_main non-const.x 
non-const.x:15:6-15:12
0013: fn fun_2<P:u32>() -> u32 {
0014:     let x = Foo<P>{};
0015:     x.get_u()
~~~~~~~~~~~^----^ NotConstantError: non-const.x:9:17-9:18 expr `P` is not constexpr.
0016: }
0017: 

P is a template parameter and as such should be considered constexpr.

This goes away if fun_2() is called with a different binding for P

so if we would, say 8 for the first and 9 for the second invocation, this works:

#[test]
fn works_test() {
    assert_eq(fun_1<u32:8>(), 8);
    assert_eq(fun_2<u32:9>(), 9);
}

Environment (this can be helpful for troubleshooting):
XLS Compiled from head.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions