-
Notifications
You must be signed in to change notification settings - Fork 466
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
Analyzer to detect RequiresPreviewFeatures attribute #5155
Conversation
dd43568
to
ce9c69b
Compare
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Show resolved
Hide resolved
} | ||
} | ||
|
||
if (propertyOrMethodSymbol.IsImplementationOfAnyImplicitInterfaceMember(out ISymbol baseInterfaceMember)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new extension method. The existing method doesn't expose baseInterfaceMember
. Instead it only returned a bool.
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureUnitTests.cs
Outdated
Show resolved
Hide resolved
csInput | ||
} | ||
}, | ||
MarkupOptions = MarkupOptions.UseFirstDescriptor, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured this out from running the unit tests with multiple diagnostics. Just out of curiosity, what is the right way to specify which diagnostic to test for with the markup syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
// TODO: The following test cannot be activated yet because it requires preview roslyn bits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you want this commented out portion removed.
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureUnitTests.cs
Show resolved
Hide resolved
ce9c69b
to
b3380dc
Compare
I think this is ready to review now. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
{ | ||
if (usageType == PreviewFeatureUsageType.StaticAbstract) | ||
{ | ||
context.ReportDiagnostic(symbol.CreateDiagnostic(StaticAbstractIsPreviewFeatureRule)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will that get reported for something like:
public abstract class C
{
public static abstract M();
}
I think it shouldn't, since the above is an error anyway (static abstract is only allowed in interfaces), and this diagnostic will make it feel like this is allowed in preview.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will yea. However, I am unable to test it at the moment because we don't consume the latest compiler yet. Without testing, I'm not sure how the parent chain will look here. I've added a TODO for now. Once we started consuming the latest compiler here, I can fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you file a tracking issue for this and assign it to yourself?
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureUnitTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
if (!requiresPreviewFeaturesSymbols.TryGetValue(symbol, out bool existing)) | ||
{ | ||
bool ret = false; | ||
// TODO: Use symbol.HasAttribute(RequiresPreviewFeaturesAttribute) once we consume the latest sdk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you file a tracking bug with all the TODOs and assigned it to yourself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM as well - will let @buyaa-n handle the final sign off for merge. I would certainly recommend doing performance measurements for this analyzer as it seems equally expensive as PlatformCompatAnalyzer.
<value>An assembly has to opt into preview features before using them.</value> | ||
</data> | ||
<data name="DetectPreviewFeaturesMessage" xml:space="preserve"> | ||
<value>Using '{0}' requires opting into preview features. See https://aka.ms/dotnet-warnings/preview-features for more information.</value> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if the link should be dropped in favor of the help link (which will contain the rule doc at some point)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! This is still being discussed. It's minor enough that I'm fine with it going in as is and once the docs team decided which way to go, I'll change the error message
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
<data name="StaticAndAbstractRequiresPreviewFeatures" xml:space="preserve"> | ||
<value>Using both static and abstract modifiers requires opting into preview features. See https://aka.ms/dotnet-warnings/preview-features for more information.</value> | ||
</data> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider locking "static" and "abstract" from localization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are single quotes the way to do this? I don't see many other examples in the resx file. Changed it to 'static' and 'abstract' now.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureAnalyzer.cs
Outdated
Show resolved
Hide resolved
var csInput = @" | ||
using System.Runtime.Versioning; using System; | ||
namespace Preview_Feature_Scratch | ||
{" + | ||
@" | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var a = new Fraction(); | ||
var b = [|+a|]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this is split into two strings being concatenated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There used to be another var after the +
, but it's since been removed, so the +
is now unnecessary. It's just painful to remove the +
for every single test case right now, so I just left it in (since it's just unit tests)
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureUnitTests.cs
Outdated
Show resolved
Hide resolved
b717068
to
b25b8ed
Compare
Codecov Report
@@ Coverage Diff @@
## release/6.0.1xx #5155 +/- ##
===================================================
- Coverage 95.62% 95.60% -0.02%
===================================================
Files 1231 1233 +2
Lines 282186 283130 +944
Branches 16900 16964 +64
===================================================
+ Hits 269827 270681 +854
- Misses 10074 10134 +60
- Partials 2285 2315 +30 |
Thank you for the review everyone! @mavasani @buyaa-n @Youssef1313 @AraHaan |
Partly addresses dotnet/runtime#52726
I think this is a good point to get the implementation reviewed. I've borrowed ideas from the PlatformCompatilityAnalyzer implementation.
Pending work (in a follow up PR):
RequiresPreviewFeaturesAttribute
and also implement logic to look for the attribute on dependencies.3 Replace UseFirstDescriptor with the DiagnosticID for each test case