Skip to content

Mono incorrectly handles conversion of negative doubles to nint #64386

@tannergooding

Description

@tannergooding

#64234 appears to surface a bug in Mono where it incorrectly handles double->nint but not float->nint conversions.

The test is in the form:

[Theory]
[InlineData(-4567.0f)]
public void NFloatToIntPtr(float value)
{
    nint result = (nint)new NFloat(value);
    Assert.Equal((nint)value, result);
}

Where NFloat is roughly defined as:

public readonly struct NFloat
{
    private readonly double _value;

    public NFloat(double value)
    {
        _value = value;
    }

    public static explicit operator nint(NFloat value) => (nint)(value._value);
}

This resulted in https://dev.azure.com/dnceng/public/_build/results?buildId=1574581&view=ms.vss-test-web.build-test-results-tab&runId=44208602&paneView=dotnet-dnceng.dnceng-build-release-tasks.helix-test-information-tab&resultId=164309

Where the exact failure is:

    System.Runtime.InteropServices.Tests.NFloatTests.NFloatToIntPtr(value: -4567) [FAIL]
      Assert.Equal() Failure
      Expected: -4567
      Actual:   4294962729
      Stack Trace:
        /_/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.cs(483,0): at System.Runtime.InteropServices.Tests.NFloatTests.NFloatToIntPtr(Single value)
        /_/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs(386,0): at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters)
    System.Runtime.InteropServices.Tests.NFloatTests.NFloatToIntPtr(value: -4567.89111) [FAIL]
      Assert.Equal() Failure
      Expected: -4567
      Actual:   4294962729
      Stack Trace:
        /_/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.cs(483,0): at System.Runtime.InteropServices.Tests.NFloatTests.NFloatToIntPtr(Single value)
        /_/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs(386,0): at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions