Skip to content

Fix: Preserve verbatim identifiers in tuple element names during code generation#80980

Merged
CyrusNajmabadi merged 3 commits intomainfrom
copilot/fix-generate-method-issue
Nov 3, 2025
Merged

Fix: Preserve verbatim identifiers in tuple element names during code generation#80980
CyrusNajmabadi merged 3 commits intomainfrom
copilot/fix-generate-method-issue

Conversation

Copy link
Contributor

Copilot AI commented Nov 2, 2025

  • Explore repository structure and understand the issue
  • Locate the code generation for tuple types with element names
  • Identify the root cause: ITypeSymbolExtensions.TypeSyntaxGeneratorVisitor.CreateTupleTypeSyntax uses Identifier(element.Name) instead of properly escaping identifiers
  • Add test case reproducing the issue
  • Fix the bug by using ToIdentifierToken() extension method for tuple element names
  • Build and verify the fix compiles
  • Format code
  • Run code review - no issues found
  • Run security check - no issues found
  • Address PR feedback - Add WorkItem attribute to test

Summary

Fixed the issue where Generate Method (and other code generation features) would drop verbatim identifiers from tuple element names.

Changes Made:

  1. Modified ITypeSymbolExtensions.TypeSyntaxGeneratorVisitor.CreateTupleTypeSyntax to use element.Name.ToIdentifierToken() instead of Identifier(element.Name) to properly escape keyword identifiers
  2. Added test TestGenerateMethodWithTupleVerbatimIdentifiers to verify verbatim identifiers are preserved with WorkItem attribute

Example:

  • Before: (char char, int int) M2() ❌ (invalid C#)
  • After: (char @char, int @int) M2() ✅ (valid C#)

The fix follows the same pattern used elsewhere in the codebase (e.g., CSharpSyntaxGenerator.TupleElementExpression).

Original prompt

This section details on the original issue you should resolve

<issue_title>"Generate method" drops verbatim identifiers from value tuple names</issue_title>
<issue_description>Version Used: VS 18.0.0 Insiders [11123.170]

Steps to Reproduce:
Open the lightbulb menu on the M2 token and select "Generate method 'M2'".

class C
{
    void M1()
    {
        (char @char, int @int) x = M2();
    }
}

Expected Behavior:
The following method is generated (verbatim identifiers are retained to get legal code):

private (char @char, int @int) M2()
{
    throw new NotImplementedException();
}

Actual Behavior:
The following method is generated, which results in lots of compiler errors.

private (char char, int int) M2()
{
    throw new NotImplementedException();
}

The same happens for all other "Generate" commands (property, field, etc).</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…im identifiers

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix generate method to retain verbatim identifiers in tuples Fix: Preserve verbatim identifiers in tuple element names during code generation Nov 2, 2025
Copilot AI requested a review from CyrusNajmabadi November 2, 2025 02:04
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 2, 2025 02:04
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 2, 2025 02:04
""",
index: 1);

[Fact]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please add the right WorkItem attribute here like so: [Fact, WorkItem("url")]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added WorkItem attribute in e4473f2.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@CyrusNajmabadi
Copy link
Contributor

@dotnet/roslyn-ide ptal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Generate method" drops verbatim identifiers from value tuple names

4 participants