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

Binder Check for Unbound Generics in Methods #45033

Merged
merged 10 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 5 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5942,9 +5942,9 @@ private BoundExpression BindMemberAccessWithBoundLeft(
options |= LookupOptions.MustBeInvocableIfMember;
}

var typeArgumentsSyntax = right.Kind() == SyntaxKind.GenericName ? ((GenericNameSyntax)right).TypeArgumentList.Arguments : default(SeparatedSyntaxList<TypeSyntax>);
SeparatedSyntaxList<TypeSyntax> typeArgumentsSyntax = right is GenericNameSyntax genericName ? genericName.TypeArgumentList.Arguments : default;
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
bool rightHasTypeArguments = typeArgumentsSyntax.Count > 0;
var typeArguments = rightHasTypeArguments ? BindTypeArguments(typeArgumentsSyntax, diagnostics) : default(ImmutableArray<TypeWithAnnotations>);
ImmutableArray<TypeWithAnnotations> typeArguments = rightHasTypeArguments ? BindTypeArguments(typeArgumentsSyntax, diagnostics) : default;

// A member-access consists of a primary-expression, a predefined-type, or a
// qualified-alias-member, followed by a "." token, followed by an identifier,
Expand Down Expand Up @@ -6207,7 +6207,9 @@ private BoundExpression BindInstanceMemberAccess(
lookupResult,
flags);

