diff options
author | Vitek Karas <10670590+vitek-karas@users.noreply.github.com> | 2022-09-01 23:52:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-01 23:52:34 +0300 |
commit | 1352b543cfdbaf33649bf164d7371a99dff72f10 (patch) | |
tree | 2802db2ebb5aa69df5cb3fca2009c4068e59304d | |
parent | 1c4e4b43beefa40ef6af4f43fe23bdcd30a22cc1 (diff) |
Fixes a null ref which happens when an XML comment cref contains generic instantiation (#3015)
* Fixes a null ref which happens when an XML comment cref contains generic instantiation
In the cref some of the symbols (for example the type argument) are missing containing symbols, which leads to null refs.
In any case, we should not perform any analysis on symbols inside crefs, only on real code.
So this modifies the analyzer to ignore any symbol inside a cref.
Adds a test to validate this.
* Simplify the test
-rw-r--r-- | src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs | 2 | ||||
-rw-r--r-- | test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs | 26 |
2 files changed, 27 insertions, 1 deletions
diff --git a/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs b/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs index e847010a0..7e24cbf4a 100644 --- a/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs +++ b/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs @@ -129,7 +129,7 @@ namespace ILLink.RoslynAnalyzer invocationExpression.Expression is IdentifierNameSyntax ident1 && ident1.Identifier.ValueText.Equals ("nameof")) return; - else if (parentNode is NameMemberCrefSyntax) + else if (parentNode is CrefSyntax) return; parentNode = parentNode.Parent; diff --git a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs index 71e6ab82d..7acce352b 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs @@ -1378,5 +1378,31 @@ namespace System // (8,3): error CS0103: The name 'type' does not exist in the current context DiagnosticResult.CompilerError ("CS0103").WithSpan (8, 3, 8, 7).WithArguments ("type")); } + + [Fact] + public Task CRefGenericParameterAnalysis () + { + var Source = """ + using System; + using System.Diagnostics.CodeAnalysis; + + class C<TOuter> + { + /// <summary> + /// <remarks> + /// <see cref="CRequires{TOuter}.IsIt"/> + /// </remarks> + /// </summary> + static CRequires<TOuter> Value => throw new Exception(); + } + + class CRequires<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TInner> { public static bool IsIt => false; } + """; + + // The actual usage (return value) should warn, about missing annotation, but the cref should not. + return VerifyDynamicallyAccessedMembersAnalyzer (Source, + // (11,9): warning IL2091: 'TInner' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in 'CRequires<TInner>'. The generic parameter 'TOuter' of 'C<TOuter>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. + VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter).WithSpan (11, 9, 11, 26).WithSpan (4, 9, 4, 15).WithArguments ("TInner", "CRequires<TInner>", "TOuter", "C<TOuter>", "'DynamicallyAccessedMemberTypes.PublicMethods'")); + } } } |