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

Interceptors do not report nullability warnings when type parameter constraints are not satisfied #70310

Open
TessenR opened this issue Oct 10, 2023 · 1 comment

Comments

@TessenR
Copy link

TessenR commented Oct 10, 2023

Version Used:

Microsoft Visual Studio Professional 2022
Version 17.8.0 Preview 2.0
VisualStudio.17.Preview/17.8.0-pre.2.0+34112.27
Microsoft .NET Framework
Version 4.8.09037

Steps to Reproduce:

Compile the following code:

#nullable enable
using System.Runtime.CompilerServices;

partial class C
{
  public string? Method1<T>(T arg) => null;
}

static class Program
{
  public static void Main()
  {
    var c = new C();
    string? x = null;

    c.Method1(x);
  }
}

partial class C
{
  [InterceptsLocation("Program.cs", 16, 7)]
  public string? Interceptor1<T>(T arg) where T: notnull => arg.ToString();
}

namespace System.Runtime.CompilerServices
{
  [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  public class InterceptsLocationAttribute : Attribute
  {
    public InterceptsLocationAttribute(string path, int line, int character) { }
  }
}

Expected Behavior:
A warning is reported for the intercepted call c.Method1(x) which is translated into C.Interceptor1<string?>(x); because the type argument doesn't satisfy the where T: notnull constraint on the interceptor method

Actual Behavior:
No warnings at all. The code crashes at runtime with a NullReferenceException

[jcouv update:] Relates to test plan #67421

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 10, 2023
@jcouv jcouv added the Bug label Oct 11, 2023
@jcouv jcouv added this to the 17.9 milestone Oct 11, 2023
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Oct 11, 2023
@RikkiGibson
Copy link
Contributor

The problem here is that the interceptor's type parameter constraints are checked during lowering. At that point, the nullable-reinferred version of the call has been discarded, and we are lowering the initially-bound version of the call.

if (!interceptor.CheckConstraints(new ConstraintsHelper.CheckConstraintsArgs(this._compilation, this._compilation.Conversions, includeNullability: true, attributeLocation, this._diagnostics)))

It's not straightforward to solve, unfortunately, and I think I'd want to pursue some of the longer-term design questions before investing effort into solving it.

@RikkiGibson RikkiGibson modified the milestones: 17.9, Backlog Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants