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

Roslyn reports false errors for interceptors for conditional invocations #70243

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

Comments

@TessenR
Copy link

TessenR commented Oct 4, 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:

using System.Runtime.CompilerServices;

var c = new object();
c.ToString(); // fine
c?.ToString(); // error CS9151

static class Interceptors
{
  [InterceptsLocation("Program.cs", 4, 3)]
  [InterceptsLocation("Program.cs", 5, 4)]
  public static string Interceptor1(this object obj) => "ToString call";
}

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

Expected Behavior:
Both invocations are intercepted

Actual Behavior:
An error is reported for the ?. invocation

Error CS9151: Possible method name 'ToString' cannot be intercepted because it is not being invoked. (10, 4)

It'd be rather strange if conditional access will not be compatible with interceptors and you'll have to fall back to good old if statements if you want to use interceptors.
Moreover, the error is very confusing - the method ToString() is obviously invoked at the given location.

[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 4, 2023
@jcouv jcouv added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 5, 2023
@RikkiGibson RikkiGibson added this to the 17.9 milestone Oct 5, 2023
@RikkiGibson RikkiGibson modified the milestones: 17.9, Backlog Nov 2, 2023
@RikkiGibson
Copy link
Contributor

I think this needs to be addressed before the interceptors feature can be considered stable. However, I moved to backlog because possible larger-scale design changes may "obsolete" this bug.

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