Skip to content

type inference in optimized netcore2.1 code differs from all other targets #2070

Closed
@gpomykala

Description

@gpomykala

Platform: Windows / Linux

runtime: dotnet 2.1.403

code optimization enabled

The code:

    [Test]
    public void BugRepro()
    {
      byte[] valueBytes = new byte[] { 0x7f, 0xff };

      byte firstByte = valueBytes[0];
      byte secondByte = valueBytes[1];

      // (firstByte << 8) should be inferred as int as in other targets and as without optimization
      ushort guilty = (ushort)((firstByte << 8) | secondByte);

      guilty.Should().Be(32767);
    }

Actual outcome:
guilty == 255

Most likely (firstByte << 8) is inferred as byte with compile time optimization enabled, and guilty is evalueated as (0 | 0xff)

It works as expected without code optimization in netcore2.1. All other targets: net462, mono, netcore2.0 produce expected result

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions