roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

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

Open ericstj opened this issue 1 year ago • 3 comments

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.

ericstj avatar Jan 24 '24 18:01 ericstj

@333fred for triage

jaredpar avatar Feb 12 '24 21:02 jaredpar

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

333fred avatar Feb 20 '24 21:02 333fred

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.

ericstj avatar Feb 21 '24 22:02 ericstj