<commit_before>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Immutable;

namespace Microsoft.CodeAnalysis
{
    internal partial struct SymbolKey
    {
        private static class ErrorTypeSymbolKey
        {
            public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor)
            {
                visitor.WriteString(symbol.Name);
                visitor.WriteSymbolKey(symbol.ContainingSymbol as INamespaceOrTypeSymbol);
                visitor.WriteInteger(symbol.Arity);

                if (!symbol.Equals(symbol.ConstructedFrom))
                {
                    visitor.WriteSymbolKeyArray(symbol.TypeArguments);
                }
                else
                {
                    visitor.WriteSymbolKeyArray(ImmutableArray<ITypeSymbol>.Empty);
                }
            }

            public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
            {
                var name = reader.ReadString();
                var containingSymbolResolution = reader.ReadSymbolKey();
                var arity = reader.ReadInteger();

                using var typeArguments = reader.ReadSymbolArray<ITypeSymbol>();
                using var errorTypes = ResolveErrorTypes(reader, containingSymbolResolution, name, arity);

                if (typeArguments.Count != arity)
                {
                    return default;
                }

                if (arity == 0)
                {
                    return CreateSymbolInfo(errorTypes);
                }

                using var result = PooledArrayBuilder<INamedTypeSymbol>.GetInstance();
                var typeArgumentsArray = typeArguments.Builder.ToArray();
                foreach (var type in errorTypes)
                {
                    result.AddIfNotNull(type.Construct(typeArgumentsArray));
                }

                return CreateSymbolInfo(result);
            }

            private static PooledArrayBuilder<INamedTypeSymbol> ResolveErrorTypes(
                SymbolKeyReader reader,
                SymbolKeyResolution containingSymbolResolution, string name, int arity)
            {
                var errorTypes = PooledArrayBuilder<INamedTypeSymbol>.GetInstance();

                if (containingSymbolResolution.GetAnySymbol() == null)
                {
                    errorTypes.AddIfNotNull(reader.Compilation.CreateErrorTypeSymbol(null, name, arity));
                }
                else
                {
                    foreach (var container in containingSymbolResolution)
                    {
                        if (container is INamespaceOrTypeSymbol containerTypeOrNS)
                        {
                            errorTypes.AddIfNotNull(reader.Compilation.CreateErrorTypeSymbol(containerTypeOrNS, name, arity));
                        }
                    }
                }

                return errorTypes;
            }
        }
    }
}<commit_msg>Simplify<commit_after>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Diagnostics;

namespace Microsoft.CodeAnalysis
{
    internal partial struct SymbolKey
    {
        private static class ErrorTypeSymbolKey
        {
            public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor)
            {
                visitor.WriteString(symbol.Name);
                visitor.WriteSymbolKey(symbol.ContainingSymbol as INamespaceOrTypeSymbol);
                visitor.WriteInteger(symbol.Arity);

                if (!symbol.Equals(symbol.ConstructedFrom))
                {
                    visitor.WriteSymbolKeyArray(symbol.TypeArguments);
                }
                else
                {
                    visitor.WriteSymbolKeyArray(ImmutableArray<ITypeSymbol>.Empty);
                }
            }

            public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
            {
                var name = reader.ReadString();
                var containingSymbolResolution = reader.ReadSymbolKey();
                var arity = reader.ReadInteger();

                using var typeArguments = reader.ReadSymbolArray<ITypeSymbol>();
                if (typeArguments.Count != arity)
                {
                    return default;
                }

                using var errorTypes = CreateErrorTypes(reader, containingSymbolResolution, name, arity);
                if (arity == 0)
                {
                    return CreateSymbolInfo(errorTypes);
                }

                using var result = PooledArrayBuilder<INamedTypeSymbol>.GetInstance();
                var typeArgumentsArray = typeArguments.Builder.ToArray();
                foreach (var type in errorTypes)
                {
                    result.AddIfNotNull(type.Construct(typeArgumentsArray));
                }

                return CreateSymbolInfo(result);
            }

            private static PooledArrayBuilder<INamedTypeSymbol> CreateErrorTypes(
                SymbolKeyReader reader,
                SymbolKeyResolution containingSymbolResolution, string name, int arity)
            {
                var errorTypes = PooledArrayBuilder<INamedTypeSymbol>.GetInstance();

                foreach (var container in containingSymbolResolution)
                {
                    if (container is INamespaceOrTypeSymbol containerTypeOrNS)
                    {
                        errorTypes.AddIfNotNull(reader.Compilation.CreateErrorTypeSymbol(containerTypeOrNS, name, arity));
                    }
                }

                // Always ensure at least one error type was created.
                if (errorTypes.Count == 0)
                {
                    errorTypes.AddIfNotNull(reader.Compilation.CreateErrorTypeSymbol(null, name, arity));
                }

                return errorTypes;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.065231466494011, 'fraction_numerical': 0.0011330527678860473, 'mean_word_length': 1.5490924092409242, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '259640', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1310, 'n_words': 288}