Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid code generated for nullable conversion to IntPtr? #42834

Open
cston opened this issue Mar 27, 2020 · 3 comments
Open

Invalid code generated for nullable conversion to IntPtr? #42834

cston opened this issue Mar 27, 2020 · 3 comments

Comments

@cston
Copy link
Member

cston commented Mar 27, 2020

using System;

class Program
{
    static System.IntPtr Convert1(double? value)
    {
        return (System.IntPtr)value;
    }

    static System.IntPtr? Convert2(double? value)
    {
        return (System.IntPtr?)value;
    }

    static void Main()
    {
        double? d = 42;
        Console.WriteLine(Convert1(d));
        Console.WriteLine(Convert2(d));
    }
}

Result:

> c.exe
42
11529392

It looks like the IL generated for Convert2 is missing a conv.i8 when compared to Convert1.

Note: this issue is referenced in test code

@cston
Copy link
Member Author

cston commented Mar 27, 2020

Similar issue with decimal, although in that case the generated code for Convert2 is missing a call to decimal.op_Explicit(decimal):

class Program
{
    static System.UIntPtr Convert1(decimal? value)
    {
        return (System.UIntPtr)value;
    }

    static System.UIntPtr? Convert2(decimal? value)
    {
        return (System.UIntPtr?)value;
    }
}

@cston
Copy link
Member Author

cston commented Mar 27, 2020

Another example where the conversion is dropped. And in this case, an assertion fails in LocalRewriter.MakeLiftedUserDefinedConversionConsequence() as well.

class Program
{
    static char? Convert(System.IntPtr? value)
    {
        return (char?)value;
    }
}

@cston cston added the untriaged Issues and PRs which have not yet been triaged by a lead label Feb 26, 2022
@jcouv
Copy link
Member

jcouv commented Mar 1, 2022

When trying on (sharplab), I'm getting an exception from Convert2:

42
System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at Program.Convert2(Nullable`1 value)
   at Program.Main()

@jcouv jcouv added this to the Compiler.Next milestone Mar 2, 2022
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 2, 2022
@jcouv jcouv added this to Active/Investigating in Compiler: Julien's umbrellas May 16, 2022
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Compiler: Julien's umbrellas
Active/Investigating
Development

No branches or pull requests

3 participants