PageRenderTime 82ms CodeModel.GetById 38ms RepoModel.GetById 2ms app.codeStats 0ms

/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
  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. Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "name").WithArguments("name"),
  1190. // (11,56): error CS1931: The range variable 'name' conflicts with a previous declaration of 'name'
  1191. // for (IEnumerable name = null; name == from name in "string" orderby name select name; ) ; // 1931
  1192. Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "name").WithArguments("name"),
  1193. // (12,69): error CS1931: The range variable 'name' conflicts with a previous declaration of 'name'
  1194. // for (IEnumerable name = null; name == null; name = from name in "string" orderby name select name ) ; // 1931
  1195. Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "name").WithArguments("name"));
  1196. }
  1197. [WorkItem(792744, "DevDiv")]
  1198. [Fact]
  1199. private void TestCollisionInsideForeach()
  1200. {
  1201. var source = @"
  1202. class Class
  1203. {
  1204. static int y = 1;
  1205. static void Main(string[] args)
  1206. {
  1207. foreach (var y in new[] {new { y = y }}){ }
  1208. //End
  1209. }
  1210. }";
  1211. CreateCompilationWithMscorlibAndSystemCore(source).VerifyDiagnostics();
  1212. }
  1213. [Fact]
  1214. private void TestCollisionInsideUsing()
  1215. {
  1216. var source = @"
  1217. class Class : System.IDisposable
  1218. {
  1219. public void Dispose() {}
  1220. int[] name3 = {};
  1221. void Method1(Class name2 = null)
  1222. {
  1223. using (var name1 = new Class())
  1224. {
  1225. int other = (name3[0]);
  1226. }
  1227. using (var name1 = new Class())
  1228. {
  1229. var other = name3[0].ToString();
  1230. using (var name3 = new Class()) // 0135 because name3 above refers to this.name3
  1231. {
  1232. int name1 = 2; // 0136 on name1
  1233. }
  1234. }
  1235. using (var name2 = new Class()) // 0136 on name2.
  1236. {
  1237. int name1 = 2;
  1238. int other = (name3[0]);
  1239. }
  1240. using (name2 = new Class())
  1241. {
  1242. int name1 = 2;
  1243. int other = (name3[0]);
  1244. }
  1245. }
  1246. }";
  1247. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1248. // (17,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
  1249. // int name1 = 2; // 0136 on name1
  1250. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(17, 21),
  1251. // (20,20): 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
  1252. // using (var name2 = new Class()) // 0136 on name2.
  1253. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name2").WithArguments("name2").WithLocation(20, 20),
  1254. // (17,21): warning CS0219: The variable 'name1' is assigned but its value is never used
  1255. // int name1 = 2; // 0136 on name1
  1256. Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "name1").WithArguments("name1").WithLocation(17, 21),
  1257. // (22,17): warning CS0219: The variable 'name1' is assigned but its value is never used
  1258. // int name1 = 2;
  1259. Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "name1").WithArguments("name1").WithLocation(22, 17),
  1260. // (27,17): warning CS0219: The variable 'name1' is assigned but its value is never used
  1261. // int name1 = 2;
  1262. Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "name1").WithArguments("name1").WithLocation(27, 17)
  1263. );
  1264. }
  1265. [Fact]
  1266. private void TestCollisionInsideLock()
  1267. {
  1268. var source = @"
  1269. using System;
  1270. class Class
  1271. {
  1272. const int[] name = null;
  1273. void Method1()
  1274. {
  1275. lock (name)
  1276. {
  1277. }
  1278. {
  1279. lock (string.Empty)
  1280. {
  1281. const int name = 0; // 0135 because name above means 'this.name'.
  1282. Console.WriteLine(name);
  1283. }
  1284. }
  1285. }
  1286. }";
  1287. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  1288. }
  1289. [Fact]
  1290. private void TestCollisionInsideSwitch()
  1291. {
  1292. var source = @"
  1293. class Class
  1294. {
  1295. int M() { return 1; }
  1296. int name1 = 1;
  1297. void Method1()
  1298. {
  1299. switch (name1)
  1300. {
  1301. case name1: break; // 0844: use of 'int name1' below before it is declared -- surprising error, but correct. Also notes that local name1 hides field.
  1302. case 2:
  1303. int name1 = 2; // 0135: because 'name1' above means 'this.name1'. Native compiler reports 0136, Roslyn reports 0135.
  1304. var name2 = 3;
  1305. System.Console.WriteLine(name1 + name2);
  1306. break;
  1307. case 3:
  1308. name2 = M(); // Not a use-before-declaration error; name2 is defined above
  1309. var name2 = M(); // 0128 on name2; switch sections share the same declaration space
  1310. for (int name3 = 5; ; )
  1311. { System.Console.WriteLine(name2 + name3); break; }
  1312. int name4 = 6;
  1313. System.Console.WriteLine(name4);
  1314. break;
  1315. case 4:
  1316. name1 = 2;
  1317. for (int name3 = 7; ; )
  1318. { System.Console.WriteLine(name3); break; }
  1319. switch (name1)
  1320. {
  1321. case 1:
  1322. int name4 = 8, name5 = 9; // 0136 on name4, name5
  1323. // Native compiler reports error 0136 on `name5 = 11` below; Roslyn reports it here.
  1324. System.Console.WriteLine(name4 + name5);
  1325. break;
  1326. }
  1327. for (int name6 = 10; ; ) // 0136 on name6; Native compiler reports 0136 on name6 below.
  1328. { System.Console.WriteLine(name6);}
  1329. default:
  1330. int name5 = 11, name6 = 12; // Native compiler reports 0136 on name5 and name6. Roslyn reports them above.
  1331. System.Console.WriteLine(name5 + name6);
  1332. break;
  1333. }
  1334. }
  1335. }";
  1336. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1337. // (10,18): error CS0844: Cannot use local variable 'name1' before it is declared. The declaration of the local variable hides the field 'Class.name1'.
  1338. // case name1: break; // 0844: use of 'int name1' below before it is declared -- surprising error, but correct. Also notes that local name1 hides field.
  1339. Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclarationAndHidesField, "name1").WithArguments("name1", "Class.name1").WithLocation(10, 18),
  1340. // (18,21): error CS0128: A local variable named 'name2' is already defined in this scope
  1341. // var name2 = M(); // 0128 on name2; switch sections share the same declaration space
  1342. Diagnostic(ErrorCode.ERR_LocalDuplicate, "name2").WithArguments("name2").WithLocation(18, 21),
  1343. // (31,29): 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
  1344. // int name4 = 8, name5 = 9; // 0136 on name4, name5
  1345. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name4").WithArguments("name4").WithLocation(31, 29),
  1346. // (31,40): 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
  1347. // int name4 = 8, name5 = 9; // 0136 on name4, name5
  1348. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name5").WithArguments("name5").WithLocation(31, 40),
  1349. // (36,26): error CS0136: A local or parameter named 'name6' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1350. // for (int name6 = 10; ; ) // 0136 on name6; Native compiler reports 0136 on name6 below.
  1351. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name6").WithArguments("name6").WithLocation(36, 26)
  1352. );
  1353. }
  1354. [Fact]
  1355. private void TestCollisionInsideTryCatch_LegalCases()
  1356. {
  1357. var source = @"
  1358. using System;
  1359. class Derived : Base
  1360. {
  1361. static long name1 = 1;
  1362. static Derived()
  1363. {
  1364. {
  1365. try
  1366. {
  1367. Console.WriteLine(name1);
  1368. }
  1369. catch (ArgumentException name1)
  1370. {
  1371. Console.WriteLine(name1.Message);
  1372. }
  1373. catch (Exception name1)
  1374. {
  1375. Console.WriteLine(name1.Message);
  1376. }
  1377. }
  1378. {
  1379. Console.WriteLine(name1);
  1380. try
  1381. {
  1382. var name4 = string.Empty;
  1383. try
  1384. {
  1385. name2 = 3;
  1386. string name5 = string.Empty;
  1387. name5.ToString(); name4.ToString();
  1388. }
  1389. catch (Exception name2)
  1390. {
  1391. Console.WriteLine(name2.Message);
  1392. string name5 = string.Empty;
  1393. name5.ToString(); name4.ToString();
  1394. }
  1395. }
  1396. catch (Exception other)
  1397. {
  1398. var name4 = string.Empty;
  1399. Console.WriteLine(name4.ToString());
  1400. name2 = 4;
  1401. Console.WriteLine(other.Message);
  1402. }
  1403. }
  1404. }
  1405. }
  1406. class Base
  1407. {
  1408. protected static long name2 = 5;
  1409. }";
  1410. CompileAndVerify(source).VerifyDiagnostics();
  1411. }
  1412. [Fact]
  1413. private void TestCollisionInsideTryCatch()
  1414. {
  1415. var source = @"
  1416. using System;
  1417. class Derived : Base
  1418. {
  1419. static long name1 = 1;
  1420. static Derived()
  1421. {
  1422. {
  1423. Console.WriteLine(name1);
  1424. try
  1425. {
  1426. Console.WriteLine(name1);
  1427. }
  1428. catch (ArgumentException name1)
  1429. {
  1430. Console.WriteLine(name1.Message);
  1431. }
  1432. catch (Exception name2)
  1433. {
  1434. Console.WriteLine(name2.Message);
  1435. }
  1436. Console.WriteLine(name2);
  1437. }
  1438. {
  1439. try
  1440. {
  1441. var name4 = string.Empty;
  1442. Console.WriteLine(name1);
  1443. }
  1444. catch (Exception name1)
  1445. {
  1446. System.Console.WriteLine(name1.Message);
  1447. var name5 = string.Empty;
  1448. try
  1449. {
  1450. }
  1451. catch (Exception name1) // 0136 on name1
  1452. {
  1453. var name5 = string.Empty; // 0136 on name5
  1454. System.Console.WriteLine(name1.Message);
  1455. }
  1456. }
  1457. var name4 = string.Empty; // Native reports 0136 here;
  1458. }
  1459. }
  1460. }
  1461. class Base
  1462. {
  1463. protected static long name2 = 2;
  1464. }";
  1465. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1466. // (27,21): 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
  1467. // var name4 = string.Empty; // 0136: Roslyn reports this here; native reports it below.
  1468. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name4").WithArguments("name4").WithLocation(27, 21),
  1469. // (38,34): 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
  1470. // catch (Exception name1) // 0136 on name1
  1471. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name1").WithArguments("name1").WithLocation(38, 34),
  1472. // (40,25): 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
  1473. // var name5 = string.Empty; // 0136 on name5
  1474. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "name5").WithArguments("name5").WithLocation(40, 25)
  1475. );
  1476. }
  1477. [Fact]
  1478. public void DifferentArities()
  1479. {
  1480. var source = @"
  1481. public class C<T>
  1482. {
  1483. public static U G<U>(U x)
  1484. {
  1485. return x;
  1486. }
  1487. void M()
  1488. {
  1489. int G = 10;
  1490. G<int>(G);
  1491. int C = 5;
  1492. C<string>.G(C);
  1493. }
  1494. }";
  1495. CompileAndVerify(source).VerifyDiagnostics();
  1496. }
  1497. [WorkItem(10556, "DevDiv_Projects/Roslyn")]
  1498. [Fact]
  1499. private void TestCollisionInsideQuery_LegalCases()
  1500. {
  1501. var source = @"
  1502. using System.Linq;
  1503. using System.Collections.Generic;
  1504. partial class Class
  1505. {
  1506. private string Property
  1507. {
  1508. set
  1509. {
  1510. var other1 = from int name1 in ""string"" select name1;
  1511. {
  1512. var query1 = from name1 in ""string""
  1513. select name1;
  1514. var query2 = from name1 in ""string""
  1515. select name1;
  1516. this.Method(from name1 in ""string""
  1517. select name1).Method(from name1 in ""string""
  1518. select name1);
  1519. }
  1520. other1 = from int name1 in ""string"" select name2;
  1521. {
  1522. var query1 = from name1 in ""string""
  1523. let name2 = 1
  1524. select name1;
  1525. var query2 = from name2 in ""string""
  1526. select name2;
  1527. this.Method(from other2 in ""string""
  1528. from name2 in ""string""
  1529. select other2).Method(from name1 in ""string""
  1530. group name1 by name1 into name2
  1531. select name2);
  1532. Method(from name1 in ""string""
  1533. group name1 by name1.ToString() into name1
  1534. select name1);
  1535. }
  1536. }
  1537. }
  1538. int name2 = 2;
  1539. Class Method(IEnumerable<char> name1)
  1540. {
  1541. return null;
  1542. }
  1543. Class Method(object name1)
  1544. {
  1545. return null;
  1546. }
  1547. }";
  1548. CompileAndVerify(source, new[] { LinqAssemblyRef }).VerifyDiagnostics();
  1549. }
  1550. [WorkItem(543045, "DevDiv")]
  1551. [Fact]
  1552. private void TestCollisionInsideQuery()
  1553. {
  1554. var source = @"
  1555. using System;
  1556. using System.Linq;
  1557. using System.Collections.Generic;
  1558. partial class Class
  1559. {
  1560. private string Property
  1561. {
  1562. set
  1563. {
  1564. Console.WriteLine(name1);
  1565. {
  1566. var query1 = from name1 in string.Empty // 1931 -- UNDONE change to 0135 because name1 above refers to Class.name1
  1567. select name1;
  1568. var query2 = from other in string.Empty
  1569. let name1 = 1 // 1931 -- UNDONE change to 0135 because name1 above refers to Class.name1
  1570. select other;
  1571. this.Method(from other in string.Empty
  1572. from name1 in string.Empty // 1931 -- UNDONE change to 0135 because name1 above refers to Class.name1
  1573. select other).Method(from other in string.Empty
  1574. group other by other.ToString() into name1 // 1931 -- UNDONE change to 0135 because name1 above refers to Class.name1
  1575. select name1);
  1576. }
  1577. {
  1578. var query1 = from name2 in string.Empty
  1579. let name2 = 2 // 1930
  1580. select name2;
  1581. this.Method(from name2 in string.Empty
  1582. from name2 in name1.ToString() // 1930
  1583. select name2);
  1584. }
  1585. }
  1586. }
  1587. int name1 = 3;
  1588. Class Method(IEnumerable<char> name1)
  1589. {
  1590. return null;
  1591. }
  1592. Class Method(object name1)
  1593. {
  1594. return null;
  1595. }
  1596. }";
  1597. CreateCompilationWithMscorlibAndSystemCore(source).VerifyDiagnostics(
  1598. // (26,34): error CS1930: The range variable 'name2' has already been declared
  1599. // let name2 = 2 // 1930
  1600. Diagnostic(ErrorCode.ERR_QueryDuplicateRangeVariable, "name2").WithArguments("name2").WithLocation(26, 34),
  1601. // (29,34): error CS1930: The range variable 'name2' has already been declared
  1602. // from name2 in name1.ToString() // 1930
  1603. Diagnostic(ErrorCode.ERR_QueryDuplicateRangeVariable, "name2").WithArguments("name2").WithLocation(29, 34)
  1604. );
  1605. }
  1606. [Fact]
  1607. public void TestCollisionInsideQuery_TypeParameter()
  1608. {
  1609. var source = @"
  1610. using System.Linq;
  1611. public class Class
  1612. {
  1613. void Method<T, U>()
  1614. {
  1615. {
  1616. var q = from T in """"
  1617. let U = T
  1618. select T;
  1619. }
  1620. }
  1621. }";
  1622. CreateCompilationWithMscorlibAndSystemCore(source).VerifyDiagnostics(
  1623. // (7,26): error CS1948: The range variable 'T' cannot have the same name as a method type parameter
  1624. // var q = from T in ""
  1625. Diagnostic(ErrorCode.ERR_QueryRangeVariableSameAsTypeParam, "T").WithArguments("T"),
  1626. // (8,25): error CS1948: The range variable 'U' cannot have the same name as a method type parameter
  1627. // let U = T
  1628. Diagnostic(ErrorCode.ERR_QueryRangeVariableSameAsTypeParam, "U").WithArguments("U"));
  1629. }
  1630. [WorkItem(542088, "DevDiv")]
  1631. [Fact]
  1632. public void LocalCollidesWithGenericType()
  1633. {
  1634. var source = @"
  1635. public class C
  1636. {
  1637. public static int G<T>(int x)
  1638. {
  1639. return x;
  1640. }
  1641. public static void Main()
  1642. {
  1643. int G = 10;
  1644. G<int>(G);
  1645. }
  1646. }";
  1647. CompileAndVerify(source).VerifyDiagnostics();
  1648. }
  1649. [WorkItem(542039, "DevDiv")]
  1650. [Fact]
  1651. public void BindingOrderCollisions01()
  1652. {
  1653. var source =
  1654. @"using System;
  1655. class A
  1656. {
  1657. static long M(Action<long> act) { return 0; }
  1658. static void What1()
  1659. {
  1660. int x1;
  1661. {
  1662. int y1 = M(x1 => { });
  1663. }
  1664. }
  1665. static void What2()
  1666. {
  1667. {
  1668. int y2 = M(x2 => { });
  1669. }
  1670. int x2;
  1671. }
  1672. }";
  1673. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1674. // (10,24): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because
  1675. // that name is used in an enclosing local scope to define a local or parameter int y1 = M(x1 => {
  1676. // });
  1677. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1"),
  1678. // (10,22): error CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
  1679. // int y1 = M(x1 => { });
  1680. Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "M(x1 => { })").WithArguments("long", "int"),
  1681. // (8,13): warning CS0168: The variable 'x1' is declared but never used
  1682. // int x1;
  1683. Diagnostic(ErrorCode.WRN_UnreferencedVar, "x1").WithArguments("x1"),
  1684. // (16,22): error CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
  1685. // int y2 = M(x2 => { });
  1686. Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "M(x2 => { })").WithArguments("long", "int"),
  1687. // (16,24): error CS0136: A local or parameter named 'x2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1688. // int y2 = M(x2 => { });
  1689. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x2").WithArguments("x2"),
  1690. // (18,13): warning CS0168: The variable 'x2' is declared but never used
  1691. // int x2;
  1692. Diagnostic(ErrorCode.WRN_UnreferencedVar, "x2").WithArguments("x2")
  1693. );
  1694. }
  1695. [WorkItem(542039, "DevDiv")]
  1696. [Fact]
  1697. public void BindingOrderCollisions02()
  1698. {
  1699. var source =
  1700. @"using System;
  1701. class A
  1702. {
  1703. static double M(Action<double> act) { return 0; }
  1704. static long M(Action<long> act) { return 0; }
  1705. static void What1()
  1706. {
  1707. int x1;
  1708. {
  1709. int y1 = M(x1 => { });
  1710. }
  1711. }
  1712. static void What2()
  1713. {
  1714. {
  1715. int y2 = M(x2 => { });
  1716. }
  1717. int x2;
  1718. }
  1719. }";
  1720. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1721. // (11,24): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used
  1722. // in an enclosing local scope to define a local or parameter int y1 = M(x1 => { });
  1723. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1"),
  1724. // (11,22): error CS0121: The call is ambiguous between the following methods or properties: 'A.M(System.Action<double>)' and 'A.M(System.Action<long>)'
  1725. // int y1 = M(x1 => { });
  1726. Diagnostic(ErrorCode.ERR_AmbigCall, "M").WithArguments("A.M(System.Action<double>)", "A.M(System.Action<long>)"),
  1727. // (9,13): warning CS0168: The variable 'x1' is declared but never used
  1728. // int x1;
  1729. Diagnostic(ErrorCode.WRN_UnreferencedVar, "x1").WithArguments("x1"),
  1730. // (17,22): error CS0121: The call is ambiguous between the following methods or properties: 'A.M(System.Action<double>)' and 'A.M(System.Action<long>)'
  1731. // int y2 = M(x2 => { });
  1732. Diagnostic(ErrorCode.ERR_AmbigCall, "M").WithArguments("A.M(System.Action<double>)", "A.M(System.Action<long>)"),
  1733. // (17,24): error CS0136: A local or parameter named 'x2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
  1734. // int y2 = M(x2 => { });
  1735. Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x2").WithArguments("x2"),
  1736. // (19,13): warning CS0168: The variable 'x2' is declared but never used
  1737. // int x2;
  1738. Diagnostic(ErrorCode.WRN_UnreferencedVar, "x2").WithArguments("x2")
  1739. );
  1740. }
  1741. [WorkItem(542039, "DevDiv")]
  1742. [Fact]
  1743. public void BindingOrderCollisions03()
  1744. {
  1745. var source =
  1746. @"using System;
  1747. class Outer
  1748. {
  1749. static void Main(string[] args)
  1750. {
  1751. }
  1752. public static int M() { return 1; }
  1753. class Inner
  1754. {
  1755. public static int M = 2;
  1756. void F1()
  1757. {
  1758. int x1 = M;
  1759. Action a = () => { int x2 = M(); };
  1760. }
  1761. void F2()
  1762. {
  1763. Action a = () => { int x2 = M(); };
  1764. int x1 = M;
  1765. }
  1766. void F3()
  1767. {
  1768. int x1 = M();
  1769. Action a = () => { int x2 = M; };
  1770. }
  1771. void F4()
  1772. {
  1773. Action a = () => { int x2 = M; };
  1774. int x1 = M();
  1775. }
  1776. }
  1777. }";
  1778. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  1779. }
  1780. [WorkItem(835569, "DevDiv")]
  1781. [Fact]
  1782. private void CollisionWithSameWhenError()
  1783. {
  1784. var source = @"
  1785. using System;
  1786. using System.Reflection;
  1787. class Program
  1788. {
  1789. static void Main()
  1790. {
  1791. Console.WriteLine(string.Join < Assembly(Environment.NewLine, Assembly.GetEntryAssembly().GetReferencedAssemblies()));
  1792. }
  1793. }
  1794. ";
  1795. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  1796. Diagnostic(ErrorCode.ERR_NonInvocableMemberCalled, "Assembly").WithArguments("System.Reflection.Assembly").WithLocation(9, 41)
  1797. );
  1798. }
  1799. }
  1800. }