Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs (C#)

Code before:
        /// type isn't found.
        /// </summary>
        internal NamedTypeSymbol GetWellKnownType(WellKnownType type, BindingDiagnosticBag diagnostics, Location location)
        {
            return GetWellKnownType(this.Compilation, type, diagnostics, location);
        }

        /// <summary>
        /// This is a layer on top of the Compilation version that generates a diagnostic if the well-known
        /// type isn't found.
        /// </summary>
        internal static NamedTypeSymbol GetWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, SyntaxNode node)
        {
            return GetWellKnownType(compilation, type, diagnostics, node.Location);
        }

#nullable enable
        internal static NamedTypeSymbol GetWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, Location location)
        {
            NamedTypeSymbol typeSymbol = compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");
            ReportUseSite(typeSymbol, diagnostics, location);
            return typeSymbol;
        }

        internal static bool TryGetWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, Location location, [NotNullWhen(true)] out NamedTypeSymbol? typeSymbol)
        {
            typeSymbol = compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");
            if (typeSymbol.GetUseSiteInfo().DiagnosticInfo?.Severity == DiagnosticSeverity.Error)
            {
                typeSymbol = null;
                return false;
            }

            ReportUseSite(typeSymbol, diagnostics, location);
            return true;
        }
#nullable disable

        /// <summary>
        /// This is a layer on top of the Compilation version that generates a diagnostic if the well-known
        /// type isn't found.
        /// </summary>
        internal NamedTypeSymbol GetWellKnownType(WellKnownType type, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
        {
            NamedTypeSymbol typeSymbol = this.Compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");
            typeSymbol.AddUseSiteInfo(ref useSiteInfo);
            return typeSymbol;
        }

Review comment (refactor):
>TryGetWellKnownType [](http://example.com/codeflow?start=29&length=19)

I think we should make it clear that this helper is to lookup optional types, for other cases we usually report errors when the type is bad or missing. Consider renaming to "TryGetOptionalWellKnownType" #Closed

Code after:
        /// type isn't found.
        /// </summary>
        internal NamedTypeSymbol GetWellKnownType(WellKnownType type, BindingDiagnosticBag diagnostics, Location location)
        {
            return GetWellKnownType(this.Compilation, type, diagnostics, location);
        }

        /// <summary>
        /// This is a layer on top of the Compilation version that generates a diagnostic if the well-known
        /// type isn't found.
        /// </summary>
        internal static NamedTypeSymbol GetWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, SyntaxNode node)
        {
            return GetWellKnownType(compilation, type, diagnostics, node.Location);
        }

#nullable enable
        internal static NamedTypeSymbol GetWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, Location location)
        {
            NamedTypeSymbol typeSymbol = compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");
            ReportUseSite(typeSymbol, diagnostics, location);
            return typeSymbol;
        }

        internal static bool TryGetOptionalWellKnownType(CSharpCompilation compilation, WellKnownType type, BindingDiagnosticBag diagnostics, Location location, [NotNullWhen(true)] out NamedTypeSymbol? typeSymbol)
        {
            typeSymbol = compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");
            var useSiteInfo = typeSymbol.GetUseSiteInfo();
            if (useSiteInfo.DiagnosticInfo?.Severity == DiagnosticSeverity.Error)
            {
                typeSymbol = null;
                return false;
            }

            // Ignore warnings
            useSiteInfo = new UseSiteInfo<AssemblySymbol>(diagnosticInfo: null, useSiteInfo.PrimaryDependency, useSiteInfo.SecondaryDependencies);
            diagnostics.Add(useSiteInfo, location);
            return true;
        }
#nullable disable

        /// <summary>
        /// This is a layer on top of the Compilation version that generates a diagnostic if the well-known
        /// type isn't found.
        /// </summary>
        internal NamedTypeSymbol GetWellKnownType(WellKnownType type, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
        {
            NamedTypeSymbol typeSymbol = this.Compilation.GetWellKnownType(type);
            Debug.Assert((object)typeSymbol != null, "Expect an error type if well-known type isn't found");