diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs index 84b3abdf4e88b..af37e7a429382 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs @@ -551,10 +551,18 @@ private bool MemberGroupFinalValidationAccessibilityChecks(BoundExpression recei if (invokedAsExtensionMethod) { - if (receiverOpt?.Kind == BoundKind.QueryClause && IsMemberAccessedThroughType(receiverOpt)) + if (IsMemberAccessedThroughType(receiverOpt)) { - // 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); + if (receiverOpt?.Kind == BoundKind.QueryClause) + { + // 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; } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs index 00e0ac2cba789..28499104b0cf8 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs @@ -2523,5 +2523,51 @@ static void Main() Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "a").WithArguments("a").WithLocation(10, 43) ); } + + [Fact, WorkItem(12052, "https://github.com/dotnet/roslyn/issues/12052")] + public void QueryOnTypeExpression() + { + var code = @" +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +class Program +{ + static void M() where T : IEnumerable + { + var query1 = from object a in IEnumerable select 1; + var query2 = from b in IEnumerable select 2; + + var query3 = from int c in IEnumerable select 3; + var query4 = from d in IEnumerable select 4; + + var query5 = from object d in T select 5; + var query6 = from d in T select 6; + } +} +"; + var comp = CreateCompilationWithMscorlibAndSystemCore(code); + comp.VerifyDiagnostics( + // (10,22): error CS0120: An object reference is required for the non-static field, method, or property 'Enumerable.Cast(IEnumerable)' + // var query1 = from object a in IEnumerable select 1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "from object a in IEnumerable").WithArguments("System.Linq.Enumerable.Cast(System.Collections.IEnumerable)").WithLocation(10, 22), + // (11,32): error CS1934: Could not find an implementation of the query pattern for source type 'IEnumerable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'b'. + // var query2 = from b in IEnumerable select 2; + Diagnostic(ErrorCode.ERR_QueryNoProviderCastable, "IEnumerable").WithArguments("System.Collections.IEnumerable", "Select", "b").WithLocation(11, 32), + // (13,22): error CS0120: An object reference is required for the non-static field, method, or property 'Enumerable.Cast(IEnumerable)' + // var query3 = from int c in IEnumerable select 3; + Diagnostic(ErrorCode.ERR_ObjectRequired, "from int c in IEnumerable").WithArguments("System.Linq.Enumerable.Cast(System.Collections.IEnumerable)").WithLocation(13, 22), + // (14,49): error CS1936: Could not find an implementation of the query pattern for source type 'IEnumerable'. 'Select' not found. + // var query4 = from d in IEnumerable select 4; + Diagnostic(ErrorCode.ERR_QueryNoProvider, "select 4").WithArguments("System.Collections.Generic.IEnumerable", "Select").WithLocation(14, 49), + // (16,22): error CS0120: An object reference is required for the non-static field, method, or property 'Enumerable.Cast(IEnumerable)' + // var query5 = from object d in T select 5; + Diagnostic(ErrorCode.ERR_ObjectRequired, "from object d in T").WithArguments("System.Linq.Enumerable.Cast(System.Collections.IEnumerable)").WithLocation(16, 22), + // (17,32): error CS1936: Could not find an implementation of the query pattern for source type 'T'. 'Select' not found. + // var query6 = from d in T select 6; + Diagnostic(ErrorCode.ERR_QueryNoProvider, "T").WithArguments("T", "Select").WithLocation(17, 32) + ); + } } }