/Src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs
C# | 2050 lines | 1855 code | 152 blank | 43 comment | 16 complexity | 482eaf29310b38b84269bda730a13cbc MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 2 3using System; 4using System.IO; 5using System.Linq; 6using Microsoft.CodeAnalysis.CSharp.Symbols; 7using Microsoft.CodeAnalysis.CSharp.Syntax; 8using Microsoft.CodeAnalysis.CSharp.Test.Utilities; 9using Roslyn.Test.Utilities; 10using Xunit; 11 12namespace Microsoft.CodeAnalysis.CSharp.UnitTests 13{ 14 public partial class QueryTests : CompilingTestBase 15 { 16 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 17 [Fact] 18 public void DegenerateQueryExpression() 19 { 20 var csSource = LINQ + @" 21class Query 22{ 23 public static void Main(string[] args) 24 { 25 List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7); 26 List1<int> r = from i in c select i; 27 if (ReferenceEquals(c, r)) throw new Exception(); 28 // List1<int> r = c.Select(i => i); 29 Console.WriteLine(r); 30 } 31}"; 32 CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]"); 33 } 34 35 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 36 [Fact] 37 public void QueryContinuation() 38 { 39 var csSource = LINQ + @" 40class Query 41{ 42 public static void Main(string[] args) 43 { 44 List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7); 45 List1<int> r = from i in c select i into q select q; 46 if (ReferenceEquals(c, r)) throw new Exception(); 47 // List1<int> r = c.Select(i => i); 48 Console.WriteLine(r); 49 } 50}"; 51 CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]"); 52 } 53 54 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 55 [Fact] 56 public void Select() 57 { 58 var csSource = LINQ + @" 59class Query 60{ 61 public static void Main(string[] args) 62 { 63 List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7); 64 List1<int> r = from i in c select i+1; 65 Console.WriteLine(r); 66 } 67}"; 68 CompileAndVerify(csSource, expectedOutput: "[2, 3, 4, 5, 6, 7, 8]"); 69 } 70 71 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 72 [Fact] 73 public void GroupBy01() 74 { 75 var csSource = LINQ + @" 76class Query 77{ 78 public static void Main(string[] args) 79 { 80 List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7); 81 var r = from i in c group i by i % 2; 82 Console.WriteLine(r); 83 } 84}"; 85 CompileAndVerify(csSource, expectedOutput: "[1:[1, 3, 5, 7], 0:[2, 4, 6]]"); 86 } 87 88 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 89 [Fact] 90 public void GroupBy02() 91 { 92 var csSource = LINQ + @" 93class Query 94{ 95 public static void Main(string[] args) 96 { 97 List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7); 98 var r = from i in c group 10+i by i % 2; 99 Console.WriteLine(r); 100 } 101}"; 102 CompileAndVerify(csSource, expectedOutput: "[1:[11, 13, 15, 17], 0:[12, 14, 16]]"); 103 } 104 105 [Fact] 106 public void Cast() 107 { 108 var csSource = LINQ + @" 109class Query 110{ 111 public static void Main(string[] args) 112 { 113 List1<object> c = new List1<object>(1, 2, 3, 4, 5, 6, 7); 114 List1<int> r = from int i in c select i; 115 Console.WriteLine(r); 116 } 117}"; 118 CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]"); 119 } 120 121 [Fact] 122 public void Where() 123 { 124 var csSource = LINQ + @" 125class Query 126{ 127 public static void Main(string[] args) 128 { 129 List1<object> c = new List1<object>(1, 2, 3, 4, 5, 6, 7); 130 List1<int> r = from int i in c where i < 5 select i; 131 Console.WriteLine(r); 132 } 133}"; 134 CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4]"); 135 } 136 137 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 138 [Fact] 139 public void FromJoinSelect() 140 { 141 var csSource = LINQ + @" 142class Query 143{ 144 public static void Main(string[] args) 145 { 146 List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7); 147 List1<int> c2 = new List1<int>(10, 30, 40, 50, 60, 70); 148 List1<int> r = from x1 in c1 149 join x2 in c2 on x1 equals x2/10 150 select x1+x2; 151 Console.WriteLine(r); 152 } 153}"; 154 CompileAndVerify(csSource, expectedOutput: "[11, 33, 44, 55, 77]"); 155 } 156 157 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 158 [Fact] 159 public void OrderBy() 160 { 161 var csSource = LINQ + @" 162class Query 163{ 164 public static void Main(string[] args) 165 { 166 List1<int> c = new List1<int>(28, 51, 27, 84, 27, 27, 72, 64, 55, 46, 39); 167 var r = 168 from i in c 169 orderby i/10 descending, i%10 170 select i; 171 Console.WriteLine(r); 172 } 173}"; 174 CompileAndVerify(csSource, expectedOutput: "[84, 72, 64, 51, 55, 46, 39, 27, 27, 27, 28]"); 175 } 176 177 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 178 [Fact] 179 public void GroupJoin() 180 { 181 var csSource = LINQ + @" 182class Query 183{ 184 public static void Main(string[] args) 185 { 186 List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7); 187 List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75); 188 List1<string> r = 189 from x1 in c1 190 join x2 in c2 on x1 equals x2 / 10 into g 191 select x1 + "":"" + g.ToString(); 192 Console.WriteLine(r); 193 } 194}"; 195 CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52], 7:[75]]"); 196 } 197 198 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 199 [Fact] 200 public void SelectMany01() 201 { 202 var csSource = LINQ + @" 203class Query 204{ 205 public static void Main(string[] args) 206 { 207 List1<int> c1 = new List1<int>(1, 2, 3); 208 List1<int> c2 = new List1<int>(10, 20, 30); 209 List1<int> r = from x in c1 from y in c2 select x + y; 210 Console.WriteLine(r); 211 } 212}"; 213 CompileAndVerify(csSource, expectedOutput: "[11, 21, 31, 12, 22, 32, 13, 23, 33]"); 214 } 215 216 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 217 [Fact] 218 public void SelectMany02() 219 { 220 var csSource = LINQ + @" 221class Query 222{ 223 public static void Main(string[] args) 224 { 225 List1<int> c1 = new List1<int>(1, 2, 3); 226 List1<int> c2 = new List1<int>(10, 20, 30); 227 List1<int> r = from x in c1 from int y in c2 select x + y; 228 Console.WriteLine(r); 229 } 230}"; 231 CompileAndVerify(csSource, expectedOutput: "[11, 21, 31, 12, 22, 32, 13, 23, 33]"); 232 } 233 234 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 235 [Fact] 236 public void Let01() 237 { 238 var csSource = LINQ + @" 239class Query 240{ 241 public static void Main(string[] args) 242 { 243 List1<int> c1 = new List1<int>(1, 2, 3); 244 List1<int> r1 = 245 from int x in c1 246 let g = x * 10 247 let z = g + x*100 248 select x + z; 249 System.Console.WriteLine(r1); 250 } 251}"; 252 CompileAndVerify(csSource, expectedOutput: "[111, 222, 333]"); 253 } 254 255 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 256 [Fact] 257 public void TransparentIdentifiers_FromLet() 258 { 259 var csSource = @" 260using C = List1<int>;" + LINQ + @" 261class Query 262{ 263 public static void Main(string[] args) 264 { 265 C c1 = new C(1, 2, 3); 266 C c2 = new C(10, 20, 30); 267 C c3 = new C(100, 200, 300); 268 C r1 = 269 from int x in c1 270 from int y in c2 271 from int z in c3 272 let g = x + y + z 273 where (x + y / 10 + z / 100) < 6 274 select g; 275 Console.WriteLine(r1); 276 } 277}"; 278 CompileAndVerify(csSource, expectedOutput: "[111, 211, 311, 121, 221, 131, 112, 212, 122, 113]"); 279 } 280 281 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 282 [Fact] 283 public void TransparentIdentifiers_Join01() 284 { 285 var csSource = @" 286using C = List1<int>;" + LINQ + @" 287class Query 288{ 289 public static void Main(string[] args) 290 { 291 C c1 = new C(1, 2, 3); 292 C c2 = new C(10, 20, 30); 293 C r1 = 294 from int x in c1 295 join y in c2 on x equals y/10 296 let z = x+y 297 select z; 298 Console.WriteLine(r1); 299 } 300}"; 301 CompileAndVerify(csSource, expectedOutput: "[11, 22, 33]"); 302 } 303 304 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 305 [Fact] 306 public void TransparentIdentifiers_Join02() 307 { 308 var csSource = LINQ + @" 309class Query 310{ 311 public static void Main(string[] args) 312 { 313 List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7); 314 List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75); 315 List1<string> r1 = from x1 in c1 316 join x2 in c2 on x1 equals x2 / 10 into g 317 where x1 < 7 318 select x1 + "":"" + g.ToString(); 319 Console.WriteLine(r1); 320 } 321}"; 322 CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52]]"); 323 } 324 325 [Fact] 326 public void CodegenBug() 327 { 328 var csSource = LINQ + @" 329class Query 330{ 331 public static void Main(string[] args) 332 { 333 List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7); 334 List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75); 335 336 List1<Tuple<int, List1<int>>> r1 = 337 c1 338 .GroupJoin(c2, x1 => x1, x2 => x2 / 10, (x1, g) => new Tuple<int, List1<int>>(x1, g)) 339 ; 340 341 Func1<Tuple<int, List1<int>>, bool> condition = (Tuple<int, List1<int>> TR1) => TR1.Item1 < 7; 342 List1<Tuple<int, List1<int>>> r2 = 343 r1 344 .Where(condition) 345 ; 346 Func1<Tuple<int, List1<int>>, string> map = (Tuple<int, List1<int>> TR1) => TR1.Item1.ToString() + "":"" + TR1.Item2.ToString(); 347 348 List1<string> r3 = 349 r2 350 .Select(map) 351 ; 352 string r4 = r3.ToString(); 353 Console.WriteLine(r4); 354 return; 355 } 356}"; 357 CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52]]"); 358 } 359 360 [Fact] 361 public void RangeVariables01() 362 { 363 var csSource = @" 364using C = List1<int>;" + LINQ + @" 365class Query 366{ 367 public static void Main(string[] args) 368 { 369 C c1 = new C(1, 2, 3); 370 C c2 = new C(10, 20, 30); 371 C c3 = new C(100, 200, 300); 372 C r1 = 373 from int x in c1 374 from int y in c2 375 from int z in c3 376 select x + y + z; 377 Console.WriteLine(r1); 378 } 379}"; 380 var compilation = CreateCompilationWithMscorlib(csSource); 381 compilation.VerifyDiagnostics(); 382 var tree = compilation.SyntaxTrees[0]; 383 var model = compilation.GetSemanticModel(tree); 384 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single(); 385 dynamic methodM = (MethodDeclarationSyntax)classC.Members[0]; 386 QueryExpressionSyntax q = methodM.Body.Statements[3].Declaration.Variables[0].Initializer.Value; 387 388 var info0 = model.GetQueryClauseInfo(q.FromClause); 389 var x = model.GetDeclaredSymbol(q.FromClause); 390 Assert.Equal(SymbolKind.RangeVariable, x.Kind); 391 Assert.Equal("x", x.Name); 392 Assert.Equal("Cast", info0.CastInfo.Symbol.Name); 393 Assert.NotEqual(MethodKind.ReducedExtension, ((IMethodSymbol)info0.CastInfo.Symbol).MethodKind); 394 Assert.Null(info0.OperationInfo.Symbol); 395 396 var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]); 397 var y = model.GetDeclaredSymbol(q.Body.Clauses[0]); 398 Assert.Equal(SymbolKind.RangeVariable, y.Kind); 399 Assert.Equal("y", y.Name); 400 Assert.Equal("Cast", info1.CastInfo.Symbol.Name); 401 Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name); 402 Assert.NotEqual(MethodKind.ReducedExtension, ((IMethodSymbol)info1.OperationInfo.Symbol).MethodKind); 403 404 var info2 = model.GetQueryClauseInfo(q.Body.Clauses[1]); 405 var z = model.GetDeclaredSymbol(q.Body.Clauses[1]); 406 Assert.Equal(SymbolKind.RangeVariable, z.Kind); 407 Assert.Equal("z", z.Name); 408 Assert.Equal("Cast", info2.CastInfo.Symbol.Name); 409 Assert.Equal("SelectMany", info2.OperationInfo.Symbol.Name); 410 411 var info3 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup); 412 Assert.NotNull(info3); 413 // what about info3's contents ??? 414 415 var xPyPz = (q.Body.SelectOrGroup as SelectClauseSyntax).Expression as BinaryExpressionSyntax; 416 var xPy = xPyPz.Left as BinaryExpressionSyntax; 417 Assert.Equal(x, model.GetSemanticInfoSummary(xPy.Left).Symbol); 418 Assert.Equal(y, model.GetSemanticInfoSummary(xPy.Right).Symbol); 419 Assert.Equal(z, model.GetSemanticInfoSummary(xPyPz.Right).Symbol); 420 } 421 422 [Fact] 423 public void RangeVariables02() 424 { 425 var csSource = @" 426using System; 427using System.Linq; 428class Query 429{ 430 public static void Main(string[] args) 431 { 432 var c1 = new int[] {1, 2, 3}; 433 var c2 = new int[] {10, 20, 30}; 434 var c3 = new int[] {100, 200, 300}; 435 var r1 = 436 from int x in c1 437 from int y in c2 438 from int z in c3 439 select x + y + z; 440 Console.WriteLine(r1); 441 } 442}"; 443 var compilation = CreateCompilationWithMscorlib(csSource, new[] { LinqAssemblyRef }); 444 foreach (var dd in compilation.GetDiagnostics()) Console.WriteLine(dd); 445 compilation.VerifyDiagnostics(); 446 var tree = compilation.SyntaxTrees[0]; 447 var model = compilation.GetSemanticModel(tree); 448 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single(); 449 dynamic methodM = (MethodDeclarationSyntax)classC.Members[0]; 450 QueryExpressionSyntax q = methodM.Body.Statements[3].Declaration.Variables[0].Initializer.Value; 451 452 var info0 = model.GetQueryClauseInfo(q.FromClause); 453 var x = model.GetDeclaredSymbol(q.FromClause); 454 Assert.Equal(SymbolKind.RangeVariable, x.Kind); 455 Assert.Equal("x", x.Name); 456 Assert.Equal("Cast", info0.CastInfo.Symbol.Name); 457 Assert.Equal(MethodKind.ReducedExtension, ((IMethodSymbol)info0.CastInfo.Symbol).MethodKind); 458 Assert.Null(info0.OperationInfo.Symbol); 459 460 var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]); 461 var y = model.GetDeclaredSymbol(q.Body.Clauses[0]); 462 Assert.Equal(SymbolKind.RangeVariable, y.Kind); 463 Assert.Equal("y", y.Name); 464 Assert.Equal("Cast", info1.CastInfo.Symbol.Name); 465 Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name); 466 Assert.Equal(MethodKind.ReducedExtension, ((IMethodSymbol)info1.OperationInfo.Symbol).MethodKind); 467 468 var info2 = model.GetQueryClauseInfo(q.Body.Clauses[1]); 469 var z = model.GetDeclaredSymbol(q.Body.Clauses[1]); 470 Assert.Equal(SymbolKind.RangeVariable, z.Kind); 471 Assert.Equal("z", z.Name); 472 Assert.Equal("Cast", info2.CastInfo.Symbol.Name); 473 Assert.Equal("SelectMany", info2.OperationInfo.Symbol.Name); 474 475 var info3 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup); 476 Assert.NotNull(info3); 477 // what about info3's contents ??? 478 479 var xPyPz = (q.Body.SelectOrGroup as SelectClauseSyntax).Expression as BinaryExpressionSyntax; 480 var xPy = xPyPz.Left as BinaryExpressionSyntax; 481 Assert.Equal(x, model.GetSemanticInfoSummary(xPy.Left).Symbol); 482 Assert.Equal(y, model.GetSemanticInfoSummary(xPy.Right).Symbol); 483 Assert.Equal(z, model.GetSemanticInfoSummary(xPyPz.Right).Symbol); 484 } 485 486 [Fact] 487 public void TestGetSemanticInfo01() 488 { 489 var csSource = @" 490using C = List1<int>;" + LINQ + @" 491class Query 492{ 493 public static void Main(string[] args) 494 { 495 C c1 = new C(1, 2, 3); 496 C c2 = new C(10, 20, 30); 497 C r1 = 498 from int x in c1 499 from int y in c2 500 select x + y; 501 Console.WriteLine(r1); 502 } 503}"; 504 var compilation = CreateCompilationWithMscorlib(csSource); 505 compilation.VerifyDiagnostics(); 506 var tree = compilation.SyntaxTrees[0]; 507 var model = compilation.GetSemanticModel(tree); 508 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single(); 509 dynamic methodM = (MethodDeclarationSyntax)classC.Members[0]; 510 QueryExpressionSyntax q = methodM.Body.Statements[2].Declaration.Variables[0].Initializer.Value; 511 512 var info0 = model.GetQueryClauseInfo(q.FromClause); 513 Assert.Equal("Cast", info0.CastInfo.Symbol.Name); 514 Assert.Null(info0.OperationInfo.Symbol); 515 Assert.Equal("x", model.GetDeclaredSymbol(q.FromClause).Name); 516 517 var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]); 518 Assert.Equal("Cast", info1.CastInfo.Symbol.Name); 519 Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name); 520 Assert.Equal("y", model.GetDeclaredSymbol(q.Body.Clauses[0]).Name); 521 522 var info2 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup); 523 // what about info2's contents? 524 } 525 526 [Fact] 527 public void TestGetSemanticInfo02() 528 { 529 var csSource = LINQ + @" 530class Query 531{ 532 public static void Main(string[] args) 533 { 534 List1<int> c = new List1<int>(28, 51, 27, 84, 27, 27, 72, 64, 55, 46, 39); 535 var r = 536 from i in c 537 orderby i/10 descending, i%10 538 select i; 539 Console.WriteLine(r); 540 } 541}"; 542 var compilation = CreateCompilationWithMscorlib(csSource); 543 compilation.VerifyDiagnostics(); 544 var tree = compilation.SyntaxTrees[0]; 545 var model = compilation.GetSemanticModel(tree); 546 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single(); 547 dynamic methodM = (MethodDeclarationSyntax)classC.Members[0]; 548 QueryExpressionSyntax q = methodM.Body.Statements[1].Declaration.Variables[0].Initializer.Value; 549 550 var info0 = model.GetQueryClauseInfo(q.FromClause); 551 Assert.Null(info0.CastInfo.Symbol); 552 Assert.Null(info0.OperationInfo.Symbol); 553 Assert.Equal("i", model.GetDeclaredSymbol(q.FromClause).Name); 554 var i = model.GetDeclaredSymbol(q.FromClause); 555 556 var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]); 557 Assert.Null(info1.CastInfo.Symbol); 558 Assert.Null(info1.OperationInfo.Symbol); 559 Assert.Null(model.GetDeclaredSymbol(q.Body.Clauses[0])); 560 561 var order = q.Body.Clauses[0] as OrderByClauseSyntax; 562 var oinfo0 = model.GetSemanticInfoSummary(order.Orderings[0]); 563 Assert.Equal("OrderByDescending", oinfo0.Symbol.Name); 564 565 var oinfo1 = model.GetSemanticInfoSummary(order.Orderings[1]); 566 Assert.Equal("ThenBy", oinfo1.Symbol.Name); 567 } 568 569 [WorkItem(541774, "DevDiv")] 570 [Fact] 571 public void MultipleFromClauseIdentifierInExprNotInContext() 572 { 573 var csSource = @" 574class Program 575{ 576 static void Main(string[] args) 577 { 578 var q2 = from n1 in nums 579 from n2 in nums 580 select n1; 581 } 582}"; 583 CreateCompilationWithMscorlibAndSystemCore(csSource, parseOptions: TestOptions.Regular).VerifyDiagnostics( 584 // (6,29): error CS0103: The name 'nums' does not exist in the current context 585 Diagnostic(ErrorCode.ERR_NameNotInContext, "nums").WithArguments("nums") 586 ); 587 } 588 589 [WorkItem(541906, "DevDiv")] 590 [Fact] 591 public void NullLiteralFollowingJoinInQuery() 592 { 593 var csSource = @" 594using System.Linq; 595 596class Program 597{ 598 static void Main(string[] args) 599 { 600 var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031 601 } 602}"; 603 CreateCompilationWithMscorlibAndSystemCore(csSource, parseOptions: TestOptions.Regular).VerifyDiagnostics( 604 // (8,55): error CS1031: Type expected 605 // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031 606 Diagnostic(ErrorCode.ERR_TypeExpected, "null"), 607 // (8,55): error CS1001: Identifier expected 608 // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031 609 Diagnostic(ErrorCode.ERR_IdentifierExpected, "null"), 610 // (8,55): error CS1003: Syntax error, 'in' expected 611 // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031 612 Diagnostic(ErrorCode.ERR_SyntaxError, "null").WithArguments("in", "null") 613 ); 614 } 615 616 [WorkItem(541779, "DevDiv")] 617 [Fact] 618 public void MultipleFromClauseQueryExpr() 619 { 620 var csSource = @" 621using System; 622using System.Linq; 623 624class Program 625{ 626 static void Main(string[] args) 627 { 628 var nums = new int[] { 3, 4 }; 629 630 var q2 = from int n1 in nums 631 from int n2 in nums 632 select n1; 633 634 string serializer = String.Empty; 635 foreach (var q in q2) 636 { 637 serializer = serializer + q + "" ""; 638 } 639 System.Console.Write(serializer.Trim()); 640 } 641}"; 642 643 CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 3 4 4"); 644 } 645 646 [WorkItem(541782, "DevDiv")] 647 [Fact] 648 public void FromSelectQueryExprOnArraysWithTypeImplicit() 649 { 650 var csSource = @" 651using System; 652using System.Linq; 653 654class Program 655{ 656 static void Main(string[] args) 657 { 658 var nums = new int[] { 3, 4 }; 659 660 var q2 = from n1 in nums select n1; 661 662 string serializer = String.Empty; 663 foreach (var q in q2) 664 { 665 serializer = serializer + q + "" ""; 666 } 667 System.Console.Write(serializer.Trim()); 668 } 669}"; 670 CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 4"); 671 } 672 673 674 [WorkItem(541788, "DevDiv")] 675 [Fact] 676 public void JoinClauseTest() 677 { 678 var csSource = @" 679using System; 680using System.Linq; 681 682class Program 683{ 684 static void Main() 685 { 686 var q2 = 687 from a in Enumerable.Range(1, 13) 688 join b in Enumerable.Range(1, 13) on 4 * a equals b 689 select a; 690 691 string serializer = String.Empty; 692 foreach (var q in q2) 693 { 694 serializer = serializer + q + "" ""; 695 } 696 System.Console.Write(serializer.Trim()); 697 } 698}"; 699 700 CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "1 2 3"); 701 } 702 703 [WorkItem(541789, "DevDiv")] 704 [WorkItem(9229, "DevDiv_Projects/Roslyn")] 705 [Fact] 706 public void WhereClauseTest() 707 { 708 var csSource = @" 709using System; 710using System.Linq; 711 712class Program 713{ 714 static void Main() 715 { 716 var nums = new int[] { 1, 2, 3, 4 }; 717 718 var q2 = from x in nums 719 where (x > 2) 720 select x; 721 722 string serializer = String.Empty; 723 foreach (var q in q2) 724 { 725 serializer = serializer + q + "" ""; 726 } 727 System.Console.Write(serializer.Trim()); 728 } 729}"; 730 731 CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 4"); 732 } 733 734 [WorkItem(541942, "DevDiv")] 735 [Fact] 736 public void WhereDefinedInType() 737 { 738 var csSource = @" 739using System; 740 741class Y 742{ 743 public int Where(Func<int, bool> predicate) 744 { 745 return 45; 746 } 747} 748 749class P 750{ 751 static void Main() 752 { 753 var src = new Y(); 754 var query = from x in src 755 where x > 0 756 select x; 757 758 Console.Write(query); 759 } 760}"; 761 762 CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "45"); 763 } 764 765 [Fact] 766 public void GetInfoForSelectExpression01() 767 { 768 string sourceCode = @" 769using System; 770using System.Linq; 771public class Test2 772{ 773 public static void Main() 774 { 775 var nums = new int[] { 1, 2, 3, 4 }; 776 777 var q2 = from x in nums 778 select x; 779 } 780}"; 781 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 782 var tree = compilation.SyntaxTrees[0]; 783 var semanticModel = compilation.GetSemanticModel(tree); 784 785 SelectClauseSyntax selectClause = (SelectClauseSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("select")).Parent; 786 var info = semanticModel.GetSemanticInfoSummary(selectClause.Expression); 787 Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType); 788 Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind); 789 var info2 = semanticModel.GetSemanticInfoSummary(selectClause); 790 var m = (MethodSymbol)info2.Symbol; 791 Assert.Equal("Select", m.ReducedFrom.Name); 792 } 793 794 [Fact] 795 public void GetInfoForSelectExpression02() 796 { 797 string sourceCode = @" 798using System; 799using System.Linq; 800public class Test2 801{ 802 public static void Main() 803 { 804 var nums = new int[] { 1, 2, 3, 4 }; 805 806 var q2 = from x in nums 807 select x into w 808 select w; 809 } 810}"; 811 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 812 var tree = compilation.SyntaxTrees[0]; 813 var semanticModel = compilation.GetSemanticModel(tree); 814 815 SelectClauseSyntax selectClause = (SelectClauseSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("select w")).Parent; 816 var info = semanticModel.GetSemanticInfoSummary(selectClause.Expression); 817 Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType); 818 Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind); 819 } 820 821 [Fact] 822 public void GetInfoForSelectExpression03() 823 { 824 string sourceCode = @" 825using System.Linq; 826public class Test2 827{ 828 public static void Main() 829 { 830 var nums = new int[] { 1, 2, 3, 4 }; 831 832 var q2 = from x in nums 833 select x+1 into w 834 select w+1; 835 } 836}"; 837 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 838 var tree = compilation.SyntaxTrees[0]; 839 compilation.VerifyDiagnostics(); 840 var semanticModel = compilation.GetSemanticModel(tree); 841 842 var e = (IdentifierNameSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("x+1")).Parent; 843 var info = semanticModel.GetSemanticInfoSummary(e); 844 Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType); 845 Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind); 846 Assert.Equal("x", info.Symbol.Name); 847 848 e = (IdentifierNameSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("w+1")).Parent; 849 info = semanticModel.GetSemanticInfoSummary(e); 850 Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType); 851 Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind); 852 Assert.Equal("w", info.Symbol.Name); 853 854 var e2 = e.Parent as ExpressionSyntax; // w+1 855 var info2 = semanticModel.GetSemanticInfoSummary(e2); 856 Assert.Equal(SpecialType.System_Int32, info2.Type.SpecialType); 857 Assert.Equal("System.Int32 System.Int32.op_Addition(System.Int32 left, System.Int32 right)", info2.Symbol.ToTestDisplayString()); 858 } 859 860 [WorkItem(541806, "DevDiv")] 861 [Fact] 862 public void GetDeclaredSymbolForQueryContinuation() 863 { 864 string sourceCode = @" 865public class Test2 866{ 867 public static void Main() 868 { 869 var nums = new int[] { 1, 2, 3, 4 }; 870 871 var q2 = from x in nums 872 select x into w 873 select w; 874 } 875}"; 876 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 877 var tree = compilation.SyntaxTrees[0]; 878 var semanticModel = compilation.GetSemanticModel(tree); 879 880 var queryContinuation = tree.GetRoot().FindToken(sourceCode.IndexOf("into w")).Parent; 881 var symbol = semanticModel.GetDeclaredSymbol(queryContinuation); 882 883 Assert.NotNull(symbol); 884 Assert.Equal("w", symbol.Name); 885 Assert.Equal(SymbolKind.RangeVariable, symbol.Kind); 886 } 887 888 [WorkItem(541899, "DevDiv")] 889 [Fact] 890 public void ComputeQueryVariableType() 891 { 892 string sourceCode = @" 893using System.Linq; 894public class Test2 895{ 896 public static void Main() 897 { 898 var nums = new int[] { 1, 2, 3, 4 }; 899 900 var q2 = from x in nums 901 select 5; 902 } 903}"; 904 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 905 var tree = compilation.SyntaxTrees[0]; 906 var semanticModel = compilation.GetSemanticModel(tree); 907 var selectExpression = tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("5")); 908 var info = semanticModel.GetSpeculativeTypeInfo(selectExpression.SpanStart, SyntaxFactory.ParseExpression("x"), SpeculativeBindingOption.BindAsExpression); 909 Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType); 910 } 911 912 [WorkItem(541893, "DevDiv")] 913 [Fact] 914 public void GetDeclaredSymbolForJoinIntoClause() 915 { 916 string sourceCode = @" 917using System; 918using System.Linq; 919 920static class Test 921{ 922 static void Main() 923 { 924 var qie = from x3 in new int[] { 0 } 925 join x7 in (new int[] { 0 }) on 5 equals 5 into x8 926 select x8; 927 } 928}"; 929 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 930 var tree = compilation.SyntaxTrees[0]; 931 var semanticModel = compilation.GetSemanticModel(tree); 932 933 var joinInto = tree.GetRoot().FindToken(sourceCode.IndexOf("into x8")).Parent; 934 var symbol = semanticModel.GetDeclaredSymbol(joinInto); 935 936 Assert.NotNull(symbol); 937 Assert.Equal("x8", symbol.Name); 938 Assert.Equal(SymbolKind.RangeVariable, symbol.Kind); 939 Assert.Equal("? x8", symbol.ToTestDisplayString()); 940 } 941 942 [WorkItem(541982, "DevDiv")] 943 [WorkItem(543494, "DevDiv")] 944 [Fact()] 945 public void GetDeclaredSymbolAddAccessorDeclIncompleteQuery() 946 { 947 string sourceCode = @" 948using System; 949using System.Linq; 950 951public class QueryExpressionTest 952{ 953 public static void Main() 954 { 955 var expr1 = new[] { 1, 2, 3, 4, 5 }; 956 957 var query1 = from event in expr1 select event; 958 var query2 = from int 959 } 960}"; 961 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 962 var tree = compilation.SyntaxTrees[0]; 963 var semanticModel = compilation.GetSemanticModel(tree); 964 965 var unknownAccessorDecls = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AccessorDeclarationSyntax>(); 966 var symbols = unknownAccessorDecls.Select(decl => semanticModel.GetDeclaredSymbol(decl)); 967 968 Assert.True(symbols.All(s => ReferenceEquals(s, null))); 969 } 970 971 [WorkItem(542235, "DevDiv")] 972 [Fact] 973 public void TwoFromClauseFollowedBySelectClause() 974 { 975 string sourceCode = @" 976using System.Linq; 977 978class Test 979{ 980 public static void Main() 981 { 982 983 var q2 = from num1 in new int[] { 4, 5 } 984 from num2 in new int[] { 4, 5 } 985 select num1; 986 } 987}"; 988 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 989 var tree = compilation.SyntaxTrees[0]; 990 var semanticModel = compilation.GetSemanticModel(tree); 991 992 var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax; 993 var fromClause1 = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => (n.IsKind(SyntaxKind.FromClause)) && (n.ToString().Contains("num1"))).Single() as FromClauseSyntax; 994 var fromClause2 = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => (n.IsKind(SyntaxKind.FromClause)) && (n.ToString().Contains("num2"))).Single() as FromClauseSyntax; 995 996 var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause); 997 var queryInfoForFrom1 = semanticModel.GetQueryClauseInfo(fromClause1); 998 var queryInfoForFrom2 = semanticModel.GetQueryClauseInfo(fromClause2); 999 1000 Assert.Null(queryInfoForFrom1.CastInfo.Symbol); 1001 Assert.Null(queryInfoForFrom1.OperationInfo.Symbol); 1002 1003 Assert.Null(queryInfoForFrom2.CastInfo.Symbol); 1004 Assert.Equal("SelectMany", queryInfoForFrom2.OperationInfo.Symbol.Name); 1005 1006 Assert.Null(symbolInfoForSelect.Symbol); 1007 Assert.Empty(symbolInfoForSelect.CandidateSymbols); 1008 Assert.Equal(CandidateReason.None, symbolInfoForSelect.CandidateReason); 1009 } 1010 1011 [WorkItem(528747, "DevDiv")] 1012 [Fact] 1013 public void SemanticInfoForOrderingClauses() 1014 { 1015 string sourceCode = @" 1016using System; 1017using System.Linq; 1018 1019public class QueryExpressionTest 1020{ 1021 public static void Main() 1022 { 1023 var q1 = 1024 from x in new int[] { 4, 5 } 1025 orderby 1026 x descending, 1027 x.ToString() ascending, 1028 x descending 1029 select x; 1030 } 1031}"; 1032 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1033 var tree = compilation.SyntaxTrees[0]; 1034 var model = compilation.GetSemanticModel(tree); 1035 int count = 0; 1036 string[] names = { "OrderByDescending", "ThenBy", "ThenByDescending" }; 1037 foreach (var ordering in tree.GetCompilationUnitRoot().DescendantNodes().OfType<OrderingSyntax>()) 1038 { 1039 var symbolInfo = model.GetSemanticInfoSummary(ordering); 1040 Assert.Equal(names[count++], symbolInfo.Symbol.Name); 1041 } 1042 Assert.Equal(3, count); 1043 } 1044 1045 [WorkItem(542266, "DevDiv")] 1046 [Fact] 1047 public void FromOrderBySelectQueryTranslation() 1048 { 1049 string sourceCode = @" 1050using System; 1051using System.Collections; 1052using System.Collections.Generic; 1053 1054public interface IOrderedEnumerable<TElement> : IEnumerable<TElement>, 1055 IEnumerable 1056{ 1057} 1058 1059public static class Extensions 1060{ 1061 public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( 1062 this IEnumerable<TSource> source, 1063 Func<TSource, TKey> keySelector) 1064 1065 { 1066 return null; 1067 } 1068 1069 public static IEnumerable<TResult> Select<TSource, TResult>( 1070 this IEnumerable<TSource> source, 1071 Func<TSource, TResult> selector) 1072 1073 { 1074 return null; 1075 } 1076} 1077 1078class Program 1079{ 1080 static void Main(string[] args) 1081 { 1082 1083 var q1 = from num in new int[] { 4, 5 } 1084 orderby num 1085 select num; 1086 } 1087}"; 1088 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1089 var tree = compilation.SyntaxTrees[0]; 1090 var semanticModel = compilation.GetSemanticModel(tree); 1091 1092 var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax; 1093 var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause); 1094 1095 Assert.Null(symbolInfoForSelect.Symbol); 1096 } 1097 1098 [WorkItem(528756, "DevDiv")] 1099 [Fact] 1100 public void FromWhereSelectTranslation() 1101 { 1102 string sourceCode = @" 1103using System; 1104using System.Collections.Generic; 1105 1106public static class Extensions 1107{ 1108 1109 public static IEnumerable<TSource> Where<TSource>( 1110 this IEnumerable<TSource> source, 1111 Func<TSource, bool> predicate) 1112 { 1113 return null; 1114 } 1115} 1116 1117class Program 1118{ 1119 static void Main(string[] args) 1120 { 1121 1122 var q1 = from num in System.Linq.Enumerable.Range(4, 5).Where(n => n > 10) 1123 select num; 1124 } 1125}"; 1126 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1127 var tree = compilation.SyntaxTrees[0]; 1128 var semanticModel = compilation.GetSemanticModel(tree); 1129 1130 semanticModel.GetDiagnostics().Verify( 1131 // (21,30): error CS1935: Could not find an implementation of the query pattern for source type 'System.Collections.Generic.IEnumerable<int>'. 'Select' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'? 1132 // var q1 = from num in System.Linq.Enumerable.Range(4, 5).Where(n => n > 10) 1133 Diagnostic(ErrorCode.ERR_QueryNoProviderStandard, "System.Linq.Enumerable.Range(4, 5).Where(n => n > 10)").WithArguments("System.Collections.Generic.IEnumerable<int>", "Select")); 1134 } 1135 1136 [WorkItem(528760, "DevDiv")] 1137 [Fact] 1138 public void FromJoinSelectTranslation() 1139 { 1140 string sourceCode = @" 1141using System.Linq; 1142 1143class Program 1144{ 1145 static void Main(string[] args) 1146 { 1147 var q1 = from num in new int[] { 4, 5 } 1148 join x1 in new int[] { 4, 5 } on num equals x1 1149 select x1 + 5; 1150 } 1151}"; 1152 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1153 var tree = compilation.SyntaxTrees[0]; 1154 var semanticModel = compilation.GetSemanticModel(tree); 1155 1156 var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax; 1157 var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause); 1158 1159 Assert.Null(symbolInfoForSelect.Symbol); 1160 } 1161 1162 [WorkItem(528761, "DevDiv")] 1163 [WorkItem(544585, "DevDiv")] 1164 [Fact] 1165 public void OrderingSyntaxWithOverloadResolutionFailure() 1166 { 1167 string sourceCode = @" 1168using System.Linq; 1169 1170class Program 1171{ 1172 static void Main(string[] args) 1173 { 1174 int[] numbers = new int[] { 4, 5 }; 1175 1176 var q1 = from num in numbers.Single() 1177 orderby (x1) => x1.ToString() 1178 select num; 1179 } 1180}"; 1181 1182 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1183 compilation.VerifyDiagnostics( 1184 // (10,30): error CS1936: Could not find an implementation of the query pattern for source type 'int'. 'OrderBy' not found. 1185 // var q1 = from num in numbers.Single() 1186 Diagnostic(ErrorCode.ERR_QueryNoProvider, "numbers.Single()").WithArguments("int", "OrderBy") 1187 ); 1188 var tree = compilation.SyntaxTrees[0]; 1189 var semanticModel = compilation.GetSemanticModel(tree); 1190 1191 var orderingClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.AscendingOrdering)).Single() as OrderingSyntax; 1192 var symbolInfoForOrdering = semanticModel.GetSemanticInfoSummary(orderingClause); 1193 1194 Assert.Null(symbolInfoForOrdering.Symbol); 1195 } 1196 1197 [WorkItem(542292, "DevDiv")] 1198 [Fact] 1199 public void EmitIncompleteQueryWithSyntaxErrors() 1200 { 1201 string sourceCode = @" 1202using System.Linq; 1203 1204class Program 1205{ 1206 static int Main() 1207 { 1208 int [] foo = new int [] {1}; 1209 var q = from x in foo 1210 select x + 1 into z 1211 select z.T 1212"; 1213 using (var output = new MemoryStream()) 1214 { 1215 Assert.False(CreateCompilationWithMscorlibAndSystemCore(sourceCode).Emit(output).Success); 1216 } 1217 } 1218 1219 [WorkItem(542294, "DevDiv")] 1220 [Fact] 1221 public void EmitQueryWithBindErrors() 1222 { 1223 string sourceCode = @" 1224using System.Linq; 1225class Program 1226{ 1227 static void Main() 1228 { 1229 int[] nums = { 0, 1, 2, 3, 4, 5 }; 1230 var query = from num in nums 1231 let num = 3 // CS1930 1232 select num; 1233 } 1234}"; 1235 using (var output = new MemoryStream()) 1236 { 1237 Assert.False(CreateCompilationWithMscorlibAndSystemCore(sourceCode).Emit(output).Success); 1238 } 1239 } 1240 1241 [WorkItem(542372, "DevDiv")] 1242 [Fact] 1243 public void BindToIncompleteSelectManyDecl() 1244 { 1245 string sourceCode = @" 1246class P 1247{ 1248 static C<X> M2<X>(X x) 1249 { 1250 return new C<X>(x); 1251 } 1252 1253 static void Main() 1254 { 1255 C<int> e1 = new C<int>(1); 1256 1257 var q = from x1 in M2<int>(x1) 1258 from x2 in e1 1259 select x1; 1260 } 1261} 1262 1263class C<T> 1264{ 1265 public C<V> SelectMany"; 1266 1267 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1268 var tree = compilation.SyntaxTrees[0]; 1269 var semanticModel = compilation.GetSemanticModel(tree); 1270 1271 var diags = semanticModel.GetDiagnostics(); 1272 1273 Assert.NotEmpty(diags); 1274 } 1275 1276 [WorkItem(542419, "DevDiv")] 1277 [Fact] 1278 public void BindIdentifierInWhereErrorTolerance() 1279 { 1280 string sourceCode = @" 1281using System; 1282using System.Collections.Generic; 1283using System.Linq; 1284 1285class Program 1286{ 1287 static void Main(string[] args) 1288 { 1289 var r = args.Where(b => b < > ); 1290 var q = from a in args 1291 where a <> 1292 } 1293}"; 1294 1295 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1296 var tree = compilation.SyntaxTrees[0]; 1297 var semanticModel = compilation.GetSemanticModel(tree); 1298 var diags = semanticModel.GetDiagnostics(); 1299 Assert.NotEmpty(diags); 1300 } 1301 1302 [WorkItem(542460, "DevDiv")] 1303 [Fact] 1304 public void QueryWithMultipleParseErrorsAndInteractiveParseOption() 1305 { 1306 string sourceCode = @" 1307using System; 1308using System.Linq; 1309 1310public class QueryExpressionTest 1311{ 1312 public static void Main() 1313 { 1314 var expr1 = new int[] { 1, 2, 3, 4, 5 }; 1315 1316 var query2 = from int namespace in expr1 select namespace; 1317 var query25 = from i in expr1 let namespace = expr1 select i; 1318 } 1319}"; 1320 1321 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode, parseOptions: TestOptions.Interactive); 1322 var tree = compilation.SyntaxTrees[0]; 1323 var semanticModel = compilation.GetSemanticModel(tree); 1324 var queryExpr = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryExpressionSyntax>().Where(x => x.ToFullString() == "from i in expr1 let ").Single(); 1325 var symbolInfo = semanticModel.GetSemanticInfoSummary(queryExpr); 1326 1327 Assert.Null(symbolInfo.Symbol); 1328 } 1329 1330 [WorkItem(542496, "DevDiv")] 1331 [Fact] 1332 public void QueryExpressionInFieldInitReferencingAnotherFieldWithInteractiveParseOption() 1333 { 1334 string sourceCode = @" 1335using System.Linq; 1336using System.Collections; 1337 1338class P 1339{ 1340 double one = 1; 1341 1342 public IEnumerable e = 1343 from x in new int[] { 1, 2, 3 } 1344 select x + one; 1345}"; 1346 1347 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode, parseOptions: TestOptions.Interactive); 1348 var tree = compilation.SyntaxTrees[0]; 1349 var semanticModel = compilation.GetSemanticModel(tree); 1350 var queryExpr = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryExpressionSyntax>().Single(); 1351 var symbolInfo = semanticModel.GetSemanticInfoSummary(queryExpr); 1352 1353 Assert.Null(symbolInfo.Symbol); 1354 } 1355 1356 [WorkItem(542559, "DevDiv")] 1357 [Fact] 1358 public void StaticTypeInFromClause() 1359 { 1360 string sourceCode = @" 1361using System; 1362using System.Linq; 1363 1364class C 1365{ 1366 static void Main() 1367 { 1368 var q2 = string.Empty.Cast<GC>().Select(x => x); 1369 var q1 = from GC x in string.Empty select x; 1370 } 1371}"; 1372 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1373 compilation.VerifyDiagnostics( 1374 // (9,36): error CS0718: 'System.GC': static types cannot be used as type arguments 1375 // var q2 = string.Empty.Cast<GC>().Select(x => x); 1376 Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "GC").WithArguments("System.GC").WithLocation(9,36), 1377 // (10,23): error CS0718: 'System.GC': static types cannot be used as type arguments 1378 // var q1 = from GC x in string.Empty select x; 1379 Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "GC").WithArguments("System.GC").WithLocation(10,23) 1380 ); 1381 } 1382 1383 [WorkItem(542560, "DevDiv")] 1384 [Fact] 1385 public void MethodGroupInFromClause() 1386 { 1387 string sourceCode = @" 1388class Program 1389{ 1390 static void Main() 1391 { 1392 var q1 = from y in Main select y; 1393 var q2 = Main.Select(y => y); 1394 } 1395}"; 1396 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1397 compilation.VerifyDiagnostics( 1398 // (6,28): error CS0119: 'Program.Main()' is a method, which is not valid in the given context 1399 // var q1 = from y in Main select y; 1400 Diagnostic(ErrorCode.ERR_BadSKunknown, "Main").WithArguments("Program.Main()", "method").WithLocation(6,28), 1401 // (7,18): error CS0119: 'Program.Main()' is a method, which is not valid in the given context 1402 // var q2 = Main.Select(y => y); 1403 Diagnostic(ErrorCode.ERR_BadSKunknown, "Main").WithArguments("Program.Main()", "method").WithLocation(7,18) 1404 ); 1405 } 1406 1407 [WorkItem(542558, "DevDiv")] 1408 [Fact] 1409 public void SelectFromType01() 1410 { 1411 string sourceCode = @"using System; 1412using System.Collections.Generic; 1413 1414class C 1415{ 1416 static void Main() 1417 { 1418 var q = from x in C select x; 1419 } 1420 1421 static IEnumerable<T> Select<T>(Func<int, T> f) { return null; } 1422}"; 1423 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1424 compilation.VerifyDiagnostics(); 1425 var tree = compilation.SyntaxTrees[0]; 1426 var model = compilation.GetSemanticModel(tree); 1427 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "C").Single(); 1428 dynamic main = (MethodDeclarationSyntax)classC.Members[0]; 1429 QueryExpressionSyntax q = main.Body.Statements[0].Declaration.Variables[0].Initializer.Value; 1430 var info0 = model.GetQueryClauseInfo(q.FromClause); 1431 var x = model.GetDeclaredSymbol(q.FromClause); 1432 Assert.Equal(SymbolKind.RangeVariable, x.Kind); 1433 Assert.Equal("x", x.Name); 1434 Assert.Equal(null, info0.CastInfo.Symbol); 1435 Assert.Null(info0.OperationInfo.Symbol); 1436 var infoSelect = model.GetSemanticInfoSummary(q.Body.SelectOrGroup); 1437 Assert.Equal("Select", infoSelect.Symbol.Name); 1438 } 1439 1440 [WorkItem(542558, "DevDiv")] 1441 [Fact] 1442 public void SelectFromType02() 1443 { 1444 string sourceCode = @"using System; 1445using System.Collections.Generic; 1446 1447class C 1448{ 1449 static void Main() 1450 { 1451 var q = from x in C select x; 1452 } 1453 1454 static Func<Func<int, object>, IEnumerable<object>> Select = null; 1455}"; 1456 var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode); 1457 compilation.VerifyDiagnostics(); 1458 var tree = compilation.SyntaxTrees[0]; 1459 var model = compilation.GetSemanticModel(tree); 1460 var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "C").Single(); 1461 dynamic main = (MethodDeclarationSyntax)classC.Members[0]; 1462 QueryExpressionSyntax q = main.Body.Statements[0].Declaration.Variables[0].Initializer.Value; 1463 var info0 = model.GetQueryClauseInfo(q.FromClause); 1464 var x = model.GetDeclaredSymbol(q.FromClause); 1465 Assert.Equal(SymbolKind.RangeVariable, x.Kind); 1466 Assert.Equal("x", x.Name); 1467 Assert.Equal(null, info0.CastInfo.Symbol); 1468 Assert.Null(info0.OperationInfo.Symbol); 1469 var infoSelect = model.GetSemanticInfoSummary(q.Body.SelectOrGroup); 1470 Assert.Equal("Invoke", infoSelect.Symbol.Name); 1471 } 1472 1473 [WorkItem(542624, "DevDiv")] 1474 [Fact] 1475 public void QueryColorColor() 1476 { 1477 string sourceCode = @" 1478using System; 1479using System.Collections.Generic; 1480 1481class Color 1482{ 1483 public static IEnumerable<T> Sele…
Large files files are truncated, but you can click here to view the full file