Skip to content
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

Account for delegate creation inside an explicitly-converted tuple expression #64882

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,14 @@ private IOperation CreateBoundConversionOperation(BoundConversion boundConversio
// overload resolution succeeded. The resulting method could be invalid for other reasons, but we don't
// hide the resolved method.
IOperation target = CreateDelegateTargetOperation(boundConversion);

// If this was an explicit tuple expression conversion, such as ((Action, int))(M, 1), we will be "explicit", because the
// original conversion was explicit in code, but the syntax node for this delegate creation and the nested method group will
// be the same. We therefore need to mark this node as implicit to ensure we don't have two explicit nodes for the same syntax.
Debug.Assert(isImplicit || target.Syntax != syntax || target.IsImplicit || boundConversion.ConversionGroupOpt != null);

isImplicit = isImplicit || (target.Syntax == syntax && !target.IsImplicit);

return new DelegateCreationOperation(target, _semanticModel, syntax, type, isImplicit);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1998,5 +1998,37 @@ void M(Action a1, Action a2, Action a3)

VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(source, expectedFlowGraph, expectedDiagnostics);
}

[Fact, WorkItem(64774, "https://github.com/dotnet/roslyn/issues/64774")]
public void ExplicitCastOnTuple()
{
var code = """
using System;

(int i, Action invoke) = /*<bind>*/((int, Action))(1, M)/*</bind>*/;

void M() {}
""";

var expectedOperationTree = """
IConversionOperation (TryCast: False, Unchecked) (OperationKind.Conversion, Type: (System.Int32, System.Action)) (Syntax: '((int, Action))(1, M)')
Conversion: CommonConversion (Exists: True, IsIdentity: False, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
Operand:
ITupleOperation (OperationKind.Tuple, Type: (System.Int32, System.Action M)) (Syntax: '(1, M)')
NaturalType: null
Elements(2):
IConversionOperation (TryCast: False, Unchecked) (OperationKind.Conversion, Type: System.Int32, Constant: 1, IsImplicit) (Syntax: '1')
Conversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
Operand:
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1) (Syntax: '1')
IDelegateCreationOperation (OperationKind.DelegateCreation, Type: System.Action, IsImplicit) (Syntax: 'M')
Target:
IMethodReferenceOperation: void M() (OperationKind.MethodReference, Type: null) (Syntax: 'M')
Instance Receiver:
IInstanceReferenceOperation (ReferenceKind: ContainingTypeInstance) (OperationKind.InstanceReference, Type: Program, IsImplicit) (Syntax: 'M')

""";
VerifyOperationTreeAndDiagnosticsForTest<CastExpressionSyntax>(code, expectedOperationTree, DiagnosticDescription.None);
}
}
}