Skip to content

Error in implicit conversions when enums are involved #5807

Open

Description

Description
DXC seems to be having some issues when there are bit shift operations with enums. Other bit operators seem to work fine but not bit shifts. While I haven't checked the C++ spec I believe HLSL mirrors C++'s behavior. Despite that this error is not something I've seen or was able to reproduce in any of the C++ compilers that I've tested. This leads me to believe that there might be an issue with DXC.

Steps to Reproduce
Pretty simple repro case:

enum E : uint {
    A,
    B
};

float4 PSMain() : SV_Target0 {
    uint e = E::A << 1u;    
    return 0.0;
}

And the error is:

error: cannot convert from 'unsigned int' to 'E'

Replacing << with | for example works fine.

Godbolt link: https://godbolt.org/z/nKThYcs1z

Actual Behavior
Pretty sure that this type of operations shouldn't throw an error.

Environment

  • DXC version: Trunk (6b4b0eb)
  • Host Operating System: Linux

Activity

  1. added
    hlsl-next
    on Oct 3, 2023
  2. llvm-beanz commented on Oct 3, 2023

    @llvm-beanz
    Collaborator

    To add a quick note. The error message here doesn't indicate it, but this is caused by HLSL's overload resolution changes. In C++ the implicit conversion sequence for an explicitly-typed enumeration that is not an enum class is the same as a non-typed enumeration (i.e. it can implicitly convert to int). In SemaHLSL.cpp under CanConvert we don't properly handle explicitly-typed non-class enumerations.

    While this is a bug that we could fix in HLSL without a language change, I've also tagged this as hlsl-next because we do have plans for HLSL 202x to adopt C++ overload resolution rules in order to support global operator overloading and const instance methods. That would fix this in the next language version.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughlsl-next

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions