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

New version of NetAnalyzers throws CA1420 for delegates #6094

Closed
mattleibow opened this issue Aug 2, 2022 · 5 comments · Fixed by #6124
Closed

New version of NetAnalyzers throws CA1420 for delegates #6094

mattleibow opened this issue Aug 2, 2022 · 5 comments · Fixed by #6124

Comments

@mattleibow
Copy link
Member

Analyzer

Diagnostic ID: CA1420 Delegates with managed types as parameters or the return type require runtime marshalling to be enabled in the assembly where the delegate is defined

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 7.0.0-preview1.22302.1

Describe the bug

D:\a_work\1\s\src\Controls\src\Core\Platform\Tizen\Extensions\DragDropExtensions.cs(89,28): error CA1420: Delegates with managed types as parameters or the return type require runtime marshalling to be enabled in the assembly where the delegate is defined. [D:\a_work\1\s\src\Controls\src\Core\Controls.Core.csproj]

I started getting this issue after updating the package from:

  • GOOD: 7.0.0-preview1.22274.2
  • BAD: 7.0.0-preview1.22302.1
  • BAD: 7.0.0-preview1.22379.1

Steps To Reproduce

  1. Clone: https://github.com/dotnet/maui/tree/release/7.0.1xx-preview7
  2. Update https://github.com/dotnet/maui/blob/release/7.0.1xx-preview7/eng/Versions.props#L39 to be the newer: 7.0.0-preview1.22302.1
  3. dotnet build Microsoft.Maui.BuildTasks.slnf /p:configuration=Release

Expected behavior

Builds.

Actual behavior

Error.

Additional context

You may need to install the tizen workload, but I also saw this on other projects. I know this is a vague issue with a "download my code and build" but I am not sure what this error even means. Is it saying that delegates are not good?

I think it seems to be if some p/invoke has a delegate that uses a IntPtr

@Youssef1313
Copy link
Member

dotnet/runtime#63714 has some info around the analyzer. To my understanding, the analyzer should fire only if you have System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute added to the assembly. Does dotnet/maui add this attribute somewhere?

@mattleibow
Copy link
Member Author

mattleibow commented Aug 8, 2022

I do not believe so. But all the parameters and things are just IntPtr, so that is really the most basic arg type...

@amin1best
Copy link

Hi,
Another reproduction:

dotnet new console

Replace the content of "Program.cs" with:

using System.Runtime.InteropServices;

class Program
{
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct testStruct
    {
        public CULong testPrp;
    }

    [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Auto)]
    internal delegate CULong fncDlg(ref testStruct strcPrm);
    static void Main()
    {
        var lb = NativeLibrary.Load(@"test.dll");
        var fnc = NativeLibrary.GetExport(lb, "testFnc");

        testStruct strcObj = new testStruct();

        //#pragma warning disable CA1420
        var dlg = Marshal.GetDelegateForFunctionPointer<fncDlg>(fnc);
        //#pragma warning restore CA1420

        dlg(ref strcObj);

        NativeLibrary.Free(lb);
    }
}

and

dotnet build

show:

Program.cs(21,19): warning CA1420: Delegates with managed types as parameters or the return type require runtime marshalling to be enabled in the assembly where the delegat 
e is defined.

dotnet version: 7.0.100-preview.7.22377.5

@mattleibow
Copy link
Member Author

Does it compile with the older nuger package?

@amin1best
Copy link

In 6.0.100-rc.2.21505.57, it works fine and doesn't warn.

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