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

Managed type system casting logic doesn't handle all array variance #85146

Closed
MichalStrehovsky opened this issue Apr 21, 2023 · 4 comments · Fixed by #91788
Closed

Managed type system casting logic doesn't handle all array variance #85146

MichalStrehovsky opened this issue Apr 21, 2023 · 4 comments · Fixed by #91788

Comments

@MichalStrehovsky
Copy link
Member

Compile following with optimizations on:

class Program
{
    static void Main()
    {
        object source = new Enum0[] { Enum0.First, Enum0.Second, Enum0.Third };
        Console.WriteLine(source is IEnumerable<Enum1>);
    }

    enum Enum0 { First, Second, Third }
    enum Enum1 { }
}

Expected: prints true
Observed: prints false

This is because the CanCastTo logic within the managed type system doesn't think these are castable. RyuJIT hardcodes this to false in codegen.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 21, 2023
@ghost
Copy link

ghost commented Apr 21, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Compile following with optimizations on:

class Program
{
    static void Main()
    {
        object source = new Enum0[] { Enum0.First, Enum0.Second, Enum0.Third };
        Console.WriteLine(source is IEnumerable<Enum1>);
    }

    enum Enum0 { First, Second, Third }
    enum Enum1 { }
}

Expected: prints true
Observed: prints false

This is because the CanCastTo logic within the managed type system doesn't think these are castable. RyuJIT hardcodes this to false in codegen.

Author: MichalStrehovsky
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@MichalStrehovsky MichalStrehovsky added this to the 8.0.0 milestone Apr 21, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 21, 2023
@Fabi
Copy link

Fabi commented Apr 21, 2023

Just a thing to clarify: The is keyword actually returns true as soon as an implicit conversion is possible? I thought it only returns true on exact type matches :D

@MichalStrehovsky
Copy link
Member Author

It returns true if a cast can succeed.

You can do ((IEnumerable<Enum1>)source).GetType() and you'll get Enum0[]. There's no conversion involved, just a type check.

@Fabi
Copy link

Fabi commented Apr 21, 2023

It returns true if a cast can succeed.

You can do ((IEnumerable<Enum1>)source).GetType() and you'll get Enum0[]. There's no conversion involved, just a type check.

That's good to know, thank you. I always expected it to only return true on exact type matches for types you defined.

@MichalStrehovsky MichalStrehovsky modified the milestones: 8.0.0, 9.0.0 Jul 11, 2023
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this issue Sep 8, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Sep 8, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Sep 13, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants