Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 10, 2025

  • Understand the issue: Generate abstract method incorrectly adds async modifier
  • Fix the code in AbstractGenerateParameterizedMemberService.SignatureInfo.cs to prevent async modifier when isAbstract is true
  • Add test case to verify abstract methods returning Task don't get async modifier
  • Build and test the changes
  • Verify the fix works correctly
  • Add WorkItem attribute to test for issue tracking

Summary

Fixed the issue where "Generate abstract method" was incorrectly adding the async modifier to abstract methods that return Task-like types. Abstract methods cannot have the async modifier because they don't have a body (CS1994 error).

Changes Made:

  1. AbstractGenerateParameterizedMemberService.SignatureInfo.cs: Modified line 108 to only apply the async modifier when the method is NOT abstract AND returns a Task-like type
  2. GenerateMethodTests.cs: Added test TestGenerateAbstractMethodReturningTask with WorkItem attribute to verify abstract methods returning Task don't get the async modifier

The fix ensures that WithAsync() is only set to true when !isAbstract && knownTypes.IsTaskLike(returnType), preventing the CS1994 error.

Original prompt

This section details on the original issue you should resolve

<issue_title>"Generate abstract method" adds disallowed async modifier</issue_title>
<issue_description>Version Used: 18.0.0 Insiders [11104.47]

Steps to Reproduce:

Open the lightbulb menu on the identifier M2 and select "Generate abstract method 'M2'".

abstract class C
{
    async Task M()
    {
        await M2();
    }
}

Expected Behavior:
A valid abstract method declaration is inserted:

protected abstract Task M2();

Actual Behavior:
The async modifier is added to the method declaration, resulting in error CS1994

protected abstract async Task M2(); // CS1994: The 'async' modifier can only be used in methods that have a body.
```</issue_description>

<agent_instructions>Fix is likely in src\Analyzers\Core\CodeFixes\GenerateParameterizedMember\AbstractGenerateParameterizedMemberService.SignatureInfo.cs

Please add tests in src\Analyzers\CSharp\Tests\GenerateMethod\GenerateMethodTests.cs</agent_instructions>

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

<comments>
</comments>

Fixes #80628


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

Only add async modifier when method is not abstract and returns Task-like type

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
}
""", new(parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp14)));

[Fact]
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Please add a WorkItem("#80628") so we can track which issue this is a test for.

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 in commit cfe1b13.

Copilot AI changed the title [WIP] Fix async modifier error in generated abstract method Fix "Generate abstract method" adding disallowed async modifier Oct 10, 2025
Copilot finished work on behalf of CyrusNajmabadi October 10, 2025 09:46
Copilot AI requested a review from CyrusNajmabadi October 10, 2025 09:46
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot finished work on behalf of CyrusNajmabadi October 10, 2025 09:51
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review October 10, 2025 10:00
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 10, 2025 10:00
@CyrusNajmabadi
Copy link
Member

@davidwengier ptal.

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (rebase) October 10, 2025 10:42
@CyrusNajmabadi CyrusNajmabadi merged commit 6a3d295 into main Oct 10, 2025
24 of 25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 10, 2025
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 abstract method" adds disallowed async modifier

3 participants