Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs (C#)

Code before:
            Debug.Assert(intType.SpecialType == SpecialType.System_Int32);

            return EnsurePrivateImplClassMethodExists(syntaxNode, PrivateImplementationDetails.SynthesizedInlineArrayAsSpanName,
                                                      static (privateImplClass, arg) =>
                                                      {
                                                          return new SynthesizedInlineArrayAsSpanMethod(
                                                              privateImplClass,
                                                              PrivateImplementationDetails.SynthesizedInlineArrayAsSpanName,
                                                              arg.spanType,
                                                              arg.intType);
                                                      },
                                                      (spanType, intType),
                                                      diagnostics);
        }

        internal NamedTypeSymbol EnsureInlineArrayTypeExists(SyntaxNode syntaxNode, SyntheticBoundNodeFactory factory, int arrayLength, DiagnosticBag diagnostics)
        {
            Debug.Assert(Compilation.Assembly.RuntimeSupportsInlineArrayTypes);
            Debug.Assert(arrayLength > 0);

            if (arrayLength is >= 2 and <= 16)
            {
                // In .NET 10 and up, the runtime provides InlineArrayN<T> types for N from 2 to 16.
                var arrayWellKnownType = WellKnownType.System_Runtime_CompilerServices_InlineArray2 + (arrayLength - 2);
                Debug.Assert(arrayWellKnownType is >= WellKnownType.System_Runtime_CompilerServices_InlineArray2 and <= WellKnownType.System_Runtime_CompilerServices_InlineArray16);
                if (Compilation.GetWellKnownType(arrayWellKnownType) is not MissingMetadataTypeSymbol and { } inlineArrayType)
                {
                    var info = inlineArrayType.GetUseSiteInfo().DiagnosticInfo;
                    if (info is { })
                    {
                        Symbol.ReportUseSiteDiagnostic(info, diagnostics, syntaxNode.Location);
                    }

                    return inlineArrayType;
                }
            }

            string typeName = GeneratedNames.MakeSynthesizedInlineArrayName(arrayLength, CurrentGenerationOrdinal);
            var privateImplClass = GetPrivateImplClass(syntaxNode, diagnostics).PrivateImplementationDetails;
            var typeAdapter = privateImplClass.GetSynthesizedType(typeName);

            if (typeAdapter is null)
            {
                var attributeConstructor = (MethodSymbol)factory.SpecialMember(SpecialMember.System_Runtime_CompilerServices_InlineArrayAttribute__ctor);
                Debug.Assert(attributeConstructor is { });

                var typeSymbol = new SynthesizedInlineArrayTypeSymbol(SourceModule, typeName, arrayLength, attributeConstructor);
                privateImplClass.TryAddSynthesizedType(typeSymbol.GetCciAdapter());
                typeAdapter = privateImplClass.GetSynthesizedType(typeName)!;
            }


Review comment (refactor):
>if (Compilation.GetWellKnownType(arrayWellKnownType) is not MissingMetadataTypeSymbol and { } inlineArrayType) [](http://example.com/codeflow?start=16&length=110)

The check for MissingMetadataTypeSymbol is not robust enough to catch a "bad" type. I think we should add a GetWellKnownType helper with ```bool isOptional``` parameter to Binder, or add an optional parameter to an existing helper. I also think that the implementation around use-site diagnostics should be consistent with behavior in  
```internal static Symbol GetWellKnownTypeMember(CSharpCompilation compilation, WellKnownMember member, out UseSiteInfo<AssemblySymbol> useSiteInfo, bool isOptional = false)```. Specifically, nothing is reported, warnings allow type usage. #Closed

Code after:
            Debug.Assert(intType.SpecialType == SpecialType.System_Int32);

            return EnsurePrivateImplClassMethodExists(syntaxNode, PrivateImplementationDetails.SynthesizedInlineArrayAsSpanName,
                                                      static (privateImplClass, arg) =>
                                                      {
                                                          return new SynthesizedInlineArrayAsSpanMethod(
                                                              privateImplClass,
                                                              PrivateImplementationDetails.SynthesizedInlineArrayAsSpanName,
                                                              arg.spanType,
                                                              arg.intType);
                                                      },
                                                      (spanType, intType),
                                                      diagnostics);
        }

        internal NamedTypeSymbol EnsureInlineArrayTypeExists(SyntaxNode syntaxNode, SyntheticBoundNodeFactory factory, int arrayLength, BindingDiagnosticBag diagnostics)
        {
            Debug.Assert(Compilation.Assembly.RuntimeSupportsInlineArrayTypes);
            Debug.Assert(arrayLength > 0);
            Debug.Assert(diagnostics.DiagnosticBag is not null);

            if (arrayLength is >= 2 and <= 16)
            {
                // In .NET 10 and up, the runtime provides InlineArrayN<T> types for N from 2 to 16.
                var arrayWellKnownType = WellKnownType.System_Runtime_CompilerServices_InlineArray2 + (arrayLength - 2);
                Debug.Assert(arrayWellKnownType is >= WellKnownType.System_Runtime_CompilerServices_InlineArray2 and <= WellKnownType.System_Runtime_CompilerServices_InlineArray16);
                if (Binder.TryGetOptionalWellKnownType(Compilation, arrayWellKnownType, diagnostics, syntaxNode.Location, out var existingType))
                {
                    return existingType;
                }
            }

            string typeName = GeneratedNames.MakeSynthesizedInlineArrayName(arrayLength, CurrentGenerationOrdinal);
            var privateImplClass = GetPrivateImplClass(syntaxNode, diagnostics.DiagnosticBag).PrivateImplementationDetails;
            var typeAdapter = privateImplClass.GetSynthesizedType(typeName);

            if (typeAdapter is null)
            {
                var attributeConstructor = (MethodSymbol)factory.SpecialMember(SpecialMember.System_Runtime_CompilerServices_InlineArrayAttribute__ctor);
                Debug.Assert(attributeConstructor is { });

                var typeSymbol = new SynthesizedInlineArrayTypeSymbol(SourceModule, typeName, arrayLength, attributeConstructor);
                privateImplClass.TryAddSynthesizedType(typeSymbol.GetCciAdapter());
                typeAdapter = privateImplClass.GetSynthesizedType(typeName)!;
            }

            Debug.Assert(typeAdapter.Name == typeName);
            return (NamedTypeSymbol)typeAdapter.GetInternalSymbol()!;
        }

        internal NamedTypeSymbol EnsureReadOnlyListTypeExists(SyntaxNode syntaxNode, SynthesizedReadOnlyListKind kind, DiagnosticBag diagnostics)