PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Src/Compilers/CSharp/Test/Semantic/Semantics/QueryTests.cs

https://github.com/EkardNT/Roslyn
C# | 2050 lines | 1855 code | 152 blank | 43 comment | 16 complexity | 482eaf29310b38b84269bda730a13cbc MD5 | raw 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. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using Microsoft.CodeAnalysis.CSharp.Symbols;
  6. using Microsoft.CodeAnalysis.CSharp.Syntax;
  7. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  8. using Roslyn.Test.Utilities;
  9. using Xunit;
  10. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  11. {
  12. public partial class QueryTests : CompilingTestBase
  13. {
  14. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  15. [Fact]
  16. public void DegenerateQueryExpression()
  17. {
  18. var csSource = LINQ + @"
  19. class Query
  20. {
  21. public static void Main(string[] args)
  22. {
  23. List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7);
  24. List1<int> r = from i in c select i;
  25. if (ReferenceEquals(c, r)) throw new Exception();
  26. // List1<int> r = c.Select(i => i);
  27. Console.WriteLine(r);
  28. }
  29. }";
  30. CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]");
  31. }
  32. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  33. [Fact]
  34. public void QueryContinuation()
  35. {
  36. var csSource = LINQ + @"
  37. class Query
  38. {
  39. public static void Main(string[] args)
  40. {
  41. List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7);
  42. List1<int> r = from i in c select i into q select q;
  43. if (ReferenceEquals(c, r)) throw new Exception();
  44. // List1<int> r = c.Select(i => i);
  45. Console.WriteLine(r);
  46. }
  47. }";
  48. CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]");
  49. }
  50. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  51. [Fact]
  52. public void Select()
  53. {
  54. var csSource = LINQ + @"
  55. class Query
  56. {
  57. public static void Main(string[] args)
  58. {
  59. List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7);
  60. List1<int> r = from i in c select i+1;
  61. Console.WriteLine(r);
  62. }
  63. }";
  64. CompileAndVerify(csSource, expectedOutput: "[2, 3, 4, 5, 6, 7, 8]");
  65. }
  66. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  67. [Fact]
  68. public void GroupBy01()
  69. {
  70. var csSource = LINQ + @"
  71. class Query
  72. {
  73. public static void Main(string[] args)
  74. {
  75. List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7);
  76. var r = from i in c group i by i % 2;
  77. Console.WriteLine(r);
  78. }
  79. }";
  80. CompileAndVerify(csSource, expectedOutput: "[1:[1, 3, 5, 7], 0:[2, 4, 6]]");
  81. }
  82. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  83. [Fact]
  84. public void GroupBy02()
  85. {
  86. var csSource = LINQ + @"
  87. class Query
  88. {
  89. public static void Main(string[] args)
  90. {
  91. List1<int> c = new List1<int>(1, 2, 3, 4, 5, 6, 7);
  92. var r = from i in c group 10+i by i % 2;
  93. Console.WriteLine(r);
  94. }
  95. }";
  96. CompileAndVerify(csSource, expectedOutput: "[1:[11, 13, 15, 17], 0:[12, 14, 16]]");
  97. }
  98. [Fact]
  99. public void Cast()
  100. {
  101. var csSource = LINQ + @"
  102. class Query
  103. {
  104. public static void Main(string[] args)
  105. {
  106. List1<object> c = new List1<object>(1, 2, 3, 4, 5, 6, 7);
  107. List1<int> r = from int i in c select i;
  108. Console.WriteLine(r);
  109. }
  110. }";
  111. CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4, 5, 6, 7]");
  112. }
  113. [Fact]
  114. public void Where()
  115. {
  116. var csSource = LINQ + @"
  117. class Query
  118. {
  119. public static void Main(string[] args)
  120. {
  121. List1<object> c = new List1<object>(1, 2, 3, 4, 5, 6, 7);
  122. List1<int> r = from int i in c where i < 5 select i;
  123. Console.WriteLine(r);
  124. }
  125. }";
  126. CompileAndVerify(csSource, expectedOutput: "[1, 2, 3, 4]");
  127. }
  128. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  129. [Fact]
  130. public void FromJoinSelect()
  131. {
  132. var csSource = LINQ + @"
  133. class Query
  134. {
  135. public static void Main(string[] args)
  136. {
  137. List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7);
  138. List1<int> c2 = new List1<int>(10, 30, 40, 50, 60, 70);
  139. List1<int> r = from x1 in c1
  140. join x2 in c2 on x1 equals x2/10
  141. select x1+x2;
  142. Console.WriteLine(r);
  143. }
  144. }";
  145. CompileAndVerify(csSource, expectedOutput: "[11, 33, 44, 55, 77]");
  146. }
  147. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  148. [Fact]
  149. public void OrderBy()
  150. {
  151. var csSource = LINQ + @"
  152. class Query
  153. {
  154. public static void Main(string[] args)
  155. {
  156. List1<int> c = new List1<int>(28, 51, 27, 84, 27, 27, 72, 64, 55, 46, 39);
  157. var r =
  158. from i in c
  159. orderby i/10 descending, i%10
  160. select i;
  161. Console.WriteLine(r);
  162. }
  163. }";
  164. CompileAndVerify(csSource, expectedOutput: "[84, 72, 64, 51, 55, 46, 39, 27, 27, 27, 28]");
  165. }
  166. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  167. [Fact]
  168. public void GroupJoin()
  169. {
  170. var csSource = LINQ + @"
  171. class Query
  172. {
  173. public static void Main(string[] args)
  174. {
  175. List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7);
  176. List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75);
  177. List1<string> r =
  178. from x1 in c1
  179. join x2 in c2 on x1 equals x2 / 10 into g
  180. select x1 + "":"" + g.ToString();
  181. Console.WriteLine(r);
  182. }
  183. }";
  184. CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52], 7:[75]]");
  185. }
  186. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  187. [Fact]
  188. public void SelectMany01()
  189. {
  190. var csSource = LINQ + @"
  191. class Query
  192. {
  193. public static void Main(string[] args)
  194. {
  195. List1<int> c1 = new List1<int>(1, 2, 3);
  196. List1<int> c2 = new List1<int>(10, 20, 30);
  197. List1<int> r = from x in c1 from y in c2 select x + y;
  198. Console.WriteLine(r);
  199. }
  200. }";
  201. CompileAndVerify(csSource, expectedOutput: "[11, 21, 31, 12, 22, 32, 13, 23, 33]");
  202. }
  203. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  204. [Fact]
  205. public void SelectMany02()
  206. {
  207. var csSource = LINQ + @"
  208. class Query
  209. {
  210. public static void Main(string[] args)
  211. {
  212. List1<int> c1 = new List1<int>(1, 2, 3);
  213. List1<int> c2 = new List1<int>(10, 20, 30);
  214. List1<int> r = from x in c1 from int y in c2 select x + y;
  215. Console.WriteLine(r);
  216. }
  217. }";
  218. CompileAndVerify(csSource, expectedOutput: "[11, 21, 31, 12, 22, 32, 13, 23, 33]");
  219. }
  220. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  221. [Fact]
  222. public void Let01()
  223. {
  224. var csSource = LINQ + @"
  225. class Query
  226. {
  227. public static void Main(string[] args)
  228. {
  229. List1<int> c1 = new List1<int>(1, 2, 3);
  230. List1<int> r1 =
  231. from int x in c1
  232. let g = x * 10
  233. let z = g + x*100
  234. select x + z;
  235. System.Console.WriteLine(r1);
  236. }
  237. }";
  238. CompileAndVerify(csSource, expectedOutput: "[111, 222, 333]");
  239. }
  240. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  241. [Fact]
  242. public void TransparentIdentifiers_FromLet()
  243. {
  244. var csSource = @"
  245. using C = List1<int>;" + LINQ + @"
  246. class Query
  247. {
  248. public static void Main(string[] args)
  249. {
  250. C c1 = new C(1, 2, 3);
  251. C c2 = new C(10, 20, 30);
  252. C c3 = new C(100, 200, 300);
  253. C r1 =
  254. from int x in c1
  255. from int y in c2
  256. from int z in c3
  257. let g = x + y + z
  258. where (x + y / 10 + z / 100) < 6
  259. select g;
  260. Console.WriteLine(r1);
  261. }
  262. }";
  263. CompileAndVerify(csSource, expectedOutput: "[111, 211, 311, 121, 221, 131, 112, 212, 122, 113]");
  264. }
  265. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  266. [Fact]
  267. public void TransparentIdentifiers_Join01()
  268. {
  269. var csSource = @"
  270. using C = List1<int>;" + LINQ + @"
  271. class Query
  272. {
  273. public static void Main(string[] args)
  274. {
  275. C c1 = new C(1, 2, 3);
  276. C c2 = new C(10, 20, 30);
  277. C r1 =
  278. from int x in c1
  279. join y in c2 on x equals y/10
  280. let z = x+y
  281. select z;
  282. Console.WriteLine(r1);
  283. }
  284. }";
  285. CompileAndVerify(csSource, expectedOutput: "[11, 22, 33]");
  286. }
  287. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  288. [Fact]
  289. public void TransparentIdentifiers_Join02()
  290. {
  291. var csSource = LINQ + @"
  292. class Query
  293. {
  294. public static void Main(string[] args)
  295. {
  296. List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7);
  297. List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75);
  298. List1<string> r1 = from x1 in c1
  299. join x2 in c2 on x1 equals x2 / 10 into g
  300. where x1 < 7
  301. select x1 + "":"" + g.ToString();
  302. Console.WriteLine(r1);
  303. }
  304. }";
  305. CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52]]");
  306. }
  307. [Fact]
  308. public void CodegenBug()
  309. {
  310. var csSource = LINQ + @"
  311. class Query
  312. {
  313. public static void Main(string[] args)
  314. {
  315. List1<int> c1 = new List1<int>(1, 2, 3, 4, 5, 7);
  316. List1<int> c2 = new List1<int>(12, 34, 42, 51, 52, 66, 75);
  317. List1<Tuple<int, List1<int>>> r1 =
  318. c1
  319. .GroupJoin(c2, x1 => x1, x2 => x2 / 10, (x1, g) => new Tuple<int, List1<int>>(x1, g))
  320. ;
  321. Func1<Tuple<int, List1<int>>, bool> condition = (Tuple<int, List1<int>> TR1) => TR1.Item1 < 7;
  322. List1<Tuple<int, List1<int>>> r2 =
  323. r1
  324. .Where(condition)
  325. ;
  326. Func1<Tuple<int, List1<int>>, string> map = (Tuple<int, List1<int>> TR1) => TR1.Item1.ToString() + "":"" + TR1.Item2.ToString();
  327. List1<string> r3 =
  328. r2
  329. .Select(map)
  330. ;
  331. string r4 = r3.ToString();
  332. Console.WriteLine(r4);
  333. return;
  334. }
  335. }";
  336. CompileAndVerify(csSource, expectedOutput: "[1:[12], 2:[], 3:[34], 4:[42], 5:[51, 52]]");
  337. }
  338. [Fact]
  339. public void RangeVariables01()
  340. {
  341. var csSource = @"
  342. using C = List1<int>;" + LINQ + @"
  343. class Query
  344. {
  345. public static void Main(string[] args)
  346. {
  347. C c1 = new C(1, 2, 3);
  348. C c2 = new C(10, 20, 30);
  349. C c3 = new C(100, 200, 300);
  350. C r1 =
  351. from int x in c1
  352. from int y in c2
  353. from int z in c3
  354. select x + y + z;
  355. Console.WriteLine(r1);
  356. }
  357. }";
  358. var compilation = CreateCompilationWithMscorlib(csSource);
  359. compilation.VerifyDiagnostics();
  360. var tree = compilation.SyntaxTrees[0];
  361. var model = compilation.GetSemanticModel(tree);
  362. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single();
  363. dynamic methodM = (MethodDeclarationSyntax)classC.Members[0];
  364. QueryExpressionSyntax q = methodM.Body.Statements[3].Declaration.Variables[0].Initializer.Value;
  365. var info0 = model.GetQueryClauseInfo(q.FromClause);
  366. var x = model.GetDeclaredSymbol(q.FromClause);
  367. Assert.Equal(SymbolKind.RangeVariable, x.Kind);
  368. Assert.Equal("x", x.Name);
  369. Assert.Equal("Cast", info0.CastInfo.Symbol.Name);
  370. Assert.NotEqual(MethodKind.ReducedExtension, ((IMethodSymbol)info0.CastInfo.Symbol).MethodKind);
  371. Assert.Null(info0.OperationInfo.Symbol);
  372. var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]);
  373. var y = model.GetDeclaredSymbol(q.Body.Clauses[0]);
  374. Assert.Equal(SymbolKind.RangeVariable, y.Kind);
  375. Assert.Equal("y", y.Name);
  376. Assert.Equal("Cast", info1.CastInfo.Symbol.Name);
  377. Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name);
  378. Assert.NotEqual(MethodKind.ReducedExtension, ((IMethodSymbol)info1.OperationInfo.Symbol).MethodKind);
  379. var info2 = model.GetQueryClauseInfo(q.Body.Clauses[1]);
  380. var z = model.GetDeclaredSymbol(q.Body.Clauses[1]);
  381. Assert.Equal(SymbolKind.RangeVariable, z.Kind);
  382. Assert.Equal("z", z.Name);
  383. Assert.Equal("Cast", info2.CastInfo.Symbol.Name);
  384. Assert.Equal("SelectMany", info2.OperationInfo.Symbol.Name);
  385. var info3 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup);
  386. Assert.NotNull(info3);
  387. // what about info3's contents ???
  388. var xPyPz = (q.Body.SelectOrGroup as SelectClauseSyntax).Expression as BinaryExpressionSyntax;
  389. var xPy = xPyPz.Left as BinaryExpressionSyntax;
  390. Assert.Equal(x, model.GetSemanticInfoSummary(xPy.Left).Symbol);
  391. Assert.Equal(y, model.GetSemanticInfoSummary(xPy.Right).Symbol);
  392. Assert.Equal(z, model.GetSemanticInfoSummary(xPyPz.Right).Symbol);
  393. }
  394. [Fact]
  395. public void RangeVariables02()
  396. {
  397. var csSource = @"
  398. using System;
  399. using System.Linq;
  400. class Query
  401. {
  402. public static void Main(string[] args)
  403. {
  404. var c1 = new int[] {1, 2, 3};
  405. var c2 = new int[] {10, 20, 30};
  406. var c3 = new int[] {100, 200, 300};
  407. var r1 =
  408. from int x in c1
  409. from int y in c2
  410. from int z in c3
  411. select x + y + z;
  412. Console.WriteLine(r1);
  413. }
  414. }";
  415. var compilation = CreateCompilationWithMscorlib(csSource, new[] { LinqAssemblyRef });
  416. foreach (var dd in compilation.GetDiagnostics()) Console.WriteLine(dd);
  417. compilation.VerifyDiagnostics();
  418. var tree = compilation.SyntaxTrees[0];
  419. var model = compilation.GetSemanticModel(tree);
  420. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single();
  421. dynamic methodM = (MethodDeclarationSyntax)classC.Members[0];
  422. QueryExpressionSyntax q = methodM.Body.Statements[3].Declaration.Variables[0].Initializer.Value;
  423. var info0 = model.GetQueryClauseInfo(q.FromClause);
  424. var x = model.GetDeclaredSymbol(q.FromClause);
  425. Assert.Equal(SymbolKind.RangeVariable, x.Kind);
  426. Assert.Equal("x", x.Name);
  427. Assert.Equal("Cast", info0.CastInfo.Symbol.Name);
  428. Assert.Equal(MethodKind.ReducedExtension, ((IMethodSymbol)info0.CastInfo.Symbol).MethodKind);
  429. Assert.Null(info0.OperationInfo.Symbol);
  430. var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]);
  431. var y = model.GetDeclaredSymbol(q.Body.Clauses[0]);
  432. Assert.Equal(SymbolKind.RangeVariable, y.Kind);
  433. Assert.Equal("y", y.Name);
  434. Assert.Equal("Cast", info1.CastInfo.Symbol.Name);
  435. Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name);
  436. Assert.Equal(MethodKind.ReducedExtension, ((IMethodSymbol)info1.OperationInfo.Symbol).MethodKind);
  437. var info2 = model.GetQueryClauseInfo(q.Body.Clauses[1]);
  438. var z = model.GetDeclaredSymbol(q.Body.Clauses[1]);
  439. Assert.Equal(SymbolKind.RangeVariable, z.Kind);
  440. Assert.Equal("z", z.Name);
  441. Assert.Equal("Cast", info2.CastInfo.Symbol.Name);
  442. Assert.Equal("SelectMany", info2.OperationInfo.Symbol.Name);
  443. var info3 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup);
  444. Assert.NotNull(info3);
  445. // what about info3's contents ???
  446. var xPyPz = (q.Body.SelectOrGroup as SelectClauseSyntax).Expression as BinaryExpressionSyntax;
  447. var xPy = xPyPz.Left as BinaryExpressionSyntax;
  448. Assert.Equal(x, model.GetSemanticInfoSummary(xPy.Left).Symbol);
  449. Assert.Equal(y, model.GetSemanticInfoSummary(xPy.Right).Symbol);
  450. Assert.Equal(z, model.GetSemanticInfoSummary(xPyPz.Right).Symbol);
  451. }
  452. [Fact]
  453. public void TestGetSemanticInfo01()
  454. {
  455. var csSource = @"
  456. using C = List1<int>;" + LINQ + @"
  457. class Query
  458. {
  459. public static void Main(string[] args)
  460. {
  461. C c1 = new C(1, 2, 3);
  462. C c2 = new C(10, 20, 30);
  463. C r1 =
  464. from int x in c1
  465. from int y in c2
  466. select x + y;
  467. Console.WriteLine(r1);
  468. }
  469. }";
  470. var compilation = CreateCompilationWithMscorlib(csSource);
  471. compilation.VerifyDiagnostics();
  472. var tree = compilation.SyntaxTrees[0];
  473. var model = compilation.GetSemanticModel(tree);
  474. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single();
  475. dynamic methodM = (MethodDeclarationSyntax)classC.Members[0];
  476. QueryExpressionSyntax q = methodM.Body.Statements[2].Declaration.Variables[0].Initializer.Value;
  477. var info0 = model.GetQueryClauseInfo(q.FromClause);
  478. Assert.Equal("Cast", info0.CastInfo.Symbol.Name);
  479. Assert.Null(info0.OperationInfo.Symbol);
  480. Assert.Equal("x", model.GetDeclaredSymbol(q.FromClause).Name);
  481. var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]);
  482. Assert.Equal("Cast", info1.CastInfo.Symbol.Name);
  483. Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name);
  484. Assert.Equal("y", model.GetDeclaredSymbol(q.Body.Clauses[0]).Name);
  485. var info2 = model.GetSemanticInfoSummary(q.Body.SelectOrGroup);
  486. // what about info2's contents?
  487. }
  488. [Fact]
  489. public void TestGetSemanticInfo02()
  490. {
  491. var csSource = LINQ + @"
  492. class Query
  493. {
  494. public static void Main(string[] args)
  495. {
  496. List1<int> c = new List1<int>(28, 51, 27, 84, 27, 27, 72, 64, 55, 46, 39);
  497. var r =
  498. from i in c
  499. orderby i/10 descending, i%10
  500. select i;
  501. Console.WriteLine(r);
  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[1].Declaration.Variables[0].Initializer.Value;
  511. var info0 = model.GetQueryClauseInfo(q.FromClause);
  512. Assert.Null(info0.CastInfo.Symbol);
  513. Assert.Null(info0.OperationInfo.Symbol);
  514. Assert.Equal("i", model.GetDeclaredSymbol(q.FromClause).Name);
  515. var i = model.GetDeclaredSymbol(q.FromClause);
  516. var info1 = model.GetQueryClauseInfo(q.Body.Clauses[0]);
  517. Assert.Null(info1.CastInfo.Symbol);
  518. Assert.Null(info1.OperationInfo.Symbol);
  519. Assert.Null(model.GetDeclaredSymbol(q.Body.Clauses[0]));
  520. var order = q.Body.Clauses[0] as OrderByClauseSyntax;
  521. var oinfo0 = model.GetSemanticInfoSummary(order.Orderings[0]);
  522. Assert.Equal("OrderByDescending", oinfo0.Symbol.Name);
  523. var oinfo1 = model.GetSemanticInfoSummary(order.Orderings[1]);
  524. Assert.Equal("ThenBy", oinfo1.Symbol.Name);
  525. }
  526. [WorkItem(541774, "DevDiv")]
  527. [Fact]
  528. public void MultipleFromClauseIdentifierInExprNotInContext()
  529. {
  530. var csSource = @"
  531. class Program
  532. {
  533. static void Main(string[] args)
  534. {
  535. var q2 = from n1 in nums
  536. from n2 in nums
  537. select n1;
  538. }
  539. }";
  540. CreateCompilationWithMscorlibAndSystemCore(csSource, parseOptions: TestOptions.Regular).VerifyDiagnostics(
  541. // (6,29): error CS0103: The name 'nums' does not exist in the current context
  542. Diagnostic(ErrorCode.ERR_NameNotInContext, "nums").WithArguments("nums")
  543. );
  544. }
  545. [WorkItem(541906, "DevDiv")]
  546. [Fact]
  547. public void NullLiteralFollowingJoinInQuery()
  548. {
  549. var csSource = @"
  550. using System.Linq;
  551. class Program
  552. {
  553. static void Main(string[] args)
  554. {
  555. var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031
  556. }
  557. }";
  558. CreateCompilationWithMscorlibAndSystemCore(csSource, parseOptions: TestOptions.Regular).VerifyDiagnostics(
  559. // (8,55): error CS1031: Type expected
  560. // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031
  561. Diagnostic(ErrorCode.ERR_TypeExpected, "null"),
  562. // (8,55): error CS1001: Identifier expected
  563. // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031
  564. Diagnostic(ErrorCode.ERR_IdentifierExpected, "null"),
  565. // (8,55): error CS1003: Syntax error, 'in' expected
  566. // var query = from int i in new int[]{ 1 } join null on true equals true select i; //CS1031
  567. Diagnostic(ErrorCode.ERR_SyntaxError, "null").WithArguments("in", "null")
  568. );
  569. }
  570. [WorkItem(541779, "DevDiv")]
  571. [Fact]
  572. public void MultipleFromClauseQueryExpr()
  573. {
  574. var csSource = @"
  575. using System;
  576. using System.Linq;
  577. class Program
  578. {
  579. static void Main(string[] args)
  580. {
  581. var nums = new int[] { 3, 4 };
  582. var q2 = from int n1 in nums
  583. from int n2 in nums
  584. select n1;
  585. string serializer = String.Empty;
  586. foreach (var q in q2)
  587. {
  588. serializer = serializer + q + "" "";
  589. }
  590. System.Console.Write(serializer.Trim());
  591. }
  592. }";
  593. CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 3 4 4");
  594. }
  595. [WorkItem(541782, "DevDiv")]
  596. [Fact]
  597. public void FromSelectQueryExprOnArraysWithTypeImplicit()
  598. {
  599. var csSource = @"
  600. using System;
  601. using System.Linq;
  602. class Program
  603. {
  604. static void Main(string[] args)
  605. {
  606. var nums = new int[] { 3, 4 };
  607. var q2 = from n1 in nums select n1;
  608. string serializer = String.Empty;
  609. foreach (var q in q2)
  610. {
  611. serializer = serializer + q + "" "";
  612. }
  613. System.Console.Write(serializer.Trim());
  614. }
  615. }";
  616. CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 4");
  617. }
  618. [WorkItem(541788, "DevDiv")]
  619. [Fact]
  620. public void JoinClauseTest()
  621. {
  622. var csSource = @"
  623. using System;
  624. using System.Linq;
  625. class Program
  626. {
  627. static void Main()
  628. {
  629. var q2 =
  630. from a in Enumerable.Range(1, 13)
  631. join b in Enumerable.Range(1, 13) on 4 * a equals b
  632. select a;
  633. string serializer = String.Empty;
  634. foreach (var q in q2)
  635. {
  636. serializer = serializer + q + "" "";
  637. }
  638. System.Console.Write(serializer.Trim());
  639. }
  640. }";
  641. CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "1 2 3");
  642. }
  643. [WorkItem(541789, "DevDiv")]
  644. [WorkItem(9229, "DevDiv_Projects/Roslyn")]
  645. [Fact]
  646. public void WhereClauseTest()
  647. {
  648. var csSource = @"
  649. using System;
  650. using System.Linq;
  651. class Program
  652. {
  653. static void Main()
  654. {
  655. var nums = new int[] { 1, 2, 3, 4 };
  656. var q2 = from x in nums
  657. where (x > 2)
  658. select x;
  659. string serializer = String.Empty;
  660. foreach (var q in q2)
  661. {
  662. serializer = serializer + q + "" "";
  663. }
  664. System.Console.Write(serializer.Trim());
  665. }
  666. }";
  667. CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "3 4");
  668. }
  669. [WorkItem(541942, "DevDiv")]
  670. [Fact]
  671. public void WhereDefinedInType()
  672. {
  673. var csSource = @"
  674. using System;
  675. class Y
  676. {
  677. public int Where(Func<int, bool> predicate)
  678. {
  679. return 45;
  680. }
  681. }
  682. class P
  683. {
  684. static void Main()
  685. {
  686. var src = new Y();
  687. var query = from x in src
  688. where x > 0
  689. select x;
  690. Console.Write(query);
  691. }
  692. }";
  693. CompileAndVerify(csSource, additionalRefs: new[] { LinqAssemblyRef }, expectedOutput: "45");
  694. }
  695. [Fact]
  696. public void GetInfoForSelectExpression01()
  697. {
  698. string sourceCode = @"
  699. using System;
  700. using System.Linq;
  701. public class Test2
  702. {
  703. public static void Main()
  704. {
  705. var nums = new int[] { 1, 2, 3, 4 };
  706. var q2 = from x in nums
  707. select x;
  708. }
  709. }";
  710. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  711. var tree = compilation.SyntaxTrees[0];
  712. var semanticModel = compilation.GetSemanticModel(tree);
  713. SelectClauseSyntax selectClause = (SelectClauseSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("select")).Parent;
  714. var info = semanticModel.GetSemanticInfoSummary(selectClause.Expression);
  715. Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType);
  716. Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind);
  717. var info2 = semanticModel.GetSemanticInfoSummary(selectClause);
  718. var m = (MethodSymbol)info2.Symbol;
  719. Assert.Equal("Select", m.ReducedFrom.Name);
  720. }
  721. [Fact]
  722. public void GetInfoForSelectExpression02()
  723. {
  724. string sourceCode = @"
  725. using System;
  726. using System.Linq;
  727. public class Test2
  728. {
  729. public static void Main()
  730. {
  731. var nums = new int[] { 1, 2, 3, 4 };
  732. var q2 = from x in nums
  733. select x into w
  734. select w;
  735. }
  736. }";
  737. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  738. var tree = compilation.SyntaxTrees[0];
  739. var semanticModel = compilation.GetSemanticModel(tree);
  740. SelectClauseSyntax selectClause = (SelectClauseSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("select w")).Parent;
  741. var info = semanticModel.GetSemanticInfoSummary(selectClause.Expression);
  742. Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType);
  743. Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind);
  744. }
  745. [Fact]
  746. public void GetInfoForSelectExpression03()
  747. {
  748. string sourceCode = @"
  749. using System.Linq;
  750. public class Test2
  751. {
  752. public static void Main()
  753. {
  754. var nums = new int[] { 1, 2, 3, 4 };
  755. var q2 = from x in nums
  756. select x+1 into w
  757. select w+1;
  758. }
  759. }";
  760. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  761. var tree = compilation.SyntaxTrees[0];
  762. compilation.VerifyDiagnostics();
  763. var semanticModel = compilation.GetSemanticModel(tree);
  764. var e = (IdentifierNameSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("x+1")).Parent;
  765. var info = semanticModel.GetSemanticInfoSummary(e);
  766. Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType);
  767. Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind);
  768. Assert.Equal("x", info.Symbol.Name);
  769. e = (IdentifierNameSyntax)tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("w+1")).Parent;
  770. info = semanticModel.GetSemanticInfoSummary(e);
  771. Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType);
  772. Assert.Equal(SymbolKind.RangeVariable, info.Symbol.Kind);
  773. Assert.Equal("w", info.Symbol.Name);
  774. var e2 = e.Parent as ExpressionSyntax; // w+1
  775. var info2 = semanticModel.GetSemanticInfoSummary(e2);
  776. Assert.Equal(SpecialType.System_Int32, info2.Type.SpecialType);
  777. Assert.Equal("System.Int32 System.Int32.op_Addition(System.Int32 left, System.Int32 right)", info2.Symbol.ToTestDisplayString());
  778. }
  779. [WorkItem(541806, "DevDiv")]
  780. [Fact]
  781. public void GetDeclaredSymbolForQueryContinuation()
  782. {
  783. string sourceCode = @"
  784. public class Test2
  785. {
  786. public static void Main()
  787. {
  788. var nums = new int[] { 1, 2, 3, 4 };
  789. var q2 = from x in nums
  790. select x into w
  791. select w;
  792. }
  793. }";
  794. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  795. var tree = compilation.SyntaxTrees[0];
  796. var semanticModel = compilation.GetSemanticModel(tree);
  797. var queryContinuation = tree.GetRoot().FindToken(sourceCode.IndexOf("into w")).Parent;
  798. var symbol = semanticModel.GetDeclaredSymbol(queryContinuation);
  799. Assert.NotNull(symbol);
  800. Assert.Equal("w", symbol.Name);
  801. Assert.Equal(SymbolKind.RangeVariable, symbol.Kind);
  802. }
  803. [WorkItem(541899, "DevDiv")]
  804. [Fact]
  805. public void ComputeQueryVariableType()
  806. {
  807. string sourceCode = @"
  808. using System.Linq;
  809. public class Test2
  810. {
  811. public static void Main()
  812. {
  813. var nums = new int[] { 1, 2, 3, 4 };
  814. var q2 = from x in nums
  815. select 5;
  816. }
  817. }";
  818. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  819. var tree = compilation.SyntaxTrees[0];
  820. var semanticModel = compilation.GetSemanticModel(tree);
  821. var selectExpression = tree.GetCompilationUnitRoot().FindToken(sourceCode.IndexOf("5"));
  822. var info = semanticModel.GetSpeculativeTypeInfo(selectExpression.SpanStart, SyntaxFactory.ParseExpression("x"), SpeculativeBindingOption.BindAsExpression);
  823. Assert.Equal(SpecialType.System_Int32, info.Type.SpecialType);
  824. }
  825. [WorkItem(541893, "DevDiv")]
  826. [Fact]
  827. public void GetDeclaredSymbolForJoinIntoClause()
  828. {
  829. string sourceCode = @"
  830. using System;
  831. using System.Linq;
  832. static class Test
  833. {
  834. static void Main()
  835. {
  836. var qie = from x3 in new int[] { 0 }
  837. join x7 in (new int[] { 0 }) on 5 equals 5 into x8
  838. select x8;
  839. }
  840. }";
  841. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  842. var tree = compilation.SyntaxTrees[0];
  843. var semanticModel = compilation.GetSemanticModel(tree);
  844. var joinInto = tree.GetRoot().FindToken(sourceCode.IndexOf("into x8")).Parent;
  845. var symbol = semanticModel.GetDeclaredSymbol(joinInto);
  846. Assert.NotNull(symbol);
  847. Assert.Equal("x8", symbol.Name);
  848. Assert.Equal(SymbolKind.RangeVariable, symbol.Kind);
  849. Assert.Equal("? x8", symbol.ToTestDisplayString());
  850. }
  851. [WorkItem(541982, "DevDiv")]
  852. [WorkItem(543494, "DevDiv")]
  853. [Fact()]
  854. public void GetDeclaredSymbolAddAccessorDeclIncompleteQuery()
  855. {
  856. string sourceCode = @"
  857. using System;
  858. using System.Linq;
  859. public class QueryExpressionTest
  860. {
  861. public static void Main()
  862. {
  863. var expr1 = new[] { 1, 2, 3, 4, 5 };
  864. var query1 = from event in expr1 select event;
  865. var query2 = from int
  866. }
  867. }";
  868. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  869. var tree = compilation.SyntaxTrees[0];
  870. var semanticModel = compilation.GetSemanticModel(tree);
  871. var unknownAccessorDecls = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AccessorDeclarationSyntax>();
  872. var symbols = unknownAccessorDecls.Select(decl => semanticModel.GetDeclaredSymbol(decl));
  873. Assert.True(symbols.All(s => ReferenceEquals(s, null)));
  874. }
  875. [WorkItem(542235, "DevDiv")]
  876. [Fact]
  877. public void TwoFromClauseFollowedBySelectClause()
  878. {
  879. string sourceCode = @"
  880. using System.Linq;
  881. class Test
  882. {
  883. public static void Main()
  884. {
  885. var q2 = from num1 in new int[] { 4, 5 }
  886. from num2 in new int[] { 4, 5 }
  887. select num1;
  888. }
  889. }";
  890. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  891. var tree = compilation.SyntaxTrees[0];
  892. var semanticModel = compilation.GetSemanticModel(tree);
  893. var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax;
  894. var fromClause1 = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => (n.IsKind(SyntaxKind.FromClause)) && (n.ToString().Contains("num1"))).Single() as FromClauseSyntax;
  895. var fromClause2 = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => (n.IsKind(SyntaxKind.FromClause)) && (n.ToString().Contains("num2"))).Single() as FromClauseSyntax;
  896. var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause);
  897. var queryInfoForFrom1 = semanticModel.GetQueryClauseInfo(fromClause1);
  898. var queryInfoForFrom2 = semanticModel.GetQueryClauseInfo(fromClause2);
  899. Assert.Null(queryInfoForFrom1.CastInfo.Symbol);
  900. Assert.Null(queryInfoForFrom1.OperationInfo.Symbol);
  901. Assert.Null(queryInfoForFrom2.CastInfo.Symbol);
  902. Assert.Equal("SelectMany", queryInfoForFrom2.OperationInfo.Symbol.Name);
  903. Assert.Null(symbolInfoForSelect.Symbol);
  904. Assert.Empty(symbolInfoForSelect.CandidateSymbols);
  905. Assert.Equal(CandidateReason.None, symbolInfoForSelect.CandidateReason);
  906. }
  907. [WorkItem(528747, "DevDiv")]
  908. [Fact]
  909. public void SemanticInfoForOrderingClauses()
  910. {
  911. string sourceCode = @"
  912. using System;
  913. using System.Linq;
  914. public class QueryExpressionTest
  915. {
  916. public static void Main()
  917. {
  918. var q1 =
  919. from x in new int[] { 4, 5 }
  920. orderby
  921. x descending,
  922. x.ToString() ascending,
  923. x descending
  924. select x;
  925. }
  926. }";
  927. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  928. var tree = compilation.SyntaxTrees[0];
  929. var model = compilation.GetSemanticModel(tree);
  930. int count = 0;
  931. string[] names = { "OrderByDescending", "ThenBy", "ThenByDescending" };
  932. foreach (var ordering in tree.GetCompilationUnitRoot().DescendantNodes().OfType<OrderingSyntax>())
  933. {
  934. var symbolInfo = model.GetSemanticInfoSummary(ordering);
  935. Assert.Equal(names[count++], symbolInfo.Symbol.Name);
  936. }
  937. Assert.Equal(3, count);
  938. }
  939. [WorkItem(542266, "DevDiv")]
  940. [Fact]
  941. public void FromOrderBySelectQueryTranslation()
  942. {
  943. string sourceCode = @"
  944. using System;
  945. using System.Collections;
  946. using System.Collections.Generic;
  947. public interface IOrderedEnumerable<TElement> : IEnumerable<TElement>,
  948. IEnumerable
  949. {
  950. }
  951. public static class Extensions
  952. {
  953. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
  954. this IEnumerable<TSource> source,
  955. Func<TSource, TKey> keySelector)
  956. {
  957. return null;
  958. }
  959. public static IEnumerable<TResult> Select<TSource, TResult>(
  960. this IEnumerable<TSource> source,
  961. Func<TSource, TResult> selector)
  962. {
  963. return null;
  964. }
  965. }
  966. class Program
  967. {
  968. static void Main(string[] args)
  969. {
  970. var q1 = from num in new int[] { 4, 5 }
  971. orderby num
  972. select num;
  973. }
  974. }";
  975. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  976. var tree = compilation.SyntaxTrees[0];
  977. var semanticModel = compilation.GetSemanticModel(tree);
  978. var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax;
  979. var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause);
  980. Assert.Null(symbolInfoForSelect.Symbol);
  981. }
  982. [WorkItem(528756, "DevDiv")]
  983. [Fact]
  984. public void FromWhereSelectTranslation()
  985. {
  986. string sourceCode = @"
  987. using System;
  988. using System.Collections.Generic;
  989. public static class Extensions
  990. {
  991. public static IEnumerable<TSource> Where<TSource>(
  992. this IEnumerable<TSource> source,
  993. Func<TSource, bool> predicate)
  994. {
  995. return null;
  996. }
  997. }
  998. class Program
  999. {
  1000. static void Main(string[] args)
  1001. {
  1002. var q1 = from num in System.Linq.Enumerable.Range(4, 5).Where(n => n > 10)
  1003. select num;
  1004. }
  1005. }";
  1006. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1007. var tree = compilation.SyntaxTrees[0];
  1008. var semanticModel = compilation.GetSemanticModel(tree);
  1009. semanticModel.GetDiagnostics().Verify(
  1010. // (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'?
  1011. // var q1 = from num in System.Linq.Enumerable.Range(4, 5).Where(n => n > 10)
  1012. Diagnostic(ErrorCode.ERR_QueryNoProviderStandard, "System.Linq.Enumerable.Range(4, 5).Where(n => n > 10)").WithArguments("System.Collections.Generic.IEnumerable<int>", "Select"));
  1013. }
  1014. [WorkItem(528760, "DevDiv")]
  1015. [Fact]
  1016. public void FromJoinSelectTranslation()
  1017. {
  1018. string sourceCode = @"
  1019. using System.Linq;
  1020. class Program
  1021. {
  1022. static void Main(string[] args)
  1023. {
  1024. var q1 = from num in new int[] { 4, 5 }
  1025. join x1 in new int[] { 4, 5 } on num equals x1
  1026. select x1 + 5;
  1027. }
  1028. }";
  1029. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1030. var tree = compilation.SyntaxTrees[0];
  1031. var semanticModel = compilation.GetSemanticModel(tree);
  1032. var selectClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.SelectClause)).Single() as SelectClauseSyntax;
  1033. var symbolInfoForSelect = semanticModel.GetSemanticInfoSummary(selectClause);
  1034. Assert.Null(symbolInfoForSelect.Symbol);
  1035. }
  1036. [WorkItem(528761, "DevDiv")]
  1037. [WorkItem(544585, "DevDiv")]
  1038. [Fact]
  1039. public void OrderingSyntaxWithOverloadResolutionFailure()
  1040. {
  1041. string sourceCode = @"
  1042. using System.Linq;
  1043. class Program
  1044. {
  1045. static void Main(string[] args)
  1046. {
  1047. int[] numbers = new int[] { 4, 5 };
  1048. var q1 = from num in numbers.Single()
  1049. orderby (x1) => x1.ToString()
  1050. select num;
  1051. }
  1052. }";
  1053. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1054. compilation.VerifyDiagnostics(
  1055. // (10,30): error CS1936: Could not find an implementation of the query pattern for source type 'int'. 'OrderBy' not found.
  1056. // var q1 = from num in numbers.Single()
  1057. Diagnostic(ErrorCode.ERR_QueryNoProvider, "numbers.Single()").WithArguments("int", "OrderBy")
  1058. );
  1059. var tree = compilation.SyntaxTrees[0];
  1060. var semanticModel = compilation.GetSemanticModel(tree);
  1061. var orderingClause = tree.GetCompilationUnitRoot().DescendantNodes().Where(n => n.IsKind(SyntaxKind.AscendingOrdering)).Single() as OrderingSyntax;
  1062. var symbolInfoForOrdering = semanticModel.GetSemanticInfoSummary(orderingClause);
  1063. Assert.Null(symbolInfoForOrdering.Symbol);
  1064. }
  1065. [WorkItem(542292, "DevDiv")]
  1066. [Fact]
  1067. public void EmitIncompleteQueryWithSyntaxErrors()
  1068. {
  1069. string sourceCode = @"
  1070. using System.Linq;
  1071. class Program
  1072. {
  1073. static int Main()
  1074. {
  1075. int [] foo = new int [] {1};
  1076. var q = from x in foo
  1077. select x + 1 into z
  1078. select z.T
  1079. ";
  1080. using (var output = new MemoryStream())
  1081. {
  1082. Assert.False(CreateCompilationWithMscorlibAndSystemCore(sourceCode).Emit(output).Success);
  1083. }
  1084. }
  1085. [WorkItem(542294, "DevDiv")]
  1086. [Fact]
  1087. public void EmitQueryWithBindErrors()
  1088. {
  1089. string sourceCode = @"
  1090. using System.Linq;
  1091. class Program
  1092. {
  1093. static void Main()
  1094. {
  1095. int[] nums = { 0, 1, 2, 3, 4, 5 };
  1096. var query = from num in nums
  1097. let num = 3 // CS1930
  1098. select num;
  1099. }
  1100. }";
  1101. using (var output = new MemoryStream())
  1102. {
  1103. Assert.False(CreateCompilationWithMscorlibAndSystemCore(sourceCode).Emit(output).Success);
  1104. }
  1105. }
  1106. [WorkItem(542372, "DevDiv")]
  1107. [Fact]
  1108. public void BindToIncompleteSelectManyDecl()
  1109. {
  1110. string sourceCode = @"
  1111. class P
  1112. {
  1113. static C<X> M2<X>(X x)
  1114. {
  1115. return new C<X>(x);
  1116. }
  1117. static void Main()
  1118. {
  1119. C<int> e1 = new C<int>(1);
  1120. var q = from x1 in M2<int>(x1)
  1121. from x2 in e1
  1122. select x1;
  1123. }
  1124. }
  1125. class C<T>
  1126. {
  1127. public C<V> SelectMany";
  1128. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1129. var tree = compilation.SyntaxTrees[0];
  1130. var semanticModel = compilation.GetSemanticModel(tree);
  1131. var diags = semanticModel.GetDiagnostics();
  1132. Assert.NotEmpty(diags);
  1133. }
  1134. [WorkItem(542419, "DevDiv")]
  1135. [Fact]
  1136. public void BindIdentifierInWhereErrorTolerance()
  1137. {
  1138. string sourceCode = @"
  1139. using System;
  1140. using System.Collections.Generic;
  1141. using System.Linq;
  1142. class Program
  1143. {
  1144. static void Main(string[] args)
  1145. {
  1146. var r = args.Where(b => b < > );
  1147. var q = from a in args
  1148. where a <>
  1149. }
  1150. }";
  1151. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1152. var tree = compilation.SyntaxTrees[0];
  1153. var semanticModel = compilation.GetSemanticModel(tree);
  1154. var diags = semanticModel.GetDiagnostics();
  1155. Assert.NotEmpty(diags);
  1156. }
  1157. [WorkItem(542460, "DevDiv")]
  1158. [Fact]
  1159. public void QueryWithMultipleParseErrorsAndInteractiveParseOption()
  1160. {
  1161. string sourceCode = @"
  1162. using System;
  1163. using System.Linq;
  1164. public class QueryExpressionTest
  1165. {
  1166. public static void Main()
  1167. {
  1168. var expr1 = new int[] { 1, 2, 3, 4, 5 };
  1169. var query2 = from int namespace in expr1 select namespace;
  1170. var query25 = from i in expr1 let namespace = expr1 select i;
  1171. }
  1172. }";
  1173. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode, parseOptions: TestOptions.Interactive);
  1174. var tree = compilation.SyntaxTrees[0];
  1175. var semanticModel = compilation.GetSemanticModel(tree);
  1176. var queryExpr = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryExpressionSyntax>().Where(x => x.ToFullString() == "from i in expr1 let ").Single();
  1177. var symbolInfo = semanticModel.GetSemanticInfoSummary(queryExpr);
  1178. Assert.Null(symbolInfo.Symbol);
  1179. }
  1180. [WorkItem(542496, "DevDiv")]
  1181. [Fact]
  1182. public void QueryExpressionInFieldInitReferencingAnotherFieldWithInteractiveParseOption()
  1183. {
  1184. string sourceCode = @"
  1185. using System.Linq;
  1186. using System.Collections;
  1187. class P
  1188. {
  1189. double one = 1;
  1190. public IEnumerable e =
  1191. from x in new int[] { 1, 2, 3 }
  1192. select x + one;
  1193. }";
  1194. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode, parseOptions: TestOptions.Interactive);
  1195. var tree = compilation.SyntaxTrees[0];
  1196. var semanticModel = compilation.GetSemanticModel(tree);
  1197. var queryExpr = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryExpressionSyntax>().Single();
  1198. var symbolInfo = semanticModel.GetSemanticInfoSummary(queryExpr);
  1199. Assert.Null(symbolInfo.Symbol);
  1200. }
  1201. [WorkItem(542559, "DevDiv")]
  1202. [Fact]
  1203. public void StaticTypeInFromClause()
  1204. {
  1205. string sourceCode = @"
  1206. using System;
  1207. using System.Linq;
  1208. class C
  1209. {
  1210. static void Main()
  1211. {
  1212. var q2 = string.Empty.Cast<GC>().Select(x => x);
  1213. var q1 = from GC x in string.Empty select x;
  1214. }
  1215. }";
  1216. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1217. compilation.VerifyDiagnostics(
  1218. // (9,36): error CS0718: 'System.GC': static types cannot be used as type arguments
  1219. // var q2 = string.Empty.Cast<GC>().Select(x => x);
  1220. Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "GC").WithArguments("System.GC").WithLocation(9,36),
  1221. // (10,23): error CS0718: 'System.GC': static types cannot be used as type arguments
  1222. // var q1 = from GC x in string.Empty select x;
  1223. Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "GC").WithArguments("System.GC").WithLocation(10,23)
  1224. );
  1225. }
  1226. [WorkItem(542560, "DevDiv")]
  1227. [Fact]
  1228. public void MethodGroupInFromClause()
  1229. {
  1230. string sourceCode = @"
  1231. class Program
  1232. {
  1233. static void Main()
  1234. {
  1235. var q1 = from y in Main select y;
  1236. var q2 = Main.Select(y => y);
  1237. }
  1238. }";
  1239. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1240. compilation.VerifyDiagnostics(
  1241. // (6,28): error CS0119: 'Program.Main()' is a method, which is not valid in the given context
  1242. // var q1 = from y in Main select y;
  1243. Diagnostic(ErrorCode.ERR_BadSKunknown, "Main").WithArguments("Program.Main()", "method").WithLocation(6,28),
  1244. // (7,18): error CS0119: 'Program.Main()' is a method, which is not valid in the given context
  1245. // var q2 = Main.Select(y => y);
  1246. Diagnostic(ErrorCode.ERR_BadSKunknown, "Main").WithArguments("Program.Main()", "method").WithLocation(7,18)
  1247. );
  1248. }
  1249. [WorkItem(542558, "DevDiv")]
  1250. [Fact]
  1251. public void SelectFromType01()
  1252. {
  1253. string sourceCode = @"using System;
  1254. using System.Collections.Generic;
  1255. class C
  1256. {
  1257. static void Main()
  1258. {
  1259. var q = from x in C select x;
  1260. }
  1261. static IEnumerable<T> Select<T>(Func<int, T> f) { return null; }
  1262. }";
  1263. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1264. compilation.VerifyDiagnostics();
  1265. var tree = compilation.SyntaxTrees[0];
  1266. var model = compilation.GetSemanticModel(tree);
  1267. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "C").Single();
  1268. dynamic main = (MethodDeclarationSyntax)classC.Members[0];
  1269. QueryExpressionSyntax q = main.Body.Statements[0].Declaration.Variables[0].Initializer.Value;
  1270. var info0 = model.GetQueryClauseInfo(q.FromClause);
  1271. var x = model.GetDeclaredSymbol(q.FromClause);
  1272. Assert.Equal(SymbolKind.RangeVariable, x.Kind);
  1273. Assert.Equal("x", x.Name);
  1274. Assert.Equal(null, info0.CastInfo.Symbol);
  1275. Assert.Null(info0.OperationInfo.Symbol);
  1276. var infoSelect = model.GetSemanticInfoSummary(q.Body.SelectOrGroup);
  1277. Assert.Equal("Select", infoSelect.Symbol.Name);
  1278. }
  1279. [WorkItem(542558, "DevDiv")]
  1280. [Fact]
  1281. public void SelectFromType02()
  1282. {
  1283. string sourceCode = @"using System;
  1284. using System.Collections.Generic;
  1285. class C
  1286. {
  1287. static void Main()
  1288. {
  1289. var q = from x in C select x;
  1290. }
  1291. static Func<Func<int, object>, IEnumerable<object>> Select = null;
  1292. }";
  1293. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1294. compilation.VerifyDiagnostics();
  1295. var tree = compilation.SyntaxTrees[0];
  1296. var model = compilation.GetSemanticModel(tree);
  1297. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "C").Single();
  1298. dynamic main = (MethodDeclarationSyntax)classC.Members[0];
  1299. QueryExpressionSyntax q = main.Body.Statements[0].Declaration.Variables[0].Initializer.Value;
  1300. var info0 = model.GetQueryClauseInfo(q.FromClause);
  1301. var x = model.GetDeclaredSymbol(q.FromClause);
  1302. Assert.Equal(SymbolKind.RangeVariable, x.Kind);
  1303. Assert.Equal("x", x.Name);
  1304. Assert.Equal(null, info0.CastInfo.Symbol);
  1305. Assert.Null(info0.OperationInfo.Symbol);
  1306. var infoSelect = model.GetSemanticInfoSummary(q.Body.SelectOrGroup);
  1307. Assert.Equal("Invoke", infoSelect.Symbol.Name);
  1308. }
  1309. [WorkItem(542624, "DevDiv")]
  1310. [Fact]
  1311. public void QueryColorColor()
  1312. {
  1313. string sourceCode = @"
  1314. using System;
  1315. using System.Collections.Generic;
  1316. class Color
  1317. {
  1318. public static IEnumerable<T> Select<T>(Func<int, T> f) { return null; }
  1319. }
  1320. class Flavor
  1321. {
  1322. public IEnumerable<T> Select<T>(Func<int, T> f) { return null; }
  1323. }
  1324. class Program
  1325. {
  1326. Color Color;
  1327. static Flavor Flavor;
  1328. static void Main()
  1329. {
  1330. var q1 = from x in Color select x;
  1331. var q2 = from x in Flavor select x;
  1332. }
  1333. }";
  1334. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1335. compilation.VerifyDiagnostics(
  1336. // (17,11): warning CS0169: The field 'Program.Color' is never used
  1337. // Color Color;
  1338. Diagnostic(ErrorCode.WRN_UnreferencedField, "Color").WithArguments("Program.Color"),
  1339. // (18,19): warning CS0649: Field 'Program.Flavor' is never assigned to, and will always have its default value null
  1340. // static Flavor Flavor;
  1341. Diagnostic(ErrorCode.WRN_UnassignedInternalField, "Flavor").WithArguments("Program.Flavor", "null")
  1342. );
  1343. }
  1344. [WorkItem(542704, "DevDiv")]
  1345. [Fact]
  1346. public void QueryOnSourceWithGroupByMethod()
  1347. {
  1348. string source = @"
  1349. delegate T Func<A, T>(A a);
  1350. class Y<U>
  1351. {
  1352. public U u;
  1353. public Y(U u)
  1354. {
  1355. this.u = u;
  1356. }
  1357. public string GroupBy(Func<U, string> keySelector)
  1358. {
  1359. return null;
  1360. }
  1361. }
  1362. class Test
  1363. {
  1364. static int Main()
  1365. {
  1366. Y<int> src = new Y<int>(2);
  1367. string q1 = src.GroupBy(x => x.GetType().Name); // ok
  1368. string q2 = from x in src group x by x.GetType().Name; // Roslyn CS1501
  1369. return 0;
  1370. }
  1371. }
  1372. ";
  1373. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1374. compilation.VerifyDiagnostics();
  1375. }
  1376. [Fact]
  1377. public void RangeTypeAlreadySpecified()
  1378. {
  1379. string sourceCode =
  1380. @"using System.Linq;
  1381. using System.Collections;
  1382. static class Test
  1383. {
  1384. public static void Main2()
  1385. {
  1386. var list = new CastableToArrayList();
  1387. var q = from int x in list
  1388. select x + 1;
  1389. }
  1390. }
  1391. class CastableToArrayList
  1392. {
  1393. public ArrayList Cast<T>() { return null; }
  1394. }";
  1395. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1396. compilation.VerifyDiagnostics(
  1397. // (9,31): error CS1936: Could not find an implementation of the query pattern for source type 'System.Collections.ArrayList'. 'Select' not found.
  1398. // var q = from int x in list
  1399. Diagnostic(ErrorCode.ERR_QueryNoProvider, "list").WithArguments("System.Collections.ArrayList", "Select")
  1400. );
  1401. }
  1402. [WorkItem(11414, "DevDiv_Projects/Roslyn")]
  1403. [Fact]
  1404. public void InvalidQueryWithAnonTypesAndKeywords()
  1405. {
  1406. string source = @"
  1407. public class QueryExpressionTest
  1408. {
  1409. public static void Main()
  1410. {
  1411. var query7 = from i in expr1 join const in expr2 on i equals const select new { i, const };
  1412. var query8 = from int i in expr1 select new { i, const };
  1413. }
  1414. }
  1415. ";
  1416. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1417. Assert.NotEmpty(compilation.GetDiagnostics());
  1418. }
  1419. [WorkItem(543787, "DevDiv")]
  1420. [Fact]
  1421. public void GetSymbolInfoOfSelectNodeWhenTypeOfRangeVariableIsErrorType()
  1422. {
  1423. string source = @"
  1424. using System.Linq;
  1425. class Test
  1426. {
  1427. static void V()
  1428. {
  1429. }
  1430. public static int Main()
  1431. {
  1432. var e1 = from i in V() select i;
  1433. }
  1434. }
  1435. ";
  1436. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1437. var tree = compilation.SyntaxTrees.First();
  1438. var index = source.IndexOf("select i");
  1439. var selectNode = tree.GetCompilationUnitRoot().FindToken(index).Parent as SelectClauseSyntax;
  1440. var model = compilation.GetSemanticModel(tree);
  1441. var symbolInfo = model.GetSymbolInfo(selectNode);
  1442. Assert.NotNull(symbolInfo);
  1443. Assert.Null(symbolInfo.Symbol); // there is no select method to call because the receiver is bad
  1444. var typeInfo = model.GetTypeInfo(selectNode);
  1445. Assert.Equal(SymbolKind.ErrorType, typeInfo.Type.Kind);
  1446. }
  1447. [WorkItem(543790, "DevDiv")]
  1448. [Fact]
  1449. public void GetQueryClauseInfoForQueryWithSyntaxErrors()
  1450. {
  1451. string source = @"
  1452. using System.Linq;
  1453. class Test
  1454. {
  1455. public static void Main ()
  1456. {
  1457. var query8 = from int i in expr1 join int delegate in expr2 on i equals delegate select new { i, delegate };
  1458. }
  1459. }
  1460. ";
  1461. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1462. var tree = compilation.SyntaxTrees.First();
  1463. var index = source.IndexOf("join int delegate in expr2 on i equals delegate");
  1464. var joinNode = tree.GetCompilationUnitRoot().FindToken(index).Parent as JoinClauseSyntax;
  1465. var model = compilation.GetSemanticModel(tree);
  1466. var queryInfo = model.GetQueryClauseInfo(joinNode);
  1467. Assert.NotNull(queryInfo);
  1468. }
  1469. [WorkItem(545797, "DevDiv")]
  1470. [Fact]
  1471. public void QueryOnNull()
  1472. {
  1473. string source = @"using System;
  1474. static class C
  1475. {
  1476. static void Main()
  1477. {
  1478. var q = from x in null select x;
  1479. }
  1480. static object Select(this object x, Func<int, int> y)
  1481. {
  1482. return null;
  1483. }
  1484. }
  1485. ";
  1486. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1487. compilation.VerifyDiagnostics(
  1488. // (6,32): error CS0186: Use of null is not valid in this context
  1489. // var q = from x in null select x;
  1490. Diagnostic(ErrorCode.ERR_NullNotValid, "select x")
  1491. );
  1492. }
  1493. [WorkItem(545797, "DevDiv")]
  1494. [Fact]
  1495. public void QueryOnLambda()
  1496. {
  1497. string source = @"using System;
  1498. static class C
  1499. {
  1500. static void Main()
  1501. {
  1502. var q = from x in y=>y select x;
  1503. }
  1504. static object Select(this object x, Func<int, int> y)
  1505. {
  1506. return null;
  1507. }
  1508. }
  1509. ";
  1510. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1511. compilation.VerifyDiagnostics(
  1512. // (6,32): error CS1936: Could not find an implementation of the query pattern for source type 'anonymous method'. 'Select' not found.
  1513. // var q = from x in y=>y select x;
  1514. Diagnostic(ErrorCode.ERR_QueryNoProvider, "select x").WithArguments("anonymous method", "Select")
  1515. );
  1516. }
  1517. [WorkItem(545444, "DevDiv")]
  1518. [Fact]
  1519. public void RefOmittedOnComCall()
  1520. {
  1521. string source = @"using System;
  1522. using System.Linq.Expressions;
  1523. using System.Runtime.InteropServices;
  1524. [ComImport]
  1525. [Guid(""A88A175D-2448-447A-B786-64682CBEF156"")]
  1526. public interface IRef1
  1527. {
  1528. int M(ref int x, int y);
  1529. }
  1530. public class Ref1Impl : IRef1
  1531. {
  1532. public int M(ref int x, int y) { return x + y; }
  1533. }
  1534. class Test
  1535. {
  1536. public static void Main()
  1537. {
  1538. IRef1 ref1 = new Ref1Impl();
  1539. Expression<Func<int, int, int>> F = (x, y) => ref1.M(x, y);
  1540. }
  1541. }";
  1542. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1543. compilation.VerifyDiagnostics(
  1544. // (22,54): error CS2037: An expression tree lambda may not contain a COM call with ref omitted on arguments
  1545. // Expression<Func<int, int, int>> F = (x, y) => ref1.M(x, y);
  1546. Diagnostic(ErrorCode.ERR_ComRefCallInExpressionTree, "ref1.M(x, y)")
  1547. );
  1548. }
  1549. [WorkItem(529350, "DevDiv")]
  1550. [Fact]
  1551. public void BindLambdaBodyWhenError()
  1552. {
  1553. string source =
  1554. @"using System.Linq;
  1555. class A
  1556. {
  1557. static void Main()
  1558. {
  1559. }
  1560. static void M(System.Reflection.Assembly[] a)
  1561. {
  1562. var q2 = a.SelectMany(assem2 => assem2.UNDEFINED, (assem2, t) => t);
  1563. var q1 = from assem1 in a
  1564. from t in assem1.UNDEFINED
  1565. select t;
  1566. }
  1567. }";
  1568. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  1569. compilation.VerifyDiagnostics(
  1570. // (10,48): error CS1061: 'System.Reflection.Assembly' does not contain a definition for 'UNDEFINED' and no extension method 'UNDEFINED' accepting a first argument of type 'System.Reflection.Assembly' could be found (are you missing a using directive or an assembly reference?)
  1571. // var q2 = a.SelectMany(assem2 => assem2.UNDEFINED, (assem2, t) => t);
  1572. Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "UNDEFINED").WithArguments("System.Reflection.Assembly", "UNDEFINED"),
  1573. // (13,35): error CS1061: 'System.Reflection.Assembly' does not contain a definition for 'UNDEFINED' and no extension method 'UNDEFINED' accepting a first argument of type 'System.Reflection.Assembly' could be found (are you missing a using directive or an assembly reference?)
  1574. // from t in assem1.UNDEFINED
  1575. Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "UNDEFINED").WithArguments("System.Reflection.Assembly", "UNDEFINED")
  1576. );
  1577. var tree = compilation.SyntaxTrees[0];
  1578. var model = compilation.GetSemanticModel(tree);
  1579. var assem2 =
  1580. tree.GetCompilationUnitRoot().DescendantNodes(n => n.ToString().Contains("assem2"))
  1581. .Where(e => e.ToString() == "assem2")
  1582. .OfType<ExpressionSyntax>()
  1583. .Single();
  1584. var typeInfo2 = model.GetTypeInfo(assem2);
  1585. Assert.NotEqual(TypeKind.Error, typeInfo2.Type.TypeKind);
  1586. Assert.Equal("Assembly", typeInfo2.Type.Name);
  1587. var assem1 =
  1588. tree.GetCompilationUnitRoot().DescendantNodes(n => n.ToString().Contains("assem1"))
  1589. .Where(e => e.ToString() == "assem1")
  1590. .OfType<ExpressionSyntax>()
  1591. .Single();
  1592. var typeInfo1 = model.GetTypeInfo(assem1);
  1593. Assert.NotEqual(TypeKind.Error, typeInfo1.Type.TypeKind);
  1594. Assert.Equal("Assembly", typeInfo1.Type.Name);
  1595. }
  1596. [Fact]
  1597. public void TestSpeculativeSemanticModel_GetQueryClauseInfo()
  1598. {
  1599. var csSource = @"
  1600. using C = List1<int>;" + LINQ + @"
  1601. class Query
  1602. {
  1603. public static void Main(string[] args)
  1604. {
  1605. C c1 = new C(1, 2, 3);
  1606. C c2 = new C(10, 20, 30);
  1607. }
  1608. }";
  1609. var speculatedSource = @"
  1610. C r1 =
  1611. from int x in c1
  1612. from int y in c2
  1613. select x + y;
  1614. ";
  1615. var queryStatement = (LocalDeclarationStatementSyntax)SyntaxFactory.ParseStatement(speculatedSource);
  1616. var compilation = CreateCompilationWithMscorlib(csSource);
  1617. compilation.VerifyDiagnostics();
  1618. var tree = compilation.SyntaxTrees[0];
  1619. var model = compilation.GetSemanticModel(tree);
  1620. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single();
  1621. var methodM = (MethodDeclarationSyntax)classC.Members[0];
  1622. SemanticModel speculativeModel;
  1623. bool success = model.TryGetSpeculativeSemanticModel(methodM.Body.Statements[1].Span.End, queryStatement, out speculativeModel);
  1624. Assert.True(success);
  1625. var q = (QueryExpressionSyntax)queryStatement.Declaration.Variables[0].Initializer.Value;
  1626. var info0 = speculativeModel.GetQueryClauseInfo(q.FromClause);
  1627. Assert.Equal("Cast", info0.CastInfo.Symbol.Name);
  1628. Assert.Null(info0.OperationInfo.Symbol);
  1629. Assert.Equal("x", speculativeModel.GetDeclaredSymbol(q.FromClause).Name);
  1630. var info1 = speculativeModel.GetQueryClauseInfo(q.Body.Clauses[0]);
  1631. Assert.Equal("Cast", info1.CastInfo.Symbol.Name);
  1632. Assert.Equal("SelectMany", info1.OperationInfo.Symbol.Name);
  1633. Assert.Equal("y", speculativeModel.GetDeclaredSymbol(q.Body.Clauses[0]).Name);
  1634. }
  1635. [Fact]
  1636. public void TestSpeculativeSemanticModel_GetSemanticInfoForSelectClause()
  1637. {
  1638. var csSource = @"
  1639. using C = List1<int>;" + LINQ + @"
  1640. class Query
  1641. {
  1642. public static void Main(string[] args)
  1643. {
  1644. C c1 = new C(1, 2, 3);
  1645. C c2 = new C(10, 20, 30);
  1646. }
  1647. }";
  1648. var speculatedSource = @"
  1649. C r1 =
  1650. from int x in c1
  1651. select x;
  1652. ";
  1653. var queryStatement = (LocalDeclarationStatementSyntax)SyntaxFactory.ParseStatement(speculatedSource);
  1654. var compilation = CreateCompilationWithMscorlib(csSource);
  1655. compilation.VerifyDiagnostics();
  1656. var tree = compilation.SyntaxTrees[0];
  1657. var model = compilation.GetSemanticModel(tree);
  1658. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Query").Single();
  1659. var methodM = (MethodDeclarationSyntax)classC.Members[0];
  1660. SemanticModel speculativeModel;
  1661. bool success = model.TryGetSpeculativeSemanticModel(methodM.Body.Statements[1].Span.End, queryStatement, out speculativeModel);
  1662. Assert.True(success);
  1663. var q = (QueryExpressionSyntax)queryStatement.Declaration.Variables[0].Initializer.Value;
  1664. var x = speculativeModel.GetDeclaredSymbol(q.FromClause);
  1665. Assert.Equal(SymbolKind.RangeVariable, x.Kind);
  1666. Assert.Equal("x", x.Name);
  1667. var selectExpression = (q.Body.SelectOrGroup as SelectClauseSyntax).Expression;
  1668. Assert.Equal(x, speculativeModel.GetSemanticInfoSummary(selectExpression).Symbol);
  1669. var selectClauseSymbolInfo = speculativeModel.GetSymbolInfo(q.Body.SelectOrGroup);
  1670. Assert.NotNull(selectClauseSymbolInfo.Symbol);
  1671. Assert.Equal("Select", selectClauseSymbolInfo.Symbol.Name);
  1672. var selectClauseTypeInfo = speculativeModel.GetTypeInfo(q.Body.SelectOrGroup);
  1673. Assert.NotNull(selectClauseTypeInfo.Type);
  1674. Assert.Equal("List1", selectClauseTypeInfo.Type.Name);
  1675. }
  1676. [Fact]
  1677. public void TestSpeculativeSemanticModel_GetDeclaredSymbolForJoinIntoClause()
  1678. {
  1679. string sourceCode = @"
  1680. public class Test
  1681. {
  1682. public static void Main()
  1683. {
  1684. }
  1685. }";
  1686. var speculatedSource = @"
  1687. var qie = from x3 in new int[] { 0 }
  1688. join x7 in (new int[] { 1 }) on 5 equals 5 into x8
  1689. select x8;
  1690. ";
  1691. var queryStatement = SyntaxFactory.ParseStatement(speculatedSource);
  1692. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1693. compilation.VerifyDiagnostics();
  1694. var tree = compilation.SyntaxTrees[0];
  1695. var model = compilation.GetSemanticModel(tree);
  1696. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Test").Single();
  1697. var methodM = (MethodDeclarationSyntax)classC.Members[0];
  1698. SemanticModel speculativeModel;
  1699. bool success = model.TryGetSpeculativeSemanticModel(methodM.Body.SpanStart, queryStatement, out speculativeModel);
  1700. var queryExpression = (QueryExpressionSyntax)((LocalDeclarationStatementSyntax)queryStatement).Declaration.Variables[0].Initializer.Value;
  1701. JoinIntoClauseSyntax joinInto = ((JoinClauseSyntax)queryExpression.Body.Clauses[0]).Into;
  1702. var symbol = speculativeModel.GetDeclaredSymbol(joinInto);
  1703. Assert.NotNull(symbol);
  1704. Assert.Equal("x8", symbol.Name);
  1705. Assert.Equal(SymbolKind.RangeVariable, symbol.Kind);
  1706. Assert.Equal("? x8", symbol.ToTestDisplayString());
  1707. }
  1708. [Fact]
  1709. public void TestSpeculativeSemanticModel_GetDeclaredSymbolForQueryContinuation()
  1710. {
  1711. string sourceCode = @"
  1712. public class Test2
  1713. {
  1714. public static void Main()
  1715. {
  1716. var nums = new int[] { 1, 2, 3, 4 };
  1717. }
  1718. }";
  1719. var speculatedSource = @"
  1720. var q2 = from x in nums
  1721. select x into w
  1722. select w;
  1723. ";
  1724. var queryStatement = SyntaxFactory.ParseStatement(speculatedSource);
  1725. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1726. compilation.VerifyDiagnostics();
  1727. var tree = compilation.SyntaxTrees[0];
  1728. var model = compilation.GetSemanticModel(tree);
  1729. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "Test2").Single();
  1730. var methodM = (MethodDeclarationSyntax)classC.Members[0];
  1731. SemanticModel speculativeModel;
  1732. bool success = model.TryGetSpeculativeSemanticModel(methodM.Body.Statements[0].Span.End, queryStatement, out speculativeModel);
  1733. Assert.True(success);
  1734. var queryExpression = (QueryExpressionSyntax)((LocalDeclarationStatementSyntax)queryStatement).Declaration.Variables[0].Initializer.Value;
  1735. var queryContinuation = queryExpression.Body.Continuation;
  1736. var symbol = speculativeModel.GetDeclaredSymbol(queryContinuation);
  1737. Assert.NotNull(symbol);
  1738. Assert.Equal("w", symbol.Name);
  1739. Assert.Equal(SymbolKind.RangeVariable, symbol.Kind);
  1740. }
  1741. [Fact]
  1742. public void TestSpeculativeSemanticModel_GetSymbolInfoForOrderingClauses()
  1743. {
  1744. string sourceCode = @"
  1745. using System.Linq; // Needed for speculative code.
  1746. public class QueryExpressionTest
  1747. {
  1748. public static void Main()
  1749. {
  1750. }
  1751. }";
  1752. var speculatedSource = @"
  1753. var q1 =
  1754. from x in new int[] { 4, 5 }
  1755. orderby
  1756. x descending,
  1757. x.ToString() ascending,
  1758. x descending
  1759. select x;
  1760. ";
  1761. var queryStatement = SyntaxFactory.ParseStatement(speculatedSource);
  1762. var compilation = CreateCompilationWithMscorlibAndSystemCore(sourceCode);
  1763. compilation.VerifyDiagnostics(
  1764. // (2,1): info CS8019: Unnecessary using directive.
  1765. // using System.Linq; // Needed for speculative code.
  1766. Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using System.Linq;"));
  1767. var tree = compilation.SyntaxTrees[0];
  1768. var model = compilation.GetSemanticModel(tree);
  1769. var classC = tree.GetCompilationUnitRoot().ChildNodes().OfType<TypeDeclarationSyntax>().Where(t => t.Identifier.ValueText == "QueryExpressionTest").Single();
  1770. var methodM = (MethodDeclarationSyntax)classC.Members[0];
  1771. SemanticModel speculativeModel;
  1772. bool success = model.TryGetSpeculativeSemanticModel(methodM.Body.SpanStart, queryStatement, out speculativeModel);
  1773. Assert.True(success);
  1774. int count = 0;
  1775. string[] names = { "OrderByDescending", "ThenBy", "ThenByDescending" };
  1776. foreach (var ordering in queryStatement.DescendantNodes().OfType<OrderingSyntax>())
  1777. {
  1778. var symbolInfo = speculativeModel.GetSemanticInfoSummary(ordering);
  1779. Assert.Equal(names[count++], symbolInfo.Symbol.Name);
  1780. }
  1781. Assert.Equal(3, count);
  1782. }
  1783. [Fact]
  1784. public void BrokenQueryPattern()
  1785. {
  1786. string sourceCode =
  1787. @"using System;
  1788. class Q<T>
  1789. {
  1790. public Q<V> SelectMany<U, V>(Func<T, U> f1, Func<T, U, V> f2) { return null; }
  1791. public Q<U> Select<U>(Func<T, U> f1) { return null; }
  1792. //public Q<T> Where(Func<T, bool> f1) { return null; }
  1793. public X Where(Func<T, bool> f1) { return null; }
  1794. }
  1795. class X
  1796. {
  1797. public X Select<U>(Func<int, U> f1) { return null; }
  1798. }
  1799. class Program
  1800. {
  1801. static void Main(string[] args)
  1802. {
  1803. Q<int> q = null;
  1804. var r =
  1805. from x in q
  1806. from y in q
  1807. where x.ToString() == y.ToString()
  1808. select x.ToString();
  1809. }
  1810. }";
  1811. CreateCompilationWithMscorlibAndSystemCore(sourceCode).VerifyDiagnostics(
  1812. // (26,20): error CS8016: Transparent identifier member access failed for field 'x' of 'int'. Does the data being queried implement the query pattern?
  1813. // select x.ToString();
  1814. Diagnostic(ErrorCode.ERR_UnsupportedTransparentIdentifierAccess, "x").WithArguments("x", "int")
  1815. );
  1816. }
  1817. }
  1818. }