Skip to content

Simple modulo into switch expression throws division by zero exception #134725

Description

@cristhiangrundmann

Description

int x = 61 % 12 switch
{
	0 => 0,
	2 => 1,
	4 => 2,
	5 => 3,
	7 => 4,
	9 => 5,
	11 => 6,
	_ => 0,
};

This code is throwing a nonsense exception: System.DivideByZeroException: Attempted to divide by zero.

Project file is

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

No extra obscure compilation options. The problem persists on net10.

Reproduction Steps

Just run the statement in the main function on a console app.

Expected behavior

Expected behavior is x = 0, coming from the default case.

Actual behavior

Throws System.DivideByZeroException.

Regression?

No response

Known Workarounds

No response

Configuration

Ran on windows 11.

Other information

Possibly some optimization/compilation bug.

Activity

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions