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

Params Collections - Adjust binding in presence of dynamic arguments #71421

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -4532,6 +4532,11 @@ static bool Goo(int x, object y, object z)
{
return true;
}

static bool Goo(long x, object y, object z)
{
return true;
}
}
""";

Expand Down Expand Up @@ -4568,7 +4573,19 @@ class C<T>
{
}
}


int this[long x, object s, object d]
{
get
{
return 0;
}

set
{
}
}

void Goo(dynamic xx)
{
var y = this[x: xx, s: "", d: (object)""];
Expand Down Expand Up @@ -4681,7 +4698,12 @@ static bool Goo(object y, int x, object z)
{
return true;
}
}

static bool Goo(object y, long x, object z)
{
return true;
}
}
""";

await VerifyCS.VerifyCodeFixAsync(source, source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private BoundExpression CheckValue(BoundExpression expr, BindValueKind valueKind
{
var methodGroup = (BoundMethodGroup)expr;
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics);
var resolution = this.ResolveMethodGroup(methodGroup, analyzedArguments: null, isMethodGroupConversion: false, useSiteInfo: ref useSiteInfo);
var resolution = this.ResolveMethodGroup(methodGroup, analyzedArguments: null, useSiteInfo: ref useSiteInfo, options: OverloadResolution.Options.None);
diagnostics.Add(expr.Syntax, useSiteInfo);
Symbol otherSymbol = null;
bool resolvedToMethodGroup = resolution.MethodGroup != null;
Expand Down
10 changes: 9 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,15 @@ private BoundAttribute BindAttributeCore(AttributeSyntax node, NamedTypeSymbol a
out var memberResolutionResult,
out var candidateConstructors,
allowProtectedConstructorsOfBaseType: true,
suppressUnsupportedRequiredMembersError: false);
out CompoundUseSiteInfo<AssemblySymbol> overloadResolutionUseSiteInfo);

ReportConstructorUseSiteDiagnostics(node.Location, diagnostics, suppressUnsupportedRequiredMembersError: false, in overloadResolutionUseSiteInfo);

if (memberResolutionResult.IsNotNull)
{
this.CheckAndCoerceArguments<MethodSymbol>(memberResolutionResult, analyzedArguments.ConstructorArguments, diagnostics, receiver: null, invokedAsExtensionMethod: false);
}

attributeConstructor = memberResolutionResult.Member;
expanded = memberResolutionResult.Resolution == MemberResolutionKind.ApplicableInExpandedForm;
argsToParamsOpt = memberResolutionResult.Result.ArgsToParamsOpt;
Expand Down
113 changes: 58 additions & 55 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ private BoundCollectionExpression ConvertCollectionExpression(
return BindCollectionExpressionForErrorRecovery(node, targetType, diagnostics);
}

var syntax = (ExpressionSyntax)node.Syntax;
var syntax = node.Syntax;
if (LocalRewriter.IsAllocatingRefStructCollectionExpression(node, collectionTypeKind, elementType, Compilation))
{
diagnostics.Add(node.HasSpreadElements(out _, out _)
Expand Down Expand Up @@ -1733,83 +1733,86 @@ private bool MemberGroupFinalValidationAccessibilityChecks(BoundExpression? rece
{
// Perform final validation of the method to be invoked.

Debug.Assert(memberSymbol.Kind != SymbolKind.Method ||
Debug.Assert(memberSymbol is not MethodSymbol { MethodKind: not MethodKind.Constructor } ||
memberSymbol.CanBeReferencedByName);
//note that the same assert does not hold for all properties. Some properties and (all indexers) are not referenceable by name, yet
//their binding brings them through here, perhaps needlessly.

if (IsTypeOrValueExpression(receiverOpt))
{
// TypeOrValue expression isn't replaced only if the invocation is late bound, in which case it can't be extension method.
// None of the checks below apply if the receiver can't be classified as a type or value.
Debug.Assert(!invokedAsExtensionMethod);
}
else if (!memberSymbol.RequiresInstanceReceiver())
if (receiverOpt != null || memberSymbol is not MethodSymbol { MethodKind: MethodKind.Constructor })
{
Debug.Assert(!invokedAsExtensionMethod || (receiverOpt != null));

if (invokedAsExtensionMethod)
if (IsTypeOrValueExpression(receiverOpt))
{
// TypeOrValue expression isn't replaced only if the invocation is late bound, in which case it can't be extension method.
// None of the checks below apply if the receiver can't be classified as a type or value.
Debug.Assert(!invokedAsExtensionMethod);
}
else if (!memberSymbol.RequiresInstanceReceiver())
{
if (IsMemberAccessedThroughType(receiverOpt))
Debug.Assert(!invokedAsExtensionMethod || (receiverOpt != null));

if (invokedAsExtensionMethod)
{
if (receiverOpt.Kind == BoundKind.QueryClause)
if (IsMemberAccessedThroughType(receiverOpt))
{
if (receiverOpt.Kind == BoundKind.QueryClause)
{
RoslynDebug.Assert(receiverOpt.Type is object);
// Could not find an implementation of the query pattern for source type '{0}'. '{1}' not found.
diagnostics.Add(ErrorCode.ERR_QueryNoProvider, node.Location, receiverOpt.Type, memberSymbol.Name);
}
else
{
// An object reference is required for the non-static field, method, or property '{0}'
diagnostics.Add(ErrorCode.ERR_ObjectRequired, node.Location, memberSymbol);
}
return true;
}
}
else if (!WasImplicitReceiver(receiverOpt) && IsMemberAccessedThroughVariableOrValue(receiverOpt))
{
if (this.Flags.Includes(BinderFlags.CollectionInitializerAddMethod))
{
diagnostics.Add(ErrorCode.ERR_InitializerAddHasWrongSignature, node.Location, memberSymbol);
}
else if (node.Kind() == SyntaxKind.AwaitExpression && memberSymbol.Name == WellKnownMemberNames.GetAwaiter)
{
RoslynDebug.Assert(receiverOpt.Type is object);
// Could not find an implementation of the query pattern for source type '{0}'. '{1}' not found.
diagnostics.Add(ErrorCode.ERR_QueryNoProvider, node.Location, receiverOpt.Type, memberSymbol.Name);
diagnostics.Add(ErrorCode.ERR_BadAwaitArg, node.Location, receiverOpt.Type);
}
else
{
// An object reference is required for the non-static field, method, or property '{0}'
diagnostics.Add(ErrorCode.ERR_ObjectRequired, node.Location, memberSymbol);
diagnostics.Add(ErrorCode.ERR_ObjectProhibited, node.Location, memberSymbol);
}
return true;
}
}
else if (!WasImplicitReceiver(receiverOpt) && IsMemberAccessedThroughVariableOrValue(receiverOpt))
else if (IsMemberAccessedThroughType(receiverOpt))
{
if (this.Flags.Includes(BinderFlags.CollectionInitializerAddMethod))
{
diagnostics.Add(ErrorCode.ERR_InitializerAddHasWrongSignature, node.Location, memberSymbol);
}
else if (node.Kind() == SyntaxKind.AwaitExpression && memberSymbol.Name == WellKnownMemberNames.GetAwaiter)
{
RoslynDebug.Assert(receiverOpt.Type is object);
diagnostics.Add(ErrorCode.ERR_BadAwaitArg, node.Location, receiverOpt.Type);
}
else
{
diagnostics.Add(ErrorCode.ERR_ObjectProhibited, node.Location, memberSymbol);
}
diagnostics.Add(ErrorCode.ERR_ObjectRequired, node.Location, memberSymbol);
return true;
}
}
else if (IsMemberAccessedThroughType(receiverOpt))
{
diagnostics.Add(ErrorCode.ERR_ObjectRequired, node.Location, memberSymbol);
return true;
}
else if (WasImplicitReceiver(receiverOpt))
{
if (InFieldInitializer && !ContainingType!.IsScriptClass || InConstructorInitializer || InAttributeArgument)
else if (WasImplicitReceiver(receiverOpt))
{
SyntaxNode errorNode = node;
if (node.Parent != null && node.Parent.Kind() == SyntaxKind.InvocationExpression)
if (InFieldInitializer && !ContainingType!.IsScriptClass || InConstructorInitializer || InAttributeArgument)
{
errorNode = node.Parent;
}
SyntaxNode errorNode = node;
if (node.Parent != null && node.Parent.Kind() == SyntaxKind.InvocationExpression)
{
errorNode = node.Parent;
}

ErrorCode code = InFieldInitializer ? ErrorCode.ERR_FieldInitRefNonstatic : ErrorCode.ERR_ObjectRequired;
diagnostics.Add(code, errorNode.Location, memberSymbol);
return true;
}
ErrorCode code = InFieldInitializer ? ErrorCode.ERR_FieldInitRefNonstatic : ErrorCode.ERR_ObjectRequired;
diagnostics.Add(code, errorNode.Location, memberSymbol);
return true;
}

// If we could access the member through implicit "this" the receiver would be a BoundThisReference.
// If it is null it means that the instance member is inaccessible.
if (receiverOpt == null || ContainingMember().IsStatic)
{
Error(diagnostics, ErrorCode.ERR_ObjectRequired, node, memberSymbol);
return true;
// If we could access the member through implicit "this" the receiver would be a BoundThisReference.
// If it is null it means that the instance member is inaccessible.
if (receiverOpt == null || ContainingMember().IsStatic)
{
Error(diagnostics, ErrorCode.ERR_ObjectRequired, node, memberSymbol);
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ private BoundExpression MakeDeconstructInvocationExpression(
// Those placeholders are also recorded in the outVar for easy access below, by the `SetInferredType` call on the outVar nodes.
BoundExpression result = BindMethodGroupInvocation(
rightSyntax, rightSyntax, methodName, (BoundMethodGroup)memberAccess, analyzedArguments, diagnostics, queryClause: null,
allowUnexpandedForm: true, anyApplicableCandidates: out anyApplicableCandidates);
ignoreNormalFormIfHasValidParamsParameter: false, anyApplicableCandidates: out anyApplicableCandidates);

result.WasCompilerGenerated = true;

Expand Down
Loading
Loading