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

IDE0051 should factor in new dependency attributes in .NET 5 #48206

Open
stephentoub opened this issue Sep 30, 2020 · 8 comments
Open

IDE0051 should factor in new dependency attributes in .NET 5 #48206

stephentoub opened this issue Sep 30, 2020 · 8 comments
Assignees
Labels
Area-IDE Concept-Continuous Improvement Feature - IDE0051 help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@stephentoub
Copy link
Member

Version Used:
3.9.0-1.20480.1+b3c3f4875957e51526000898673291d11708d35b

Steps to Reproduce:

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

class Program
{
    static void Main() => new Program().Bar();

    [DynamicDependency("Foo")]
    private void Bar() => typeof(Program).GetMethod("Foo", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, null);

    private void Foo() => Console.WriteLine("Hello");
}

Expected Behavior:
.NET 5 adds new attributes that enable dependencies not obvious in the code, e.g. those via reflection, to be used for dead code elimination tools like https://github.com/mono/linker to better understand such dependencies. IDE0051 is such a tool and should ideally respect these attributes, such that in this example, IDE0051 doesn't fire.

Actual Behavior:
IDE0051 fires and suggests deleting the "unused" method:
image

cc: @eerhardt, @NickCraver

@Youssef1313
Copy link
Member

@stephentoub Couple of questions to better understand this:

  • Can the attribute reference a field/method outside of the current class?
  • If there are method overloads, what's the correct way to use the attribute to specify a specific overload?
  • For Visual Basic, which is case insensitive, should the attribute be case insensitive too?

@jinujoseph jinujoseph added Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Sep 30, 2020
@jinujoseph jinujoseph added this to the 16.9 milestone Sep 30, 2020
@stephentoub
Copy link
Member Author

stephentoub commented Oct 1, 2020

Can the attribute reference a field/method outside of the current class?

Yes. Even outside of the current assembly. So, there are certainly places where there's no way IDE0051 could appropriately respect the attribute (since it can't necessarily see the entire application), and potentially even times when it could theoretically but doing so could be prohibitively expensive. It'd be good, though, if the appropriate thought could be put into if/where/when/how it would make sense, so as to have a complete rather than disjoint story around this kind of trimming / dead-code removal.

If there are method overloads, what's the correct way to use the attribute to specify a specific overload?

@marek-safar, @eerhardt, where is the syntax documented? @Youssef1313, you can use the simple name, in which case it says that all of the overloads should be considered used, or you can name a specific overload by specifying the parameter types, e.g. https://github.com/dotnet/runtime/blob/c44154b15126786260f8a6397248dd04c0e015e3/src/libraries/System.Private.CoreLib/src/System/String.cs#L48

For Visual Basic, which is case insensitive, should the attribute be case insensitive too?

@marek-safar, what does the linker do here?

@Youssef1313
Copy link
Member

It'd be good, though, if the appropriate thought could be put into if/where/when/how it would make sense, so as to have a complete rather than disjoint story around this kind of trimming / dead-code removal.

Probably the IDE team will want to take this to design meeting to decide what are the essential situations to handle and what are the situations that doesn't need to be handled.

@marek-safar
Copy link
Contributor

where is the syntax documented

ILLinker uses the same syntax as Roslyn for documentation so it's documented at https://github.com/dotnet/csharplang/blob/master/spec/documentation-comments.md#processing-the-documentation-file

For Visual Basic, which is case insensitive, should the attribute be case insensitive too?

Not sure why it should be. ILLinker is IL-based tool and the attribute specifies metadata value.

@CyrusNajmabadi
Copy link
Member

you can use the simple name, in which case it says that all of the overloads should be considered used,

fwiw, that's challenging as i don't think we have such a concept at all in roslyn. Would it be more appropriate to remove hte 'overload' concept from these strings and say that all dependencies must be individually specified?

@huoyaoyuan
Copy link
Member

@mavasani
Copy link
Member

mavasani commented Jan 7, 2021

Tagging @sbomer who implemented the parser for the string arguments to DynamicDependencyAttribute in dotnet/linker#1197.

@sbomer What is the best way for Roslyn based tooling to convert the string argument to this attribute to Roslyn symbols? Given that the parser you added is adapted from Roslyn's DocumentationCommentId parser, would passing the string to DocumentationCommentId .GetSymbolsForDeclarationId cover all valid allowed values?

@jinujoseph jinujoseph modified the milestones: 16.9, 16.10 Mar 28, 2021
@jinujoseph jinujoseph modified the milestones: 16.10, Backlog Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Concept-Continuous Improvement Feature - IDE0051 help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
Development

No branches or pull requests

7 participants