Skip to content

Switch case after or pattern does not match #55668

@WiebeCnossen

Description

@WiebeCnossen

Version Used: 5.0.302

Steps to Reproduce:

Given this method:

    private static string GetTextBuggy(Position? position, Wrap wrap) {
      return position switch {
        not Position.First when wrap.Sub is Zero => "Not First and Zero",

        null => "Null",

        _ when wrap is {
          Sub: One or Two
        } => "One or Two",

        Position.Last => "Last",

        Position.First => "First",

        _ => "Other"
      };
    }

This is called with argument values Position.First and Wrap { Sub = new Zero() }.

Expected Behavior: The return value is "First"

Actual Behavior: The return value is "Other".

Remarks: The code works as expected when the or pattern is split into to cases:

    private static string GetTextWorkaround(Position? position, Wrap wrap) {
      return position switch {
        not Position.First when wrap.Sub is Zero => "Not First and Zero",

        null => "Null",

        _ when wrap is {
          Sub: One
        } => "Just one",

        _ when wrap is {
          Sub: Two
        } => "Just two",

        Position.Last => "Last",

        Position.First => "First",

        _ => "Other"
      };
    }

Complete project: Switcher.zip

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Active/Investigating

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions