shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

`Optional` type doesn't work in generic function. #4343

Closed csyonghe closed 3 weeks ago

csyonghe commented 3 weeks ago

The lowering logic of optional types is not robust against generics. The following test case reproduces a crash:

//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUF):-slang -compute -vk

interface IFoo
{
    void foo();
}

struct S : IFoo { int x; void foo(); }

struct P
{
    IFoo f;
}
struct Tr
{
    int test<T:IArithmetic>(T t, inout P p)
    {
        const IFoo hit = p.f;
        let castResult = hit as S;
        if (!castResult.hasValue)
            return 0;
        return castResult.value.x;
    }
}

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name buffer

RWStructuredBuffer<int> buffer;

[numthreads(1,1,1)]
void computeMain()
{
    P p;
    S s;
    s.x = 2;
    p.f = s;
    Tr tt;
    // BUF: 2
    buffer[0] = tt.test(0, p);
}