Skip to content

LibraryImportGenerator ConvertToLibraryImportAnalyzerTests failing with new function pointer support #71891

Closed

Description

@steveharter

Please investigate cause of test failure related to function pointers from #69273,

The file src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/ConvertToLibraryImportAnalyzerTests.cs has this test that is failing for a function pointer. The line with the function pointer has been commented out (instead of disabling the whole test).

    public class ConvertToLibraryImportAnalyzerTests
    {
...
        public static IEnumerable<object[]> NoMarshallingRequiredTypes() => new[]
        {
            new object[] { typeof(byte) },
            new object[] { typeof(int) },
            new object[] { typeof(byte*) },
            new object[] { typeof(int*) },
            new object[] { typeof(bool*) },
            new object[] { typeof(char*) },
>>          new object[] { typeof(delegate* <void>) },
            new object[] { typeof(IntPtr) },
            new object[] { typeof(ConsoleKey) }, // enum
        };

new object[] { typeof(delegate* ) },

 Discovering: LibraryImportGenerator.Unit.Tests (method display = ClassAndMethod, method display options = None)
  Discovered:  LibraryImportGenerator.Unit.Tests (found 122 of 127 test cases)
  Starting:    LibraryImportGenerator.Unit.Tests (parallel test collections = on, max threads = 2)
    LibraryImportGenerator.UnitTests.Compiles.ValidateSnippetsWithMarshalType [SKIP]
      No current scenarios to test.
    LibraryImportGenerator.UnitTests.ConvertToLibraryImportAnalyzerTests.ByRef_ReportsDiagnostic(type: typeof(*())) [FAIL]
      Microsoft.CodeAnalysis.Testing.Verifiers.EqualWithMessageException : Context: Diagnostics of test state
      Mismatch between number of diagnostics returned, expected "3" actual "9"
      
      Diagnostics:
      // /0/Test0.cs(6,31): info SYSLIB1054: Mark the method 'Method_In' with 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
      VerifyCS.Diagnostic().WithSpan(6, 31, 6, 40).WithArguments("Method_In"),
      // /0/Test0.cs(6,45): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
      DiagnosticResult.CompilerError("CS0246").WithSpan(6, 45, 6, 46).WithArguments("p"),
      // /0/Test0.cs(6,46): error CS1001: Identifier expected
      DiagnosticResult.CompilerError("CS1001").WithSpan(6, 46, 6, 47),
      // /0/Test0.cs(9,31): info SYSLIB1054: Mark the method 'Method_Out' with 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
      VerifyCS.Diagnostic().WithSpan(9, 31, 9, 41).WithArguments("Method_Out"),
      // /0/Test0.cs(9,47): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
      DiagnosticResult.CompilerError("CS0246").WithSpan(9, 47, 9, 48).WithArguments("p"),
      // /0/Test0.cs(9,48): error CS1001: Identifier expected
      DiagnosticResult.CompilerError("CS1001").WithSpan(9, 48, 9, 49),
      // /0/Test0.cs(12,31): info SYSLIB1054: Mark the method 'Method_Ref' with 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
      VerifyCS.Diagnostic().WithSpan(12, 31, 12, 41).WithArguments("Method_Ref"),
      // /0/Test0.cs(12,47): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
      DiagnosticResult.CompilerError("CS0246").WithSpan(12, 47, 12, 48).WithArguments("p"),
      // /0/Test0.cs(12,48): error CS1001: Identifier expected
      DiagnosticResult.CompilerError("CS1001").WithSpan(12, 48, 12, 49),
      
      
      Assert.Equal() Failure
      Expected: 3
      Actual:   9
      Stack Trace:
        /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.XUnit/XUnitVerifier.cs(49,0): at Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier.Equal[T](T expected, T actual, String message)
        /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs(361,0): at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.VerifyDiagnosticResults(IEnumerable`1 actualResults, ImmutableArray`1 analyzers, DiagnosticResult[] expectedResults, IVerifier verifier)
        /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs(274,0): at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.VerifyDiagnosticsAsync(EvaluatedProjectState primaryProject, ImmutableArray`1 additionalProjects, DiagnosticResult[] expected, IVerifier verifier, CancellationToken cancellationToken)
        /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs(304,0): at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.RunImplAsync(CancellationToken cancellationToken)
        /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs(182,0): at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.RunAsync(CancellationToken cancellationToken)
        /_/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Verifiers/CSharpAnalyzerVerifier.cs(37,0): at LibraryImportGenerator.UnitTests.Verifiers.CSharpAnalyzerVerifier`1.VerifyAnalyzerAsync(String source, DiagnosticResult[] expected)
        /_/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/ConvertToLibraryImportAnalyzerTests.cs(87,0): at LibraryImportGenerator.UnitTests.ConvertToLibraryImportAnalyzerTests.ByRef_ReportsDiagnostic(Type type)
        --- End of stack trace from previous location ---
      Microsoft.CodeAnalysis.Testing.Verifiers.EqualWithMessageException : Context: Diagnostics of test state
      Mismatch between number of diagnostics returned, expected "2" actual "6"
      
      Diagnostics:
      // /0/Test0.cs(6,31): info SYSLIB1054: Mark the method 'Method_Parameter' with 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
      VerifyCS.Diagnostic().WithSpan(6, 31, 6, 47).WithArguments("Method_Parameter"),
      // /0/Test0.cs(6,49): error CS0246: The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)
      DiagnosticResult.CompilerError("CS0246").WithSpan(6, 49, 6, 50).WithArguments("p"),
      // /0/Test0.cs(6,50): error CS1001: Identifier expected
      DiagnosticResult.CompilerError("CS1001").WithSpan(6, 50, 6, 51),
      // /0/Test0.cs(8,6): error CS0592: Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations.
      DiagnosticResult.CompilerError("CS0592").WithSpan(8, 6, 8, 15).WithArguments("DllImport", "method"),
      // /0/Test0.cs(9,27): error CS0107: More than one protection modifier
      DiagnosticResult.CompilerError("CS0107").WithSpan(9, 27, 9, 40),
      // /0/Test0.cs(9,27): error CS1520: Method must have a return type
      DiagnosticResult.CompilerError("CS1520").WithSpan(9, 27, 9, 40),

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 9, 2022
  2. added this to the 7.0.0 milestone on Jul 10, 2022
  3. AaronRobinsonMSFT commented on Jul 10, 2022

    @AaronRobinsonMSFT
    Member
  4. elinor-fung commented on Jul 11, 2022

    @elinor-fung
    Member

    The tests generate their code snippets using Type.FullName, which now returns null with #71516.

    string typeName = type.FullName!;
    string source = @$"
    using System.Runtime.InteropServices;
    unsafe partial class Test
    {{
    [DllImport(""DoesNotExist"")]
    public static extern void {{|#0:Method_In|}}(in {typeName} p);
    [DllImport(""DoesNotExist"")]
    public static extern void {{|#1:Method_Out|}}(out {typeName} p);
    [DllImport(""DoesNotExist"")]
    public static extern void {{|#2:Method_Ref|}}(ref {typeName} p);
    }}
    ";

  5. modified the milestones: 7.0.0, 8.0.0 on Jul 18, 2022
  6. AaronRobinsonMSFT commented on Jul 18, 2022

    @AaronRobinsonMSFT
    Member

    Moving to 8 since the work has been postponed.

  7. MichalStrehovsky commented on Jul 19, 2022

    @MichalStrehovsky
    Member

    Moving to 8 since the work has been postponed.

    Worth noting that the

                new object[] { typeof(delegate* <void>) },
                new object[] { typeof(IntPtr) },
    

    entries are currently indistinguishable (typeof(delegate* <void>) == typeof(IntPtr)) - so if the intent was to have a test for this with delegate*, there currently isn't one, and it's a test hole. Once function pointer work is done, the test would still need work because FullName of function pointers won't be the C# syntax for them.

  8. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 19, 2023
  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 26, 2023
  10. ghost locked as resolved and limited conversation to collaborators on Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions