dotnet / dotnet/runtime

JIT: (bug) constant-folds typeof(void).IsPrimitive to true

Open
#133,964 1 comment 1 reaction 2 assignees Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

The importer folds `Type.IsPrimitive` for a known `System.Void` type handle to `true`, disagreeing with reflection, which reports `false` for `typeof(void).IsPrimitive`.

### Minimal Repro

```csharp
using System;
using System.Runtime.CompilerServices;

class Program
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static bool Folded() => typeof(void).IsPrimitive;

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
static bool Runtime(Type t) => t.IsPrimitive;

static void Main()
{
Console.WriteLine("folded: " + Folded());
Console.WriteLine("runtime: " + Runtime(typeof(void)));
}
}
```

### Expected

```text
folded: False
runtime: False
```

### Actual

```text
folded: True
runtime: False
```

### Notes

`Compiler::impIntrinsic` handles `NI_System_Type_get_IsPrimitive` by checking `getTypeForPrimitiveValueClass`, which returns a primitive type value for `System.Void`.
The fold should explicitly reject `CORINFO_TYPE_VOID` so it matches `System.Type.IsPrimitive`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.