PageRenderTime 66ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/EkardNT/Roslyn
C# | 1871 lines | 1513 code | 49 blank | 309 comment | 0 complexity | a20f5fe3856d4364d0a2d073a4920966 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. using Microsoft.CodeAnalysis.Test.Utilities;
  3. using Roslyn.Test.Utilities;
  4. using Xunit;
  5. // The way the specification describes, and the way the native compiler reports name
  6. // collision errors is inconsistent and confusing. In Roslyn we will implement
  7. // the following more rational behaviours:
  8. //
  9. // ------------------
  10. //
  11. // These two error messages are to be reworded:
  12. //
  13. // CS0135: (ERR_NameIllegallyOverrides)
  14. //
  15. // Original: 'X' conflicts with the declaration 'C.X'
  16. //
  17. // New: A local, parameter or range variable named 'X' cannot be declared in this scope
  18. // because that name is used in an enclosing local scope to refer to 'C.X'.
  19. //
  20. // CS0136: (ERR_LocalIllegallyOverrides)
  21. //
  22. // Original: A local variable named 'X' cannot be declared in this scope
  23. // because it would give a different meaning to 'X', which is
  24. // already used in a 'parent or current' / 'child'
  25. // scope to denote something else
  26. //
  27. // New: A local or parameter named 'X' cannot be declared in this scope
  28. // because that name is used in an enclosing local scope to define
  29. // a local or parameter.
  30. //
  31. // Note now the error messages are now nicely parallel, and much more clear about
  32. // precisely which rule has been violated.
  33. //
  34. // The rules for what error to report in each name collision scenario are as follows:
  35. //
  36. // ---------------------------
  37. //
  38. // Errors for simple names being used to refer to a member in one place and a declared
  39. // entity in another:
  40. //
  41. // CS0135: (ERR_NameIllegallyOverrides)
  42. // A local, parameter or range variable cannot be named 'X' because
  43. // that name is used in an enclosing local scope to refer to 'C.X'.
  44. //
  45. // Reported *only* when there is a local variable, local constant, lambda parameter or range variable
  46. // that would change the meaning of a *simple name* in an *expression* in an enclosing declaration
  47. // space to refer to a member, namespace, type, type parameter etc. Report it on the *inner* usage,
  48. // never the "outer" usage. eg:
  49. //
  50. // class C { int x; void M() { int y = x; { int x = y; } } }
  51. //
  52. // ---------------------------
  53. //
  54. // Errors for a local being used before it is defined:
  55. //
  56. // CS0841: (ERR_VariableUsedBeforeDeclaration)
  57. // Cannot use local variable 'X' before it is declared
  58. //
  59. // Reported when a local variable is used before it is declared, and the offending
  60. // usage was probably not intended to refer to a field. eg:
  61. //
  62. // class C { void M() { int y = x; int x; } }
  63. //
  64. // CS0844: (ERR_VariableUsedBeforeDeclarationAndHidesField)
  65. // Cannot use local variable 'X' before it is declared. The
  66. // declaration of the local variable hides the field 'C.X'.
  67. //
  68. // Reported if the offending usage might have been intended to refer to a field, eg:
  69. //
  70. // class C { int x; void M() { int y = x; int x; } }
  71. //
  72. // ---------------------------
  73. //
  74. // Errors for two of the same identifier being used to declare two different
  75. // things in overlapping or identical declaration spaces:
  76. //
  77. // CS0100: (ERR_DuplicateParamName)
  78. // The parameter name 'x' is a duplicate
  79. //
  80. // Reported when one parameter list contains two identically-named parameters. Eg:
  81. //
  82. // void M(int x, int x) {} or (x, x)=>{}
  83. //
  84. // CS0128: (ERR_LocalDuplicate)
  85. // A local variable named 'x' is already defined in this scope
  86. //
  87. // Reported *only* when there are two local variables or constants defined in the
  88. // *exact* same declaration space with the same name. eg:
  89. //
  90. // void M() { int x; int x; }
  91. //
  92. // CS0136: (ERR_LocalIllegallyOverrides)
  93. // New: A local or parameter named 'X' cannot be declared in this scope
  94. // because that name is used in an enclosing local scope to define
  95. // a local or parameter.
  96. //
  97. // Reported *only* when there is a local variable, local constant or lambda parameter
  98. // but NOT range variable that shadows a local variable, local constant, formal parameter,
  99. // range variable, or lambda parameter that was declared in an enclosing local declaration space. Again,
  100. // report it on the inner usage. eg:
  101. //
  102. // void M() { int y; { int y; } }
  103. //
  104. // CS0412: (ERR_LocalSameNameAsTypeParam)
  105. // 'X': a parameter or local variable cannot have the same name as a method type parameter
  106. //
  107. // Reported *only* when a local variable, local constant, formal parameter or lambda parameter
  108. // has the same name as a method type parameter. eg:
  109. //
  110. // void M<X>(){ int X; }
  111. //
  112. // CS1948: (ERR_QueryRangeVariableSameAsTypeParam)
  113. // The range variable 'X' cannot have the same name as a method type parameter
  114. //
  115. // Reported *only* when a range variable has the same name as a method type parameter. eg:
  116. //
  117. // void M<X>(){ var q = from X in z select X; }
  118. //
  119. // CS1930: (ERR_QueryDuplicateRangeVariable)
  120. // The range variable 'x' has already been declared
  121. //
  122. // Reported *only* if a range variable shadows another range variable that is in scope. eg:
  123. //
  124. // from x in y from x in z select q
  125. //
  126. // CS1931: (ERR_QueryRangeVariableOverrides)
  127. // The range variable 'x' conflicts with a previous declaration of 'x'
  128. //
  129. // Reported when there is a range variable that shadows a non-range variable from an
  130. // enclosing scope. eg:
  131. //
  132. // int x; var y = from x in q select m;
  133. //
  134. namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics
  135. {
  136. public class NameCollisionTests : CompilingTestBase
  137. {
  138. [Fact]
  139. private void TestNamesFromTypeAndExpressionContextsDontCollide()
  140. {
  141. var source = @"
  142. using name1 = System.Exception;
  143. namespace Namespace
  144. {
  145. using name3 = System.Type;
  146. class Class
  147. {
  148. Class(name2 other1, name1 other2)
  149. {
  150. name3 other3 = typeof(name1);
  151. if (typeof(name1) != typeof(name2) ||
  152. typeof(name2) is name3 ||
  153. typeof(name1) is name3)
  154. {
  155. foreach (var name1 in ""string"")
  156. {
  157. for (var name2 = 1; name2 > --name2; name2++)
  158. { int name3 = name2; }
  159. }
  160. }
  161. else
  162. {
  163. name2 name1 = null, name2 = name1;
  164. name3 name3 = typeof(name2);
  165. }
  166. }
  167. }
  168. }
  169. class name2
  170. {
  171. }";
  172. CompileAndVerify(source).VerifyDiagnostics();
  173. }
  174. [Fact]
  175. private void TestLocalAndLabelDontCollide()
  176. {
  177. var source = @"
  178. using System;
  179. namespace Namespace
  180. {
  181. using name1 = System.Type;
  182. class Class
  183. {
  184. Class(name1 name1)
  185. {
  186. goto name2;
  187. name2: Console.WriteLine();
  188. var name2 = new name2();
  189. goto name1;
  190. name1: Console.WriteLine();
  191. }
  192. }
  193. }
  194. class name2
  195. {
  196. }";
  197. CompileAndVerify(source).VerifyDiagnostics();
  198. }
  199. [Fact]
  200. private void TestCollisionOfLabelWithLabel()
  201. {
  202. var source = @"
  203. using System;
  204. namespace Namespace
  205. {
  206. using name1 = System.Type;
  207. class Class
  208. {
  209. Class(name1 name1)
  210. {
  211. goto name1;
  212. name1: Console.WriteLine();
  213. {
  214. goto name1;
  215. name1: Console.WriteLine();
  216. var name2 = new name2();
  217. goto name2;
  218. name2: Console.WriteLine();
  219. }
  220. goto name2;
  221. name2: Console.WriteLine();
  222. }
  223. internal int Property
  224. {
  225. set
  226. {
  227. goto name1;
  228. name1: Console.WriteLine();
  229. Action lambda1 = () =>
  230. {
  231. Action lambda2 = () =>
  232. {
  233. goto name1;
  234. name1: Console.WriteLine();
  235. var name2 = new name2();
  236. goto name2;
  237. name2: Console.WriteLine();
  238. };
  239. goto name2;
  240. name2: Console.WriteLine();
  241. };
  242. }
  243. }
  244. }
  245. }
  246. class name2
  247. {
  248. }";
  249. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  250. // (14,13): error CS0158: The label 'name1' shadows another label by the same name in a contained scope
  251. // name1: Console.WriteLine();
  252. Diagnostic(ErrorCode.ERR_LabelShadow, "name1").WithArguments("name1"),
  253. // (17,13): error CS0158: The label 'name2' shadows another label by the same name in a contained scope
  254. // name2: Console.WriteLine();
  255. Diagnostic(ErrorCode.ERR_LabelShadow, "name2").WithArguments("name2"),
  256. // (34,21): error CS0158: The label 'name1' shadows another label by the same name in a contained scope
  257. // name1: Console.WriteLine();
  258. Diagnostic(ErrorCode.ERR_LabelShadow, "name1").WithArguments("name1"),
  259. // (37,21): error CS0158: The label 'name2' shadows another label by the same name in a contained scope
  260. // name2: Console.WriteLine();
  261. Diagnostic(ErrorCode.ERR_LabelShadow, "name2").WithArguments("name2"));
  262. }
  263. [Fact]
  264. private void TestCollisionOfLocalWithTypeOrMethodOrProperty_LegalCases()
  265. {
  266. var source = @"
  267. using System;
  268. namespace name1
  269. {
  270. class Class
  271. {
  272. void name1()
  273. {
  274. {
  275. name1();
  276. }
  277. {
  278. int name1 = name1 = 1;
  279. }
  280. foreach(var name1 in ""string"") ;
  281. }
  282. }
  283. }
  284. class name2
  285. {
  286. Action lambda = () =>
  287. {
  288. {
  289. int name2 = name2 = 2;
  290. Console.WriteLine(name3);
  291. }
  292. {
  293. int name3 = name3 = 3;
  294. }
  295. };
  296. static int name3
  297. {
  298. get
  299. {
  300. return 4;
  301. }
  302. }
  303. }";
  304. CompileAndVerify(source).VerifyDiagnostics();
  305. }
  306. [Fact]
  307. private void TestCollisionOfLocalWithType()
  308. {
  309. var source = @"
  310. using name1 = System.Console;
  311. class Class
  312. {
  313. Class()
  314. {
  315. {
  316. name1.WriteLine(); // Legal
  317. name2.Equals(null, null); // Legal
  318. }
  319. {
  320. int name1 = (name1 = 1), name2 = name2 = 2; // Legal -- strange, but legal
  321. }
  322. {
  323. name1.WriteLine();
  324. name2.Equals(null, null);
  325. {
  326. int name1 = 3, name2 = name1; // 0135 on name1, name2
  327. // Native compiler reports 0136 here; Roslyn reports 0135.
  328. }
  329. }
  330. }
  331. }
  332. class name2
  333. {
  334. }";
  335. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  336. }
  337. [Fact]
  338. private void TestCollisionOfLocalWithMethodOrProperty()
  339. {
  340. var source = @"
  341. using System;
  342. namespace name1
  343. {
  344. class Class
  345. {
  346. void name1()
  347. {
  348. name1();
  349. {
  350. name1();
  351. }
  352. {
  353. int name1 = name1 = 1; // 0135: Roslyn reports 0135, native reports 0136.
  354. }
  355. foreach (var name1 in ""string"") ; // 0135: Roslyn reports 0135, native reports 0136.
  356. }
  357. Action lambda = () =>
  358. {
  359. {
  360. int name2 = name2 = 2; // 0135: conflicts with usage of name2 as the static property below.
  361. // Roslyn reports this here; native compiler reports it below.
  362. Console.WriteLine(name2);
  363. }
  364. Console.WriteLine(name2); // Native compiler reports 0135 here; Roslyn reports it above.
  365. };
  366. static int name2
  367. {
  368. get
  369. {
  370. return 3;
  371. }
  372. }
  373. }
  374. }";
  375. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  376. }
  377. [WorkItem(542039, "DevDiv")]
  378. [Fact]
  379. public void TestCollisionOfDelegateWithConst()
  380. {
  381. var source = @"class A
  382. {
  383. delegate void D();
  384. static void Foo() { }
  385. class B
  386. {
  387. const int Foo = 123;
  388. static void Main()
  389. {
  390. Foo();
  391. Bar(Foo);
  392. }
  393. static void Main2()
  394. {
  395. Bar(Foo);
  396. Foo();
  397. }
  398. static void Bar(int x) { }
  399. static void Bar(D x) { }
  400. }
  401. }";
  402. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  403. }
  404. [Fact]
  405. private void TestCollisionOfLocalWithTypeParameter()
  406. {
  407. var source = @"
  408. class Class<name1, name2>
  409. {
  410. void Method<name3, name4>(name1 other1, name4 name4) // 0412 on name4
  411. {
  412. {
  413. int name3 = 10; // 0412 on name3
  414. System.Console.WriteLine(name3); // Eliminate warning
  415. foreach (var name2 in ""string"")
  416. {
  417. for (var name1 = 1; name1 <= name1++; name1++) // legal; name1 conflicts with a class type parameter which is not in the local variable decl space
  418. name1 = name2.GetHashCode();
  419. }
  420. }
  421. {
  422. name1 other2 = typeof(name1) is name1 ? other1 : other1; // no error; all the name1's refer to the type, not the local.
  423. int name1 = (name1 = 2), name2 = name2 = 3; // legal; name1 conflicts with a class type parameter which is not in the local variable decl space
  424. foreach (var name3 in ""string"") // 0412 on name3
  425. {
  426. System.Console.WriteLine(name3); // Eliminate warning
  427. for (var name4 = 4; ; ) // 0412 on name4
  428. {
  429. name1 = name2.GetHashCode();
  430. System.Console.WriteLine(name4); // Eliminate warning
  431. }
  432. }
  433. try {}
  434. catch(System.Exception name3) // 0412 on name3
  435. { System.Console.WriteLine(name3); }
  436. }
  437. }
  438. }";
  439. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  440. // (4,51): error CS0412: 'name4': a parameter or local variable cannot have the same name as a method type parameter
  441. // void Method<name3, name4>(name1 other1, name4 name4) // 0412 on name4
  442. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name4").WithArguments("name4").WithLocation(4, 51),
  443. // (7,17): error CS0412: 'name3': a parameter or local variable cannot have the same name as a method type parameter
  444. // int name3 = 10; // 0412 on name3
  445. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name3").WithArguments("name3").WithLocation(7, 17),
  446. // (18,26): error CS0412: 'name3': a parameter or local variable cannot have the same name as a method type parameter
  447. // foreach (var name3 in "string") // 0412 on name3
  448. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name3").WithArguments("name3").WithLocation(18, 26),
  449. // (21,26): error CS0136: A local or parameter named 'name4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  450. // for (var name4 = 4; ; ) // 0412 on name4
  451. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name4").WithArguments("name4"),
  452. // (28,36): error CS0412: 'name3': a parameter or local variable cannot have the same name as a method type parameter
  453. // catch(System.Exception name3) // 0412 on name3
  454. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name3").WithArguments("name3"));
  455. }
  456. [Fact]
  457. private void TestCollisionOfLocalWithField_LegalCases()
  458. {
  459. var source = @"
  460. partial class Derived : Base
  461. {
  462. private Derived()
  463. {
  464. this.name1 = 1;
  465. long name1 = this.name1;
  466. if (true)
  467. {
  468. name1 = this.name1 = name1;
  469. name2 = this.name2 = name2 + name1;
  470. }
  471. {
  472. while (name1 == 1)
  473. {
  474. long name2 = 2; name1 = name2;
  475. }
  476. do
  477. {
  478. long name2 = 3; name1 = name2;
  479. name1 = this.name1;
  480. }
  481. while (name1 != 1);
  482. }
  483. }
  484. }
  485. class Base
  486. {
  487. public long name2 = name1;
  488. private static int name1 = 4;
  489. }
  490. partial class Derived
  491. {
  492. internal long name1 = 5;
  493. }";
  494. CompileAndVerify(source).VerifyDiagnostics();
  495. }
  496. [Fact]
  497. private void TestCollisionOfLocalWithField1()
  498. {
  499. var source = @"
  500. class Derived : Base
  501. {
  502. static long name1 = 1;
  503. static Derived()
  504. {
  505. while(name1 == 2)
  506. {
  507. int name1 = 3, other = name1, name2 = other; // 0135 on name1 and name2
  508. // Native reports 0136 on name1 here and 0135 on name2 below.
  509. }
  510. do
  511. {
  512. }
  513. while (name2 == 4); // Native reports 0135 on name2 here; Roslyn reports it above.
  514. }
  515. }
  516. class Base
  517. {
  518. protected static long name2 = 5;
  519. }";
  520. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  521. }
  522. [Fact]
  523. private void TestCollisionOfLocalWithField2()
  524. {
  525. var source = @"
  526. class Class
  527. {
  528. public static int M() { return 1; }
  529. internal int Property
  530. {
  531. set
  532. {
  533. for (int i = 0; i < int.MaxValue; ++i)
  534. {
  535. if (i == 0)
  536. {
  537. int other = M(), name = M(), name = other; // 0128, 0135
  538. }
  539. else
  540. {
  541. {
  542. int name = M(); name = M(); // 0135
  543. }
  544. }
  545. }
  546. for (int i = 0; i > int.MinValue; ++i)
  547. {
  548. { i += 1; }
  549. }
  550. name = M();
  551. }
  552. }
  553. private const int x = 123;
  554. private void M1(int x = x) {} // UNDONE: Native and Roslyn compilers both allow this; should they?
  555. private void M2(int y = x)
  556. {
  557. int x = M(); // UNDONE: Native and Roslyn compilers both allow this; should they?
  558. }
  559. private long other = 0, name = 6;
  560. }";
  561. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  562. // (13,50): error CS0128: A local variable named 'name' is already defined in this scope
  563. // int other = M(), name = M(), name = other; // 0128, 0135
  564. Diagnostic(ErrorCode.ERR_LocalDuplicate, "name").WithArguments("name"),
  565. // (37,18): warning CS0414: The field 'Class.other' is assigned but its value is never used
  566. // private long other = 0, name = 6;
  567. Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "other").WithArguments("Class.other")
  568. );
  569. }
  570. [Fact]
  571. private void TestCollisionInsideFieldDeclaration()
  572. {
  573. // A close reading of the spec would indicate that this is not an error because the
  574. // offending simple name 'x' does not appear in any local variable declaration space.
  575. // A field initializer is not a declaration space. However, it seems plausible
  576. // that we want to report the error here. The native compiler does so as well.
  577. var source = @"
  578. class Class
  579. {
  580. private static int M() { return 1; }
  581. private static int x = 123;
  582. private static int z = x + ((System.Func<int>)( ()=>{ int x = M(); return x; } ))();
  583. }";
  584. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  585. }
  586. [Fact]
  587. private void TestCollisionOfLocalWithField_PartialType()
  588. {
  589. var source = @"
  590. partial struct PartialStruct
  591. {
  592. private void Method()
  593. {
  594. if (true)
  595. {
  596. {
  597. int name = 1, other = name;
  598. }
  599. }
  600. name = 2; // Native compiler reports 0135 here; Roslyn no longer reports 0135.
  601. }
  602. }
  603. partial struct PartialStruct
  604. {
  605. internal long name;
  606. }";
  607. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  608. }
  609. [Fact]
  610. private void TestCollisionOfLocalWithLocal_LegalCases1()
  611. {
  612. var source = @"
  613. partial class Derived : Base
  614. {
  615. private string Property
  616. {
  617. get
  618. {
  619. if (true)
  620. {
  621. int name = (name = 1); name += name;
  622. }
  623. {
  624. {
  625. int name = 2; name -= name;
  626. }
  627. }
  628. for(long name = 3; name <= 4; ++name)
  629. name += 5;
  630. foreach(var name in ""string"")
  631. {
  632. name.ToString();
  633. }
  634. return this.name;
  635. }
  636. }
  637. }
  638. class Base
  639. {
  640. public string name = null;
  641. }";
  642. CompileAndVerify(source).VerifyDiagnostics();
  643. }
  644. [Fact]
  645. private void TestCollisionOfLocalWithLocal_LegalCases2()
  646. {
  647. var source = @"
  648. using System;
  649. using System.Linq;
  650. partial class Derived : Base
  651. {
  652. private string Property
  653. {
  654. get
  655. {
  656. // http://blogs.msdn.com/b/ericlippert/archive/2009/11/02/simple-names-are-not-so-simple.aspx
  657. foreach (var name in from name in ""string"" orderby name select name)
  658. Console.WriteLine(name);
  659. return this.name;
  660. }
  661. }
  662. }
  663. class Base
  664. {
  665. public string name = null;
  666. }";
  667. CompileAndVerify(source, new[] { LinqAssemblyRef }).VerifyDiagnostics();
  668. }
  669. [Fact]
  670. private void TestCollisionOfLocalWithLocal_LegalCases3()
  671. {
  672. var source = @"
  673. using System;
  674. using System.Linq;
  675. partial class Derived : Base
  676. {
  677. private string Property
  678. {
  679. get
  680. {
  681. // http://blogs.msdn.com/b/ericlippert/archive/2009/11/02/simple-names-are-not-so-simple.aspx
  682. foreach(var name in ""string"".OrderBy(name => name).Select(name => name))
  683. {
  684. Console.WriteLine(name);
  685. }
  686. return this.name;
  687. }
  688. }
  689. }
  690. class Base
  691. {
  692. public string name = null;
  693. }";
  694. CompileAndVerify(source, new[] { LinqAssemblyRef }).VerifyDiagnostics();
  695. }
  696. [Fact]
  697. private void TestCollisionOfLocalWithLocal()
  698. {
  699. var source = @"
  700. class Class
  701. {
  702. public Class()
  703. {
  704. long name1 = 1; System.Console.WriteLine(name1); // Eliminate unused warning.
  705. name4 = name6; // 0841 on name4; used before declared. 0103 on name6; not defined in this context
  706. if (true)
  707. {
  708. {
  709. int other1 = 2, name1 = other1, name2 = name1; // 0136 on name1; already used in parent scope to mean something else.
  710. } // Native compiler reports 0136 on 'long name2' below; Roslyn reports it on 'int ... name2' here and 'var name2' below
  711. {
  712. if (true)
  713. {
  714. for (long name1 = this.name2; name1 >= --name1; name1++) // 0136 on name1;
  715. {
  716. name1.ToString(); name5.ToString(); // 0841: name5 is used before the declaration
  717. string name6 = ""string"";
  718. }
  719. }
  720. foreach (var name2 in ""string"") name2.ToString(); // 0136: Native reports this on 'long name2' below; Roslyn reports it here, and above.
  721. }
  722. string @name3 = ""string"", other2 = name3, name3 = other2; // 0128: name3 is defined twice.
  723. long @name2 = 3; System.Console.WriteLine(@name2); // eliminated unused warning.
  724. // Native compiler reports 0136 on 'long name2' here; Roslyn reports it on 'int ... name2' above.
  725. }
  726. string name4 = ""string"", name5 = name4;
  727. name6 = name3; // 0103 on both name6 and name3; not defined in this context.
  728. }
  729. public long name2 = 4;
  730. }";
  731. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  732. // (7,9): error CS0841: Cannot use local variable 'name4' before it is declared
  733. // name4 = name6; // 0841 on name4; used before declared. 0103 on name6; not defined in this context
  734. Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "name4").WithArguments("name4").WithLocation(7, 9),
  735. // (7,17): error CS0103: The name 'name6' does not exist in the current context
  736. // name4 = name6; // 0841 on name4; used before declared. 0103 on name6; not defined in this context
  737. Diagnostic(ErrorCode.ERR_NameNotInContext, "name6").WithArguments("name6").WithLocation(7, 17),
  738. // (11,33): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  739. // int other1 = 2, name1 = other1, name2 = name1; // 0136 on name1; already used in parent scope to mean something else.
  740. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(11, 33),
  741. // (11,49): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  742. // int other1 = 2, name1 = other1, name2 = name1; // 0136 on name1; already used in parent scope to mean something else.
  743. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(11, 49),
  744. // (16,31): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  745. // for (long name1 = this.name2; name1 >= --name1; name1++) // 0136 on name1;
  746. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(16, 31),
  747. // (18,43): error CS0841: Cannot use local variable 'name5' before it is declared
  748. // name1.ToString(); name5.ToString(); // 0841: name5 is used before the declaration
  749. Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "name5").WithArguments("name5").WithLocation(18, 43),
  750. // (22,30): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  751. // foreach (var name2 in "string") name2.ToString(); // 0136: Native reports this on 'long name2' below; Roslyn reports it here, and above.
  752. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(22, 30),
  753. // (24,55): error CS0128: A local variable named 'name3' is already defined in this scope
  754. // string @name3 = "string", other2 = name3, name3 = other2; // 0128: name3 is defined twice.
  755. Diagnostic(ErrorCode.ERR_LocalDuplicate, "name3").WithArguments("name3").WithLocation(24, 55),
  756. // (29,9): error CS0103: The name 'name6' does not exist in the current context
  757. // name6 = name3; // 0103 on both name6 and name3; not defined in this context.
  758. Diagnostic(ErrorCode.ERR_NameNotInContext, "name6").WithArguments("name6").WithLocation(29, 9),
  759. // (29,17): error CS0103: The name 'name3' does not exist in the current context
  760. // name6 = name3; // 0103 on both name6 and name3; not defined in this context.
  761. Diagnostic(ErrorCode.ERR_NameNotInContext, "name3").WithArguments("name3").WithLocation(29, 17),
  762. // (19,32): warning CS0219: The variable 'name6' is assigned but its value is never used
  763. // string name6 = "string";
  764. Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "name6").WithArguments("name6").WithLocation(19, 32)
  765. );
  766. }
  767. [Fact]
  768. private void TestCollisionOfLocalWithParam()
  769. {
  770. var source = @"
  771. using System;
  772. class Class
  773. {
  774. public Func<int, int, int> Method(int name1, int name2)
  775. {
  776. foreach (var name1 in ""string"") // 0136
  777. {
  778. foreach (var name2 in ""string"") // 0136
  779. {
  780. int name1 = name2.GetHashCode(); // 0136
  781. }
  782. }
  783. Action<int> lambda = (name3) =>
  784. {
  785. int name1 = 1; // 0136
  786. if(name1 == 2)
  787. {
  788. name2 = name3 = name1;
  789. }
  790. else
  791. {
  792. int name2 = 3; // 0136
  793. System.Console.WriteLine(name2);
  794. {
  795. int name3 = 2; // 0136
  796. System.Console.WriteLine(name3);
  797. }
  798. }
  799. };
  800. return (name1, name2) => name1; // 0136 on both name1 and name2
  801. }
  802. }";
  803. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  804. // (7,22): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  805. // foreach (var name1 in "string") // 0136
  806. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(7, 22),
  807. // (9,26): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  808. // foreach (var name2 in "string") // 0136
  809. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(9, 26),
  810. // (11,21): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  811. // int name1 = name2.GetHashCode(); // 0136
  812. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(11, 21),
  813. // (17,17): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  814. // int name1 = 1; // 0136
  815. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(17, 17),
  816. // (24,21): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  817. // int name2 = 3; // 0136
  818. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(24, 21),
  819. // (27,25): error CS0412: 'name3': a parameter or local variable cannot have the same name as a method type parameter
  820. // int name3 = 2; // 0136
  821. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name3").WithArguments("name3").WithLocation(27, 25),
  822. // (32,17): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  823. // return (name1, name2) => name1; // 0136 on both name1 and name2
  824. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(32, 17),
  825. // (32,24): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  826. // return (name1, name2) => name1; // 0136 on both name1 and name2
  827. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(32, 24)
  828. );
  829. }
  830. [Fact]
  831. private void TestCollisionOfParamWithParam()
  832. {
  833. var source = @"
  834. using System;
  835. class Class
  836. {
  837. public static void Method(int name1, int name2, int name2) // 0100 on name2
  838. {
  839. Action<int, int> lambda = (other, name3) =>
  840. {
  841. Action<int, int, int, int> nestedLambda = (name1, name4, name4, name3) => // 0100 on name4, 0136 on name1 and name3
  842. {
  843. };
  844. };
  845. }
  846. }";
  847. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  848. // (5,57): error CS0100: The parameter name 'name2' is a duplicate
  849. // public static void Method(int name1, int name2, int name2) // 0100 on name2
  850. Diagnostic(ErrorCode.ERR_DuplicateParamName, "name2").WithArguments("name2").WithLocation(5, 57),
  851. // (9,56): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  852. // Action<int, int, int, int> nestedLambda = (name1, name4, name4, name3) => // 0100 on name4, 0136 on name1 and name3
  853. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(9, 56),
  854. // (9,70): error CS0100: The parameter name 'name4' is a duplicate
  855. // Action<int, int, int, int> nestedLambda = (name1, name4, name4, name3) => // 0100 on name4, 0136 on name1 and name3
  856. Diagnostic(ErrorCode.ERR_DuplicateParamName, "name4").WithArguments("name4").WithLocation(9, 70),
  857. // (9,77): error CS0412: 'name3': a parameter or local variable cannot have the same name as a method type parameter
  858. // Action<int, int, int, int> nestedLambda = (name1, name4, name4, name3) => // 0100 on name4, 0136 on name1 and name3
  859. Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "name3").WithArguments("name3").WithLocation(9, 77)
  860. );
  861. }
  862. [WorkItem(930252)]
  863. [Fact]
  864. private void TestCollisionOfParamWithParam1()
  865. {
  866. var source = @"
  867. class Program
  868. {
  869. delegate int D(int x, int y);
  870. static void X()
  871. {
  872. D d1 = (int x, int x) => { return 1; };
  873. D d2 = (x, x) => { return 1; };
  874. }
  875. }";
  876. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  877. // (7,28): error CS0100: The parameter name 'x' is a duplicate
  878. // D d1 = (int x, int x) => { return 1; };
  879. Diagnostic(ErrorCode.ERR_DuplicateParamName, "x").WithArguments("x").WithLocation(7, 28),
  880. // (8,20): error CS0100: The parameter name 'x' is a duplicate
  881. // D d2 = (x, x) => { return 1; };
  882. Diagnostic(ErrorCode.ERR_DuplicateParamName, "x").WithArguments("x").WithLocation(8, 20)
  883. );
  884. }
  885. [Fact]
  886. private void TestCollisionInsideLambda_LegalCases()
  887. {
  888. var source = @"
  889. using System;
  890. partial class Class
  891. {
  892. private string Property
  893. {
  894. set
  895. {
  896. this.
  897. Method((name1) =>
  898. {
  899. name1 = string.Empty;
  900. for (int name2 = name2 = 1; ; ) ;
  901. }).
  902. Method((name1) => name1.ToString()).
  903. Method((name1) =>
  904. {
  905. foreach (var name2 in string.Empty) ;
  906. return name1;
  907. });
  908. }
  909. }
  910. Class Method(Action<string> name1)
  911. {
  912. return null;
  913. }
  914. Class Method(Func<string, string> name1)
  915. {
  916. return null;
  917. }
  918. }";
  919. CompileAndVerify(source).VerifyDiagnostics();
  920. }
  921. [Fact]
  922. private void TestCollisionInsideLambda1()
  923. {
  924. var source = @"
  925. using System;
  926. class Derived : Base
  927. {
  928. static int M() { return 1; }
  929. static long name1 = 1;
  930. Action lambda = () =>
  931. {
  932. name1 = 2;
  933. {
  934. int name1 = 3, other = name1, name2 = other; // 0135: on name1 and name2.
  935. // Native compiler reports 0136 here on name1 and 0135 on name2 below.
  936. // Roslyn reports them both as 0135 here.
  937. }
  938. name2 = 4; // Native compiler reports 0135 here; Roslyn reports above.
  939. int name3 = M();
  940. {
  941. {
  942. name3 = 6;
  943. }
  944. if (true)
  945. {
  946. int name3 = M(); // 0136: Native compiler says 0135, Roslyn says 0136. The conflict is with the other local.
  947. }
  948. }
  949. };
  950. Action anonMethod = delegate()
  951. {
  952. name1 = 8;
  953. if (true)
  954. {
  955. int name1 = 9, other = name1, name2 = other; // 0135: on name1, name2
  956. // Native compiler reports 0136 on name1, Roslyn reports 0135.
  957. // Native compiler reports 0135 on name2 below, Roslyn reports it here.
  958. }
  959. {
  960. foreach (var name3 in ""string"") name3.ToString(); // 0136: Native compiler reports 0136 below, Roslyn reports it here.
  961. }
  962. name2 = 10; // Native compiler reports 0135 here; Roslyn reports it above.
  963. int name3 = M();
  964. };
  965. }
  966. class Base
  967. {
  968. protected static long name2 = 12;
  969. }";
  970. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  971. // (24,21): error CS0136: A local or parameter named 'name3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  972. // int name3 = M(); // 0136: Native compiler says 0135, Roslyn says 0136. The conflict is with the other local.
  973. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name3").WithArguments("name3").WithLocation(24, 21),
  974. // (39,26): error CS0136: A local or parameter named 'name3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  975. // foreach (var name3 in "string") name3.ToString(); // 0136: Native compiler reports 0136 below, Roslyn reports it here.
  976. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name3").WithArguments("name3").WithLocation(39, 26),
  977. // (6,17): warning CS0414: The field 'Derived.name1' is assigned but its value is never used
  978. // static long name1 = 1;
  979. Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "name1").WithArguments("Derived.name1").WithLocation(6, 17)
  980. );
  981. }
  982. [Fact]
  983. private void TestCollisionInsideLambda2()
  984. {
  985. var source = @"
  986. using System;
  987. class Class
  988. {
  989. void Method(Action lambda)
  990. {
  991. }
  992. void Method()
  993. {
  994. const long name1 = 1; System.Console.WriteLine(name1); // Eliminate warning.
  995. Method(() =>
  996. {
  997. Console.WriteLine(name1);
  998. {
  999. const int name1 = 2; // 0136
  1000. int other = name1, name2 = other; // 0136: Native compiler reports this on 'const long name' below; Roslyn reports it here.
  1001. }
  1002. name2 = 3; // 0841: local used before declared
  1003. const int name3 = 4;
  1004. {
  1005. {
  1006. Console.WriteLine(name3);
  1007. }
  1008. if (true)
  1009. {
  1010. const int name3 = 5; // 0136
  1011. Console.WriteLine(name3);
  1012. }
  1013. }
  1014. });
  1015. Method(delegate()
  1016. {
  1017. Console.WriteLine(name1);
  1018. if (true)
  1019. {
  1020. const int name1 = 6, other = name1, name2 = other; // 0136 on name1 and name2
  1021. Console.WriteLine(name1 + other + name2);
  1022. } // Roslyn reports 0136 on name2 above; native compiler reports it on 'const long name2' below.
  1023. {
  1024. foreach (var name3 in ""string"") name3.ToString(); // 0136: Roslyn reports this here, native reports it below.
  1025. }
  1026. Console.WriteLine(name2); // 0814: local used before declared
  1027. const int name3 = 7; // Native compiler reports 0136 here, Roslyn reports it on 'var name3' above.
  1028. Console.WriteLine(name3); // eliminate warning
  1029. });
  1030. const long name2 = 8; // Native compiler reports 0136 here; Roslyn reports it on both offending nested decls above.
  1031. Console.WriteLine(name2);
  1032. }
  1033. }";
  1034. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1035. // (15,27): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1036. // const int name1 = 2; // 0136
  1037. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(15, 27),
  1038. // (16,36): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1039. // int other = name1, name2 = other;
  1040. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(16, 36),
  1041. // (18,13): error CS0841: Cannot use local variable 'name2' before it is declared
  1042. // name2 = 3; // 0841: local used before declared
  1043. Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "name2").WithArguments("name2").WithLocation(18, 13),
  1044. // (27,31): error CS0136: A local or parameter named 'name3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1045. // const int name3 = 5; // 0136
  1046. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name3").WithArguments("name3").WithLocation(27, 31),
  1047. // (38,27): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1048. // const int name1 = 6, other = name1, name2 = other; // 0136 on name1 and name2
  1049. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(38, 27),
  1050. // (38,53): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1051. // const int name1 = 6, other = name1, name2 = other; // 0136 on name1 and name2
  1052. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(38, 53),
  1053. // (42,30): error CS0841: Cannot use local variable 'name3' before it is declared
  1054. // foreach (var name3 in ""string"") name3.ToString(); // 0136
  1055. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name3").WithArguments("name3").WithLocation(42, 30),
  1056. // (44,31): error CS0841: Cannot use local variable 'name2' before it is declared
  1057. // Console.WriteLine(name2); // 0814: local used before declared
  1058. Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "name2").WithArguments("name2").WithLocation(44, 31));
  1059. }
  1060. [Fact]
  1061. private void TestCollisionInsideOperator()
  1062. {
  1063. var source = @"
  1064. using System;
  1065. class Class
  1066. {
  1067. static long name1 = 1;
  1068. public static Class operator +(Class name1, Class other)
  1069. {
  1070. var lambda = (Action)(() =>
  1071. {
  1072. const int name1 = @name2; // 0136 on name1 because it conflicts with parameter
  1073. if (true)
  1074. {
  1075. int name2 = name1; // 0135 because name2 conflicts with usage of name2 as Class.name2 above
  1076. Console.WriteLine(name2);
  1077. }
  1078. });
  1079. return other;
  1080. }
  1081. const int name2 = 2;
  1082. public static void Other()
  1083. {
  1084. Console.WriteLine(name1);
  1085. }
  1086. }";
  1087. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1088. // (10,23): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1089. // const int name1 = @name2; // 0136 on name1 because it conflicts with parameter
  1090. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(10, 23)
  1091. );
  1092. }
  1093. [Fact]
  1094. private void TestCollisionInsideIndexer()
  1095. {
  1096. var source = @"
  1097. class Class
  1098. {
  1099. static long name1 = 1;
  1100. public int this[int name1]
  1101. {
  1102. get
  1103. {
  1104. foreach (var name2 in ""string"")
  1105. {
  1106. foreach (var name2 in ""string"") // 0136 on name2
  1107. {
  1108. int name1 = name2.GetHashCode(); // 0136 on name1
  1109. }
  1110. }
  1111. return name1;
  1112. }
  1113. }
  1114. static long name2 = name1 + name2;
  1115. }";
  1116. CreateCompilationWithMscorlibAndSystemCore(source).VerifyDiagnostics(
  1117. // (11,30): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1118. // foreach (var name2 in "string") // 0136 on name2
  1119. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2"),
  1120. // (13,25): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1121. // int name1 = name2.GetHashCode(); // 0136 on name1
  1122. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1"));
  1123. }
  1124. [Fact]
  1125. private void TestCollisionInsideFor1()
  1126. {
  1127. var source = @"
  1128. class Class
  1129. {
  1130. void Method1(int name4 = 1, params int[] name5)
  1131. {
  1132. for (int name1 = 2; name1 <= name1++; ++name1)
  1133. {
  1134. foreach (var name2 in ""string"")
  1135. {
  1136. for (name1 = 3; ; ) { break; }
  1137. for (int name2 = name1; name2 <= name2++; ++name2) // 0136 on name2
  1138. {
  1139. int name3 = 4, name4 = 5, name5 = 6; // 0136 on name3, name4 and name5
  1140. // Native compiler reports 0136 on name3 below, Roslyn reports it above.
  1141. System.Console.WriteLine(name3 + name4 + name5); // Eliminate warning
  1142. }
  1143. }
  1144. foreach (var name1 in ""string"") ; // 0136 on name1
  1145. }
  1146. int name3 = 7; // Native compiler reports 0136 on name3 here; Roslyn reports it above.
  1147. System.Console.WriteLine(name3); // Eliminate warning
  1148. }
  1149. }";
  1150. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1151. // (11,26): error CS0136: A local or parameter named 'name2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1152. // for (int name2 = name1; name2 <= name2++; ++name2) // 0136 on name2
  1153. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(11, 26),
  1154. // (13,25): error CS0136: A local or parameter named 'name3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1155. // int name3 = 4, name4 = 5, name5 = 6; // 0136
  1156. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name3").WithArguments("name3").WithLocation(13, 25),
  1157. // (13,36): error CS0136: A local or parameter named 'name4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1158. // int name3 = 4, name4 = 5, name5 = 6; // 0136
  1159. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name4").WithArguments("name4").WithLocation(13, 36),
  1160. // (13,47): error CS0136: A local or parameter named 'name5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1161. // int name3 = 4, name4 = 5, name5 = 6; // 0136
  1162. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name5").WithArguments("name5").WithLocation(13, 47),
  1163. // (13,47): error CS0136: A local or parameter named 'name1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1164. // foreach (var name1 in ""string"") ; // 0136 on name1
  1165. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(18, 26));
  1166. }
  1167. [Fact]
  1168. private void TestCollisionInsideFor2()
  1169. {
  1170. var source = @"
  1171. using System.Linq;
  1172. using System.Collections;
  1173. partial class Class
  1174. {
  1175. private string Property
  1176. {
  1177. get
  1178. {
  1179. for (var name = from name in ""string"" orderby name select name; name != null; ) ; // 1931
  1180. for (IEnumerable name = null; name == from name in ""string"" orderby name select name; ) ; // 1931
  1181. for (IEnumerable name = null; name == null; name = from name in ""string"" orderby name select name ) ; // 1931
  1182. return string.Empty;
  1183. }
  1184. }
  1185. }";
  1186. CreateCompilationWithMscorlibAndSystemCore(source).VerifyDiagnostics(
  1187. // (10,34): error CS1931: The range variable 'name' conflicts with a previous declaration of 'name'
  1188. // for (var name = from name in "string" orderby name select name; name != null; ) ; // 1931
  1189. Diagno

Large files files are truncated, but you can click here to view the full file