Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 2, 2025

Fixes the "Introduce local" refactoring generating invalid code for function pointer types by using the type's display string as the variable name (e.g., delegate*<void> delegate*<void> = field;).

Changes

  • ITypeSymbolExtensions.CreateParameterName: Added case for IFunctionPointerTypeSymbol to return default parameter name "value" instead of type display string
  • Pointer handling preserved: Normal pointers like Customer* continue to unwrap to the pointed-at type to generate meaningful names (e.g., Customer*customer)
  • Explanatory comments: Added "why, not what" comments explaining the different handling for normal pointers vs function pointers
  • Tests: Added TestFunctionPointerType and TestPointerType covering both function pointers and custom pointer types

Example

Before:

// Function pointers
delegate*<void> delegate*<void> = field;  // Invalid syntax
return delegate*<void>;

After:

// Function pointers
delegate*<void> value = field;
return value;

// Normal pointers (preserved behavior)
Customer* customer = field;
return customer;

Fixes #80982

Original prompt

This section details on the original issue you should resolve

<issue_title>"Introduce local" generates invalid code for function pointer types</issue_title>
<issue_description>Version Used: VS 18.0.0 Insiders [11123.170]

Steps to Reproduce:
Invoke the lightbulb menu at the marked location and select "Introduce local for 'field'".

unsafe class C
{
    public delegate* managed<void> D
    {
        get
        {
            return field$$;
        }
    }
}

Expected Behavior:
Code similar to this is generated:

public delegate* managed<void> D
{
    get
    {
        delegate*<void> v = field;
        return v;
    }
}

Actual Behavior:
This code is generated, which is syntactically invalid (it appears as if the type name is chosen as the name of the local):

public delegate* managed<void> D
{
    get
    {
        delegate*<void> delegate*<void> = field;
        return delegate*<void>;
    }
}
Image

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.

@CyrusNajmabadi
Copy link
Member

@copilot check normal pointers as well.

Copilot AI changed the title [WIP] Fix invalid code generation for function pointer types Fix "Introduce local" generating invalid code for function pointer types Nov 2, 2025
Copilot AI requested a review from CyrusNajmabadi November 2, 2025 19:57
Copilot finished work on behalf of CyrusNajmabadi November 2, 2025 19:57
Copy link
Contributor Author

Copilot AI commented Nov 2, 2025

@copilot check normal pointers as well.

Done in d860c92. Normal pointer types now return "value" instead of unwrapping to the pointed-at type (which could be keywords like "void" or "int"). Added test coverage for void*.

Copilot AI changed the title Fix "Introduce local" generating invalid code for function pointer types Fix "Introduce local" generating invalid code for pointer and function pointer types Nov 2, 2025
Copilot finished work on behalf of CyrusNajmabadi November 2, 2025 20:03
Copilot AI requested a review from CyrusNajmabadi November 2, 2025 20:16
Copilot finished work on behalf of CyrusNajmabadi November 2, 2025 20:16
case IPointerTypeSymbol pointerType:
type = pointerType.PointedAtType;
continue;
case IPointerTypeSymbol:
Copy link
Member

Choose a reason for hiding this comment

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

@copilot undo the change to pointers. If the user has Customer* we want to still make that as if it was just Customer. Add tests for this as well and add a 'why, not what's comment for this in the code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in b81b069. Restored pointer unwrapping so Customer* generates "customer". Added "why" comments explaining that normal pointers unwrap to get meaningful names while function pointers use the default. Updated test to use Customer*customer.

Copilot AI changed the title Fix "Introduce local" generating invalid code for pointer and function pointer types Fix "Introduce local" generating invalid code for function pointer types Nov 2, 2025
Copilot AI requested a review from CyrusNajmabadi November 2, 2025 22:04
Copilot finished work on behalf of CyrusNajmabadi November 2, 2025 22:04
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 3, 2025 18:10
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 3, 2025 18:10
@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (squash) November 3, 2025 18:10
@CyrusNajmabadi CyrusNajmabadi merged commit 7b48e08 into main Nov 3, 2025
24 of 25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 3, 2025
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-introduce-local-code-gen branch November 3, 2025 20:45
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.

"Introduce local" generates invalid code for function pointer types

3 participants