Skip to content

Improve IOperation shape for a cast utilizing user-defined conversions - #85262

Merged
AlekseyTs merged 1 commit into
dotnet:mainfrom
AlekseyTs:Issue84215
Sep 16, 2026
Merged

AlekseyTs merged 1 commit into
dotnet:mainfrom
AlekseyTs:Issue84215

Conversation

@AlekseyTs

@AlekseyTs AlekseyTs commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #81781
Related to #85267.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings September 11, 2026 15:50
@AlekseyTs
AlekseyTs requested a review from a team as a code owner September 11, 2026 15:50
@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.

@AlekseyTs AlekseyTs changed the title Improve IOperation shape for a cast itilizing user-defined conversions Improve IOperation shape for a cast utilizing user-defined conversions Sep 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical conversion-path assertions and operation mapping failures remain, and the explicit operation shape needs correction.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Improves IOperation trees for explicit casts involving user-defined conversions, particularly nullable targets.

Changes:

  • Adjusts conversion grouping and implicitness.
  • Adds regression tests and updates operation-tree baselines.
  • Updates conversion-tree validation and return-type adjustment handling.
File summaries
File Review summary
src/Compilers/CSharp/Test/Semantic/Semantics/FunctionPointerTests.cs Updates expected operation-tree baselines.
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IConversionExpression.cs Moderate issue (1 vote): the outer explicit conversion does not expose the user-defined operator method as expected.
src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Critical issue (3 votes): valid return-type-adjustment nodes are treated as unexpected and can throw.
src/Compilers/CSharp/Portable/BoundTree/BoundConversion.cs Critical issue (3 votes): unconditional assertions abort a supported conversion path.
src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs Critical issue (3 votes): an unconditional assertion aborts a legal return-type-adjustment branch.
Review details

Suppressed comments (2)

src/Compilers/CSharp/Portable/BoundTree/BoundConversion.cs:136

  • This new invariant rejects a tree shape that the binder deliberately constructs: when UserDefinedReturnTypeAdjustment is inserted in CreateUserDefinedConversion, it becomes the operand of UserDefinedFinal, so operandAsConversion.Conversion.IsUserDefined is false. The previous assertion explicitly allowed that flag combination, and the added Cast_UserDefined_03 exercises it; in a DEBUG build this assertion fires while binding a valid program. Keep the adjustment form valid, or update the binder and operation-factory handling together.
                        Debug.Assert(Operand is BoundConversion operandAsConversion &&
                                     operandAsConversion.ConversionGroupOpt == ConversionGroupOpt &&
                                     operandAsConversion.WasCompilerGenerated &&
                                     operandAsConversion.Conversion.IsUserDefined &&
                                     operandAsConversion.Syntax == Syntax);

src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IConversionExpression.cs:3519

  • The cast syntax is mapped to this outer non-implicit operation, but the expected tree leaves OperatorMethod null on it and exposes S1.op_Implicit only on an implicit child. OperationMapBuilder records and returns the outer operation for GetOperation(castSyntax), so this does not meet the linked issue's stated result that the returned IConversionOperation carries the user-defined operator method. The explicit node should be built from the full conversion-group conversion and original operand (and the corresponding expectations should be updated).
IConversionOperation (TryCast: False, Unchecked) (OperationKind.Conversion, Type: S1?) (Syntax: '(S1?)10')
    Conversion: CommonConversion (Exists: True, IsIdentity: False, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
    Operand:
    IConversionOperation (TryCast: False, Unchecked) (OperatorMethod: S1 S1.op_Implicit(System.Int32 x)) (OperationKind.Conversion, Type: S1, IsImplicit) (Syntax: '(S1?)10')
  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Lite

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler Please review

1 similar comment
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler Please review

@jjonescz

jjonescz commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #84215
Related to #85267.

#84215 is a closed PR, not an issue. But I guess the issue is described in the description of that PR?

}
else
{
ExceptionUtilities.UnexpectedValue(boundConversion); // Unexpected tree shape

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.

Suggested change
ExceptionUtilities.UnexpectedValue(boundConversion); // Unexpected tree shape
throw ExceptionUtilities.UnexpectedValue(boundConversion); // Unexpected tree shape

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is no intent to throw here.

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.

Then it feels confusing to use this helper which by its name suggests an exception should be thrown. Consider replacing this with a simple assert instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then it feels confusing to use this helper which by its name suggests an exception should be thrown. Consider replacing this with a simple assert instead.

I prefer to leave the code as is. This helper is used in this manner in many other places.

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

But I guess the issue is described in the description of that PR?

Fixed

@AlekseyTs
AlekseyTs requested review from jjonescz and a lite review from Copilot September 15, 2026 13:50
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@jjonescz, @dotnet/roslyn-compiler Please review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Note

This error may be related to your runner configuration. You can now configure runners for Copilot code review separately from Copilot cloud agent by creating a copilot-code-review.yml file with your setup steps. Read the docs for details.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetOperation doesn't return IOperation node for a user-defined conversion in some scenarios

3 participants