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

LibraryImportGenerator ConvertToLibraryImportAnalyzerTests failing with new function pointer support #71891

Closed
steveharter opened this issue Jul 9, 2022 · 5 comments · Fixed by #89225
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature test-bug Problem in test source code (most likely)
Milestone

Comments

@steveharter
Copy link
Member

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),
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 9, 2022
@AaronRobinsonMSFT AaronRobinsonMSFT added area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature and removed area-VM-coreclr untriaged New issue has not been triaged by the area owner labels Jul 10, 2022
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 7.0.0 milestone Jul 10, 2022
@ghost
Copy link

ghost commented Jul 10, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

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),
Author: steveharter
Assignees: -
Labels:

area-System.Runtime.InteropServices, source-generator

Milestone: -

@AaronRobinsonMSFT
Copy link
Member

/cc @jkoritzinsky @elinor-fung

@elinor-fung
Copy link
Member

elinor-fung commented Jul 11, 2022

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);
}}
";

@AaronRobinsonMSFT AaronRobinsonMSFT modified the milestones: 7.0.0, 8.0.0 Jul 18, 2022
@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented Jul 18, 2022

Moving to 8 since the work has been postponed.

@MichalStrehovsky
Copy link
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.

@jkoritzinsky jkoritzinsky added the test-bug Problem in test source code (most likely) label Jul 19, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 19, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 26, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature test-bug Problem in test source code (most likely)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants