google / google/xls

parametric arguments that are not used in fields are forgotten before they're needed

Open
#1,848 2 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**

```
pub struct SomeType { data: uN[FIRST] }

pub fn dostuff
(a: SomeType) -> SomeType {
a
}

#[test]
fn test_dostuff() {
let a = SomeType { data: u4:0 };
let result = dostuff(a);
assert_eq(result.data, u4:0);
}
```
error:
```
0113: pub fn dostuff
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^----^ TypeInferenceError: Parametric expression `SECOND` referred to `SECOND` which is not present in the parametric environment;
```

**To Reproduce**
Copy above and build.

**Expected behavior**
I expect this to compile without trouble, and that `COMPUTED` is a parametric argument equal to `SECOND` (which I would then use later in the parametric section or body of the function).

**Additional context**
There appears to be too-early parametric type erasure. Any parametric types not used to define a field of a struct are erased before function parametric type inference occurs.

This problem appears to be caused by the fact that SECOND is not used to define a field in `struct SomeType`. This can be demonstrated two ways:

1. Change: `COMPUTED: u32 = {FIRST}`. This compiles (because FIRST is used by `data`). Now change `data: uN[SECOND]` and it fails to compile (because FIRST is no longer used by `data`.)
2. (don't do # 1 before doing:) Add another field to the struct, and define the field using `SECOND`, e.g. like `unused: uN[SECOND]`. Resolve the compile errors that result from this new field. Now it compiles.

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.