PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Syntax/Parsing/RoundTrippingTests.cs

https://github.com/EkardNT/Roslyn
C# | 1685 lines | 1549 code | 126 blank | 10 comment | 5 complexity | fc97f378ef19479f3acf59883f6d4524 MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis.CSharp.Symbols;
  4. using Microsoft.CodeAnalysis.CSharp.Syntax;
  5. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  6. using Microsoft.CodeAnalysis.Text;
  7. using Roslyn.Test.Utilities;
  8. using Xunit;
  9. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  10. {
  11. public class RoundTrippingTests
  12. {
  13. #region Helper
  14. internal static void ParseAndRoundTripping(string text, int errorCount = 0, int memberCount = 0)
  15. {
  16. ParseAndRoundTripping(text, TestOptions.RegularWithDocumentationComments, errorCount, memberCount);
  17. }
  18. internal static void ParseAndRoundTripping(string text, CSharpParseOptions options, int errorCount = 0, int memberCount = 0)
  19. {
  20. var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(text), options);
  21. var toText = tree.GetCompilationUnitRoot().ToFullString();
  22. Assert.Equal(text, toText);
  23. // -1 mean there are errors but actual number of errors is not important.
  24. // it makes the test more robust in case error count changes
  25. if (errorCount == -1)
  26. {
  27. Assert.NotEqual(0, tree.GetCompilationUnitRoot().ErrorsAndWarnings().Length);
  28. }
  29. else
  30. {
  31. Assert.Equal(errorCount, tree.GetCompilationUnitRoot().ErrorsAndWarnings().Length);
  32. }
  33. // check member count only if > 0
  34. if (memberCount > 0)
  35. {
  36. Assert.Equal(memberCount, tree.GetCompilationUnitRoot().Members.Count);
  37. }
  38. ParentChecker.CheckParents(tree.GetCompilationUnitRoot(), tree);
  39. }
  40. public static void ParseAndCheckTerminalSpans(string text)
  41. {
  42. var tree = SyntaxFactory.ParseSyntaxTree(text);
  43. var toText = tree.GetCompilationUnitRoot().ToFullString();
  44. Assert.Equal(text, toText);
  45. var nodes = tree.GetCompilationUnitRoot().DescendantTokens(tk => tk.FullWidth > 0).ToList();
  46. if (nodes.Count > 0)
  47. {
  48. var prevSpan = nodes[0].FullSpan;
  49. for (int i = 1; i < nodes.Count; i++)
  50. {
  51. var span = nodes[i].FullSpan;
  52. Assert.Equal(prevSpan.End, span.Start);
  53. prevSpan = span;
  54. }
  55. }
  56. }
  57. #endregion
  58. [Fact]
  59. public void AutoPropInitializers()
  60. {
  61. var experimental = TestOptions.ExperimentalParseOptions;
  62. ParseAndRoundTripping("class C { int GetInt { get; } = 0; }", experimental, memberCount: 1);
  63. ParseAndRoundTripping("class C { int GetInt { get; } = 0 }", experimental, 1, 1);
  64. ParseAndRoundTripping("class C { public int GetInt { get; } = 0; }", experimental, memberCount: 1);
  65. ParseAndRoundTripping("class C { int GetInt { get; } = 0;; }", experimental, 1, 1);
  66. ParseAndRoundTripping("class C { int GetInt { get;; } = 0;; }", experimental, 2, 1);
  67. ParseAndRoundTripping("interface I { int GetInt { get; } = 0; }", experimental, memberCount: 1);
  68. ParseAndRoundTripping("interface I { int GetInt { get; } = 0 }", experimental, 1, 1);
  69. ParseAndRoundTripping("interface I { public int GetInt { get; } = 0; }", experimental, memberCount: 1);
  70. }
  71. [Fact()]
  72. [WorkItem(530410, "DevDiv")]
  73. public void NullChar()
  74. {
  75. ParseAndRoundTripping("\0", 1);
  76. ParseAndRoundTripping("abc\0def", 3);
  77. ParseAndRoundTripping("\0abc", 2);
  78. ParseAndRoundTripping("class C { string s = \"\0\"; }", 0);
  79. }
  80. [Fact()]
  81. [WorkItem(530410, "DevDiv")]
  82. public void CharMaxValue()
  83. {
  84. string text = "abc" + char.MaxValue + "def";
  85. var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(text), path: "");
  86. var toText = tree.GetCompilationUnitRoot().ToFullString();
  87. Assert.Equal(text, toText);
  88. }
  89. [Fact]
  90. public void TestOptionalFloat()
  91. {
  92. ParseAndRoundTripping(Resources.OptionalFloat);
  93. }
  94. [Fact]
  95. public void TestOptionalParamsArray()
  96. {
  97. ParseAndRoundTripping(Resources.OptionalParamsArray);
  98. }
  99. [WorkItem(862632, "DevDiv/Personal")]
  100. [Fact]
  101. public void TestNegInvalidExternAlias01()
  102. {
  103. ParseAndRoundTripping(Resources.InvalidExternAlias01, -1);
  104. }
  105. [WorkItem(901348, "DevDiv/Personal")]
  106. [Fact]
  107. public void TestNegPartialAliasedName()
  108. {
  109. ParseAndRoundTripping(Resources.PartialAliasedName, -1);
  110. }
  111. [WorkItem(894884, "DevDiv/Personal")]
  112. [Fact]
  113. public void TestNegPartialInKeyword()
  114. {
  115. ParseAndRoundTripping(Resources.PartialInKeyword, -1);
  116. }
  117. [WorkItem(901493, "DevDiv/Personal")]
  118. [Fact]
  119. public void TestNegPartialAttribute()
  120. {
  121. // although this code snippet has multiple statements on top level we report that as semantic errors, not parse errors:
  122. ParseAndRoundTripping(Resources.PartialNewAttributeArray, 0);
  123. }
  124. [WorkItem(901498, "DevDiv/Personal")]
  125. [Fact]
  126. public void TestNegPartialPreProcessorExpression()
  127. {
  128. ParseAndRoundTripping(Resources.PartialPreprocessorExpression, -1);
  129. }
  130. [WorkItem(901508, "DevDiv/Personal")]
  131. [Fact]
  132. public void TestNegPartialUnicodeIdentifier()
  133. {
  134. ParseAndRoundTripping(Resources.PartialUnicodeIdentifier, -1);
  135. }
  136. [WorkItem(901516, "DevDiv/Personal")]
  137. [Fact]
  138. public void TestNegIncompleteSwitchBlock()
  139. {
  140. ParseAndRoundTripping(Resources.PartialSwitchBlock, -1);
  141. }
  142. [Fact]
  143. public void TestNegBug862116()
  144. {
  145. var text = @"
  146. namespace x
  147. {
  148. public class a
  149. {
  150. public int hiddenMember2;
  151. }
  152. public class b : a
  153. {
  154. public override int hiddenMember2
  155. {
  156. public static void Main()
  157. {
  158. }
  159. }
  160. }
  161. ";
  162. ParseAndRoundTripping(text, -1);
  163. }
  164. [Fact]
  165. public void TestNegBug862635()
  166. {
  167. var text = @"
  168. class Test
  169. {
  170. static void Main()
  171. {
  172. ::Console.WriteLine(""Missing identifier before :: "");
  173. }
  174. }
  175. ";
  176. ParseAndRoundTripping(text, -1);
  177. }
  178. [Fact]
  179. public void Bug862637()
  180. {
  181. var text = @"
  182. using System;
  183. public class Test
  184. {
  185. static void Main()
  186. {
  187. Console.WriteLine(dDep.GetType().Assembly.FullName);
  188. }
  189. }
  190. ";
  191. ParseAndRoundTripping(text);
  192. }
  193. [Fact]
  194. public void Bug862640()
  195. {
  196. var text = @"
  197. public class Production
  198. {
  199. public Production()
  200. {
  201. ((VoidDelegate)delegate
  202. {
  203. this.someType.Iterate(delegate(object o)
  204. {
  205. System.Console.WriteLine(((BoolDelegate)delegate { return object.Equals(o, this.epsilon); })());
  206. });
  207. })();
  208. }
  209. }
  210. ";
  211. ParseAndRoundTripping(text);
  212. }
  213. [Fact]
  214. public void TestNegBug862642()
  215. {
  216. var text = @"
  217. alias myAlias;
  218. class myClass
  219. {
  220. }
  221. ";
  222. // top-level field declaration is a semantic error
  223. ParseAndRoundTripping(text);
  224. }
  225. [Fact]
  226. public void TestNegBug862643()
  227. {
  228. var text = @"
  229. using System;
  230. [AttributeUsage(AttributeTargets.All)]
  231. public class Foo : Attribute
  232. {
  233. public int Name;
  234. public Foo (int sName) {Name = sName;}
  235. }
  236. public class Class1 {
  237. int Meth2 ([event:Foo(5)]int parm) {return 0;}
  238. public int IP { get {return 0;} set {}}
  239. }
  240. ";
  241. ParseAndRoundTripping(text);
  242. }
  243. [Fact]
  244. public void Bug862644()
  245. {
  246. var text = @"
  247. using System;
  248. public class Test
  249. {
  250. [method:MyAttribute(TypeObject = new int[1].GetType())]
  251. public void foo()
  252. {
  253. }
  254. }
  255. ";
  256. ParseAndRoundTripping(text);
  257. }
  258. [Fact]
  259. public void Bug862646()
  260. {
  261. var text = @"
  262. // C# compiler emits Void& type
  263. ";
  264. ParseAndRoundTripping(text);
  265. }
  266. [Fact]
  267. public void Bug862648()
  268. {
  269. var text = @"
  270. class TestClass
  271. {
  272. static void Main()
  273. {
  274. int partial;
  275. }
  276. }
  277. ";
  278. ParseAndRoundTripping(text);
  279. }
  280. [Fact]
  281. public void Bug870754()
  282. {
  283. var text = @"class C{
  284. C(){
  285. int y = 3;
  286. (y).ToString();
  287. }
  288. }
  289. ";
  290. ParseAndRoundTripping(text);
  291. }
  292. [Fact]
  293. public void TestNegBug875711()
  294. {
  295. var text = @"
  296. using System;
  297. public class A
  298. {
  299. public static operator ++ A(int i)
  300. {
  301. }
  302. }
  303. ";
  304. ParseAndRoundTripping(text, -1);
  305. }
  306. [Fact]
  307. public void TestNegBug876359()
  308. {
  309. var text = @"
  310. class C {
  311. fixed x;
  312. }
  313. ";
  314. ParseAndRoundTripping(text, -1);
  315. }
  316. [Fact]
  317. public void TestNegBug876363()
  318. {
  319. var text = @"
  320. class X { void f() {
  321. int a = 1; \u000a int b = (int)2.3;
  322. }
  323. }
  324. ";
  325. ParseAndRoundTripping(text, -1);
  326. }
  327. [Fact]
  328. public void Bug876565()
  329. {
  330. var text = @"
  331. public class C
  332. {
  333. public static int Main()
  334. {
  335. int result = 0;
  336. Func<int?, IStr0?> f1 = (int? x) => x;
  337. }
  338. }
  339. ";
  340. ParseAndRoundTripping(text);
  341. }
  342. [Fact]
  343. public void Bug876573()
  344. {
  345. var text = @"
  346. [partial]
  347. partial class partial { }
  348. partial class partial
  349. {
  350. public partial()
  351. {
  352. fld1 = fld2 = fld3 = fld4 = -1;
  353. }
  354. }
  355. ";
  356. ParseAndRoundTripping(text);
  357. }
  358. [Fact]
  359. public void TestNegBug876575()
  360. {
  361. var text = @"partial enum E{}";
  362. ParseAndRoundTripping(text, 1);
  363. }
  364. [Fact]
  365. public void Bug877232()
  366. {
  367. var text = @"
  368. class MyClass : MyBase {
  369. MyClass() : base() {
  370. }
  371. }
  372. ";
  373. ParseAndRoundTripping(text);
  374. }
  375. [Fact]
  376. public void TestNegBug877242()
  377. {
  378. var text = @"
  379. private namespace test
  380. {
  381. }
  382. ";
  383. ParseAndRoundTripping(text, -1);
  384. }
  385. [Fact]
  386. public void Bug877246()
  387. {
  388. var text = @"
  389. public struct Test
  390. {
  391. static int Main()
  392. {
  393. test.emptyStructGenNullableArr = new EmptyStructGen<string, EmptyClass>?[++i];
  394. }
  395. }
  396. ";
  397. ParseAndRoundTripping(text);
  398. }
  399. [Fact]
  400. public void Bug877251()
  401. {
  402. var text = @"
  403. static class Test
  404. {
  405. static void Main()
  406. {
  407. A a = new A { 5, { 1, 2, {1, 2} }, 3 };
  408. }
  409. }
  410. ";
  411. ParseAndRoundTripping(text, -1);
  412. }
  413. [Fact]
  414. public void TestNegBug877256()
  415. {
  416. var text = @"
  417. using System;
  418. public static class Extensions
  419. {
  420. static ~Extensions(this Struct s) {}
  421. static ~Extensions(this Struct? s) {}
  422. }
  423. ";
  424. ParseAndRoundTripping(text, -1);
  425. }
  426. [Fact]
  427. public void Bug877313()
  428. {
  429. var text = @"
  430. partial class ConstaintsDef
  431. {
  432. partial void PM<T1, T2>(T1 v1, ref T2 v2, params T2[] v3)
  433. where T2 : new()
  434. where T1 : System.IComparable<T1>, System.Collections.IEnumerable;
  435. partial void PM<T1, T2>(T1 v1, ref T2 v2, params T2[] v3)
  436. {
  437. v2 = v3[0];
  438. }
  439. }
  440. ";
  441. ParseAndRoundTripping(text);
  442. }
  443. [Fact]
  444. public void TestNegBug877318()
  445. {
  446. var text = @"
  447. partial class PartialPartial
  448. {
  449. int i = 1;
  450. partial partial void PM();
  451. partial partial void PM()
  452. {
  453. i = 0;
  454. }
  455. static int Main()
  456. {
  457. PartialPartial t = new PartialPartial();
  458. t.PM();
  459. return t.i;
  460. }
  461. }
  462. ";
  463. ParseAndRoundTripping(text, -1);
  464. }
  465. [Fact]
  466. public void TestNegBug879395()
  467. {
  468. var text = @"
  469. module m1
  470. end module
  471. ";
  472. ParseAndRoundTripping(text, 2);
  473. }
  474. [Fact]
  475. public void Bug880479()
  476. {
  477. var text = @"
  478. class c1
  479. {
  480. void foo(int a, int b, int c)
  481. {
  482. }
  483. }
  484. ";
  485. ParseAndCheckTerminalSpans(text);
  486. }
  487. [Fact]
  488. public void TestNegBug881436()
  489. {
  490. var text = @"
  491. public class Test
  492. {
  493. public static void Main()
  494. {
  495. var v1 = new { X = x; Y = y, Z = z };
  496. }
  497. }
  498. ";
  499. ParseAndRoundTripping(text, -1);
  500. }
  501. [Fact]
  502. public void Bug881480()
  503. {
  504. var text = @"
  505. // <Code>
  506. public static class SubGenericClass<T> : GenericClass<T>
  507. {
  508. public SubGenericClass() : base() { }
  509. }
  510. ";
  511. ParseAndRoundTripping(text);
  512. }
  513. [Fact]
  514. public void Bug881485()
  515. {
  516. var text = @"
  517. class Test
  518. {
  519. public static int Test2()
  520. {
  521. var testResult = testFunc((long?)-1);
  522. }
  523. }
  524. ";
  525. ParseAndRoundTripping(text);
  526. }
  527. [Fact]
  528. public void TestNegBug881488()
  529. {
  530. var text = @"partial class A
  531. {
  532. partial void C<T>(T )=>{ t) { }
  533. }
  534. ";
  535. ParseAndRoundTripping(text, -1);
  536. }
  537. [Fact]
  538. public void TestNegBug882388()
  539. {
  540. var text = @"
  541. public class Class1
  542. {
  543. public int Meth2(int i) {
  544. [return:Foo(5)]
  545. return 0;
  546. }
  547. }
  548. ";
  549. ParseAndRoundTripping(text, -1);
  550. }
  551. [Fact]
  552. public void Bug882417()
  553. {
  554. var text = @"
  555. [AttributeUsage(AttributeTargets.Class)]
  556. public class HelpAttribute : Attribute
  557. {
  558. public HelpAttribute(byte b1) {
  559. b = b1;
  560. }
  561. byte b = 0;
  562. public byte Verify {get {return b;} }
  563. }
  564. ";
  565. ParseAndRoundTripping(text);
  566. }
  567. [Fact]
  568. public void TestNegBug882424()
  569. {
  570. var text = @"
  571. public class MyClass {
  572. //invalid simple name
  573. int -foo(){
  574. return 1;
  575. }
  576. }
  577. ";
  578. ParseAndRoundTripping(text, -1);
  579. }
  580. [Fact]
  581. public void TestNegBug882432()
  582. {
  583. var text = @"
  584. public class Base1 {
  585. public static E1 {a, b, c, };
  586. }
  587. ";
  588. ParseAndRoundTripping(text, -1);
  589. }
  590. [Fact]
  591. public void TestNegBug882444()
  592. {
  593. var text = @"
  594. namespace nms {
  595. public class MyException : ApplicationException {
  596. public MyException(String str) : base ApplicationException (str)
  597. {}
  598. };
  599. ";
  600. ParseAndRoundTripping(text, -1, 1);
  601. }
  602. [Fact]
  603. public void Bug882459()
  604. {
  605. var text = @"
  606. public partial class Base
  607. {
  608. ViolateClassConstraint Fld01 = new Base().Meth<ViolateClassConstraint>(new ViolateClassConstraint()); //E:CS0315
  609. }
  610. ";
  611. ParseAndRoundTripping(text);
  612. }
  613. [Fact]
  614. public void TestNegBug882463()
  615. {
  616. var text = @"
  617. public class Test
  618. {
  619. yield break;
  620. static int Main()
  621. {
  622. return 1;
  623. }
  624. }
  625. ";
  626. ParseAndRoundTripping(text, -1);
  627. }
  628. [Fact]
  629. public void TestNegBug882465()
  630. {
  631. var text = @"
  632. public class Comments
  633. {
  634. // /* This is a comment
  635. This is a comment
  636. */
  637. }
  638. ";
  639. ParseAndRoundTripping(text, -1);
  640. }
  641. [Fact]
  642. public void Bug882471()
  643. {
  644. var text = @"
  645. class Test
  646. {
  647. static int Main()
  648. {
  649. string \u0069f;
  650. }
  651. }
  652. ";
  653. ParseAndRoundTripping(text, 0, 1);
  654. }
  655. [Fact]
  656. public void Bug882481()
  657. {
  658. var text = @"
  659. #define \u0066oxbar
  660. #if foxbar
  661. class Foo { }
  662. #endif
  663. ";
  664. ParseAndRoundTripping(text, 0, 1);
  665. }
  666. [Fact]
  667. public void TestNegBug882482()
  668. {
  669. var text = @"
  670. using System;
  671. class main
  672. {
  673. public static void Main()
  674. {
  675. i\u0066 (true)
  676. Console.WriteLine(""This should not have worked!"");
  677. }
  678. }
  679. ";
  680. ParseAndRoundTripping(text, -1);
  681. }
  682. [Fact]
  683. public void Bug882957()
  684. {
  685. var text = @"
  686. partial class CNExp
  687. {
  688. public static long? operator &(CNExp v1, long? v2)
  689. {
  690. return null;
  691. }
  692. }
  693. ";
  694. ParseAndRoundTripping(text);
  695. }
  696. [Fact]
  697. public void Bug882984()
  698. {
  699. var text = @"
  700. unsafe partial class C
  701. {
  702. byte* buf;
  703. public byte* ubuf
  704. {
  705. set { buf = value;}
  706. }
  707. }
  708. ";
  709. ParseAndRoundTripping(text);
  710. }
  711. [Fact]
  712. public void Bug883177()
  713. {
  714. var text = @"
  715. unsafe
  716. struct Test
  717. {
  718. public fixed int i[1];
  719. byte* myIntBuf;
  720. static int Main()
  721. {
  722. int retval = 0;
  723. Test t = new Test();
  724. t.i[0] = 0;
  725. t.myIntBuf = (byte*) t.i;
  726. if (*t.myIntBuf !=0)
  727. retval = 1;
  728. if (retval != 0)
  729. System.Console.WriteLine(""Failed."");
  730. return retval;
  731. }
  732. }
  733. ";
  734. ParseAndRoundTripping(text);
  735. }
  736. [Fact]
  737. [Trait("Regression", "Spans")]
  738. public void Bug884246()
  739. {
  740. var text = @"
  741. using System.Reflection;
  742. #if VER1
  743. [assembly:AssemblyVersionAttribute(""1.0.0.0"")]
  744. #elif VER2
  745. [assembly:AssemblyVersionAttribute(""2.0.0.0"")]
  746. #endif
  747. public class otherClass{}";
  748. ParseAndCheckTerminalSpans(text);
  749. }
  750. [Fact]
  751. public void Bug890389()
  752. {
  753. var text = @"using System;
  754. class C
  755. {
  756. void Foo()
  757. {
  758. Func<string> i = 3.ToString;
  759. }
  760. }
  761. ";
  762. ParseAndRoundTripping(text);
  763. }
  764. [Fact]
  765. public void TestNegBug892249()
  766. {
  767. var text = @"using System;
  768. class AAttribute : Attribute {
  769. public AAttribute(object o) { }
  770. }
  771. [A(new string[] is { ""hello"" })]
  772. class C {
  773. }
  774. ";
  775. ParseAndRoundTripping(text, -1);
  776. }
  777. [Fact]
  778. public void TestNegBug892255()
  779. {
  780. var text = @"using System;
  781. public class Test
  782. {
  783. public static int Main(string [] args)
  784. {
  785. int ret = 1;
  786. switch (false) {
  787. case true:
  788. ret = 1;
  789. break;
  790. default false:
  791. ret = 1;
  792. }
  793. return(ret);
  794. }
  795. }
  796. ";
  797. ParseAndRoundTripping(text, -1);
  798. }
  799. [WorkItem(894494, "DevDiv/Personal")]
  800. [Fact]
  801. public void TestRegressNegExtraPublicKeyword()
  802. {
  803. var text = @"
  804. using System;
  805. using System.Collections.Generic;
  806. using System.Linq;
  807. using System.Text;
  808. namespace ConsoleApplication1
  809. {
  810. class Program
  811. {
  812. static void Main(string[] args)
  813. {
  814. }
  815. }
  816. }
  817.  
  818. public class Class_1_L0
  819. {
  820. /";
  821. ParseAndRoundTripping(text, -1);
  822. }
  823. [Fact]
  824. public void TestNegBug894884()
  825. {
  826. var text = @"
  827. class C
  828. {
  829. public static int Main()
  830. {
  831. switch(str)
  832. {
  833. default:
  834. List<in
  835. ";
  836. ParseAndRoundTripping(text, -1);
  837. }
  838. [WorkItem(895762, "DevDiv/Personal")]
  839. [Fact]
  840. public void TestRegressInfiniteLoopXmlDoc()
  841. {
  842. var text = @"
  843. public struct MemberClass<T>
  844. {
  845. /// <summary>
  846. /// We use this to get the values we cannot get directly
  847. /// </summary>
  848. /// <param n";
  849. ParseAndRoundTripping(text, -1);
  850. }
  851. [Fact]
  852. public void Bug909041()
  853. {
  854. var text = @"
  855. interface A
  856. {
  857. void M<T>(T t) where T : class;
  858. }
  859. ";
  860. ParseAndRoundTripping(text);
  861. }
  862. [Fact]
  863. public void Bug909041b()
  864. {
  865. var text = @"
  866. public delegate void Del<T>(T t) where T : IEnumerable;
  867. public class A {}
  868. ";
  869. ParseAndRoundTripping(text);
  870. }
  871. [Fact]
  872. public void Bug909041c()
  873. {
  874. var text = @"
  875. class A
  876. {
  877. static extern bool Bar<U>() where U : class;
  878. }
  879. ";
  880. ParseAndRoundTripping(text);
  881. }
  882. [Fact]
  883. public void Bug909045()
  884. {
  885. var text = @"
  886. public class A
  887. {
  888. public void M<T, V>(T t, V v)
  889. where T : new()
  890. where V : class { }
  891. }
  892. ";
  893. ParseAndRoundTripping(text);
  894. }
  895. [WorkItem(909049, "DevDiv/Personal")]
  896. [WorkItem(911392, "DevDiv/Personal")]
  897. [Fact]
  898. public void RegressError4AttributeWithTarget()
  899. {
  900. var text = @"using System;
  901. using System.Runtime.InteropServices;
  902. interface IFoo
  903. {
  904. [return: MarshalAs(UnmanagedType.I2)]
  905. short M();
  906. int Prop
  907. {
  908. [return: MarshalAs(UnmanagedType.I4)]
  909. get;
  910. [param: MarshalAs(UnmanagedType.I4)]
  911. set;
  912. }
  913. long this[[MarshalAs(UnmanagedType.BStr)] string s]
  914. {
  915. [return: MarshalAs(UnmanagedType.I8)]
  916. get;
  917. set;
  918. }
  919. }
  920. public class Foo
  921. {
  922. public delegate void MyDelegate();
  923. public event MyDelegate eventMethod
  924. {
  925. [method: ComVisible(true)]
  926. add { }
  927. remove { }
  928. }
  929. }";
  930. ParseAndRoundTripping(text);
  931. }
  932. [Fact]
  933. public void Bug909063()
  934. {
  935. var text = @"
  936. class A
  937. {
  938. public int i, j;
  939. public static void Main()
  940. {
  941. var v = new A() { i=0, j=1, };
  942. var vv = new[] { 1, 2, };
  943. }
  944. }
  945. ";
  946. ParseAndRoundTripping(text);
  947. }
  948. [Fact]
  949. public void Bug909333()
  950. {
  951. var text = @"
  952. extern alias libAlias;
  953. class myClass
  954. {
  955. static int Main()
  956. {
  957. libAlias::otherClass oc = new libAlias.otherClass(); // '::' and '.'
  958. return 0;
  959. }
  960. }
  961. ";
  962. ParseAndRoundTripping(text);
  963. }
  964. [Fact]
  965. public void Bug909334()
  966. {
  967. var text = @"
  968. class Test
  969. {
  970. unsafe static int Main()
  971. {
  972. global::System.Int32* p = stackalloc global::System.Int32[5];
  973. return 0;
  974. }
  975. }
  976. ";
  977. ParseAndRoundTripping(text);
  978. }
  979. [Fact]
  980. public void Bug909337()
  981. {
  982. var text = @"
  983. using System.Collections;
  984. public class Test<I>
  985. {
  986. //ctor
  987. public Test(I i)
  988. {
  989. }
  990. // dtor
  991. ~Test() {}
  992. }
  993. ";
  994. ParseAndRoundTripping(text);
  995. }
  996. [Fact]
  997. public void Bug909338()
  998. {
  999. var text = @"
  1000. class Test
  1001. {
  1002. static void Main()
  1003. {
  1004. var v = typeof(void);
  1005. }
  1006. }
  1007. ";
  1008. ParseAndRoundTripping(text);
  1009. }
  1010. [Fact]
  1011. public void Bug909350()
  1012. {
  1013. var text = @"
  1014. [Author(""Brian Kernighan""), Author(""Dennis Ritchie""),]
  1015. class Class1
  1016. {
  1017. }
  1018. enum E
  1019. {
  1020. One, Two,
  1021. }
  1022. ";
  1023. ParseAndRoundTripping(text);
  1024. }
  1025. [Fact]
  1026. public void Bug909371()
  1027. {
  1028. var text = @"
  1029. class A
  1030. {
  1031. byte M(byte b)
  1032. {
  1033. return b;
  1034. }
  1035. }
  1036. ";
  1037. ParseAndRoundTripping(text);
  1038. }
  1039. [Fact]
  1040. public void Bug909371b()
  1041. {
  1042. var text = @"
  1043. [AttributeUsage(AttributeTargets.Class)]
  1044. public class HelpAttribute : Attribute
  1045. {
  1046. byte b = 0;
  1047. public HelpAttribute(byte b1) {
  1048. b = b1;
  1049. }
  1050. }
  1051. ";
  1052. ParseAndRoundTripping(text);
  1053. }
  1054. [WorkItem(909372, "DevDiv/Personal")]
  1055. [Fact]
  1056. public void RegressError4ValidOperatorOverloading()
  1057. {
  1058. var text = @"using System;
  1059. public class A
  1060. {
  1061. // unary
  1062. public static bool operator true(A a)
  1063. {
  1064. return false;
  1065. }
  1066. public static bool operator false(A a)
  1067. {
  1068. return true;
  1069. }
  1070. public static A operator ++(A a)
  1071. {
  1072. return a;
  1073. }
  1074. public static A operator --(A a)
  1075. {
  1076. return a;
  1077. }
  1078. // binary
  1079. public static bool operator <(A a, A b)
  1080. {
  1081. return true;
  1082. }
  1083. public static bool operator >(A a, A b)
  1084. {
  1085. return false;
  1086. }
  1087. public static bool operator <=(A a, A b)
  1088. {
  1089. return true;
  1090. }
  1091. public static bool operator >=(A a, A b)
  1092. {
  1093. return false;
  1094. }
  1095. }";
  1096. ParseAndRoundTripping(text);
  1097. }
  1098. [Fact]
  1099. public void Bug909419()
  1100. {
  1101. var text = @"
  1102. class Test
  1103. {
  1104. static int Main()
  1105. {
  1106. int n1 = test is Test ? 0 : 1;
  1107. int n2 = null == test as Test ? 0 : 1;
  1108. return n1 + n2;
  1109. }
  1110. }
  1111. ";
  1112. ParseAndRoundTripping(text);
  1113. }
  1114. [Fact]
  1115. public void Bug909425()
  1116. {
  1117. var text = @"
  1118. public class MyClass
  1119. {
  1120. public static int Main()
  1121. {
  1122. float f1 = 0.7e-44f;
  1123. double d1 = 5.0e-324;
  1124. return M(0.0e+999);
  1125. }
  1126. static int M(double d) { return 0; }
  1127. }
  1128. ";
  1129. ParseAndRoundTripping(text);
  1130. }
  1131. [WorkItem(909449, "DevDiv/Personal")]
  1132. [Fact]
  1133. public void RegressOverAggressiveWarningForUlongSuffix()
  1134. {
  1135. var text = @"class Program
  1136. {
  1137. static void Main()
  1138. {
  1139. ulong x1 = 7L;
  1140. // ulong x2 = 7l; // should Warn
  1141. ulong x4 = 7Ul; // should NOT warn
  1142. ulong x6 = 7ul; // should NOT warn
  1143. }
  1144. }";
  1145. ParseAndRoundTripping(text);
  1146. }
  1147. [Fact]
  1148. public void Bug909451()
  1149. {
  1150. var text = @"
  1151. public class AnonymousTypeTest : ParentClass
  1152. {
  1153. public void Run()
  1154. {
  1155. var p1 = new { base.Number };
  1156. }
  1157. }
  1158. ";
  1159. ParseAndRoundTripping(text);
  1160. }
  1161. [WorkItem(909828, "DevDiv/Personal")]
  1162. [Fact]
  1163. public void RegressError4ValidUlongLiteral()
  1164. {
  1165. var text = @"public class Test
  1166. {
  1167. public static int Main()
  1168. {
  1169. ulong n = 9223372036854775808U; //this should fit ulong
  1170. ulong n1 = 9223372036854775808Ul;
  1171. ulong n2 = 9223372036854775808uL;
  1172. ulong n3 = 9223372036854775808u;
  1173. return 0;
  1174. }
  1175. }
  1176. ";
  1177. ParseAndRoundTripping(text);
  1178. }
  1179. [WorkItem(922886, "DevDiv/Personal")]
  1180. [Fact]
  1181. public void RegressError4ValidNumericLiteral()
  1182. {
  1183. var text = @"public class Test
  1184. {
  1185. public static void Main()
  1186. {
  1187. sbyte min1 = -128, max1 = 127;
  1188. short min2 = -32768, max2 = 32767;
  1189. int min3 = -2147483648, max3 = 2147483647;
  1190. long min4 = -9223372036854775808L, max4 = 9223372036854775807L;
  1191. byte max5 = 255;
  1192. ushort max6 = 65535;
  1193. uint max7 = 4294967295;
  1194. ulong max8 = 18446744073709551615;
  1195. }
  1196. }
  1197. ";
  1198. ParseAndRoundTripping(text);
  1199. }
  1200. [WorkItem(911058, "DevDiv/Personal")]
  1201. [Fact]
  1202. public void RegressError4IdentifierStartWithAt1()
  1203. {
  1204. var text = @"using System;
  1205. [AttributeUsage(AttributeTargets.All)]
  1206. public class X : Attribute { }
  1207. [@X]
  1208. class A { }
  1209. namespace @namespace
  1210. {
  1211. class C1 { }
  1212. class @class : C1 { }
  1213. }
  1214. namespace N2
  1215. {
  1216. class Test
  1217. {
  1218. static int Main()
  1219. {
  1220. global::@namespace.@class c1 = new global::@namespace.@class();
  1221. global::@namespace.C1 c2 = new global::@namespace.@C1();
  1222. return 0;
  1223. }
  1224. }
  1225. }";
  1226. ParseAndRoundTripping(text);
  1227. }
  1228. [WorkItem(911059, "DevDiv/Personal")]
  1229. [Fact]
  1230. public void RegressError4IdentifierStartWithAt2()
  1231. {
  1232. var text = @"public class A
  1233. {
  1234. public int @__namespace = 0;
  1235. class @public // ok
  1236. {
  1237. private void M(int @int) { } // error
  1238. }
  1239. internal class @private // error
  1240. {
  1241. }
  1242. }";
  1243. ParseAndRoundTripping(text);
  1244. }
  1245. [WorkItem(911418, "DevDiv/Personal")]
  1246. [Fact]
  1247. public void RegressNegNoError4InvalidAttributeTarget()
  1248. {
  1249. var text = @"using System;
  1250. using System.Reflection;
  1251. public class foo
  1252. {
  1253. [method: method:A]
  1254. public static void Main()
  1255. {
  1256. }
  1257. }
  1258. [AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
  1259. public class A : Attribute
  1260. {
  1261. }";
  1262. ParseAndRoundTripping(text, -1);
  1263. // Assert.Equal((int)ErrorCode.ERR_SyntaxError, tree.Errors()[0].Code); // CS1003
  1264. // Assert.Equal((int)ErrorCode.ERR_InvalidMemberDecl, tree.Errors()[1].Code); // CS1519
  1265. }
  1266. [WorkItem(911477, "DevDiv/Personal")]
  1267. [Fact]
  1268. public void RegressWrongError4WarningExternOnCtor()
  1269. {
  1270. var text = @"using System;
  1271. public class C
  1272. {
  1273. extern C();
  1274. public static int Main()
  1275. {
  1276. return 1;
  1277. }
  1278. }";
  1279. // The warning WRN_ExternCtorNoImplementation is given in semantic analysis.
  1280. ParseAndRoundTripping(text); // , 1);
  1281. // Assert.Equal((int)ErrorCode.WRN_ExternCtorNoImplementation, tree.Errors()[0].Code); // W CS0824
  1282. }
  1283. [WorkItem(911488, "DevDiv/Personal")]
  1284. [Fact]
  1285. public void RegressError4MemberOnSimpleTypeAsKeyword()
  1286. {
  1287. var text = @"using System;
  1288. public class Test
  1289. {
  1290. public void M()
  1291. {
  1292. var v = int.MaxValue; // error
  1293. bool b = false;
  1294. string s = ""true"";
  1295. Boolean.TryParse(s, out b); // ok
  1296. bool.TryParse(s, out b); // error
  1297. }
  1298. }";
  1299. ParseAndRoundTripping(text);
  1300. }
  1301. [WorkItem(911505, "DevDiv/Personal")]
  1302. [Fact]
  1303. public void RegressWarning4EscapeCharInXmlDocAsText()
  1304. {
  1305. var text = @"using System;
  1306. /// <summary>
  1307. /// << A '&' B >>
  1308. /// </summary>
  1309. public class Test
  1310. {
  1311. bool Find()
  1312. {
  1313. return false;
  1314. }
  1315. }";
  1316. ParseAndRoundTripping(text, -1);
  1317. }
  1318. [WorkItem(911518, "DevDiv/Personal")]
  1319. [Fact]
  1320. public void RegressError4AnonmousTypeWithTailingComma()
  1321. {
  1322. var text = @"using System;
  1323. public class Test
  1324. {
  1325. public static void Main()
  1326. {
  1327. int x = 0;
  1328. var v1 = new { X = x, };
  1329. }
  1330. }";
  1331. ParseAndRoundTripping(text);
  1332. }
  1333. [WorkItem(911521, "DevDiv/Personal")]
  1334. [Fact]
  1335. public void RegressError4QueryWithVarInLet()
  1336. {
  1337. var text = @"class Q
  1338. {
  1339. static void Main()
  1340. {
  1341. var expr1 = new[] { 1, 2, 3, };
  1342. var expr2 = new[] { 3, 4, 5, };
  1343. var q = from i in (expr1) let j = expr2 select i;
  1344. var qq = from x1 in new[] { 3, 9, 5, 5, 0, 8, 6, 8, 9, }
  1345. orderby x1 descending
  1346. let x47 = x1
  1347. select (x47) - (x1);
  1348. }
  1349. }";
  1350. ParseAndRoundTripping(text);
  1351. }
  1352. [WorkItem(911525, "DevDiv/Personal")]
  1353. [Fact]
  1354. public void RegressError4AttributeWithNamedParam()
  1355. {
  1356. var text = @"using System;
  1357. public class TestAttribute : Attribute
  1358. {
  1359. public TestAttribute(int i = 0, int j = 1) { }
  1360. public int i { get; set; }
  1361. }
  1362. [Test(123, j:-1)]
  1363. public class A
  1364. {
  1365. public static int Main()
  1366. {
  1367. return 0;
  1368. }
  1369. }
  1370. ";
  1371. ParseAndRoundTripping(text);
  1372. }
  1373. [WorkItem(917285, "DevDiv/Personal")]
  1374. [Fact]
  1375. public void RegressAssertForLFCRSequence()
  1376. {
  1377. var text = "class Test\r\n{\n\r}\r\n";
  1378. ParseAndRoundTripping(text);
  1379. }
  1380. [WorkItem(917771, "DevDiv/Personal")]
  1381. [WorkItem(918947, "DevDiv/Personal")]
  1382. [Fact]
  1383. public void RegressNotCheckNullRef()
  1384. {
  1385. var text = @"public struct MyStruct
  1386. {
  1387. public delegate void TypeName<T>(ref T t, dynamic d);
  1388. public delegate Y @dynamic<X, Y>(X u, params dynamic[] ary);
  1389. public enum EM { };
  1390. }
  1391. ";
  1392. ParseAndRoundTripping(text);
  1393. }
  1394. [WorkItem(917771, "DevDiv/Personal")]
  1395. [Fact]
  1396. public void RegressNegNotCheckNullRef()
  1397. {
  1398. var text = @"class A
  1399. {
  1400. A a { 0, 1 };
  1401. }
  1402. ";
  1403. ParseAndRoundTripping(text, 2);
  1404. }
  1405. [WorkItem(922887, "DevDiv/Personal")]
  1406. [Fact]
  1407. public void RegressError4ExternOperator()
  1408. {
  1409. var text = @"public class A
  1410. {
  1411. public static extern int operator !(A a);
  1412. public static extern int operator +(A a, int n);
  1413. }
  1414. ";
  1415. ParseAndRoundTripping(text);
  1416. }
  1417. [Fact, WorkItem(536922, "DevDiv")]
  1418. public void RegressError4QueryWithNullable()
  1419. {
  1420. var text = @"using System.Linq;
  1421. class A
  1422. {
  1423. static void Main()
  1424. {
  1425. object[] p = { 1, 2, 3 };
  1426. var q = from x in p
  1427. where x is int?
  1428. select x;
  1429. }
  1430. }";
  1431. ParseAndRoundTripping(text);
  1432. }
  1433. [Fact, WorkItem(537265, "DevDiv")]
  1434. public void PartialMethodWithLanguageVersion2()
  1435. {
  1436. var text = @"partial class P
  1437. {
  1438. partial void M();
  1439. }
  1440. ";
  1441. CSharpParseOptions options = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp2);
  1442. var itext = SourceText.From(text);
  1443. var tree = SyntaxFactory.ParseSyntaxTree(itext, options, "");
  1444. var newTest = tree.GetCompilationUnitRoot().ToFullString();
  1445. Assert.Equal(text, newTest);
  1446. }
  1447. [WorkItem(527490, "DevDiv")]
  1448. [Fact]
  1449. public void VariableDeclarationAsTypeOfArgument()
  1450. {
  1451. string text = "typeof(System.String value)";
  1452. var typeOfExpression = SyntaxFactory.ParseExpression(text, consumeFullText: true);
  1453. Assert.Equal(text, typeOfExpression.ToFullString());
  1454. Assert.NotEmpty(typeOfExpression.GetDiagnostics());
  1455. typeOfExpression = SyntaxFactory.ParseExpression(text, consumeFullText: false);
  1456. Assert.Equal("typeof(System.String ", typeOfExpression.ToFullString());
  1457. Assert.NotEmpty(typeOfExpression.GetDiagnostics());
  1458. }
  1459. [WorkItem(540809, "DevDiv")]
  1460. [Fact]
  1461. public void IncompleteGlobalAlias()
  1462. {
  1463. var text = @"namespace N2
  1464. {
  1465. [global:";
  1466. ParseAndRoundTripping(text, errorCount: 3);
  1467. }
  1468. [WorkItem(542229, "DevDiv")]
  1469. [Fact]
  1470. public void MethodCallWithQueryArgInsideQueryExpr()
  1471. {
  1472. var text = @"
  1473. using System;
  1474. using System.Collections.Generic;
  1475. using System.Linq;
  1476. class Program
  1477. {
  1478. public static bool Method1(IEnumerable<int> f1)
  1479. {
  1480. return true;
  1481. }
  1482. static void Main(string[] args)
  1483. {
  1484. var numbers = new int[] { 4, 5 };
  1485. var f1 = from num1 in numbers where Method1(from num2 in numbers select num2) select num1;
  1486. }
  1487. }";
  1488. ParseAndRoundTripping(text, 0);
  1489. }
  1490. [WorkItem(542229, "DevDiv")]
  1491. [Fact]
  1492. public void MethodCallWithFromArgInsideQueryExpr()
  1493. {
  1494. var text = @"
  1495. using System;
  1496. using System.Collections.Generic;
  1497. using System.Linq;
  1498. class Program
  1499. {
  1500. public static bool Method1(IEnumerable<int> f1)
  1501. {
  1502. return true;
  1503. }
  1504. static void Main(string[] args)
  1505. {
  1506. var numbers = new int[] { 4, 5 };
  1507. var f1 = from num1 in numbers where Method1(from) select num1;
  1508. }
  1509. }";
  1510. ParseAndRoundTripping(text, -1);
  1511. }
  1512. [WorkItem(542229, "DevDiv")]
  1513. [Fact]
  1514. public void ArrayCreationWithQueryArgInsideQueryExpr()
  1515. {
  1516. var text = @"
  1517. using System;
  1518. using System.Collections.Generic;
  1519. using System.Linq;
  1520. class Program
  1521. {
  1522. public static bool Method1(IEnumerable<int> f1)
  1523. {
  1524. return true;
  1525. }
  1526. static void Main(string[] args)
  1527. {
  1528. var numbers = new int[] { 4, 5 };
  1529. var f1 = from num1 in new int[from num2 in numbers select num2] select num1; //not valid but this is only a parser test
  1530. }
  1531. }";
  1532. ParseAndRoundTripping(text, 0);
  1533. }
  1534. }
  1535. }