Skip to content

Support renaming anonymous type members - #80701

Merged
CyrusNajmabadi merged 5 commits into
mainfrom
copilot/support-anonymous-type-members
Oct 14, 2025
Merged

Support renaming anonymous type members#80701
CyrusNajmabadi merged 5 commits into
mainfrom
copilot/support-anonymous-type-members

Conversation

Copilot AI commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

Fixes #883

This PR enables renaming of anonymous type members, which was previously blocked with a "not supported" error dialog.

Problem

Renaming anonymous type members was artificially restricted, even though the underlying infrastructure (Find References) already correctly finds all references to these members. For example:

var x = new
{
    Prop = 3,
    args,
};

Console.WriteLine(x.Prop);   // Renaming Prop was blocked
Console.WriteLine(x.args);   // Renaming args was blocked

Both inline rename and rename tracking would show an error message instead of allowing the rename operation.

Solution

Removed the blocking check in SymbolicRenameInfo.cs that prevented renaming properties when symbol.ContainingType.IsAnonymousType was true. Since Find References already correctly identifies all references to anonymous type members, no additional infrastructure changes were needed.

Testing

Added comprehensive tests covering:

  • Renaming explicit property names in anonymous type declarations
  • Renaming properties when accessed via member access (e.g., x.Prop)
  • Renaming inferred member names (e.g., the args parameter becomes a member named args)
  • Renaming properties that appear in multiple anonymous type instances
  • Verifying that IEditorInlineRenameService.GetRenameInfoAsync returns CanRename = true for anonymous type members

All tests follow existing patterns in RenameEngineTests.vb and CSharpInlineRenameServiceTests.cs.

Cleanup

Removed the now-obsolete error message resource "Renaming_anonymous_type_members_is_not_yet_supported" from FeaturesResources.resx and all localization files.

Original prompt

This section details on the original issue you should resolve

<issue_title>Rename: Support anonymous type members</issue_title>
<issue_description>Version Used:
VS2017

Steps to Reproduce:

using System;

class Program
{
    static void Main(string[] args)
    {
        var x = new
        {
            Prop = 3,
            args,
        };

        Console.WriteLine(x.Prop);
        Console.WriteLine(x.args);
    }
}

Expected Behavior:
Renaming (either through rename tracking or inline rename) works for args, Prop, x.Prop, x.args

Actual Behavior:
Inline rename is disabled with a "not supported" dialog. Rename tracking works, but inconsistently.</issue_description>

<agent_instructions>Copilot, this may just work if we remove the block. Find References already finds all the references to these members. Add tests to validate this as well.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #18425


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI and others added 2 commits October 14, 2025 12:52
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix renaming support for anonymous type members Support renaming anonymous type members Oct 14, 2025
Copilot AI requested a review from CyrusNajmabadi October 14, 2025 13:01
Comment thread src/EditorFeatures/CSharpTest/Rename/CSharpInlineRenameServiceTests.cs Outdated
@CyrusNajmabadi
CyrusNajmabadi marked this pull request as ready for review October 14, 2025 13:05
@CyrusNajmabadi
CyrusNajmabadi requested a review from a team as a code owner October 14, 2025 13:05
@CyrusNajmabadi
CyrusNajmabadi enabled auto-merge (rebase) October 14, 2025 13:14
@CyrusNajmabadi
CyrusNajmabadi force-pushed the copilot/support-anonymous-type-members branch from 699f1db to f3ceb52 Compare October 14, 2025 15:31
@jcouv jcouv added the Area-IDE label Oct 14, 2025
@CyrusNajmabadi
CyrusNajmabadi merged commit fa9128d into main Oct 14, 2025
24 of 25 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Oct 14, 2025
@jasonmalinowski

Copy link
Copy Markdown
Member

@CyrusNajmabadi @JoeRobich I was just about to block this PR. It's missing any tests for the cases where anonymous type members have names that are inferred from the use, where renaming it will involve updating the syntax with an explicit rename. I'll see if Copilot can fix this up, otherwise we'll need to revert this.

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

@jasonmalinowski i don't know why we'd need to revert this. even if that case doesn't work, we can still make cases like this work fine.

@CyrusNajmabadi
CyrusNajmabadi deleted the copilot/support-anonymous-type-members branch December 2, 2025 11:53
@davidwengier davidwengier modified the milestones: Next, 18.3 Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rename: Support anonymous type members

6 participants