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

Casting function pointer directly to void and then a function pointer type causes crash. #44489

Closed
PathogenDavid opened this issue May 21, 2020 · 8 comments
Assignees

Comments

@PathogenDavid
Copy link
Contributor

This may be a bit premature, but I stumbled upon it while checking out the function pointers branch. Since it only happens when you cast all on one line I thought it might be worth mentioning.

Attn: @333fred


Version Used: Current features/function-pointers head (64d1878d61ac27d5273d8277c786c375fa5c38e8) via Roslyn CI.

Steps to Reproduce:

Build this code (SharpLab)

using System;

namespace CSharp9FunctionPointerTest
{
    public static class Program
    {
        private static unsafe void Test(delegate*<int, void> f)
            => f(42);

        private static void PrintNum(int x)
            => Console.WriteLine($"The number is: {x}");

        public static unsafe void Main(string[] args)
        {
#if false // This one works fine
            void* x = (delegate*<int, void>)&PrintNum;
            Test((delegate*<int, void>)x);
#elif false // This also works fine
            Test((delegate*<int, void>)(void*)(delegate*<int, void>)&PrintNum);
#else // This goes bang
            Test((delegate*<int, void>)(void*)&PrintNum);
#endif
        }
    }
}

Expected Behavior: No crash

Actual Behavior: Compiler goes bang:

1>------ Build started: Project: CSharp9FunctionPointerTest, Configuration: Debug Any CPU ------
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error : Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.SymbolDistinguisher.MakeDescriptions() in /_/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs:line 89
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.SymbolDistinguisher.Description.System.IFormattable.ToString(String format, IFormatProvider formatProvider) in /_/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs:line 246
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at System.String.Format(IFormatProvider provider, String format, Object[] args)
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.DiagnosticFormatter.Format(Diagnostic diagnostic, IFormatProvider formatter) in /_/src/Compilers/Core/Portable/Diagnostic/DiagnosticFormatter.cs:line 58
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.PrintError(Diagnostic diagnostic, TextWriter consoleOutput) in /_/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs:line 609
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.<ReportDiagnostics>g__reportDiagnostic|52_0(Diagnostic diag, <>c__DisplayClass52_0& ) in /_/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs:line 558
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.ReportDiagnostics(IEnumerable`1 diagnostics, TextWriter consoleOutput, ErrorLogger errorLoggerOpt) in /_/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs:line 499
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, CancellationToken cancellationToken) in /_/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs:line 811
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.Run(TextWriter consoleOutput, CancellationToken cancellationToken) in /_/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs:line 676
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.<>c__DisplayClass1_0.<Run>b__0(TextWriter tw) in /_/src/Compilers/Shared/Csc.cs:line 25
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommandLine.ConsoleUtil.RunWithUtf8Output[T](Func`2 func) in /_/src/Compilers/Core/CommandLine/ConsoleUtil.cs:line 28
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.Run(String[] args, BuildPaths buildPaths, TextWriter textWriter, IAnalyzerAssemblyLoader analyzerLoader)
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunLocalCompilation(String[] arguments, BuildPaths buildPaths, TextWriter textWriter) in /_/src/Compilers/Shared/BuildClient.cs:line 201
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunCompilation(IEnumerable`1 originalArguments, BuildPaths buildPaths, TextWriter textWriter, String pipeName) in /_/src/Compilers/Shared/BuildClient.cs:line 142
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.Run(IEnumerable`1 arguments, RequestLanguage language, CompileFunc compileFunc) in /_/src/Compilers/Shared/BuildClient.cs:line 78
1>C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(String[] args) in /_/src/Compilers/CSharp/csc/Program.cs:line 17
1>Done building project "CSharp9FunctionPointerTest.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Probably unnecessary, but here's the doozy of a command that MSBuild generated:

C:\Users\Pathogen-David\.nuget\packages\microsoft.net.compilers.toolset\3.7.0-ci.final\tasks\net472\csc.exe /noconfig /unsafe+ /checked- /nowarn:1701,1702,1701,1702,2008 /fullpaths /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE;DEBUG;NETCOREAPP;NETCOREAPP5_0 /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\Microsoft.CSharp.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\Microsoft.VisualBasic.Core.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\Microsoft.VisualBasic.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\Microsoft.Win32.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\mscorlib.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\netstandard.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.AppContext.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Buffers.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Collections.Concurrent.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Collections.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Collections.Immutable.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Collections.NonGeneric.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Collections.Specialized.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.Annotations.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.DataAnnotations.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.EventBasedAsync.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ComponentModel.TypeConverter.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Configuration.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Console.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Core.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Data.Common.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Data.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.Contracts.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.Debug.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.DiagnosticSource.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.FileVersionInfo.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.Process.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.StackTrace.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.TextWriterTraceListener.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.Tools.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.TraceSource.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Diagnostics.Tracing.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Drawing.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Drawing.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Dynamic.Runtime.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Globalization.Calendars.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Globalization.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Globalization.Extensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.Compression.Brotli.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.Compression.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.Compression.FileSystem.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.Compression.ZipFile.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.FileSystem.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.FileSystem.DriveInfo.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.FileSystem.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.FileSystem.Watcher.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.IsolatedStorage.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.MemoryMappedFiles.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.Pipes.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.IO.UnmanagedMemoryStream.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Linq.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Linq.Expressions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Linq.Parallel.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Linq.Queryable.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Memory.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Http.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Http.Json.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.HttpListener.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Mail.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.NameResolution.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.NetworkInformation.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Ping.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Requests.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Security.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.ServicePoint.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.Sockets.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.WebClient.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.WebHeaderCollection.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.WebProxy.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.WebSockets.Client.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Net.WebSockets.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Numerics.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Numerics.Vectors.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ObjectModel.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.DispatchProxy.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Emit.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Emit.ILGeneration.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Emit.Lightweight.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Extensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Metadata.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Reflection.TypeExtensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Resources.Reader.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Resources.ResourceManager.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Resources.Writer.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.CompilerServices.Unsafe.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.CompilerServices.VisualC.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Extensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Handles.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.InteropServices.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.InteropServices.RuntimeInformation.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.InteropServices.WindowsRuntime.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Intrinsics.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Loader.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Numerics.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Serialization.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Serialization.Formatters.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Serialization.Json.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Serialization.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Runtime.Serialization.Xml.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Claims.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Cryptography.Algorithms.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Cryptography.Csp.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Cryptography.Encoding.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Cryptography.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Cryptography.X509Certificates.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.Principal.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Security.SecureString.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ServiceModel.Web.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ServiceProcess.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.Encoding.CodePages.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.Encoding.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.Encoding.Extensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.Encodings.Web.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.Json.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Text.RegularExpressions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Channels.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Overlapped.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Tasks.Dataflow.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Tasks.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Tasks.Extensions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Tasks.Parallel.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Thread.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.ThreadPool.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Threading.Timer.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Transactions.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Transactions.Local.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.ValueTuple.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Web.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Web.HttpUtility.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Windows.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.Linq.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.ReaderWriter.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.Serialization.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.XDocument.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.XmlDocument.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.XmlSerializer.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.XPath.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\System.Xml.XPath.XDocument.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.4.20251.6\ref\netcoreapp5.0\WindowsBase.dll" /debug+ /debug:portable /filealign:512 /optimize- /out:obj\Debug\net5.0\CSharp9FunctionPointerTest.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /target:exe /warnaserror- /utf8output /deterministic+ /langversion:preview Program.cs "obj\Debug\net5.0\.NETCoreApp,Version=v5.0.AssemblyAttributes.cs" obj\Debug\net5.0\CSharp9FunctionPointerTest.AssemblyInfo.cs /warnaserror+:NU1605
@333fred 333fred self-assigned this May 21, 2020
@333fred
Copy link
Member

333fred commented May 21, 2020

Thanks, I'll take a look at this. It's certainly not valid code no matter what: an address of expression has no type and cannot be cast to void* directly. But we should report an error, not crash 😅

@PathogenDavid
Copy link
Contributor Author

No problem, thanks for looking into it! I'm pretty excited for this feature.


an address of expression has no type and cannot be cast to void* directly.

The proposed spec says otherwise, so it might need to be updated. Specifically this part from Allow address-of to target methods:

An implicit conversion exists from an address-of expression whose target is a method group E to void* if there is only one static method M in E.
If there is one static method, then the single best method from E is M.
Otherwise, a compile-time error occurs.

There's a handful of code examples using this too. I specifically ran into this issue following along with the code at the end of the NativeCallableAttribute section.

void* v = &CloseHandle;
delegate* cdecl<IntPtr, bool> f1 = (delegate* cdecl<IntPtr, bool>)v;

@333fred
Copy link
Member

333fred commented May 21, 2020

Huh. I wrote that, but have no memory of adding it, and certainly never intended for it to be in there.

@PathogenDavid
Copy link
Contributor Author

PathogenDavid commented May 21, 2020

😬 March Fred seemed to like the idea, he even mentioned it in the PR summary.

Happens to the best of us. If it were up to me it'd be an error too since I'd want an error if the method signature changed.

@tannergooding
Copy link
Member

Why wouldn't that be legal? It works for other pointer types:

public unsafe void M(int x) {
    int* px = &x;
    int* py = (int*)(void*)&x;
}

I agree it isn't necessarily "good" practice, but given pointers are implicitly convertible to void*:

Can implicitly convert delegate* to void*.

And that the speclet explicitly declares that the type of address-of to target methods is a delegate*:

Method groups will now be allowed as arguments to an address-of expression. The type of such an expression will be a delegate* which has the equivalent signature of the target method and a managed calling convention:

I would fully expect void* x = &method and void* x = (void*)&method (and various other bits, such as passing to a method which takes void*) would all work.
This makes it consistent with the other parts of the speclet and with how pointers in general currently work with regards to void*

@333fred
Copy link
Member

333fred commented May 21, 2020

I don't know why March Fred thought that. It's a terrible idea, it means that even introducing a non-conflicting overload could be a source-breaking change, which is just awful.

It works for other pointer types:

Other pointer types have a natural type. Function pointers, like other method groups, do not, therefore until there is a function pointer type we don't have a type to convert to void*. I'm going to update my current PR to the spec to clear that up, as they do not have natural types, just like other method groups. It will certainly not work in the first previews, and I don't expect it will ever work.

@tannergooding
Copy link
Member

Wouldn't the natural type be a delegate* which has the equivalent signature of the target method and a managed calling convention?

Otherwise, how does the language rationalize that &Method where static void Method() is convertible to delegate* managed<void> but not to delegate* unmanaged<void> or int*/void*?

  • As an aside, is that bit of the speclet still correct in the face of UnmanagedCallersOnlyAttribute since it is no longer usable as a managed function pointer?

@333fred
Copy link
Member

333fred commented May 22, 2020

The language rationalizes it in the same way it rationalizes every method group conversion: overload resolution against a known target type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants