Skip to content

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

@dank-openai

Description

@dank-openai

Describe the bug

pub struct SomeType<FIRST: u32, SECOND: u32> { data: uN[FIRST] }

pub fn dostuff<FIRST: u32, SECOND: u32, COMPUTED: u32 = {SECOND}>
    (a: SomeType<FIRST, SECOND>) -> SomeType<FIRST, SECOND> {
    a
}

#[test]
fn test_dostuff() {
    let a = SomeType<u32:4, u32:4> { data: u4:0 };
    let result = dostuff(a);
    assert_eq(result.data, u4:0);
}

error:

0113: pub fn dostuff<FIRST: u32, SECOND: u32, COMPUTED: u32 = {SECOND}>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^----^ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dslxDSLX (domain specific language) implementation / front-end

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions