Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

Error CS9293 was too generic when extension parameters were referenced from static members, giving "Cannot use extension parameter in this context" without explaining the static/instance mismatch.

Changes

  • New error CS9347: "Static members cannot refer to extension parameter '{0}'. Use an instance member instead."
  • Modified Binder_Expressions.cs to detect static member context and emit the specific error with simplified logic using extracted boolean variables for clarity
  • Error message format: The parameter name is shown without its type (e.g., 'p' instead of 'int p') for conciseness
  • Preserved CS9293 for other invalid contexts (parameter defaults, attributes, nested types)
  • Updated 13 existing tests to expect the new error where applicable

Example

public static class Extensions
{
    extension(object o)
    {
        static object M() => o;  // Now: CS9347 (was: CS9293)
        void M2(int x = o) { }   // Still: CS9293 (correct context)
    }
}

Before: "Cannot use extension parameter 'object o' in this context."
After: "Static members cannot refer to extension parameter 'o'. Use an instance member instead."

Original prompt

The error message for using the extension parameter in a static extension method is confusing: today it just says "CS9293: Cannot use an extension parameter in this context". Please add a dedicated message just for this static/instance mismatch, with a message along the lines of "static extensions cannot refer to the extension parameter, use an instance method instead".


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 9, 2025 19:20
- Added ERR_ExtensionParameterInStaticContext error code (CS9347)
- Added error message in CSharpResources.resx
- Updated xlf localization files
- Modified Binder_Expressions.cs to detect static member case specifically
- Updated existing tests to use new error code where applicable

Co-authored-by: 333fred <2371880+333fred@users.noreply.github.com>
- Fixed variable naming conflict in Binder_Expressions.cs
- Added new error code to ErrorFacts.cs IsBuildOnlyDiagnostic
- Added focused test to verify new error message for static context
- All extension tests pass (2064 tests)

Co-authored-by: 333fred <2371880+333fred@users.noreply.github.com>
- Renamed variable to isStaticContextInExtension for clarity
- Added comment explaining why parameter defaults and attributes use general error

Co-authored-by: 333fred <2371880+333fred@users.noreply.github.com>
Copilot AI changed the title [WIP] Add dedicated error message for static extension parameter misuse Add specific error for extension parameter usage in static members Dec 9, 2025
Copilot AI requested a review from 333fred December 9, 2025 19:41
@333fred 333fred marked this pull request as ready for review December 9, 2025 22:25
@333fred 333fred requested a review from a team as a code owner December 9, 2025 22:25
- Refactored complex condition using extracted variables for clarity
- Changed error message parameter from full parameter symbol to just parameter.Name
- Removed blank line before test method
- Updated all test expectations to match new error format (parameter name only, no type)

Co-authored-by: 333fred <2371880+333fred@users.noreply.github.com>
Copilot AI requested a review from 333fred December 10, 2025 01:36
@333fred
Copy link
Member

333fred commented Dec 11, 2025

@jcouv @jjonescz for another review please

jcouv
jcouv previously approved these changes Dec 11, 2025
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (commit 8)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 11, 2025

@333fred, @jcouv Not sure who assigned this work to copilot. Is there a specific GitHub issue that this PR is trying to address? #Closed

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 11, 2025

Done with review pass (commit 8) #Closed

@jcouv jcouv dismissed their stale review December 11, 2025 21:09

Other review identified concern I'd missed

@333fred
Copy link
Member

333fred commented Dec 12, 2025

Is there a specific GitHub issue that this PR is trying to address?

It was in response to a feedback tweet, not to a filed bug.

@333fred
Copy link
Member

333fred commented Dec 12, 2025

@AlekseyTs @jcouv I reverted Binder_Extensions.cs to before this PR, then did the suggested splitting of the condition inside the existing check. I'm going to follow up with a simplification of the outer pattern after this PR.

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 12, 2025

Done with review pass (commit 9) #Closed

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 10)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (commit 10)

@333fred 333fred merged commit 5210072 into main Dec 15, 2025
25 checks passed
@333fred 333fred deleted the copilot/fix-static-extension-error-message branch December 15, 2025 19:00
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 15, 2025
@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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants