Skip to content

C# compiler casting bug (erroneous CS0675) #42816

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


Is this really the expected and documented compiler behaviour, that casting an integral to uint is in reality producing something different, that to really get an uint you need to cast that to uint? Issue title was selected from the expectation not even the C# language specification can be that messed up, and this is therefore a compiler bug.

class C
{
  static uint fn(sbyte a, sbyte b)
  {
    uint ret;
    ret = (uint)(a << 8) | (uint)b; // CS0675
    ret = (uint)(uint)(a << 8) | (uint)b; // CS0675
    ret = (uint)(a << 8) | (uint) (uint)b;
    ret = (uint)(uint)(a << 8) | (uint) (uint)b;
    return ret;
  }
}

that is, the only way to silence the compiler warning is to twice cast the second operand of the binary operation to the target type!

The type of the statement (uint)b shall be uint, only uint and nothing but uint. It can be nothing else, the explicit cast tells the compiler it is so. Yet the compiler seemingly thinks it's not (without telling us what it has really converted that into).


Original Comments

Visual Studio Feedback System on 3/22/2020, 06:46 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-ExternalResolution-ExternalThe behavior lies outside the functionality covered by this repository

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions