-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
[Proposal] GetAllTypesByMetadataName #57802
Comments
@AlekseyTs would you take a look at this API and let me know your thoughts? I've marked it needs-work for now until we agree that it's ready to go to full design review. |
The proposal looks good to me. I don't particularly like the name of the structure, but we can discuss that during review. Also, I would use "GetTypesByMetadataName" as a name for the API. |
FWIW, I did consider this, but I felt it was too similar to |
FWIW< that doesn't bother me. You'll get very distinct types back, so it should be readily apparent if you're doing something wrong. I would not like it if the signatures were the same (including return type) and tehre was a one letter difference like this :) |
I'd call |
API ReviewWe had a lengthy discussion about our philosophies in this area today. We generally think an API shaped similar to this is a good primitive to have for building heuristics, and there is some concern about adding heuristics to the compiler layer itself. We do, however, need to consider how we want to help users with common heuristics, like the equivalent of the compiler's GetWellKnownType internal API. Now that we've agreed on this API concept, we can look at specifics of the API itself in the next design meeting. Some initial comments:
|
I've updated the proposal here: in particular, I've removed the custom lazy enumerable Similarly, while we could add filtering overloads, we can't effectively cache their results, and thus the filtering methods would have to do one of two things:
|
Closes dotnet#57802. Implements the GetTypesByMetadataName API, which returns all named types in the current compilation and referenced assemblies that match the given CLR metatdata name. Also updates the IDE's GetBestTypeByMetadataName to use this API under the hood, rather than the existing manual walk they were doing, simplyfing that codepath and proving out the API itself.
API ReviewAPI is approved. We should additionally update the docs on GetTypeByMetadataName to clarify the ambiguity scenarios, and look into an analyzer to warn when the result of GetTypeByMetadataName is unconditionally accessed. |
Background and Motivation
We've had a number of requests over the years for a
GetTypeByMetadataName
that works differently. The IDE has aGetBestTypeByMetadataName
extension method that works well for their purposes, and #52399 requested making it a public API onCompilation
itself. However, this API is just a different heuristic for picking the "best" among all potential types exposed for a given metadata name in a compilation; it's one that works well for the IDE, but it's not a one-size-fits all algorithm. Rather than making another heuristic API and trying to determine if we made it extensible enough for any given use case, we instead propose an API that will get all types that match a given metadata name in a Compilation and all referenced assemblies: the caller can than look through those instances and decide which one it would like to use.Proposed API
namespace Microsoft.CodeAnalysis { public class Compilation { + public ImmutableArray<INamedTypeSymbol> GetTypesByMetadataName(string fullyQualifiedMetadataName); } }
Usage Examples
Alternative Designs
I've opted to add a few extensions to try address what I view as the common use case for this API, but it's possible we don't really see those extensions being useful/needed, so we can choose to omit them if needed.
We could soften our "no heuristics" stance slightly by adding a default bool parameter
includeInaccessibleTypes
that would control whether the returned enumerable includes types that the match the requested FQN, but are not visible to the current Compilation (because they are internal without IVT/private). This could be a common enough heuristic that it would make sense to include in the API, but there's definitely a slippery slope of how far down this rabbit hole do we want to go.Risks
Any new API adds risks, of course, but I believe the approach here leads to less risk for both the compiler and consumers, and the heuristics expected are clearly defined by the consumer's code.
The text was updated successfully, but these errors were encountered: