Skip to content

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

Closed

Description

@MichalStrehovsky

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.

Activity

  1. ghost added
    untriaged
    on Apr 21, 2023
  2. added this to the 8.0.0 milestone on Apr 21, 2023
  3. ghost removed
    untriaged
    on Apr 21, 2023
  4. Fabi commented on Apr 21, 2023

    @Fabi

    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

  5. MichalStrehovsky commented on Apr 21, 2023

    @MichalStrehovsky
    MemberAuthor

    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.

  6. Fabi commented on Apr 21, 2023

    @Fabi

    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.

  7. modified the milestones: 8.0.0, 9.0.0 on Jul 11, 2023
  8. added a commit that references this issue on Sep 8, 2023
    b34faae
  9. ghost added
    in-pr
    on Sep 8, 2023
  10. added a commit that references this issue on Sep 13, 2023
    ba10d55
  11. ghost removed
    in-pr
    on Sep 13, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions