Skip to content

Commit c0bd208

Browse files
Change analyzer versions such that the repo can be built with .NET 7 RC2 SDK (#3077)
Change analyzer versions such that the repo can be built with .NET 7 RC2 SDK. - Took the versions from dotnet/runtime. - Remove CheckAttributeInstantiation method since is no longer necessary - Remove global attributes since they are no longer necessary - Workaround the fact that compiler injects System.Runtime.CompilerServices.RefSafetyRulesAttribute into every assembly to describe the language version Co-authored-by: tlakollo <[email protected]>
1 parent d8ca185 commit c0bd208

File tree

9 files changed

+18
-38
lines changed

9 files changed

+18
-38
lines changed

eng/Analyzers.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<ItemGroup Condition="'$(RunAnalyzers)' == 'true'">
33
<PackageReference Include="Microsoft.DotNet.CodeAnalysis" Version="$(MicrosoftDotNetCodeAnalysisVersion)" PrivateAssets="all" IsImplicitlyDefined="true" />
44
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="$(MicrosoftCodeAnalysisCSharpCodeStyleVersion)" PrivateAssets="all" />
5+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="$(MicrosoftCodeAnalysisNetAnalyzersVersion)" PrivateAssets="all" />
56
<!-- <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205" PrivateAssets="all" /> -->
67
</ItemGroup>
78

eng/Versions.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
<MicrosoftDotNetApiCompatVersion>8.0.0-beta.22520.1</MicrosoftDotNetApiCompatVersion>
2222
<MicrosoftDotNetCodeAnalysisVersion>6.0.0-beta.21271.1</MicrosoftDotNetCodeAnalysisVersion>
2323
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
24-
<MicrosoftCodeAnalysisVersion>4.4.0-1.final</MicrosoftCodeAnalysisVersion>
24+
<MicrosoftCodeAnalysisVersion>4.5.0-1.22517.9</MicrosoftCodeAnalysisVersion>
2525
<MicrosoftNetCompilersToolsetVersion>$(MicrosoftCodeAnalysisVersion)</MicrosoftNetCompilersToolsetVersion>
2626
<MicrosoftCodeAnalysisCSharpAnalyzerTestingXunitVersion>1.0.1-beta1.*</MicrosoftCodeAnalysisCSharpAnalyzerTestingXunitVersion>
2727
<MicrosoftCodeAnalysisBannedApiAnalyzersVersion>3.3.2</MicrosoftCodeAnalysisBannedApiAnalyzersVersion>
28+
<MicrosoftCodeAnalysisNetAnalyzersVersion>7.0.0-preview1.22513.1</MicrosoftCodeAnalysisNetAnalyzersVersion>
2829
<MicrosoftILVerificationVersion>7.0.0-preview.7.22375.6</MicrosoftILVerificationVersion>
2930
<!-- This controls the version of the cecil package, or the version of cecil in the project graph
3031
when we build the cecil submodule. The reference assembly package will depend on this version of cecil.

eng/ilasm.ilproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
project. -->
55

66
<PropertyGroup>
7-
<TargetFramework>net5.0</TargetFramework>
7+
<TargetFramework>net7.0</TargetFramework>
88
</PropertyGroup>
99

1010
<Target Name="CopyILAsmTool" DependsOnTargets="ResolveIlasmToolPaths" Condition="'$(MonoBuild)' == ''">

src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,11 @@ public override void Initialize (AnalysisContext context)
4747
if (methodSymbol.IsStaticConstructor () && methodSymbol.HasAttribute (RequiresAttributeName))
4848
ReportRequiresOnStaticCtorDiagnostic (symbolAnalysisContext, methodSymbol);
4949
CheckMatchingAttributesInOverrides (symbolAnalysisContext, methodSymbol);
50-
CheckAttributeInstantiation (symbolAnalysisContext, methodSymbol);
51-
foreach (var typeParameter in methodSymbol.TypeParameters)
52-
CheckAttributeInstantiation (symbolAnalysisContext, typeParameter);
53-
5450
}, SymbolKind.Method);
5551

5652
context.RegisterSymbolAction (symbolAnalysisContext => {
5753
var typeSymbol = (INamedTypeSymbol) symbolAnalysisContext.Symbol;
5854
CheckMatchingAttributesInInterfaces (symbolAnalysisContext, typeSymbol);
59-
CheckAttributeInstantiation (symbolAnalysisContext, typeSymbol);
60-
foreach (var typeParameter in typeSymbol.TypeParameters)
61-
CheckAttributeInstantiation (symbolAnalysisContext, typeParameter);
62-
6355
}, SymbolKind.NamedType);
6456

6557

@@ -68,24 +60,15 @@ public override void Initialize (AnalysisContext context)
6860
if (AnalyzerDiagnosticTargets.HasFlag (DiagnosticTargets.Property)) {
6961
CheckMatchingAttributesInOverrides (symbolAnalysisContext, propertySymbol);
7062
}
71-
72-
CheckAttributeInstantiation (symbolAnalysisContext, propertySymbol);
7363
}, SymbolKind.Property);
7464

7565
context.RegisterSymbolAction (symbolAnalysisContext => {
7666
var eventSymbol = (IEventSymbol) symbolAnalysisContext.Symbol;
7767
if (AnalyzerDiagnosticTargets.HasFlag (DiagnosticTargets.Event)) {
7868
CheckMatchingAttributesInOverrides (symbolAnalysisContext, eventSymbol);
7969
}
80-
81-
CheckAttributeInstantiation (symbolAnalysisContext, eventSymbol);
8270
}, SymbolKind.Event);
8371

84-
context.RegisterSymbolAction (symbolAnalysisContext => {
85-
var fieldSymbol = (IFieldSymbol) symbolAnalysisContext.Symbol;
86-
CheckAttributeInstantiation (symbolAnalysisContext, fieldSymbol);
87-
}, SymbolKind.Field);
88-
8972
context.RegisterOperationAction (operationContext => {
9073
var methodInvocation = (IInvocationOperation) operationContext.Operation;
9174
CheckCalledMember (operationContext, methodInvocation.TargetMethod, incompatibleMembers);
@@ -205,21 +188,6 @@ public override void Initialize (AnalysisContext context)
205188
foreach (var extraSymbolAction in ExtraSymbolActions)
206189
context.RegisterSymbolAction (extraSymbolAction.Action, extraSymbolAction.SymbolKind);
207190

208-
void CheckAttributeInstantiation (
209-
SymbolAnalysisContext symbolAnalysisContext,
210-
ISymbol symbol)
211-
{
212-
if (symbol.IsInRequiresScope (RequiresAttributeName))
213-
return;
214-
215-
foreach (var attr in symbol.GetAttributes ()) {
216-
if (attr.AttributeConstructor?.DoesMemberRequire (RequiresAttributeName, out var requiresAttribute) == true) {
217-
symbolAnalysisContext.ReportDiagnostic (Diagnostic.Create (RequiresDiagnosticRule,
218-
symbol.Locations[0], attr.AttributeConstructor.GetDisplayName (), GetMessageFromAttribute (requiresAttribute), GetUrlFromAttribute (requiresAttribute)));
219-
}
220-
}
221-
}
222-
223191
void CheckCalledMember (
224192
OperationAnalysisContext operationContext,
225193
ISymbol member,

test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public class C
620620
[RequiresAssemblyFiles("Calls Wrapper()")]
621621
Action M2()
622622
{
623-
[global::System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("Calls C.M1()")] void Wrapper () => M1();
623+
[RequiresAssemblyFiles("Calls C.M1()")] void Wrapper () => M1();
624624
return Wrapper;
625625
}
626626
}

test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public class C
200200
[RequiresDynamicCode("Calls Wrapper()")]
201201
Action M2()
202202
{
203-
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Calls C.M1()")] void Wrapper () => M1();
203+
[RequiresDynamicCode("Calls C.M1()")] void Wrapper () => M1();
204204
return Wrapper;
205205
}
206206
}

test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public class C
236236
[RequiresUnreferencedCode("Calls Wrapper()")]
237237
Action M2()
238238
{
239-
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Calls C.M1()")] void Wrapper () => M1();
239+
[RequiresUnreferencedCode("Calls C.M1()")] void Wrapper () => M1();
240240
return Wrapper;
241241
}
242242
}

test/ILLink.RoslynAnalyzer.Tests/UnconditionalSuppressMessageCodeFixTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public class C
389389
390390
Action M2()
391391
{
392-
[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute(""Trimming"", ""IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code"", Justification = ""<Pending>"")] void Wrapper () => M1();
392+
[UnconditionalSuppressMessage(""Trimming"", ""IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code"", Justification = ""<Pending>"")] void Wrapper () => M1();
393393
return Wrapper;
394394
}
395395
}";

test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public void Verify ()
4949
return s.FullName;
5050
}), StringComparer.Ordinal);
5151

52+
// Workaround for compiler injected attribute to describe the language version
53+
linkedMembers.Remove ("System.Void Microsoft.CodeAnalysis.EmbeddedAttribute::.ctor()");
54+
linkedMembers.Remove ("System.Int32 System.Runtime.CompilerServices.RefSafetyRulesAttribute::Version");
55+
linkedMembers.Remove ("System.Void System.Runtime.CompilerServices.RefSafetyRulesAttribute::.ctor(System.Int32)");
56+
5257
var membersToAssert = originalAssembly.MainModule.Types;
5358
foreach (var originalMember in membersToAssert) {
5459
if (originalMember is TypeDefinition td) {
@@ -91,6 +96,10 @@ protected virtual void VerifyModule (ModuleDefinition original, ModuleDefinition
9196

9297
protected virtual void VerifyTypeDefinition (TypeDefinition original, TypeDefinition linked)
9398
{
99+
// Workaround for compiler injected attribute to describe the language version
100+
verifiedGeneratedTypes.Add ("Microsoft.CodeAnalysis.EmbeddedAttribute");
101+
verifiedGeneratedTypes.Add ("System.Runtime.CompilerServices.RefSafetyRulesAttribute");
102+
94103
if (linked != null && verifiedGeneratedTypes.Contains (linked.FullName))
95104
return;
96105

@@ -839,6 +848,7 @@ protected virtual IEnumerable<string> FilterLinkedAttributes (ICustomAttributePr
839848
case "System.Runtime.CompilerServices.RuntimeCompatibilityAttribute":
840849
case "System.Runtime.CompilerServices.CompilerGeneratedAttribute":
841850
case "System.Runtime.CompilerServices.IsReadOnlyAttribute":
851+
case "System.Runtime.CompilerServices.RefSafetyRulesAttribute":
842852
continue;
843853

844854
// When mcs is used to compile the test cases, backing fields end up with this attribute on them

0 commit comments

Comments
 (0)