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

Public API for partial properties #73411

Open
RikkiGibson opened this issue May 9, 2024 · 0 comments
Open

Public API for partial properties #73411

RikkiGibson opened this issue May 9, 2024 · 0 comments
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request

Comments

@RikkiGibson
Copy link
Contributor

RikkiGibson commented May 9, 2024

Background and Motivation

Test plan: #73090

We need to introduce public APIs related to the upcoming partial properties feature.

These APIs generally correspond to existing APIs on IMethodSymbol, which are used for key IDE features such as go-to-definition and find-all-references. We expect these APIs to be used by the same features for corresponding scenarios relating to properties.

Proposed API

namespace Microsoft.CodeAnalysis
{
     public interface IPropertySymbol
     {
+        /// <summary>If this is the implementation part of a partial property, returns the symbol for the definition part. Otherwise, returns null.</summary>
+        IPropertySymbol? PartialDefinitionPart { get; }

+        /// <summary>If this is the definition part of a partial property, returns the symbol for the implementation part. Otherwise, returns null.</summary>
+        IPropertySymbol? PartialImplementationPart { get; }
     }
partial class C
{
    // Symbol for this declaration of 'Prop' will:
    // - return null for PartialDefinitionPart
    // - return symbol for below 'Prop' declaration for PartialImplementationPart
    public partial int Prop { get; set; }

    // Symbol for this declaration of 'Prop' will:
    // - return symbol for above 'Prop' declaration for PartialDefinitionPart
    // - return null for PartialImplementationPart
    public partial int Prop { get => 1; set { } }
}

Notable behaviors:

  • These APIs only return values for original definition source symbols. So, for example, a retargeting symbol with a partial property underlying symbol will return null from these APIs.
  • If one of the partial declarations required by the language is missing, the APIs will return null. For example, if only the the definition part of a partial property exists, then accessing PartialImplementationPart on it will return null.
  • The accessors of a partial property are considered partial methods, and their counterparts are returned from the corresponding APIs on IMethodSymbol. For example, if we had the definition part symbol for C.Prop.get, and we accessed PartialImplementationPart on it, we would get the implementation part symbol for C.Prop.get. Also, IMethodSymbol.IsPartialDefinition will return true for the definition part symbol for C.Prop.get.

Usage Examples

The following is based on the existing helper ParameterSymbolReferenceFinder.CascadeBetweenPartialMethodParameters:

namespace Microsoft.CodeAnalysis.FindSymbols.Finders;

internal class ParameterSymbolReferenceFinder
{
    private static void CascadeBetweenPartialPropertyParameters(
        IParameterSymbol parameter,
        ArrayBuilder<ISymbol> results)
    {
        // When we find-all-refs to a partial indexer parameter, for example, also include references to the corresponding parameter within the other partial declaration.
        if (parameter.ContainingSymbol is IPropertySymbol property)
        {
            var ordinal = parameter.Ordinal;
            if (ordinal < property.PartialDefinitionPart?.Parameters.Length)
                results.Add(property.PartialDefinitionPart.Parameters[ordinal]);

            if (ordinal < property.PartialImplementationPart?.Parameters.Length)
                results.Add(property.PartialImplementationPart.Parameters[ordinal]);
        }
    }
}

Alternative Designs

  • Also include public bool IPropertySymbol.IsPartialDefinition { get; } for uniformity with existing IMethodSymbol.IsPartialDefinition.

The reason this API is not included in the main proposal because partial properties are required to have an implementation part. IMethodSymbol.IsPartialDefinition was introduced to address EnC scenarios around partial methods lacking an implementation part. But perhaps no such scenario exists for partial properties, since a compilation error occurs when the property lacks an implementation part.

  • Don't ship any new APIs and have users dig through the accessor symbols to locate the other property part instead. This seems cumbersome.

Risks

N/A

@RikkiGibson RikkiGibson added Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request labels May 9, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 9, 2024
@RikkiGibson RikkiGibson added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

No branches or pull requests

1 participant