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
19.07k stars 4.04k forks source link

Add an API to discover if a symbol is a WellKnownType that effects public API behavior #71790

Open ericstj opened 9 months ago

ericstj commented 9 months ago

Background and Motivation

When writing tools that examine an assembly for compatibility, or for public API - it's important to know what's relevant.

There are many types that are "known" by the compiler and used to effect the behavior of API - even when those are not public in the assembly.

It would be nice if the compiler exposed an API that listed these known types so that we wouldn't need to consider them ourselves.

Related https://github.com/dotnet/roslyn/issues/68968

Proposed API

namespace Microsoft.CodeAnalysis
{
     public static class WellKnownTypes
     {
         // returns true if the compiler will treat this type as a well known type that impacts language behavior
         public static bool IsWellKnownType(ISymbol symbol)
     }

Usage Examples

// returns true if the symbol is meaningful to public API
public bool ShouldIncludeSymbol(ISymbol symbol)
{
    return symbol.IsVisible(_visibilitySettings) || WellKnownTypes.IsWellKnownType(symbol);
}

Alternative Designs

We can maintain a static list ourselves - or ask users to specify - but it's error prone and incomplete. The compiler knows which symbols it allows to be defined in user assemblies to augment its behavior - it would d be nice if that was exposed to callers.

Risks

Potentially some performance risk - though this could be implemented entirely on the side.

jaredpar commented 9 months ago

@333fred for triage

333fred commented 8 months ago

@ericstj is this something that you still want since the namespace idea?

ericstj commented 8 months ago

I think this request still has merit regardless but its less important if we have a heuristic that works well. Let's wait and see how the namespace suggestion works in practice.