Skip to content

Fix ConstructorProvider.Initializer not populated for custom constructors #7855

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

Merged
merged 10 commits into from
Jul 9, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 8, 2025

The ConstructorProvider.Initializer property was not being populated when parsing custom constructors with initializers (: this(...) or : base(...)). This meant that constructor initializer information was lost during code generation.

Problem

When custom constructors contained initializers like:

public Foo(int bar) : this(bar, "default")
{
}

The ConstructorProvider.Signature.Initializer property would be null instead of containing the initializer information.

Root Cause

The issue was in NamedTypeSymbolProvider.BuildConstructors() method which creates ConstructorSignature objects from Roslyn IMethodSymbol data. The method was not extracting constructor initializer information from the syntax tree, so the Initializer parameter was always defaulting to null.

Solution

  • Added ExtractConstructorInitializer method - Analyzes constructor syntax using Roslyn to detect and extract initializer information from ConstructorDeclarationSyntax
  • Enhanced BuildConstructors - Now passes the extracted initializer to the ConstructorSignature constructor
  • Added expression conversion logic - Handles conversion of constructor arguments to appropriate ValueExpression types for both literals and identifiers
  • Comprehensive test coverage - Added tests covering this initializers, base initializers, and constructors without initializers

Testing

Added comprehensive test cases in ConstructorInitializerTests:

  • CustomConstructorWithThisInitializerShouldHaveInitializerPopulated - Validates : this(...) initializers
  • CustomConstructorWithBaseInitializerShouldHaveInitializerPopulated - Validates : base(...) initializers
  • CustomConstructorWithoutInitializerShouldHaveNullInitializer - Ensures constructors without initializers still work correctly

All tests pass, including existing test suites (41 NamedTypeSymbolProvider tests, 2 ConstructorProvider tests).

Fixes #6717.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 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: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] ConstructorProvider.Initalizer not populated for custom constructors Fix ConstructorProvider.Initializer not populated for custom constructors Jul 8, 2025
@Copilot Copilot AI requested a review from JoshLove-msft July 8, 2025 22:20
Copilot finished work on behalf of JoshLove-msft July 8, 2025 22:20
…established pattern

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI requested a review from JoshLove-msft July 9, 2025 18:44
Copilot finished work on behalf of JoshLove-msft July 9, 2025 18:44
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…eneratorAsync and remove Setup method

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot finished work on behalf of JoshLove-msft July 9, 2025 19:00
…d refactor to use ModelProvider.CustomCodeView

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI requested a review from JoshLove-msft July 9, 2025 19:21
Copilot finished work on behalf of JoshLove-msft July 9, 2025 19:21
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI requested a review from JoshLove-msft July 9, 2025 19:25
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot finished work on behalf of JoshLove-msft July 9, 2025 19:31
@JoshLove-msft JoshLove-msft marked this pull request as ready for review July 9, 2025 19:33
…izerTests to follow established pattern

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI requested a review from JoshLove-msft July 9, 2025 20:01
Copilot finished work on behalf of JoshLove-msft July 9, 2025 20:01
…ConstructorProviders and TestData under ConstructorProviders/TestData/ConstructorInitializerTests

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@Copilot Copilot AI requested a review from JoshLove-msft July 9, 2025 20:10
Copilot finished work on behalf of JoshLove-msft July 9, 2025 20:10
@JoshLove-msft JoshLove-msft enabled auto-merge July 9, 2025 20:17
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jul 9, 2025
@JoshLove-msft JoshLove-msft disabled auto-merge July 9, 2025 20:22
@JoshLove-msft JoshLove-msft enabled auto-merge July 9, 2025 20:22
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Jul 9, 2025
Merged via the queue into main with commit f242320 Jul 9, 2025
22 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/fix-6717 branch July 9, 2025 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConstructorProvider.Initalizer not populated for custom constructors
4 participants