dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.97k stars 4.03k forks source link

[API proposal]: GetDeclaredSymbol for lambdas #63051

Open Youssef1313 opened 2 years ago

Youssef1313 commented 2 years ago

Background and Motivation

The node kinds SyntaxKind.AnonymousMethodExpression, SyntaxKind.ParenthesizedLambdaExpression, and SyntaxKind.SimpleLambdaExpression declares lambda symbols, but not supported by GetDeclaredSymbol

Proposed API

namespace Microsoft.CodeAnalysis.CSharp
{
     internal class CSharpSemanticModel
     {
+        public abstract ISymbol GetDeclaredSymbol(AnonymousFunctionExpressionSyntax anonymousFunctionSyntax, CancellationToken cancellationToken = default(CancellationToken));
     }

namespace Microsoft.CodeAnalysis
{
     public static class CSharpExtensions
     {
+        public static ISymbol? GetDeclaredSymbol(this SemanticModel? semanticModel, AnonymousFunctionExpressionSyntax anonymousFunctionSyntax, CancellationToken cancellationToken = default(CancellationToken))
     }
}

There will be parallel changes to VB.

Usage Examples

Alternative Designs

Risks

CyrusNajmabadi commented 2 years ago

I would support this. But it's worht a discussion. These guys are very funky in that they really can declare entirely fresh symbols (Esp. now with the work for lambda defaults and whatnot), but you have to do GetSymbolInfo on them. The latter does makes sense as these are also expressions though.

Generally speaking though, this divergence has always been odd on the IDE side. We generally have had to constantly special case these guys, rather than uniformly going through GetDeclaredSymbol. It has felt weird that virtually all other type/member symbols that come from something in source use a one mechanism, but these use another.

I'm curious what compiler thinks though. That said, i'm guessing IDE is more the primary consumer of these APIs, so maybe we defer more to that need?

333fred commented 2 years ago

API Review