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
Use lambda expression and method group signature in type inference #55786
Changes from 9 commits
2572169
3b3f294
9424c62
b3176ff
5da002f
763f5b5
7b21e1a
3fefaa3
dca307d
a0f8caf
821e1bc
6aa9504
aaf8881
7223b1b
f2f885d
450a4ac
a9ea853
af7cdd5
5875469
5a2911a
df2a2d9
772d7ed
24e29e9
890e3f3
63f5795
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,11 @@ internal partial class Binder | |
return CreateAnonymousFunctionConversion(syntax, source, conversion, isCast: isCast, conversionGroupOpt, destination, diagnostics); | ||
} | ||
|
||
if (conversion.Kind == ConversionKind.FunctionType) | ||
{ | ||
return CreateFunctionTypeConversion(syntax, source, conversion, isCast: isCast, conversionGroupOpt, destination, diagnostics); | ||
} | ||
|
||
if (conversion.IsStackAlloc) | ||
{ | ||
return CreateStackAllocConversion(syntax, source, conversion, isCast, conversionGroupOpt, destination, diagnostics); | ||
|
@@ -546,69 +551,67 @@ private BoundExpression ConvertSwitchExpression(BoundUnconvertedSwitchExpression | |
return finalConversion; | ||
} | ||
|
||
private BoundExpression CreateAnonymousFunctionConversion(SyntaxNode syntax, BoundExpression source, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, BindingDiagnosticBag diagnostics) | ||
private BoundExpression CreateFunctionTypeConversion(SyntaxNode syntax, BoundExpression source, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, BindingDiagnosticBag diagnostics) | ||
{ | ||
// We have a successful anonymous function conversion; rather than producing a node | ||
// which is a conversion on top of an unbound lambda, replace it with the bound | ||
// lambda. | ||
Debug.Assert(source.Kind is BoundKind.MethodGroup or BoundKind.UnboundLambda); | ||
Debug.Assert(syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType)); | ||
|
||
// UNDONE: Figure out what to do about the error case, where a lambda | ||
// UNDONE: is converted to a delegate that does not match. What to surface then? | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
var delegateType = source.GetInferredDelegateType(ref useSiteInfo); | ||
Debug.Assert(delegateType is { }); | ||
|
||
var unboundLambda = (UnboundLambda)source; | ||
if ((destination.SpecialType == SpecialType.System_Delegate || destination.IsNonGenericExpressionType()) && | ||
syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType)) | ||
if (source.Kind == BoundKind.UnboundLambda && | ||
destination.IsNonGenericExpressionType()) | ||
{ | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
var delegateType = unboundLambda.InferDelegateType(ref useSiteInfo); | ||
BoundLambda boundLambda; | ||
if (delegateType is { }) | ||
{ | ||
bool isExpressionTree = destination.IsNonGenericExpressionType(); | ||
if (isExpressionTree) | ||
{ | ||
delegateType = Compilation.GetWellKnownType(WellKnownType.System_Linq_Expressions_Expression_T).Construct(delegateType); | ||
delegateType.AddUseSiteInfo(ref useSiteInfo); | ||
} | ||
boundLambda = unboundLambda.Bind(delegateType, isExpressionTree); | ||
} | ||
else | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_CannotInferDelegateType, syntax.GetLocation()); | ||
delegateType = CreateErrorType(); | ||
boundLambda = unboundLambda.BindForErrorRecovery(); | ||
} | ||
diagnostics.AddRange(boundLambda.Diagnostics); | ||
var expr = createAnonymousFunctionConversion(syntax, source, boundLambda, conversion, isCast, conversionGroup, delegateType); | ||
conversion = Conversions.ClassifyConversionFromExpression(expr, destination, ref useSiteInfo); | ||
diagnostics.Add(syntax, useSiteInfo); | ||
return CreateConversion(syntax, expr, conversion, isCast, conversionGroup, destination, diagnostics); | ||
delegateType = Compilation.GetWellKnownType(WellKnownType.System_Linq_Expressions_Expression_T).Construct(delegateType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests where |
||
delegateType.AddUseSiteInfo(ref useSiteInfo); | ||
} | ||
|
||
conversion = Conversions.ClassifyConversionFromExpression(source, delegateType, ref useSiteInfo); | ||
BoundExpression expr; | ||
if (!conversion.Exists) | ||
{ | ||
GenerateImplicitConversionError(diagnostics, syntax, conversion, source, delegateType); | ||
expr = new BoundConversion(syntax, source, conversion, @checked: false, explicitCastInCode: isCast, conversionGroup, constantValueOpt: ConstantValue.NotAvailable, type: delegateType, hasErrors: true) { WasCompilerGenerated = source.WasCompilerGenerated }; | ||
} | ||
else | ||
{ | ||
#if DEBUG | ||
// Test inferring a delegate type for all callers. | ||
var discardedUseSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded; | ||
_ = unboundLambda.InferDelegateType(ref discardedUseSiteInfo); | ||
#endif | ||
var boundLambda = unboundLambda.Bind((NamedTypeSymbol)destination, isExpressionTree: destination.IsGenericOrNonGenericExpressionType(out _)); | ||
diagnostics.AddRange(boundLambda.Diagnostics); | ||
return createAnonymousFunctionConversion(syntax, source, boundLambda, conversion, isCast, conversionGroup, destination); | ||
expr = CreateConversion(syntax, source, conversion, isCast, conversionGroup, delegateType, diagnostics); | ||
} | ||
|
||
static BoundConversion createAnonymousFunctionConversion(SyntaxNode syntax, BoundExpression source, BoundLambda boundLambda, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination) | ||
conversion = Conversions.ClassifyConversionFromExpression(expr, destination, ref useSiteInfo); | ||
if (!conversion.Exists) | ||
{ | ||
return new BoundConversion( | ||
syntax, | ||
boundLambda, | ||
conversion, | ||
@checked: false, | ||
explicitCastInCode: isCast, | ||
conversionGroup, | ||
constantValueOpt: ConstantValue.NotAvailable, | ||
type: destination) | ||
{ WasCompilerGenerated = source.WasCompilerGenerated }; | ||
GenerateImplicitConversionError(diagnostics, syntax, conversion, source, destination); | ||
} | ||
|
||
diagnostics.Add(syntax, useSiteInfo); | ||
return CreateConversion(syntax, expr, conversion, isCast, conversionGroup, destination, diagnostics); | ||
} | ||
|
||
private BoundExpression CreateAnonymousFunctionConversion(SyntaxNode syntax, BoundExpression source, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, BindingDiagnosticBag diagnostics) | ||
{ | ||
// We have a successful anonymous function conversion; rather than producing a node | ||
// which is a conversion on top of an unbound lambda, replace it with the bound | ||
// lambda. | ||
|
||
// UNDONE: Figure out what to do about the error case, where a lambda | ||
// UNDONE: is converted to a delegate that does not match. What to surface then? | ||
|
||
var unboundLambda = (UnboundLambda)source; | ||
|
||
var boundLambda = unboundLambda.Bind((NamedTypeSymbol)destination, isExpressionTree: destination.IsGenericOrNonGenericExpressionType(out _)); | ||
diagnostics.AddRange(boundLambda.Diagnostics); | ||
return new BoundConversion( | ||
syntax, | ||
boundLambda, | ||
conversion, | ||
@checked: false, | ||
explicitCastInCode: isCast, | ||
conversionGroup, | ||
constantValueOpt: ConstantValue.NotAvailable, | ||
type: destination) | ||
{ WasCompilerGenerated = source.WasCompilerGenerated }; | ||
} | ||
|
||
private BoundExpression CreateMethodGroupConversion(SyntaxNode syntax, BoundExpression source, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, BindingDiagnosticBag diagnostics) | ||
|
@@ -627,29 +630,7 @@ private BoundExpression CreateMethodGroupConversion(SyntaxNode syntax, BoundExpr | |
hasErrors = true; | ||
} | ||
|
||
if (destination.SpecialType == SpecialType.System_Delegate && | ||
syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType)) | ||
{ | ||
// https://github.com/dotnet/roslyn/issues/52869: Avoid calculating the delegate type multiple times during conversion. | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
var delegateType = GetMethodGroupDelegateType(group, ref useSiteInfo); | ||
var expr = createMethodGroupConversion(syntax, group, conversion, isCast, conversionGroup, delegateType!, hasErrors); | ||
conversion = Conversions.ClassifyConversionFromExpression(expr, destination, ref useSiteInfo); | ||
diagnostics.Add(syntax, useSiteInfo); | ||
return CreateConversion(syntax, expr, conversion, isCast, conversionGroup, destination, diagnostics); | ||
} | ||
|
||
#if DEBUG | ||
// Test inferring a delegate type for all callers. | ||
var discardedUseSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded; | ||
_ = GetMethodGroupDelegateType(group, ref discardedUseSiteInfo); | ||
#endif | ||
return createMethodGroupConversion(syntax, group, conversion, isCast, conversionGroup, destination, hasErrors); | ||
|
||
static BoundConversion createMethodGroupConversion(SyntaxNode syntax, BoundMethodGroup group, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, bool hasErrors) | ||
{ | ||
return new BoundConversion(syntax, group, conversion, @checked: false, explicitCastInCode: isCast, conversionGroup, constantValueOpt: ConstantValue.NotAvailable, type: destination, hasErrors: hasErrors) { WasCompilerGenerated = group.WasCompilerGenerated }; | ||
} | ||
return new BoundConversion(syntax, group, conversion, @checked: false, explicitCastInCode: isCast, conversionGroup, constantValueOpt: ConstantValue.NotAvailable, type: destination, hasErrors: hasErrors) { WasCompilerGenerated = group.WasCompilerGenerated }; | ||
} | ||
|
||
private BoundExpression CreateStackAllocConversion(SyntaxNode syntax, BoundExpression source, Conversion conversion, bool isCast, ConversionGroup? conversionGroup, TypeSymbol destination, BindingDiagnosticBag diagnostics) | ||
|
@@ -814,6 +795,7 @@ private BoundMethodGroup FixMethodGroupWithTypeOrValue(BoundMethodGroup group, C | |
group.LookupSymbolOpt, | ||
group.LookupError, | ||
group.Flags, | ||
group.Signature, | ||
receiverOpt, //only change | ||
group.ResultKind); | ||
} | ||
|
@@ -1226,13 +1208,12 @@ static ErrorCode getRefMismatchErrorCode(TypeKind type) | |
TypeSymbol delegateOrFuncPtrType, | ||
BindingDiagnosticBag diagnostics) | ||
{ | ||
Debug.Assert(delegateOrFuncPtrType.SpecialType == SpecialType.System_Delegate || delegateOrFuncPtrType.TypeKind == TypeKind.Delegate || delegateOrFuncPtrType.TypeKind == TypeKind.FunctionPointer); | ||
|
||
Debug.Assert(Conversions.IsAssignableFromMulticastDelegate(delegateOrFuncPtrType) || delegateOrFuncPtrType.TypeKind == TypeKind.Delegate || delegateOrFuncPtrType.TypeKind == TypeKind.FunctionPointer); | ||
Debug.Assert(conversion.Method is object); | ||
MethodSymbol selectedMethod = conversion.Method; | ||
|
||
var location = syntax.Location; | ||
if (delegateOrFuncPtrType.SpecialType != SpecialType.System_Delegate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume you hunted down all the System_Delegate checks that were added earlier as part of this feature. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I've reviewed all cases where |
||
if (!Conversions.IsAssignableFromMulticastDelegate(delegateOrFuncPtrType)) | ||
{ | ||
if (!MethodIsCompatibleWithDelegateOrFunctionPointer(receiverOpt, isExtensionMethod, selectedMethod, delegateOrFuncPtrType, location, diagnostics) || | ||
MemberGroupFinalValidation(receiverOpt, selectedMethod, syntax, diagnostics, isExtensionMethod)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,18 +396,15 @@ internal BoundExpression BindToNaturalType(BoundExpression expression, BindingDi | |
private BoundExpression BindToInferredDelegateType(BoundExpression expr, BindingDiagnosticBag diagnostics) | ||
{ | ||
var syntax = expr.Syntax; | ||
CheckFeatureAvailability(syntax, MessageID.IDS_FeatureInferredDelegateType, diagnostics); | ||
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics); | ||
var delegateType = expr switch | ||
{ | ||
UnboundLambda unboundLambda => unboundLambda.InferDelegateType(ref useSiteInfo), | ||
BoundMethodGroup methodGroup => GetMethodGroupDelegateType(methodGroup, ref useSiteInfo), | ||
_ => throw ExceptionUtilities.UnexpectedValue(expr), | ||
}; | ||
var delegateType = expr.GetInferredDelegateType(ref useSiteInfo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding assertion here (only BoundMethodGroup or UnboundLambda) #Closed |
||
diagnostics.Add(syntax, useSiteInfo); | ||
if (delegateType is null) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_CannotInferDelegateType, syntax.GetLocation()); | ||
if (CheckFeatureAvailability(syntax, MessageID.IDS_FeatureInferredDelegateType, diagnostics)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_CannotInferDelegateType, syntax.GetLocation()); | ||
} | ||
delegateType = CreateErrorType(); | ||
} | ||
return GenerateConversionForAssignment(delegateType, expr, diagnostics); | ||
|
@@ -2656,28 +2653,20 @@ private static string GetName(ExpressionSyntax syntax) | |
|
||
// Given a list of arguments, create arrays of the bound arguments and the names of those | ||
// arguments. | ||
private void BindArgumentsAndNames(ArgumentListSyntax argumentListOpt, BindingDiagnosticBag diagnostics, AnalyzedArguments result, bool allowArglist = false, bool isDelegateCreation = false) | ||
private void BindArgumentsAndNames(BaseArgumentListSyntax argumentListOpt, BindingDiagnosticBag diagnostics, AnalyzedArguments result, bool allowArglist = false, bool isDelegateCreation = false) | ||
{ | ||
if (argumentListOpt != null) | ||
{ | ||
BindArgumentsAndNames(argumentListOpt.Arguments, diagnostics, result, allowArglist, isDelegateCreation: isDelegateCreation); | ||
} | ||
} | ||
|
||
private void BindArgumentsAndNames(BracketedArgumentListSyntax argumentListOpt, BindingDiagnosticBag diagnostics, AnalyzedArguments result) | ||
{ | ||
if (argumentListOpt != null) | ||
{ | ||
BindArgumentsAndNames(argumentListOpt.Arguments, diagnostics, result, allowArglist: false); | ||
} | ||
} | ||
|
||
private void BindArgumentsAndNames( | ||
SeparatedSyntaxList<ArgumentSyntax> arguments, | ||
BindingDiagnosticBag diagnostics, | ||
AnalyzedArguments result, | ||
bool allowArglist, | ||
bool isDelegateCreation = false) | ||
bool isDelegateCreation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could inline this method now (only one usage above from a small method) #Closed |
||
{ | ||
// Only report the first "duplicate name" or "named before positional" error, | ||
// so as to avoid "cascading" errors. | ||
|
@@ -2729,7 +2718,7 @@ private bool RefMustBeObeyed(bool isDelegateCreation, ArgumentSyntax argumentSyn | |
ref bool hadLangVersionError, | ||
ArgumentSyntax argumentSyntax, | ||
bool allowArglist, | ||
bool isDelegateCreation = false) | ||
bool isDelegateCreation) | ||
{ | ||
RefKind origRefKind = argumentSyntax.RefOrOutKeyword.Kind().GetRefKind(); | ||
// The old native compiler ignores ref/out in a delegate creation expression. | ||
|
@@ -4405,7 +4394,7 @@ private BoundExpression BindDelegateCreationExpression(SyntaxNode node, NamedTyp | |
{ | ||
var boundMethodGroup = new BoundMethodGroup( | ||
argument.Syntax, default, WellKnownMemberNames.DelegateInvokeName, ImmutableArray.Create(sourceDelegate.DelegateInvokeMethod), | ||
sourceDelegate.DelegateInvokeMethod, null, BoundMethodGroupFlags.None, argument, LookupResultKind.Viable); | ||
sourceDelegate.DelegateInvokeMethod, null, BoundMethodGroupFlags.None, signature: null, argument, LookupResultKind.Viable); | ||
if (!Conversions.ReportDelegateOrFunctionPointerMethodGroupDiagnostics(this, boundMethodGroup, type, diagnostics)) | ||
{ | ||
// If we could not produce a more specialized diagnostic, we report | ||
|
@@ -6486,7 +6475,8 @@ private BoundExpression MakeMemberAccessValue(BoundExpression expr, BindingDiagn | |
rightName, | ||
lookupResult.Symbols.All(s => s.Kind == SymbolKind.Method) ? lookupResult.Symbols.SelectAsArray(s_toMethodSymbolFunc) : ImmutableArray<MethodSymbol>.Empty, | ||
lookupResult, | ||
flags); | ||
flags, | ||
this); | ||
|
||
if (!boundMethodGroup.HasErrors && typeArgumentsSyntax.Any(SyntaxKind.OmittedTypeArgument)) | ||
{ | ||
|
@@ -6636,6 +6626,7 @@ private BoundExpression BindMemberAccessBadResult(BoundMethodGroup node) | |
methods.Length == 1 ? methods[0] : null, | ||
lookupError, | ||
flags: BoundMethodGroupFlags.None, | ||
signature: null, | ||
receiverOpt: boundLeft, | ||
resultKind: lookupKind, | ||
hasErrors: true); | ||
|
@@ -8512,10 +8503,11 @@ private ErrorPropertySymbol CreateErrorPropertySymbol(ImmutableArray<PropertySym | |
} | ||
|
||
#nullable enable | ||
internal NamedTypeSymbol? GetMethodGroupDelegateType(BoundMethodGroup node, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo) | ||
internal NamedTypeSymbol? GetMethodGroupDelegateType(BoundMethodGroup node, out MethodSymbol? method) | ||
{ | ||
if (GetUniqueSignatureFromMethodGroup(node) is { } method && | ||
GetMethodGroupOrLambdaDelegateType(method.RefKind, method.ReturnsVoid ? default : method.ReturnTypeWithAnnotations, method.ParameterRefKinds, method.ParameterTypesWithAnnotations, ref useSiteInfo) is { } delegateType) | ||
method = GetUniqueSignatureFromMethodGroup(node); | ||
if (method is { } && | ||
GetMethodGroupOrLambdaDelegateType(method.RefKind, method.ReturnsVoid ? default : method.ReturnTypeWithAnnotations, method.ParameterRefKinds, method.ParameterTypesWithAnnotations) is { } delegateType) | ||
{ | ||
return delegateType; | ||
} | ||
|
@@ -8603,8 +8595,7 @@ static bool isCandidateUnique(ref MethodSymbol? method, MethodSymbol candidate) | |
RefKind returnRefKind, | ||
TypeWithAnnotations returnTypeOpt, | ||
ImmutableArray<RefKind> parameterRefKinds, | ||
ImmutableArray<TypeWithAnnotations> parameterTypes, | ||
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo) | ||
ImmutableArray<TypeWithAnnotations> parameterTypes) | ||
{ | ||
Debug.Assert(parameterRefKinds.IsDefault || parameterRefKinds.Length == parameterTypes.Length); | ||
Debug.Assert(returnTypeOpt.Type?.IsVoidType() != true); // expecting !returnTypeOpt.HasType rather than System.Void | ||
|
@@ -8636,7 +8627,6 @@ static bool isCandidateUnique(ref MethodSymbol? method, MethodSymbol candidate) | |
if (wkDelegateType != WellKnownType.Unknown) | ||
{ | ||
var delegateType = Compilation.GetWellKnownType(wkDelegateType); | ||
delegateType.AddUseSiteInfo(ref useSiteInfo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caller of |
||
if (typeArguments.Length == 0) | ||
{ | ||
return delegateType; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intentional that we're not using this parameter's value? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't realized the parameter value was not used, but it makes sense since the conversion does include any additional state. I've kept the parameter for consistency with other
Create*Conversion()
methods and added an assert forconversion.Kind
.