microsoft / microsoft/DirectXShaderCompiler

Overloading unary operator +- definition accepted but doesn't work

Open
#7,944 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug diagnostic hlsl2021
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description
Following simple compute shader fails to compile:

struct MyFloat {
    float Value;
    MyFloat operator-() {
        MyFloat Result;
        Result.Value = -Value;
        return Result;
    }
};
RWByteAddressBuffer Buf : register(u0);
[numthreads(1, 1, 1)]
void mainCS(uint3 dtid: SV_DispatchThreadID) {
    MyFloat f1;
    f1.Value = 1.0;
    MyFloat f2 = -f1;
    Buf.Store<float>(0, f2.Value);
}

Steps to Reproduce
Compile the shader with command line:

dxc.exe -T cs_6_0 -E mainCS -Fo MyCS.dxil MyCS.hlsl

Actual Behavior

MyCS.hlsl:14:18: error: scalar, vector, or matrix expected
    MyFloat f2 = -f1;
                 ^

It seems that overloading unary operators +MyVar, -MyVar doesn't work. I understand this, but I think in this case it would be better if the compiler reported an error while the operator is defined, not only when I try to actually use it.

Environment

  • DXC version: dxcompiler.dll: 1.8 - 1.8.2502.11 (239921522); dxil.dll: 1.8(1.8.2502.11), as bundled in Windows SDK 10.0.26100.0.
  • Host Operating System: Windows 11 x86-64 25H2 (OS Build 26200.7171)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure from MyCS.hlsl with the reported dxc.exe command and compare the accepted unary operator definition with the later use-site error. The issue is complete when the compiler reports the unsupported unary operator at its definition, with a clear diagnostic, or otherwise handles the construct consistently.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.