if (!boundMethodGroup.HasErrors && boundMethodGroup.ResultKind == LookupResultKind.Empty && typeArgumentsSyntax.Any(SyntaxKind.OmittedTypeArgument))
if (!boundMethodGroup.HasErrors && (boundMethodGroup.ResultKind == LookupResultKind.Empty ||
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
(right is GenericNameSyntax genericNameRight && !IsUnboundTypeAllowed(genericNameRight))) &&
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
typeArgumentsSyntax.Any(SyntaxKind.OmittedTypeArgument))
{
Error(diagnostics, ErrorCode.ERR_BadArity, node, rightName, MessageID.IDS_MethodGroup.Localize(), typeArgumentsSyntax.Count);
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@
<data name="ERR_AttributeCantBeGeneric" xml:space="preserve">
<value>Cannot apply attribute class '{0}' because it is generic</value>
</data>
<data name="ERR_DuplicateBound" xml:space="preserve">
<data name="ERR_DuplicateBound" xml:space="preserve">
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
<value>Duplicate constraint '{0}' for type parameter '{1}'</value>
</data>
<data name="ERR_ClassBoundNotFirst" xml:space="preserve">
Expand Down
32 changes: 16 additions & 16 deletions src/Compilers/CSharp/Test/Semantic/Semantics/BindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3519,7 +3519,7 @@ public void Test()
// (13,27): error CS0305: Using the generic method group 'ExtensionMethod0' requires 1 type arguments
// var omittedArg0 = "string literal".ExtensionMethod0<>();
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod0<>").WithArguments("ExtensionMethod0", "method group", "1").WithLocation(13, 27),
// (13,44): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (13,44): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no accessible extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var omittedArg0 = "string literal".ExtensionMethod0<>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod0<>").WithArguments("string", "ExtensionMethod0").WithLocation(13, 44),
// (14,27): error CS0305: Using the generic method group 'ExtensionMethod1' requires 1 type arguments
Expand All @@ -3528,55 +3528,55 @@ public void Test()
// (15,27): error CS0305: Using the generic method group 'ExtensionMethod2' requires 1 type arguments
// var omittedArg2 = "string literal".ExtensionMethod2<>();
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod2<>").WithArguments("ExtensionMethod2", "method group", "1").WithLocation(15, 27),
// (15,44): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (15,44): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no accessible extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var omittedArg2 = "string literal".ExtensionMethod2<>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod2<>").WithArguments("string", "ExtensionMethod2").WithLocation(15, 44),
// (17,31): error CS0305: Using the generic method group 'ExtensionMethod0' requires 1 type arguments
// var omittedArgFunc0 = "string literal".ExtensionMethod0<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod0<>").WithArguments("ExtensionMethod0", "method group", "1").WithLocation(17, 31),
// (17,48): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (17,48): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no accessible extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var omittedArgFunc0 = "string literal".ExtensionMethod0<>;
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod0<>").WithArguments("string", "ExtensionMethod0").WithLocation(17, 48),
// (18,31): error CS0305: Using the generic method group 'ExtensionMethod1' requires 1 type arguments
// var omittedArgFunc1 = "string literal".ExtensionMethod1<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod1<>").WithArguments("ExtensionMethod1", "method group", "1").WithLocation(18, 31),
// (18,13): error CS0815: Cannot assign method group to an implicitly-typed variable
// var omittedArgFunc1 = "string literal".ExtensionMethod1<>;
Diagnostic(ErrorCode.ERR_ImplicitlyTypedVariableAssignedBadValue, @"omittedArgFunc1 = ""string literal"".ExtensionMethod1<>").WithArguments("method group").WithLocation(18, 13),
// (18,31): error CS0305: Using the generic method group 'ExtensionMethod1' requires 1 type arguments
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
// var omittedArgFunc1 = "string literal".ExtensionMethod1<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod1<>").WithArguments("ExtensionMethod1", "method group", "1").WithLocation(18, 31),
// (19,31): error CS0305: Using the generic method group 'ExtensionMethod2' requires 1 type arguments
// var omittedArgFunc2 = "string literal".ExtensionMethod2<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod2<>").WithArguments("ExtensionMethod2", "method group", "1").WithLocation(19, 31),
// (19,48): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (19,48): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no accessible extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var omittedArgFunc2 = "string literal".ExtensionMethod2<>;
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod2<>").WithArguments("string", "ExtensionMethod2").WithLocation(19, 48),
// (21,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (21,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no accessible extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var moreArgs0 = "string literal".ExtensionMethod0<int>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod0<int>").WithArguments("string", "ExtensionMethod0").WithLocation(21, 42),
// (22,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod1' and no extension method 'ExtensionMethod1' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (22,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod1' and no accessible extension method 'ExtensionMethod1' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var moreArgs1 = "string literal".ExtensionMethod1<int, bool>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod1<int, bool>").WithArguments("string", "ExtensionMethod1").WithLocation(22, 42),
// (23,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (23,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no accessible extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var moreArgs2 = "string literal".ExtensionMethod2<int, bool, string>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod2<int, bool, string>").WithArguments("string", "ExtensionMethod2").WithLocation(23, 42),
// (25,42): error CS0411: The type arguments for method 'FooExtensions.ExtensionMethod1<T>(object)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
// var lessArgs1 = "string literal".ExtensionMethod1();
Diagnostic(ErrorCode.ERR_CantInferMethTypeArgs, "ExtensionMethod1").WithArguments("FooExtensions.ExtensionMethod1<T>(object)").WithLocation(25, 42),
// (26,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (26,42): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no accessible extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var lessArgs2 = "string literal".ExtensionMethod2<int>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod2<int>").WithArguments("string", "ExtensionMethod2").WithLocation(26, 42),
// (28,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound0' and no extension method 'ExtensionMethodNotFound0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (28,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound0' and no accessible extension method 'ExtensionMethodNotFound0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var nonExistingMethod0 = "string literal".ExtensionMethodNotFound0();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethodNotFound0").WithArguments("string", "ExtensionMethodNotFound0").WithLocation(28, 51),
// (29,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound1' and no extension method 'ExtensionMethodNotFound1' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (29,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound1' and no accessible extension method 'ExtensionMethodNotFound1' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var nonExistingMethod1 = "string literal".ExtensionMethodNotFound1<int>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethodNotFound1<int>").WithArguments("string", "ExtensionMethodNotFound1").WithLocation(29, 51),
// (30,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound2' and no extension method 'ExtensionMethodNotFound2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (30,51): error CS1061: 'string' does not contain a definition for 'ExtensionMethodNotFound2' and no accessible extension method 'ExtensionMethodNotFound2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// var nonExistingMethod2 = "string literal".ExtensionMethodNotFound2<int, string>();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethodNotFound2<int, string>").WithArguments("string", "ExtensionMethodNotFound2").WithLocation(30, 51),
// (32,51): error CS0305: Using the generic method group 'ExtensionMethod0' requires 1 type arguments
// System.Func<object> delegateConversion0 = "string literal".ExtensionMethod0<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod0<>").WithArguments("ExtensionMethod0", "method group", "1").WithLocation(32, 51),
// (32,68): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (32,68): error CS1061: 'string' does not contain a definition for 'ExtensionMethod0' and no accessible extension method 'ExtensionMethod0' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// System.Func<object> delegateConversion0 = "string literal".ExtensionMethod0<>;
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod0<>").WithArguments("string", "ExtensionMethod0").WithLocation(32, 68),
// (33,51): error CS0305: Using the generic method group 'ExtensionMethod1' requires 1 type arguments
Expand All @@ -3588,7 +3588,7 @@ public void Test()
// (34,51): error CS0305: Using the generic method group 'ExtensionMethod2' requires 1 type arguments
// System.Func<object> delegateConversion2 = "string literal".ExtensionMethod2<>;
Diagnostic(ErrorCode.ERR_BadArity, @"""string literal"".ExtensionMethod2<>").WithArguments("ExtensionMethod2", "method group", "1").WithLocation(34, 51),
// (34,68): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// (34,68): error CS1061: 'string' does not contain a definition for 'ExtensionMethod2' and no accessible extension method 'ExtensionMethod2' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
// System.Func<object> delegateConversion2 = "string literal".ExtensionMethod2<>;
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ExtensionMethod2<>").WithArguments("string", "ExtensionMethod2").WithLocation(34, 68));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Test/Semantic/Semantics/NameOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class Test<T>
// (28,20): error CS0103: The name 'List2' does not exist in the current context
// s = nameof(List2<>.Add);
Diagnostic(ErrorCode.ERR_NameNotInContext, "List2<>").WithArguments("List2").WithLocation(28, 20),
// (31,20): error CS8149: An alias-qualified name is not an expression.
// (31,20): error CS8083: An alias-qualified name is not an expression.
// s = nameof(global::Program); // not an expression
Diagnostic(ErrorCode.ERR_AliasQualifiedNameNotAnExpression, "global::Program").WithLocation(31, 20),
// (32,20): error CS0305: Using the generic type 'Test<T>' requires 1 type arguments
Expand All @@ -307,7 +307,7 @@ class Test<T>
// (33,20): error CS0841: Cannot use local variable 'b' before it is declared
// s = nameof(b); // cannot use before declaration
Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "b").WithArguments("b").WithLocation(33, 20),
// (35,20): error CS8150: Type parameters are not allowed on a method group as an argument to 'nameof'.
// (35,20): error CS8084: Type parameters are not allowed on a method group as an argument to 'nameof'.
// s = nameof(System.Linq.Enumerable.Select<int, int>); // type parameters not allowed on method group in nameof
Diagnostic(ErrorCode.ERR_NameofMethodGroupWithTypeParameters, "System.Linq.Enumerable.Select<int, int>").WithLocation(35, 20),
// (43,13): error CS0103: The name 'nameof' does not exist in the current context
Expand Down
Loading