Skip to content

Add predicate version of IOperation.Descendants and DescendantsAndSelf - #85898

Open
SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:feature/53621-ioperation-descendants-predicate
Open

SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:feature/53621-ioperation-descendants-predicate

Conversation

@SkyDevLab

@SkyDevLab SkyDevLab commented Oct 2, 2026 •

Copy link
Copy Markdown

Fixes #53621

Summary

Adds overloads for IOperation.Descendants and IOperation.DescendantsAndSelf accepting an optional Func<IOperation, bool>? descendIntoChildren predicate, as approved in API review (#53621).

API Changes

namespace Microsoft.CodeAnalysis.Operations
{
    public static class OperationExtensions
    {
        public static IEnumerable<IOperation> Descendants(this IOperation? operation, Func<IOperation, bool>? descendIntoChildren);
        public static IEnumerable<IOperation> DescendantsAndSelf(this IOperation? operation, Func<IOperation, bool>? descendIntoChildren);
    }
}

Details

  • Updates internal OperationExtensions.Descendants traversal to check descendIntoChildren(operation) on the root (yielding the root if includeSelf: true, but pruning child traversal if false), and descendIntoChildren(current) before pushing children of traversed operations.
  • Preserves existing behavior when descendIntoChildren is null (the parameterless overloads forward descendIntoChildren: null).
  • Registers public symbols in src/Compilers/Core/Portable/PublicAPI.Unshipped.txt.
  • Adds unit tests in OperationTests.cs verifying null operation handling, null predicate equivalence, lambda/local function pruning, and root pruning semantics.
Microsoft Reviewers: Open in CodeFlow

Add Descendants and DescendantsAndSelf overloads taking a descendIntoChildren predicate to OperationExtensions, enabling callers to prune operation tree traversal.

Fixes dotnet#53621
@SkyDevLab
SkyDevLab requested review from a team as code owners October 2, 2026 18:43
@dotnet-policy-service dotnet-policy-service Bot added Community The pull request was submitted by a contributor who is not a Microsoft employee. Needs API Review Needs to be reviewed by the API review council labels Oct 2, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR modifies public API files. Please follow the instructions at https://github.com/dotnet/roslyn/blob/main/docs/contributing/API%20Review%20Process.md for ensuring all public APIs are reviewed before merging.

Comment on lines +430 to +436
// The anonymous function itself is yielded
Assert.Contains(prunedDescendants, op => op is IAnonymousFunctionOperation);
// But operations inside the lambda (like literal '2') are pruned
Assert.DoesNotContain(prunedDescendants, op => op is ILiteralOperation { ConstantValue: { HasValue: true, Value: 2 } });
// And operations outside the lambda (literal '1' and '3') are present
Assert.Contains(prunedDescendants, op => op is ILiteralOperation { ConstantValue: { HasValue: true, Value: 1 } });
Assert.Contains(prunedDescendants, op => op is ILiteralOperation { ConstantValue: { HasValue: true, Value: 3 } });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's just directly assert the operations in the list.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to directly assert the exact sequence of OperationKinds in prunedDescendants.

This branch has not been deployed

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

Labels

Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. Needs API Review Needs to be reviewed by the API review council

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add predicate version of IOperation.Descendants

2 participants