Skip to content

InvalidProgramException while expression compilation with C++/CLI field #118837

@thoenissen

Description

@thoenissen

Description

Since .NET 10.0.0-preview.7.25380.108 the compilation of the following expression fails with a InvalidProgramException.

Reproduction Steps

.NET 10.0.0-preview.7.25380.108 x86 Release - Console Application

System.Linq.Expressions.Expression<Func<CSharpClass>> expression = () => new CSharpClass();
expression.Compile();
public class CSharpClass
{
    private CppClass _field = new();
}

.NET 10.0.0-preview.7.25380.108 x86 Release - C++/CLI Library

public ref class CppBase abstract
{
public:
   ~CppBase() {}
   !CppBase() { _pValue = nullptr; }
protected:
   void* _pValue;
};

public ref class CppClass : public CppBase
{
public:
   CppClass() { _pValue = new int(); }
};

Expected behavior

The Compile method returns the compiled expression.

Actual behavior

The Compile method throws a InvalidProgramException.

System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.Reflection.Emit.DynamicResolver.ResolveToken(Int32 token, IntPtr& typeHandle, IntPtr& methodHandle, IntPtr& fieldHandle)
   at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
   at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.Expression`1.Compile()
   at Program.<Main>$(String[] args)

Regression?

The exception occurs since .NET 10.0.0-preview.7.25380.108.

Known Workarounds

When disabling the inlining of the constructor the error does not occur.

public class CSharpClass
{
    private CppClass _field;

    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
    public CSharpClass()
    {
        _field = new();
    }
}

Configuration

.NET 10.0.0-preview.7.25380.108
Windows 11 24h2 (OS Build 26120.5742)
Architecture x86

Other information

No response

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions