Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid IL generated for intercepted callsite #70841

Closed
jkotas opened this issue Nov 15, 2023 · 2 comments · Fixed by #71655
Closed

Invalid IL generated for intercepted callsite #70841

jkotas opened this issue Nov 15, 2023 · 2 comments · Fixed by #71655

Comments

@jkotas
Copy link
Member

jkotas commented Nov 15, 2023

Version Used:

.NET SDK 8.0.100

Steps to Reproduce:

  1. dotnet new console
  2. Copy & paste attached Program.cs (adjust full path to match your system)
  3. Add <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);MyInterceptors</InterceptorsPreviewNamespaces to the .csproj file
  4. dotnet run

Expected Behavior:

Program runs to completion

Actual Behavior:

InvalidProgramException.

The problem is that Roslyn generates invalid IL

    .locals init (valuetype MyEnum V_0)
    IL_0000:  ldc.i4.1
    IL_0001:  stloc.0
    IL_0002:  ldloc.0 <- MyEnum is not assignable to Enum in IL!
    IL_0003:  call       string MyInterceptors.Interceptors::OtherToString(class [System.Runtime]System.Enum)

Program.cs

using System.Runtime.CompilerServices;

var value = MyEnum.Second;
Console.WriteLine(value.ToString());

public enum MyEnum
{
    First,
    Second,
}

namespace MyInterceptors
{
    public static class Interceptors
    {
        [InterceptsLocation(@"C:\repro\Program.cs", line: 4, column: 25)]
        public static string OtherToString(this System.Enum value)
            => "Wrong Value" + value;
    }
}

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    sealed class InterceptsLocationAttribute : Attribute
    {
        public InterceptsLocationAttribute(string filePath, int line, int column)
        {
        }
    }
}
@jkotas
Copy link
Member Author

jkotas commented Nov 15, 2023

Originally reported by @andrewlock in dotnet/runtime#57748 (comment):

@jaredpar jaredpar added this to the 17.9 milestone Nov 16, 2023
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 16, 2023
@jaredpar
Copy link
Member

@RikkiGibson PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants