Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash compiling extension static method #78042

Open
stephentoub opened this issue Apr 8, 2025 · 1 comment
Open

Crash compiling extension static method #78042

stephentoub opened this issue Apr 8, 2025 · 1 comment
Assignees
Labels
Area-Compilers Feature - Extension Everything The extension everything feature
Milestone

Comments

@stephentoub
Copy link
Member

Version Used:
5.0.0-1.25206.1

Steps to Reproduce:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>Preview</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="5.0.0-1.25207.5">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>
using System.Runtime.CompilerServices;

Console.WriteLine("Hello, World!");

namespace System.Linq
{
    public static partial class AsyncEnumerable
    {
        extension<TSource>(IAsyncEnumerable<TSource> source)
        {
            /// <summary>
            /// Casts the elements of an <see cref="IAsyncEnumerable{Object}"/> to the specified type.
            /// </summary>
            /// <typeparam name="TResult">The type to cast the elements of source to.</typeparam>
            /// <returns>An <see cref="IAsyncEnumerable{TResult}"/> that contains each element of the source sequence cast to the <typeparamref name="TResult"/> type.</returns>
            public IAsyncEnumerable<TResult> Cast<TResult>() // satisfies the C# query-expression pattern
            {
                return Impl(source, default);

                static async IAsyncEnumerable<TResult> Impl(
                    IAsyncEnumerable<TSource?> source,
                    [EnumeratorCancellation] CancellationToken cancellationToken)
                {
                    await foreach (object? item in source.WithCancellation(cancellationToken))
                    {
                        yield return (TResult)item!;
                    }
                }
            }
        }
    }
}

Expected Behavior:
Compiles

Actual Behavior:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Cci.MetadataWriter.CheckNameLength(String name, INamedEntity errorEntity)
   at Microsoft.Cci.MetadataWriter.PopulateFieldTableRows()
   at Microsoft.Cci.MetadataWriter.PopulateTypeSystemTables(Int32[] methodBodyOffsets, PooledBlobBuilder& mappedFieldDataWriter, PooledBlobBuilder& resourceWriter, BlobBuilder dynamicAnalysisData, Blob& mvidFixup)
   at Microsoft.Cci.MetadataWriter.BuildMetadataAndIL(PdbWriter nativePdbWriterOpt, BlobBuilder ilBuilder, PooledBlobBuilder& mappedFieldDataBuilder, PooledBlobBuilder& managedResourceDataBuilder, Blob& mvidFixup, Blob& mvidStringFixup)
   at Microsoft.Cci.PeWriter.WritePeToStream(EmitContext context, CommonMessageProvider messageProvider, Func`1 getPeStream, Func`1 getPortablePdbStreamOpt, PdbWriter nativePdbWriterOpt, String pdbPathOpt, Boolean metadataOnly, Boolean isDeterministic, Boolean emitTestCoverageData, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.SerializePeToStream(CommonPEModuleBuilder moduleBeingBuilt, DiagnosticBag metadataDiagnostics, CommonMessageProvider messageProvider, Func`1 getPeStream, Func`1 getMetadataPeStreamOpt, Func`1 getPortablePdbStreamOpt, PdbWriter nativePdbWriterOpt, String pdbPathOpt, RebuildData rebuildData, Boolean metadataOnly, Boolean includePrivateMembers, Boolean isDeterministic, Boolean emitTestCoverageData, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.SerializeToPeStream(CommonPEModuleBuilder moduleBeingBuilt, EmitStreamProvider peStreamProvider, EmitStreamProvider metadataPEStreamProvider, EmitStreamProvider pdbStreamProvider, RebuildData rebuildData, Func`2 testSymWriterFactory, DiagnosticBag diagnostics, EmitOptions emitOptions, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CommonCompiler.CompileAndEmit(TouchedFileLogger touchedFilesLogger, Compilation& compilation, ImmutableArray`1 analyzers, ImmutableArray`1 generators, ImmutableArray`1 additionalTextFiles, AnalyzerConfigSet analyzerConfigSet, ImmutableArray`1 sourceFileAnalyzerConfigOptions, ImmutableArray`1 embeddedTexts, DiagnosticBag diagnostics, ErrorLogger errorLogger, CancellationToken cancellationToken, CancellationTokenSource& analyzerCts, AnalyzerDriver& analyzerDriver, Nullable`1& generatorTimingInfo)
   at Microsoft.CodeAnalysis.CommonCompiler.RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CommonCompiler.Run(TextWriter consoleOutput, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.<>c__DisplayClass1_0.<Run>b__0(TextWriter tw)
   at Microsoft.CodeAnalysis.CommandLine.ConsoleUtil.RunWithUtf8Output[T](Func`2 func)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.Run(String[] args, BuildPaths buildPaths, TextWriter textWriter, IAnalyzerAssemblyLoader analyzerLoader)
   at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunLocalCompilation(String[] arguments, BuildPaths buildPaths, TextWriter textWriter)
   at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunCompilation(IEnumerable`1 originalArguments, BuildPaths buildPaths, TextWriter textWriter, String pipeName)
   at Microsoft.CodeAnalysis.CommandLine.BuildClient.Run(IEnumerable`1 arguments, RequestLanguage language, CompileFunc compileFunc, CompileOnServerFunc compileOnServerFunc, ICompilerServerLogger logger)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.MainCore(String[] args)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(String[] args)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 8, 2025
@stephentoub
Copy link
Member Author

cc: @jaredpar, @jcouv

@jaredpar jaredpar added Feature - Extension Everything The extension everything feature and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 8, 2025
@jaredpar jaredpar added this to the Next milestone Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Extension Everything The extension everything feature
Projects
None yet
Development

No branches or pull requests

3 participants