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

Report binding error for typed LINQ query on a type expression #22412

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,5 +2523,51 @@ static void Main()
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "a").WithArguments("a").WithLocation(10, 43)
);
}

[Fact, WorkItem(21484, "https://github.com/dotnet/roslyn/issues/21484")]
public void QueryOnTypeExpression()
{
var code = @"
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void M<T>() 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<int> select 3;
var query4 = from d in IEnumerable<int> 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<object>(IEnumerable)'
// var query1 = from object a in IEnumerable select 1;
Diagnostic(ErrorCode.ERR_ObjectRequired, "from object a in IEnumerable").WithArguments("System.Linq.Enumerable.Cast<object>(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<int>(IEnumerable)'
// var query3 = from int c in IEnumerable<int> select 3;
Diagnostic(ErrorCode.ERR_ObjectRequired, "from int c in IEnumerable<int>").WithArguments("System.Linq.Enumerable.Cast<int>(System.Collections.IEnumerable)").WithLocation(13, 22),
// (14,49): error CS1936: Could not find an implementation of the query pattern for source type 'IEnumerable<int>'. 'Select' not found.
// var query4 = from d in IEnumerable<int> select 4;
Diagnostic(ErrorCode.ERR_QueryNoProvider, "select 4").WithArguments("System.Collections.Generic.IEnumerable<int>", "Select").WithLocation(14, 49),
// (16,22): error CS0120: An object reference is required for the non-static field, method, or property 'Enumerable.Cast<object>(IEnumerable)'
// var query5 = from object d in T select 5;
Diagnostic(ErrorCode.ERR_ObjectRequired, "from object d in T").WithArguments("System.Linq.Enumerable.Cast<object>(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)
);
}
}
}