PageRenderTime 94ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs

https://github.com/EkardNT/Roslyn
C# | 7065 lines | 6589 code | 370 blank | 106 comment | 117 complexity | 65b60a20701d305e57ea57ce19e17487 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.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Microsoft.CodeAnalysis.CSharp.Symbols;
  6. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  7. using Microsoft.CodeAnalysis.Test.Utilities;
  8. using Roslyn.Test.Utilities;
  9. using Roslyn.Utilities;
  10. using Xunit;
  11. namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
  12. {
  13. public class CodeGenAsyncTests : EmitMetadataTestBase
  14. {
  15. private CSharpCompilation CreateCompilation(string source, IEnumerable<MetadataReference> references = null, CSharpCompilationOptions compOptions = null)
  16. {
  17. SynchronizationContext.SetSynchronizationContext(null);
  18. compOptions = compOptions ?? TestOptions.OptimizedExe;
  19. IEnumerable<MetadataReference> asyncRefs = new[] { SystemRef_v4_0_30319_17929, SystemCoreRef_v4_0_30319_17929, CSharpRef };
  20. references = (references != null) ? references.Concat(asyncRefs) : asyncRefs;
  21. return CreateCompilationWithMscorlib45(source, compOptions: compOptions, references: references);
  22. }
  23. private CompilationVerifier CompileAndVerify(string source, string expectedOutput, IEnumerable<MetadataReference> references = null, EmitOptions emitOptions = EmitOptions.All, CSharpCompilationOptions compOptions = null, bool emitPdb = false)
  24. {
  25. SynchronizationContext.SetSynchronizationContext(null);
  26. var compilation = this.CreateCompilation(source, references: references, compOptions: compOptions);
  27. return base.CompileAndVerify(compilation, expectedOutput: expectedOutput, emitOptions: emitOptions, emitPdb: emitPdb);
  28. }
  29. [Fact]
  30. public void StructVsClass()
  31. {
  32. var source = @"
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. class Test
  36. {
  37. public static async Task F(int a)
  38. {
  39. await Task.Factory.StartNew(() => { System.Console.WriteLine(a); });
  40. }
  41. public static void Main()
  42. {
  43. F(123).Wait();
  44. }
  45. }";
  46. var c = CreateCompilationWithMscorlib45(source);
  47. CompilationOptions options;
  48. options = TestOptions.OptimizedExe;
  49. Assert.False(options.EnableEditAndContinue);
  50. CompileAndVerify(c.WithOptions(options), symbolValidator: module =>
  51. {
  52. var stateMachine = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test").GetMember<NamedTypeSymbol>("<F>d__1");
  53. Assert.Equal(TypeKind.Struct, stateMachine.TypeKind);
  54. }, expectedOutput: "123");
  55. options = TestOptions.UnoptimizedExe;
  56. Assert.False(options.EnableEditAndContinue);
  57. CompileAndVerify(c.WithOptions(options), symbolValidator: module =>
  58. {
  59. var stateMachine = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test").GetMember<NamedTypeSymbol>("<F>d__1");
  60. Assert.Equal(TypeKind.Struct, stateMachine.TypeKind);
  61. }, expectedOutput: "123");
  62. options = TestOptions.OptimizedExe.WithDebugInformationKind(DebugInformationKind.Full);
  63. Assert.False(options.EnableEditAndContinue);
  64. CompileAndVerify(c.WithOptions(options), symbolValidator: module =>
  65. {
  66. var stateMachine = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test").GetMember<NamedTypeSymbol>("<F>d__1");
  67. Assert.Equal(TypeKind.Struct, stateMachine.TypeKind);
  68. }, expectedOutput: "123");
  69. options = TestOptions.UnoptimizedExe.WithDebugInformationKind(DebugInformationKind.PdbOnly);
  70. Assert.False(options.EnableEditAndContinue);
  71. CompileAndVerify(c.WithOptions(options), symbolValidator: module =>
  72. {
  73. var stateMachine = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test").GetMember<NamedTypeSymbol>("<F>d__1");
  74. Assert.Equal(TypeKind.Struct, stateMachine.TypeKind);
  75. }, expectedOutput: "123");
  76. options = TestOptions.UnoptimizedExe.WithDebugInformationKind(DebugInformationKind.Full);
  77. Assert.True(options.EnableEditAndContinue);
  78. CompileAndVerify(c.WithOptions(options), symbolValidator: module =>
  79. {
  80. var stateMachine = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test").GetMember<NamedTypeSymbol>("<F>d__1");
  81. Assert.Equal(TypeKind.Class, stateMachine.TypeKind);
  82. }, expectedOutput: "123");
  83. }
  84. [Fact]
  85. public void VoidReturningAsync()
  86. {
  87. var source = @"
  88. using System;
  89. using System.Diagnostics;
  90. using System.Threading;
  91. using System.Threading.Tasks;
  92. class Test
  93. {
  94. static int i = 0;
  95. public static async void F(AutoResetEvent handle)
  96. {
  97. try
  98. {
  99. await Task.Factory.StartNew(() =>
  100. {
  101. Interlocked.Increment(ref Test.i);
  102. });
  103. }
  104. finally
  105. {
  106. handle.Set();
  107. }
  108. }
  109. public static void Main()
  110. {
  111. var handle = new AutoResetEvent(false);
  112. F(handle);
  113. handle.WaitOne(1000 * 60);
  114. Console.WriteLine(i);
  115. }
  116. }";
  117. var expected = @"
  118. 1
  119. ";
  120. CompileAndVerify(source, expectedOutput: expected);
  121. }
  122. [Fact]
  123. [WorkItem(624970, "DevDiv")]
  124. public void AsyncWithEH()
  125. {
  126. var source = @"
  127. using System;
  128. using System.Diagnostics;
  129. using System.Threading;
  130. using System.Threading.Tasks;
  131. class Test
  132. {
  133. static int awaitCount = 0;
  134. static int finallyCount = 0;
  135. static void LogAwait()
  136. {
  137. Interlocked.Increment(ref awaitCount);
  138. }
  139. static void LogException()
  140. {
  141. Interlocked.Increment(ref finallyCount);
  142. }
  143. public static async void F(AutoResetEvent handle)
  144. {
  145. try
  146. {
  147. await Task.Factory.StartNew(LogAwait);
  148. try
  149. {
  150. await Task.Factory.StartNew(LogAwait);
  151. try
  152. {
  153. await Task.Factory.StartNew(LogAwait);
  154. try
  155. {
  156. await Task.Factory.StartNew(LogAwait);
  157. throw new Exception();
  158. }
  159. catch (Exception)
  160. {
  161. }
  162. finally
  163. {
  164. LogException();
  165. }
  166. await Task.Factory.StartNew(LogAwait);
  167. throw new Exception();
  168. }
  169. catch (Exception)
  170. {
  171. }
  172. finally
  173. {
  174. LogException();
  175. }
  176. await Task.Factory.StartNew(LogAwait);
  177. throw new Exception();
  178. }
  179. catch (Exception)
  180. {
  181. }
  182. finally
  183. {
  184. LogException();
  185. }
  186. await Task.Factory.StartNew(LogAwait);
  187. }
  188. finally
  189. {
  190. handle.Set();
  191. }
  192. }
  193. public static void Main2(int i)
  194. {
  195. try
  196. {
  197. awaitCount = 0;
  198. finallyCount = 0;
  199. var handle = new AutoResetEvent(false);
  200. F(handle);
  201. var completed = handle.WaitOne(1000 * 60);
  202. if (completed)
  203. {
  204. if (awaitCount != 7 || finallyCount != 3)
  205. {
  206. throw new Exception(""failed at i="" + i);
  207. }
  208. }
  209. else
  210. {
  211. Console.WriteLine(""Test did not complete in time."");
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. Console.WriteLine(""unexpected exception thrown:"");
  217. Console.WriteLine(ex.ToString());
  218. }
  219. }
  220. public static void Main()
  221. {
  222. for (int i = 0; i < 1500; i++)
  223. {
  224. Main2(i);
  225. }
  226. }
  227. }";
  228. var expected = @"";
  229. CompileAndVerify(source, expectedOutput: expected);
  230. }
  231. [Fact, WorkItem(855080, "DevDiv")]
  232. public void GenericCatchVariableInAsyncMethod()
  233. {
  234. var source = @"
  235. using System;
  236. using System.Collections.Generic;
  237. using System.Linq;
  238. using System.Text;
  239. using System.Threading.Tasks;
  240. namespace ConsoleApplication1
  241. {
  242. class Program
  243. {
  244. static void Main(string[] args)
  245. {
  246. Console.WriteLine(Bar().Result);
  247. }
  248. static async Task<int> Bar()
  249. {
  250. NotImplementedException ex = await Foo<NotImplementedException>();
  251. return 3;
  252. }
  253. public static async Task<T> Foo<T>() where T : Exception
  254. {
  255. Task<int> task = null;
  256. if (task != null) await task;
  257. T result = null;
  258. try
  259. {
  260. }
  261. catch (T ex)
  262. {
  263. result = ex;
  264. }
  265. return result;
  266. }
  267. }
  268. }
  269. ";
  270. CompileAndVerify(source, expectedOutput: "3");
  271. }
  272. [Fact]
  273. public void TaskReturningAsync()
  274. {
  275. var source = @"
  276. using System;
  277. using System.Diagnostics;
  278. using System.Threading.Tasks;
  279. class Test
  280. {
  281. static int i = 0;
  282. public static async Task F()
  283. {
  284. await Task.Factory.StartNew(() =>
  285. {
  286. Test.i = 42;
  287. });
  288. }
  289. public static void Main()
  290. {
  291. Task t = F();
  292. t.Wait(1000 * 60);
  293. Console.WriteLine(Test.i);
  294. }
  295. }";
  296. var expected = @"
  297. 42
  298. ";
  299. CompileAndVerify(source, expectedOutput: expected);
  300. }
  301. [Fact]
  302. public void GenericTaskReturningAsync()
  303. {
  304. var source = @"
  305. using System;
  306. using System.Diagnostics;
  307. using System.Threading.Tasks;
  308. class Test
  309. {
  310. public static async Task<string> F()
  311. {
  312. return await Task.Factory.StartNew(() => { return ""O brave new world...""; });
  313. }
  314. public static void Main()
  315. {
  316. Task<string> t = F();
  317. t.Wait(1000 * 60);
  318. Console.WriteLine(t.Result);
  319. }
  320. }";
  321. var expected = @"
  322. O brave new world...
  323. ";
  324. CompileAndVerify(source, expectedOutput: expected);
  325. }
  326. [Fact]
  327. public void AsyncWithLocals()
  328. {
  329. var source = @"
  330. using System;
  331. using System.Threading.Tasks;
  332. class Test
  333. {
  334. public static async Task<int> F(int x)
  335. {
  336. return await Task.Factory.StartNew(() => { return x; });
  337. }
  338. public static async Task<int> G(int x)
  339. {
  340. int c = 0;
  341. await F(x);
  342. c += x;
  343. await F(x);
  344. c += x;
  345. return c;
  346. }
  347. public static void Main()
  348. {
  349. Task<int> t = G(21);
  350. t.Wait(1000 * 60);
  351. Console.WriteLine(t.Result);
  352. }
  353. }";
  354. var expected = @"
  355. 42
  356. ";
  357. CompileAndVerify(source, expectedOutput: expected);
  358. }
  359. [Fact]
  360. public void AsyncWithTernary()
  361. {
  362. var source = @"
  363. using System;
  364. using System.Threading.Tasks;
  365. class Test
  366. {
  367. public static Task<T> F<T>(T x)
  368. {
  369. Console.WriteLine(""F("" + x + "")"");
  370. return Task.Factory.StartNew(() => { return x; });
  371. }
  372. public static async Task<int> G(bool b1, bool b2)
  373. {
  374. int c = 0;
  375. c = c + (b1 ? 1 : await F(2));
  376. c = c + (b2 ? await F(4) : 8);
  377. return await F(c);
  378. }
  379. public static int H(bool b1, bool b2)
  380. {
  381. Task<int> t = G(b1, b2);
  382. t.Wait(1000 * 60);
  383. return t.Result;
  384. }
  385. public static void Main()
  386. {
  387. Console.WriteLine(H(false, false));
  388. Console.WriteLine(H(false, true));
  389. Console.WriteLine(H(true, false));
  390. Console.WriteLine(H(true, true));
  391. }
  392. }";
  393. var expected = @"
  394. F(2)
  395. F(10)
  396. 10
  397. F(2)
  398. F(4)
  399. F(6)
  400. 6
  401. F(9)
  402. 9
  403. F(4)
  404. F(5)
  405. 5
  406. ";
  407. CompileAndVerify(source, expectedOutput: expected);
  408. }
  409. [Fact]
  410. public void AsyncWithAnd()
  411. {
  412. var source = @"
  413. using System;
  414. using System.Threading.Tasks;
  415. class Test
  416. {
  417. public static Task<T> F<T>(T x)
  418. {
  419. Console.WriteLine(""F("" + x + "")"");
  420. return Task.Factory.StartNew(() => { return x; });
  421. }
  422. public static async Task<int> G(bool b1, bool b2)
  423. {
  424. bool x1 = b1 && await F(true);
  425. bool x2 = b1 && await F(false);
  426. bool x3 = b2 && await F(true);
  427. bool x4 = b2 && await F(false);
  428. int c = 0;
  429. if (x1) c += 1;
  430. if (x2) c += 2;
  431. if (x3) c += 4;
  432. if (x4) c += 8;
  433. return await F(c);
  434. }
  435. public static int H(bool b1, bool b2)
  436. {
  437. Task<int> t = G(b1, b2);
  438. t.Wait(1000 * 60);
  439. return t.Result;
  440. }
  441. public static void Main()
  442. {
  443. Console.WriteLine(H(false, true));
  444. }
  445. }";
  446. var expected = @"
  447. F(True)
  448. F(False)
  449. F(4)
  450. 4
  451. ";
  452. CompileAndVerify(source, expectedOutput: expected);
  453. }
  454. [Fact]
  455. public void AsyncWithOr()
  456. {
  457. var source = @"
  458. using System;
  459. using System.Threading.Tasks;
  460. class Test
  461. {
  462. public static Task<T> F<T>(T x)
  463. {
  464. Console.WriteLine(""F("" + x + "")"");
  465. return Task.Factory.StartNew(() => { return x; });
  466. }
  467. public static async Task<int> G(bool b1, bool b2)
  468. {
  469. bool x1 = b1 || await F(true);
  470. bool x2 = b1 || await F(false);
  471. bool x3 = b2 || await F(true);
  472. bool x4 = b2 || await F(false);
  473. int c = 0;
  474. if (x1) c += 1;
  475. if (x2) c += 2;
  476. if (x3) c += 4;
  477. if (x4) c += 8;
  478. return await F(c);
  479. }
  480. public static int H(bool b1, bool b2)
  481. {
  482. Task<int> t = G(b1, b2);
  483. t.Wait(1000 * 60);
  484. return t.Result;
  485. }
  486. public static void Main()
  487. {
  488. Console.WriteLine(H(false, true));
  489. }
  490. }";
  491. var expected = @"
  492. F(True)
  493. F(False)
  494. F(13)
  495. 13
  496. ";
  497. CompileAndVerify(source, expectedOutput: expected);
  498. }
  499. [Fact]
  500. public void AsyncWithCoalesce()
  501. {
  502. var source = @"
  503. using System;
  504. using System.Threading.Tasks;
  505. class Test
  506. {
  507. public static Task<string> F(string x)
  508. {
  509. Console.WriteLine(""F("" + (x ?? ""null"") + "")"");
  510. return Task.Factory.StartNew(() => { return x; });
  511. }
  512. public static async Task<string> G(string s1, string s2)
  513. {
  514. var result = await F(s1) ?? await F(s2);
  515. Console.WriteLine("" "" + (result ?? ""null""));
  516. return result;
  517. }
  518. public static string H(string s1, string s2)
  519. {
  520. Task<string> t = G(s1, s2);
  521. t.Wait(1000 * 60);
  522. return t.Result;
  523. }
  524. public static void Main()
  525. {
  526. H(null, null);
  527. H(null, ""a"");
  528. H(""b"", null);
  529. H(""c"", ""d"");
  530. }
  531. }";
  532. var expected = @"
  533. F(null)
  534. F(null)
  535. null
  536. F(null)
  537. F(a)
  538. a
  539. F(b)
  540. b
  541. F(c)
  542. c
  543. ";
  544. CompileAndVerify(source, expectedOutput: expected);
  545. }
  546. [Fact]
  547. public void AsyncWithParam()
  548. {
  549. var source = @"
  550. using System;
  551. using System.Threading.Tasks;
  552. class Test
  553. {
  554. public static async Task<int> G(int x)
  555. {
  556. await Task.Factory.StartNew(() => { return x; });
  557. x += 21;
  558. await Task.Factory.StartNew(() => { return x; });
  559. x += 21;
  560. return x;
  561. }
  562. public static void Main()
  563. {
  564. Task<int> t = G(0);
  565. t.Wait(1000 * 60);
  566. Console.WriteLine(t.Result);
  567. }
  568. }";
  569. var expected = @"
  570. 42
  571. ";
  572. CompileAndVerify(source, expectedOutput: expected);
  573. }
  574. [Fact]
  575. public void AwaitInExpr()
  576. {
  577. var source = @"
  578. using System;
  579. using System.Threading.Tasks;
  580. class Test
  581. {
  582. public static async Task<int> F()
  583. {
  584. return await Task.Factory.StartNew(() => 21);
  585. }
  586. public static async Task<int> G()
  587. {
  588. int c = 0;
  589. c = (await F()) + 21;
  590. return c;
  591. }
  592. public static void Main()
  593. {
  594. Task<int> t = G();
  595. t.Wait(1000 * 60);
  596. Console.WriteLine(t.Result);
  597. }
  598. }";
  599. var expected = @"
  600. 42
  601. ";
  602. CompileAndVerify(source, expectedOutput: expected);
  603. }
  604. [Fact]
  605. public void AsyncWithParamsAndLocals_UnHoisted()
  606. {
  607. var source = @"
  608. using System;
  609. using System.Threading.Tasks;
  610. class Test
  611. {
  612. public static async Task<int> F(int x)
  613. {
  614. return await Task.Factory.StartNew(() => { return x; });
  615. }
  616. public static async Task<int> G(int x)
  617. {
  618. int c = 0;
  619. c = await F(x);
  620. return c;
  621. }
  622. public static void Main()
  623. {
  624. Task<int> t = G(21);
  625. t.Wait(1000 * 60);
  626. Console.WriteLine(t.Result);
  627. }
  628. }";
  629. var expected = @"
  630. 21
  631. ";
  632. CompileAndVerify(source, expectedOutput: expected);
  633. }
  634. [Fact]
  635. public void AsyncWithParamsAndLocals_Hoisted()
  636. {
  637. var source = @"
  638. using System;
  639. using System.Threading.Tasks;
  640. class Test
  641. {
  642. public static async Task<int> F(int x)
  643. {
  644. return await Task.Factory.StartNew(() => { return x; });
  645. }
  646. public static async Task<int> G(int x)
  647. {
  648. int c = 0;
  649. c = await F(x);
  650. return c;
  651. }
  652. public static void Main()
  653. {
  654. Task<int> t = G(21);
  655. t.Wait(1000 * 60);
  656. Console.WriteLine(t.Result);
  657. }
  658. }";
  659. var expected = @"
  660. 21
  661. ";
  662. CompileAndVerify(source, expectedOutput: expected);
  663. }
  664. [Fact]
  665. public void AsyncWithParamsAndLocals_DoubleAwait_Spilling()
  666. {
  667. var source = @"
  668. using System;
  669. using System.Threading.Tasks;
  670. class Test
  671. {
  672. public static async Task<int> F(int x)
  673. {
  674. return await Task.Factory.StartNew(() => { return x; });
  675. }
  676. public static async Task<int> G(int x)
  677. {
  678. int c = 0;
  679. c = (await F(x)) + c;
  680. c = (await F(x)) + c;
  681. return c;
  682. }
  683. public static void Main()
  684. {
  685. Task<int> t = G(21);
  686. t.Wait(1000 * 60);
  687. Console.WriteLine(t.Result);
  688. }
  689. }";
  690. var expected = @"
  691. 42
  692. ";
  693. // When the local 'c' gets hoisted, the statement:
  694. // c = (await F(x)) + c;
  695. // Gets rewritten to:
  696. // this.c_field = (await F(x)) + this.c_field;
  697. //
  698. // The code-gen for the assignment is something like this:
  699. // ldarg0 // load the 'this' reference to the stack
  700. // <emitted await expression>
  701. // stfld
  702. //
  703. // What we really want is to evaluate any parts of the lvalue that have side-effects (which is this case is
  704. // nothing), and then defer loading the address for the field reference until after the await expression:
  705. // <emitted await expression>
  706. // <store to tmp>
  707. // ldarg0
  708. // <load tmp>
  709. // stfld
  710. //
  711. // So this case actually requires stack spilling, which is not yet implemented. This has the unfortunate
  712. // consequence of preventing await expressions from being assigned to hoisted locals.
  713. //
  714. CompileAndVerify(source, expectedOutput: expected);
  715. }
  716. [Fact]
  717. public void AsyncWithDynamic()
  718. {
  719. var source = @"
  720. using System;
  721. using System.Threading.Tasks;
  722. class Test
  723. {
  724. public static async Task<int> F(dynamic t)
  725. {
  726. return await t;
  727. }
  728. public static void Main()
  729. {
  730. Task<int> t = F(Task.Factory.StartNew(() => { return 42; }));
  731. t.Wait(1000 * 60);
  732. Console.WriteLine(t.Result);
  733. }
  734. }";
  735. var expected = @"
  736. 42
  737. ";
  738. CompileAndVerify(source, expectedOutput: expected, references: new[] { CSharpRef });
  739. }
  740. [Fact]
  741. public void AsyncWithThisRef()
  742. {
  743. var source = @"
  744. using System;
  745. using System.Threading.Tasks;
  746. class C
  747. {
  748. int x = 42;
  749. public async Task<int> F()
  750. {
  751. int c = this.x;
  752. return await Task.Factory.StartNew(() => c);
  753. }
  754. }
  755. class Test
  756. {
  757. public static void Main()
  758. {
  759. Task<int> t = new C().F();
  760. t.Wait(1000 * 60);
  761. Console.WriteLine(t.Result);
  762. }
  763. }";
  764. var expected = @"
  765. 42
  766. ";
  767. CompileAndVerify(source, expectedOutput: expected);
  768. }
  769. [Fact]
  770. public void AsyncWithBaseRef()
  771. {
  772. var source = @"
  773. using System;
  774. using System.Threading.Tasks;
  775. class B
  776. {
  777. protected int x = 42;
  778. }
  779. class C : B
  780. {
  781. public async Task<int> F()
  782. {
  783. int c = base.x;
  784. return await Task.Factory.StartNew(() => c);
  785. }
  786. }
  787. class Test
  788. {
  789. public static void Main()
  790. {
  791. Task<int> t = new C().F();
  792. t.Wait(1000 * 60);
  793. Console.WriteLine(t.Result);
  794. }
  795. }";
  796. var expected = @"
  797. 42
  798. ";
  799. CompileAndVerify(source, expectedOutput: expected);
  800. }
  801. [Fact]
  802. public void AsyncWithException1()
  803. {
  804. var source = @"
  805. using System;
  806. using System.Threading.Tasks;
  807. class Test
  808. {
  809. static async Task<int> F()
  810. {
  811. throw new Exception();
  812. }
  813. static async Task<int> G()
  814. {
  815. try
  816. {
  817. return await F();
  818. }
  819. catch(Exception)
  820. {
  821. return -1;
  822. }
  823. }
  824. public static void Main()
  825. {
  826. Task<int> t2 = G();
  827. t2.Wait(1000 * 60);
  828. Console.WriteLine(t2.Result);
  829. }
  830. }";
  831. var expected = @"
  832. -1
  833. ";
  834. CompileAndVerify(source, expectedOutput: expected);
  835. }
  836. [Fact]
  837. public void AsyncWithException2()
  838. {
  839. var source = @"
  840. using System;
  841. using System.Threading.Tasks;
  842. class Test
  843. {
  844. static async Task<int> F()
  845. {
  846. throw new Exception();
  847. }
  848. static async Task<int> H()
  849. {
  850. return await F();
  851. }
  852. public static void Main()
  853. {
  854. Task<int> t1 = H();
  855. try
  856. {
  857. t1.Wait(1000 * 60);
  858. }
  859. catch (AggregateException)
  860. {
  861. Console.WriteLine(""exception"");
  862. }
  863. }
  864. }";
  865. var expected = @"
  866. exception
  867. ";
  868. CompileAndVerify(source, expectedOutput: expected);
  869. }
  870. [Fact]
  871. public void AsyncInFinally001()
  872. {
  873. var source = @"
  874. using System;
  875. using System.Threading.Tasks;
  876. class Test
  877. {
  878. static async Task<int> F()
  879. {
  880. return 2;
  881. }
  882. static async Task<int> G()
  883. {
  884. int x = 42;
  885. try
  886. {
  887. }
  888. finally
  889. {
  890. x = await F();
  891. }
  892. return x;
  893. }
  894. public static void Main()
  895. {
  896. Task<int> t2 = G();
  897. t2.Wait(1000 * 60);
  898. Console.WriteLine(t2.Result);
  899. }
  900. }";
  901. var expected = @"
  902. 2
  903. ";
  904. CompileAndVerify(source, expectedOutput: expected).
  905. VerifyIL("Test.<G>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext()", @"
  906. {
  907. // Code size 229 (0xe5)
  908. .maxstack 3
  909. .locals init (int V_0,
  910. int V_1,
  911. object V_2,
  912. System.Runtime.CompilerServices.TaskAwaiter<int> V_3,
  913. object V_4,
  914. System.Exception V_5)
  915. IL_0000: ldarg.0
  916. IL_0001: ldfld ""int Test.<G>d__1.<>1__state""
  917. IL_0006: stloc.0
  918. .try
  919. {
  920. IL_0007: ldloc.0
  921. IL_0008: brfalse.s IL_000e
  922. IL_000a: ldloc.0
  923. IL_000b: ldc.i4.1
  924. IL_000c: beq.s IL_005f
  925. IL_000e: ldarg.0
  926. IL_000f: ldnull
  927. IL_0010: stfld ""object Test.<G>d__1.<>7__wrap1""
  928. IL_0015: ldarg.0
  929. IL_0016: ldc.i4.0
  930. IL_0017: stfld ""int Test.<G>d__1.<>7__wrap2""
  931. .try
  932. {
  933. IL_001c: leave.s IL_0028
  934. }
  935. catch object
  936. {
  937. IL_001e: stloc.2
  938. IL_001f: ldarg.0
  939. IL_0020: ldloc.2
  940. IL_0021: stfld ""object Test.<G>d__1.<>7__wrap1""
  941. IL_0026: leave.s IL_0028
  942. }
  943. IL_0028: call ""System.Threading.Tasks.Task<int> Test.F()""
  944. IL_002d: callvirt ""System.Runtime.CompilerServices.TaskAwaiter<int> System.Threading.Tasks.Task<int>.GetAwaiter()""
  945. IL_0032: stloc.3
  946. IL_0033: ldloca.s V_3
  947. IL_0035: call ""bool System.Runtime.CompilerServices.TaskAwaiter<int>.IsCompleted.get""
  948. IL_003a: brtrue.s IL_007b
  949. IL_003c: ldarg.0
  950. IL_003d: ldc.i4.1
  951. IL_003e: dup
  952. IL_003f: stloc.0
  953. IL_0040: stfld ""int Test.<G>d__1.<>1__state""
  954. IL_0045: ldarg.0
  955. IL_0046: ldloc.3
  956. IL_0047: stfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  957. IL_004c: ldarg.0
  958. IL_004d: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  959. IL_0052: ldloca.s V_3
  960. IL_0054: ldarg.0
  961. IL_0055: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Test.<G>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Test.<G>d__1)""
  962. IL_005a: leave IL_00e4
  963. IL_005f: ldarg.0
  964. IL_0060: ldfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  965. IL_0065: stloc.3
  966. IL_0066: ldarg.0
  967. IL_0067: ldflda ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  968. IL_006c: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  969. IL_0072: ldarg.0
  970. IL_0073: ldc.i4.m1
  971. IL_0074: dup
  972. IL_0075: stloc.0
  973. IL_0076: stfld ""int Test.<G>d__1.<>1__state""
  974. IL_007b: ldloca.s V_3
  975. IL_007d: call ""int System.Runtime.CompilerServices.TaskAwaiter<int>.GetResult()""
  976. IL_0082: ldloca.s V_3
  977. IL_0084: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  978. IL_008a: ldarg.0
  979. IL_008b: ldfld ""object Test.<G>d__1.<>7__wrap1""
  980. IL_0090: stloc.s V_4
  981. IL_0092: ldloc.s V_4
  982. IL_0094: brfalse.s IL_00ad
  983. IL_0096: ldloc.s V_4
  984. IL_0098: isinst ""System.Exception""
  985. IL_009d: dup
  986. IL_009e: brtrue.s IL_00a3
  987. IL_00a0: ldloc.s V_4
  988. IL_00a2: throw
  989. IL_00a3: call ""System.Runtime.ExceptionServices.ExceptionDispatchInfo System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(System.Exception)""
  990. IL_00a8: callvirt ""void System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()""
  991. IL_00ad: ldarg.0
  992. IL_00ae: ldnull
  993. IL_00af: stfld ""object Test.<G>d__1.<>7__wrap1""
  994. IL_00b4: stloc.1
  995. IL_00b5: leave.s IL_00d0
  996. }
  997. catch System.Exception
  998. {
  999. IL_00b7: stloc.s V_5
  1000. IL_00b9: ldarg.0
  1001. IL_00ba: ldc.i4.s -2
  1002. IL_00bc: stfld ""int Test.<G>d__1.<>1__state""
  1003. IL_00c1: ldarg.0
  1004. IL_00c2: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  1005. IL_00c7: ldloc.s V_5
  1006. IL_00c9: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetException(System.Exception)""
  1007. IL_00ce: leave.s IL_00e4
  1008. }
  1009. IL_00d0: ldarg.0
  1010. IL_00d1: ldc.i4.s -2
  1011. IL_00d3: stfld ""int Test.<G>d__1.<>1__state""
  1012. IL_00d8: ldarg.0
  1013. IL_00d9: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  1014. IL_00de: ldloc.1
  1015. IL_00df: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetResult(int)""
  1016. IL_00e4: ret
  1017. }
  1018. ");
  1019. }
  1020. [Fact]
  1021. public void AsyncInFinally002()
  1022. {
  1023. var source = @"
  1024. using System;
  1025. using System.Threading.Tasks;
  1026. class Test
  1027. {
  1028. static async Task<int> F()
  1029. {
  1030. System.Console.Write(""F"");
  1031. return 2;
  1032. }
  1033. static async Task G()
  1034. {
  1035. int x = 0;
  1036. try
  1037. {
  1038. throw new Exception(""hello"");
  1039. }
  1040. finally
  1041. {
  1042. x += await F();
  1043. }
  1044. }
  1045. public static void Main()
  1046. {
  1047. Task t2 = G();
  1048. try
  1049. {
  1050. t2.Wait(1000 * 60);
  1051. }
  1052. catch (Exception ex)
  1053. {
  1054. Console.WriteLine(ex.Message);
  1055. }
  1056. }
  1057. }";
  1058. var expected = @"FOne or more errors occurred.
  1059. ";
  1060. CompileAndVerify(source, expectedOutput: expected);
  1061. }
  1062. [Fact]
  1063. public void AsyncInFinally003()
  1064. {
  1065. var source = @"
  1066. using System;
  1067. using System.Threading.Tasks;
  1068. class Test
  1069. {
  1070. static async Task<int> F()
  1071. {
  1072. return 2;
  1073. }
  1074. static async Task<int> G()
  1075. {
  1076. int x = 0;
  1077. try
  1078. {
  1079. x = await F();
  1080. return x;
  1081. }
  1082. finally
  1083. {
  1084. x += await F();
  1085. }
  1086. }
  1087. public static void Main()
  1088. {
  1089. Task<int> t2 = G();
  1090. t2.Wait(1000 * 60);
  1091. Console.WriteLine(t2.Result);
  1092. }
  1093. }";
  1094. var expected = @"
  1095. 2
  1096. ";
  1097. CompileAndVerify(source, expectedOutput: expected);
  1098. }
  1099. [Fact]
  1100. public void AsyncInFinally004()
  1101. {
  1102. var source = @"
  1103. using System;
  1104. using System.Threading.Tasks;
  1105. class Test
  1106. {
  1107. static async Task<int> F()
  1108. {
  1109. return 2;
  1110. }
  1111. static async Task<int> G()
  1112. {
  1113. int x = 0;
  1114. try
  1115. {
  1116. try
  1117. {
  1118. throw new Exception();
  1119. }
  1120. finally
  1121. {
  1122. x += await F();
  1123. }
  1124. }
  1125. catch
  1126. {
  1127. return x;
  1128. }
  1129. }
  1130. public static void Main()
  1131. {
  1132. Task<int> t2 = G();
  1133. t2.Wait(1000 * 60);
  1134. Console.WriteLine(t2.Result);
  1135. }
  1136. }";
  1137. var expected = @"
  1138. 2
  1139. ";
  1140. CompileAndVerify(source, expectedOutput: expected);
  1141. }
  1142. [Fact]
  1143. public void AsyncInFinallyNested001()
  1144. {
  1145. var source = @"
  1146. using System;
  1147. using System.Threading.Tasks;
  1148. class Test
  1149. {
  1150. static async Task<int> F(int a)
  1151. {
  1152. await Task.Yield();
  1153. return a;
  1154. }
  1155. static async Task<int> G()
  1156. {
  1157. int x = 0;
  1158. try
  1159. {
  1160. try
  1161. {
  1162. x = await F(1);
  1163. goto L1;
  1164. System.Console.WriteLine(""FAIL"");
  1165. }
  1166. finally
  1167. {
  1168. x += await F(2);
  1169. }
  1170. }
  1171. finally
  1172. {
  1173. try
  1174. {
  1175. x += await F(4);
  1176. }
  1177. finally
  1178. {
  1179. x += await F(8);
  1180. }
  1181. }
  1182. System.Console.WriteLine(""FAIL"");
  1183. L1:
  1184. return x;
  1185. }
  1186. public static void Main()
  1187. {
  1188. Task<int> t2 = G();
  1189. t2.Wait(1000 * 60);
  1190. Console.WriteLine(t2.Result);
  1191. }
  1192. }";
  1193. var expected = @"15";
  1194. CompileAndVerify(source, expectedOutput: expected);
  1195. }
  1196. [Fact]
  1197. public void AsyncInFinallyNested002()
  1198. {
  1199. var source = @"
  1200. using System;
  1201. using System.Threading.Tasks;
  1202. class Test
  1203. {
  1204. static async Task<int> F(int a)
  1205. {
  1206. await Task.Yield();
  1207. return a;
  1208. }
  1209. static async Task<int> G()
  1210. {
  1211. int x = 0;
  1212. try
  1213. {
  1214. try
  1215. {
  1216. try
  1217. {
  1218. x = await F(1);
  1219. throw new Exception(""hello"");
  1220. System.Console.WriteLine(""FAIL"");
  1221. }
  1222. finally
  1223. {
  1224. x += await F(2);
  1225. }
  1226. System.Console.WriteLine(""FAIL"");
  1227. }
  1228. finally
  1229. {
  1230. try
  1231. {
  1232. x += await F(4);
  1233. }
  1234. finally
  1235. {
  1236. x += await F(8);
  1237. }
  1238. }
  1239. System.Console.WriteLine(""FAIL"");
  1240. }
  1241. catch(Exception ex)
  1242. {
  1243. System.Console.WriteLine(ex.Message);
  1244. }
  1245. return x;
  1246. }
  1247. public static void Main()
  1248. {
  1249. Task<int> t2 = G();
  1250. t2.Wait(1000 * 60);
  1251. Console.WriteLine(t2.Result);
  1252. }
  1253. }";
  1254. var expected = @"hello
  1255. 15";
  1256. CompileAndVerify(source, expectedOutput: expected);
  1257. }
  1258. [Fact]
  1259. public void AsyncInFinallyNested003()
  1260. {
  1261. var source = @"
  1262. using System;
  1263. using System.Threading.Tasks;
  1264. class Test
  1265. {
  1266. static async Task<int> F(int a)
  1267. {
  1268. await Task.Yield();
  1269. return a;
  1270. }
  1271. static async Task<int> G()
  1272. {
  1273. int x = 0;
  1274. try
  1275. {
  1276. try
  1277. {
  1278. try
  1279. {
  1280. x = await F(1);
  1281. throw new Exception(""hello"");
  1282. System.Console.WriteLine(""FAIL"");
  1283. }
  1284. finally
  1285. {
  1286. x += await F(2);
  1287. }
  1288. System.Console.WriteLine(""FAIL"");
  1289. }
  1290. finally
  1291. {
  1292. try
  1293. {
  1294. x += await F(4);
  1295. }
  1296. finally
  1297. {
  1298. x += await F(8);
  1299. throw new Exception(""bye"");
  1300. }
  1301. }
  1302. System.Console.WriteLine(""FAIL"");
  1303. }
  1304. catch(Exception ex)
  1305. {
  1306. System.Console.WriteLine(ex.Message);
  1307. }
  1308. return x;
  1309. }
  1310. public static void Main()
  1311. {
  1312. Task<int> t2 = G();
  1313. t2.Wait(1000 * 60);
  1314. Console.WriteLine(t2.Result);
  1315. }
  1316. }";
  1317. var expected = @"bye
  1318. 15";
  1319. CompileAndVerify(source, expectedOutput: expected);
  1320. }
  1321. [Fact]
  1322. public void AsyncInCatch001()
  1323. {
  1324. var source = @"
  1325. using System;
  1326. using System.Threading.Tasks;
  1327. class Test
  1328. {
  1329. static async Task<int> F()
  1330. {
  1331. return 2;
  1332. }
  1333. static async Task<int> G()
  1334. {
  1335. int x = 0;
  1336. try
  1337. {
  1338. x = x / x;
  1339. }
  1340. catch
  1341. {
  1342. x = await F();
  1343. }
  1344. return x;
  1345. }
  1346. public static void Main()
  1347. {
  1348. Task<int> t2 = G();
  1349. t2.Wait(1000 * 60);
  1350. Console.WriteLine(t2.Result);
  1351. }
  1352. }";
  1353. var expected = @"
  1354. 2
  1355. ";
  1356. CompileAndVerify(source, expectedOutput: expected).
  1357. VerifyIL("Test.<G>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext()", @"
  1358. {
  1359. // Code size 182 (0xb6)
  1360. .maxstack 3
  1361. .locals init (int V_0,
  1362. int V_1,
  1363. int V_2, //x
  1364. int V_3,
  1365. System.Runtime.CompilerServices.TaskAwaiter<int> V_4,
  1366. System.Exception V_5)
  1367. IL_0000: ldarg.0
  1368. IL_0001: ldfld ""int Test.<G>d__1.<>1__state""
  1369. IL_0006: stloc.0
  1370. .try
  1371. {
  1372. IL_0007: ldloc.0
  1373. IL_0008: brfalse.s IL_000e
  1374. IL_000a: ldloc.0
  1375. IL_000b: ldc.i4.1
  1376. IL_000c: beq.s IL_0057
  1377. IL_000e: ldc.i4.0
  1378. IL_000f: stloc.2
  1379. IL_0010: ldc.i4.0
  1380. IL_0011: stloc.3
  1381. .try
  1382. {
  1383. IL_0012: ldloc.2
  1384. IL_0013: dup
  1385. IL_0014: div
  1386. IL_0015: stloc.2
  1387. IL_0016: leave.s IL_001d
  1388. }
  1389. catch object
  1390. {
  1391. IL_0018: pop
  1392. IL_0019: ldc.i4.1
  1393. IL_001a: stloc.3
  1394. IL_001b: leave.s IL_001d
  1395. }
  1396. IL_001d: ldloc.3
  1397. IL_001e: ldc.i4.1
  1398. IL_001f: bne.un.s IL_0084
  1399. IL_0021: call ""System.Threading.Tasks.Task<int> Test.F()""
  1400. IL_0026: callvirt ""System.Runtime.CompilerServices.TaskAwaiter<int> System.Threading.Tasks.Task<int>.GetAwaiter()""
  1401. IL_002b: stloc.s V_4
  1402. IL_002d: ldloca.s V_4
  1403. IL_002f: call ""bool System.Runtime.CompilerServices.TaskAwaiter<int>.IsCompleted.get""
  1404. IL_0034: brtrue.s IL_0074
  1405. IL_0036: ldarg.0
  1406. IL_0037: ldc.i4.1
  1407. IL_0038: dup
  1408. IL_0039: stloc.0
  1409. IL_003a: stfld ""int Test.<G>d__1.<>1__state""
  1410. IL_003f: ldarg.0
  1411. IL_0040: ldloc.s V_4
  1412. IL_0042: stfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  1413. IL_0047: ldarg.0
  1414. IL_0048: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  1415. IL_004d: ldloca.s V_4
  1416. IL_004f: ldarg.0
  1417. IL_0050: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Test.<G>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Test.<G>d__1)""
  1418. IL_0055: leave.s IL_00b5
  1419. IL_0057: ldarg.0
  1420. IL_0058: ldfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  1421. IL_005d: stloc.s V_4
  1422. IL_005f: ldarg.0
  1423. IL_0060: ldflda ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<G>d__1.<>u__$awaiter0""
  1424. IL_0065: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  1425. IL_006b: ldarg.0
  1426. IL_006c: ldc.i4.m1
  1427. IL_006d: dup
  1428. IL_006e: stloc.0
  1429. IL_006f: stfld ""int Test.<G>d__1.<>1__state""
  1430. IL_0074: ldloca.s V_4
  1431. IL_0076: call ""int System.Runtime.CompilerServices.TaskAwaiter<int>.GetResult()""
  1432. IL_007b: ldloca.s V_4
  1433. IL_007d: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  1434. IL_0083: stloc.2
  1435. IL_0084: ldloc.2
  1436. IL_0085: stloc.1
  1437. IL_0086: leave.s IL_00a1
  1438. }
  1439. catch System.Exception
  1440. {
  1441. IL_0088: stloc.s V_5
  1442. IL_008a: ldarg.0
  1443. IL_008b: ldc.i4.s -2
  1444. IL_008d: stfld ""int Test.<G>d__1.<>1__state""
  1445. IL_0092: ldarg.0
  1446. IL_0093: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  1447. IL_0098: ldloc.s V_5
  1448. IL_009a: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetException(System.Exception)""
  1449. IL_009f: leave.s IL_00b5
  1450. }
  1451. IL_00a1: ldarg.0
  1452. IL_00a2: ldc.i4.s -2
  1453. IL_00a4: stfld ""int Test.<G>d__1.<>1__state""
  1454. IL_00a9: ldarg.0
  1455. IL_00aa: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<G>d__1.<>t__builder""
  1456. IL_00af: ldloc.1
  1457. IL_00b0: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetResult(int)""
  1458. IL_00b5: ret
  1459. }
  1460. ");
  1461. }
  1462. [Fact]
  1463. public void AsyncInCatchRethrow()
  1464. {
  1465. var source = @"
  1466. using System;
  1467. using System.Threading.Tasks;
  1468. class Test
  1469. {
  1470. static async Task<int> F()
  1471. {
  1472. await Task.Yield();
  1473. return 2;
  1474. }
  1475. static async Task<int> G()
  1476. {
  1477. int x = 0;
  1478. try
  1479. {
  1480. try
  1481. {
  1482. x = x / x;
  1483. }
  1484. catch
  1485. {
  1486. x = await F();
  1487. throw;
  1488. }
  1489. }
  1490. catch(DivideByZeroException ex)
  1491. {
  1492. x = await F();
  1493. System.Console.WriteLine(ex.Message);
  1494. }
  1495. return x;
  1496. }
  1497. public static void Main()
  1498. {
  1499. Task<int> t2 = G();
  1500. t2.Wait(1000 * 60);
  1501. Console.WriteLine(t2.Result);
  1502. }
  1503. }";
  1504. var expected = @"
  1505. Attempted to divide by zero.
  1506. 2
  1507. ";
  1508. CompileAndVerify(source, expectedOutput: expected);
  1509. }
  1510. [Fact]
  1511. public void AsyncInCatchFilter()
  1512. {
  1513. var source = @"
  1514. using System;
  1515. using System.Threading.Tasks;
  1516. class Test
  1517. {
  1518. static async Task<int> F()
  1519. {
  1520. await Task.Yield();
  1521. return 2;
  1522. }
  1523. static async Task<int> G()
  1524. {
  1525. int x = 0;
  1526. try
  1527. {
  1528. try
  1529. {
  1530. x = x / x;
  1531. }
  1532. catch if(x != 0)
  1533. {
  1534. x = await F();
  1535. throw;
  1536. }
  1537. }
  1538. catch(Exception ex) if(x == 0 && ((ex = new Exception(""hello"")) != null))
  1539. {
  1540. x = await F();
  1541. System.Console.WriteLine(ex.Message);
  1542. }
  1543. return x;
  1544. }
  1545. public static void Main()
  1546. {
  1547. Task<int> t2 = G();
  1548. t2.Wait(1000 * 60);
  1549. Console.WriteLine(t2.Result);
  1550. }
  1551. }";
  1552. var expected = @"
  1553. hello
  1554. 2
  1555. ";
  1556. CompileAndVerify(source, expectedOutput: expected);
  1557. }
  1558. [Fact]
  1559. public void AsyncInCatchFilterLifted()
  1560. {
  1561. var source = @"
  1562. using System;
  1563. using System.Threading.Tasks;
  1564. class Test
  1565. {
  1566. static async Task<int> F()
  1567. {
  1568. await Task.Yield();
  1569. return 2;
  1570. }
  1571. static bool T(Func<bool> f, ref Exception ex)
  1572. {
  1573. var result = f();
  1574. ex = new Exception(result.ToString());
  1575. return result;
  1576. }
  1577. static async Task<int> G()
  1578. {
  1579. int x = 0;
  1580. try
  1581. {
  1582. x = x / x;
  1583. }
  1584. catch(Exception ex) if(T(()=>ex.Message == null, ref ex))
  1585. {
  1586. x = await F();
  1587. System.Console.WriteLine(ex.Message);
  1588. }
  1589. catch(Exception ex) if(T(()=>ex.Message != null, ref ex))
  1590. {
  1591. x = await F();
  1592. System.Console.WriteLine(ex.Message);
  1593. }
  1594. return x;
  1595. }
  1596. public static void Main()
  1597. {
  1598. Task<int> t2 = G();
  1599. t2.Wait(1000 * 60);
  1600. Console.WriteLine(t2.Result);
  1601. }
  1602. }";
  1603. var expected = @"True
  1604. 2
  1605. ";
  1606. CompileAndVerify(source, expectedOutput: expected);
  1607. }
  1608. [Fact]
  1609. public void AsyncInCatchFinallyMixed()
  1610. {
  1611. var source = @"
  1612. using System;
  1613. using System.Threading.Tasks;
  1614. class Test
  1615. {
  1616. static async Task<int> F(int x)
  1617. {
  1618. await Task.Yield();
  1619. return x;
  1620. }
  1621. static async Task<int> G()
  1622. {
  1623. int x = 0;
  1624. try
  1625. {
  1626. try
  1627. {
  1628. for (int i = 0; i < 5; i++)
  1629. {
  1630. try
  1631. {
  1632. try
  1633. {
  1634. x = x / await F(0);
  1635. }
  1636. catch (DivideByZeroException) if (i < 3)
  1637. {
  1638. await Task.Yield();
  1639. continue;
  1640. }
  1641. catch (DivideByZeroException)
  1642. {
  1643. x = 2 + await F(x);
  1644. throw;
  1645. }
  1646. System.Console.WriteLine(""FAIL"");
  1647. }
  1648. finally
  1649. {
  1650. x = await F(x) + 3;
  1651. if (i >= 3)
  1652. {
  1653. throw new Exception(""hello"");
  1654. }
  1655. }
  1656. }
  1657. }
  1658. finally
  1659. {
  1660. x = 11 + await F(x);
  1661. }
  1662. }
  1663. catch (Exception ex)
  1664. {
  1665. x = await F(x) + 17;
  1666. System.Console.WriteLine(ex.Message);
  1667. }
  1668. return x;
  1669. }
  1670. public static void Main()
  1671. {
  1672. Task<int> t2 = G();
  1673. t2.Wait(1000 * 60);
  1674. Console.WriteLine(t2.Result);
  1675. }
  1676. }";
  1677. var expected = @"
  1678. hello
  1679. 42
  1680. ";
  1681. CompileAndVerify(source, expectedOutput: expected);
  1682. }
  1683. [Fact]
  1684. public void AsyncInCatchFinallyMixed_InAsyncLambda()
  1685. {
  1686. var source = @"
  1687. using System;
  1688. using System.Threading.Tasks;
  1689. class Test
  1690. {
  1691. static async Task<int> F(int x)
  1692. {
  1693. await Task.Yield();
  1694. return x;
  1695. }
  1696. static Func<Task<int>> G()
  1697. {
  1698. int x = 0;
  1699. return async () =>
  1700. {
  1701. try
  1702. {
  1703. try
  1704. {
  1705. for (int i = 0; i < 5; i++)
  1706. {
  1707. try
  1708. {
  1709. try
  1710. {
  1711. x = x / await F(0);
  1712. }
  1713. catch (DivideByZeroException) if (i < 3)
  1714. {
  1715. await Task.Yield();
  1716. continue;
  1717. }
  1718. catch (DivideByZeroException)
  1719. {
  1720. x = 2 + await F(x);
  1721. throw;
  1722. }
  1723. System.Console.WriteLine(""FAIL"");
  1724. }
  1725. finally
  1726. {
  1727. x = await F(x) + 3;
  1728. if (i >= 3)
  1729. {
  1730. throw new Exception(""hello"");
  1731. }
  1732. }
  1733. }
  1734. }
  1735. finally
  1736. {
  1737. x = 11 + await F(x);
  1738. }
  1739. }
  1740. catch (Exception ex)
  1741. {
  1742. x = await F(x) + 17;
  1743. System.Console.WriteLine(ex.Message);
  1744. }
  1745. return x;
  1746. };
  1747. }
  1748. public static void Main()
  1749. {
  1750. Task<int> t2 = G()();
  1751. t2.Wait(1000 * 60);
  1752. Console.WriteLine(t2.Result);
  1753. }
  1754. }";
  1755. var expected = @"
  1756. hello
  1757. 42
  1758. ";
  1759. CompileAndVerify(source, expectedOutput: expected);
  1760. }
  1761. [Fact]
  1762. public void AsyncInConditionalAccess()
  1763. {
  1764. var source = @"
  1765. using System;
  1766. using System.Collections.Generic;
  1767. using System.Threading.Tasks;
  1768. class Test
  1769. {
  1770. class C1
  1771. {
  1772. public int M(int x)
  1773. {
  1774. return x;
  1775. }
  1776. }
  1777. public static int Get(int x)
  1778. {
  1779. Console.WriteLine(""> "" + x);
  1780. return x;
  1781. }
  1782. public static async Task<int> F(int x)
  1783. {
  1784. return await Task.Factory.StartNew(() => x);
  1785. }
  1786. public static async Task<int?> G()
  1787. {
  1788. var c = new C1();
  1789. return c?.M(await F(Get(42)));
  1790. }
  1791. public static void Main()
  1792. {
  1793. var t = G();
  1794. System.Console.WriteLine(t.Result);
  1795. }
  1796. }";
  1797. var expected = @"
  1798. > 42
  1799. 42";
  1800. CompileAndVerifyExperimental(source, expected);
  1801. }
  1802. [Fact]
  1803. public void Conformance_Awaiting_Methods_Generic01()
  1804. {
  1805. var source = @"
  1806. using System;
  1807. using System.Runtime.CompilerServices;
  1808. using System.Threading;
  1809. //Implementation of you own async pattern
  1810. public class MyTask<T>
  1811. {
  1812. public MyTaskAwaiter<T> GetAwaiter()
  1813. {
  1814. return new MyTaskAwaiter<T>();
  1815. }
  1816. public async void Run<U>(U u) where U : MyTask<int>, new()
  1817. {
  1818. try
  1819. {
  1820. int tests = 0;
  1821. tests++;
  1822. var rez = await u;
  1823. if (rez == 0)
  1824. Driver.Count++;
  1825. Driver.Result = Driver.Count - tests;
  1826. }
  1827. finally
  1828. {
  1829. //When test complete, set the flag.
  1830. Driver.CompletedSignal.Set();
  1831. }
  1832. }
  1833. }
  1834. public class MyTaskAwaiter<T> : INotifyCompletion
  1835. {
  1836. public void OnCompleted(Action continuationAction)
  1837. {
  1838. }
  1839. public T GetResult()
  1840. {
  1841. return default(T);
  1842. }
  1843. public bool IsCompleted { get { return true; } }
  1844. }
  1845. //-------------------------------------
  1846. class Driver
  1847. {
  1848. public static int Result = -1;
  1849. public static int Count = 0;
  1850. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  1851. static void Main()
  1852. {
  1853. new MyTask<int>().Run<MyTask<int>>(new MyTask<int>());
  1854. CompletedSignal.WaitOne();
  1855. // 0 - success
  1856. // 1 - failed (test completed)
  1857. // -1 - failed (test incomplete - deadlock, etc)
  1858. Console.WriteLine(Driver.Result);
  1859. }
  1860. }";
  1861. CompileAndVerify(source, "0");
  1862. }
  1863. [Fact]
  1864. public void Conformance_Awaiting_Methods_Method01()
  1865. {
  1866. var source = @"
  1867. using System.Threading;
  1868. using System.Threading.Tasks;
  1869. using System;
  1870. public interface IExplicit
  1871. {
  1872. Task Method(int x = 4);
  1873. }
  1874. class C1 : IExplicit
  1875. {
  1876. Task IExplicit.Method(int x)
  1877. {
  1878. //This will fail until Run and RunEx are merged back together
  1879. return Task.Run(async () =>
  1880. {
  1881. await Task.Delay(1);
  1882. Driver.Count++;
  1883. });
  1884. }
  1885. }
  1886. class TestCase
  1887. {
  1888. public async void Run()
  1889. {
  1890. try
  1891. {
  1892. int tests = 0;
  1893. tests++;
  1894. C1 c = new C1();
  1895. IExplicit e = (IExplicit)c;
  1896. await e.Method();
  1897. Driver.Result = Driver.Count - tests;
  1898. }
  1899. finally
  1900. {
  1901. //When test complete, set the flag.
  1902. Driver.CompletedSignal.Set();
  1903. }
  1904. }
  1905. }
  1906. class Driver
  1907. {
  1908. public static int Result = -1;
  1909. public static int Count = 0;
  1910. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  1911. static void Main()
  1912. {
  1913. var t = new TestCase();
  1914. t.Run();
  1915. CompletedSignal.WaitOne();
  1916. // 0 - success
  1917. // 1 - failed (test completed)
  1918. // -1 - failed (test incomplete - deadlock, etc)
  1919. Console.WriteLine(Driver.Result);
  1920. }
  1921. }";
  1922. CompileAndVerify(source, "0");
  1923. }
  1924. [Fact]
  1925. public void DoFinallyBodies()
  1926. {
  1927. var source = @"
  1928. using System.Threading.Tasks;
  1929. using System;
  1930. class Driver
  1931. {
  1932. public static int finally_count = 0;
  1933. static async Task F()
  1934. {
  1935. try
  1936. {
  1937. await Task.Factory.StartNew(() => { });
  1938. }
  1939. finally
  1940. {
  1941. Driver.finally_count++;
  1942. }
  1943. }
  1944. static void Main()
  1945. {
  1946. var t = F();
  1947. t.Wait();
  1948. Console.WriteLine(Driver.finally_count);
  1949. }
  1950. }";
  1951. var expected = @"
  1952. 1
  1953. ";
  1954. CompileAndVerify(source, expected);
  1955. }
  1956. [Fact]
  1957. public void Conformance_Awaiting_Methods_Parameter003()
  1958. {
  1959. var source = @"
  1960. using System;
  1961. using System.Threading.Tasks;
  1962. using System.Collections.Generic;
  1963. using System.Threading;
  1964. class TestCase
  1965. {
  1966. public static int Count = 0;
  1967. public static T Foo<T>(T t)
  1968. {
  1969. return t;
  1970. }
  1971. public async static Task<T> Bar<T>(T t)
  1972. {
  1973. await Task.Delay(1);
  1974. return t;
  1975. }
  1976. public static async void Run()
  1977. {
  1978. try
  1979. {
  1980. int x1 = Foo(await Bar(4));
  1981. Task<int> t = Bar(5);
  1982. int x2 = Foo(await t);
  1983. if (x1 != 4)
  1984. Count++;
  1985. if (x2 != 5)
  1986. Count++;
  1987. }
  1988. finally
  1989. {
  1990. Driver.CompletedSignal.Set();
  1991. }
  1992. }
  1993. }
  1994. class Driver
  1995. {
  1996. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  1997. static void Main()
  1998. {
  1999. TestCase.Run();
  2000. CompletedSignal.WaitOne();
  2001. // 0 - success
  2002. Console.WriteLine(TestCase.Count);
  2003. }
  2004. }";
  2005. CompileAndVerify(source, expectedOutput: "0");
  2006. }
  2007. [Fact]
  2008. public void Conformance_Awaiting_Methods_Method05()
  2009. {
  2010. var source = @"
  2011. using System.Threading;
  2012. using System.Threading.Tasks;
  2013. using System;
  2014. class C
  2015. {
  2016. public int Status;
  2017. public C(){}
  2018. }
  2019. interface IImplicit
  2020. {
  2021. T Method<T>(params decimal[] d) where T : Task<C>;
  2022. }
  2023. class Impl : IImplicit
  2024. {
  2025. public T Method<T>(params decimal[] d) where T : Task<C>
  2026. {
  2027. //this will fail until Run and RunEx<C> are merged
  2028. return (T) Task.Run(async() =>
  2029. {
  2030. await Task.Delay(1);
  2031. Driver.Count++;
  2032. return new C() { Status = 1 };
  2033. });
  2034. }
  2035. }
  2036. class TestCase
  2037. {
  2038. public async void Run()
  2039. {
  2040. try
  2041. {
  2042. int tests = 0;
  2043. Impl i = new Impl();
  2044. tests++;
  2045. await i.Method<Task<C>>(3m, 4m);
  2046. Driver.Result = Driver.Count - tests;
  2047. }
  2048. finally
  2049. {
  2050. //When test complete, set the flag.
  2051. Driver.CompletedSignal.Set();
  2052. }
  2053. }
  2054. }
  2055. class Driver
  2056. {
  2057. public static int Result = -1;
  2058. public static int Count = 0;
  2059. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2060. static void Main()
  2061. {
  2062. var t = new TestCase();
  2063. t.Run();
  2064. CompletedSignal.WaitOne();
  2065. // 0 - success
  2066. // 1 - failed (test completed)
  2067. // -1 - failed (test incomplete - deadlock, etc)
  2068. Console.WriteLine(Driver.Result);
  2069. }
  2070. }";
  2071. CompileAndVerify(source, "0");
  2072. }
  2073. [Fact]
  2074. public void Conformance_Awaiting_Methods_Accessible010()
  2075. {
  2076. var source = @"
  2077. using System;
  2078. using System.Collections.Generic;
  2079. using System.Threading.Tasks;
  2080. using System.Threading;
  2081. class TestCase:Test
  2082. {
  2083. public static int Count = 0;
  2084. public async static void Run()
  2085. {
  2086. try
  2087. {
  2088. int x = await Test.GetValue<int>(1);
  2089. if (x != 1)
  2090. Count++;
  2091. }
  2092. finally
  2093. {
  2094. Driver.CompletedSignal.Set();
  2095. }
  2096. }
  2097. }
  2098. class Test
  2099. {
  2100. protected async static Task<T> GetValue<T>(T t)
  2101. {
  2102. await Task.Delay(1);
  2103. return t;
  2104. }
  2105. }
  2106. class Driver
  2107. {
  2108. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2109. static void Main()
  2110. {
  2111. TestCase.Run();
  2112. CompletedSignal.WaitOne();
  2113. // 0 - success
  2114. Console.WriteLine(TestCase.Count);
  2115. }
  2116. }";
  2117. CompileAndVerify(source, "0");
  2118. }
  2119. [Fact]
  2120. public void NestedUnary()
  2121. {
  2122. var source = @"
  2123. using System;
  2124. using System.Threading.Tasks;
  2125. class Test
  2126. {
  2127. public static async Task<int> F()
  2128. {
  2129. return 1;
  2130. }
  2131. public static async Task<int> G1()
  2132. {
  2133. return -(await F());
  2134. }
  2135. public static async Task<int> G2()
  2136. {
  2137. return -(-(await F()));
  2138. }
  2139. public static async Task<int> G3()
  2140. {
  2141. return -(-(-(await F())));
  2142. }
  2143. public static void WaitAndPrint(Task<int> t)
  2144. {
  2145. t.Wait();
  2146. Console.WriteLine(t.Result);
  2147. }
  2148. public static void Main()
  2149. {
  2150. WaitAndPrint(G1());
  2151. WaitAndPrint(G2());
  2152. WaitAndPrint(G3());
  2153. }
  2154. }";
  2155. var expected = @"
  2156. -1
  2157. 1
  2158. -1
  2159. ";
  2160. CompileAndVerify(source, expected);
  2161. }
  2162. [Fact]
  2163. public void SpillCall()
  2164. {
  2165. var source = @"
  2166. using System;
  2167. using System.Collections.Generic;
  2168. using System.Threading.Tasks;
  2169. class Test
  2170. {
  2171. public static void Printer(int a, int b, int c, int d, int e)
  2172. {
  2173. foreach (var x in new List<int>() { a, b, c, d, e })
  2174. {
  2175. Console.WriteLine(x);
  2176. }
  2177. }
  2178. public static int Get(int x)
  2179. {
  2180. Console.WriteLine(""> "" + x);
  2181. return x;
  2182. }
  2183. public static async Task<int> F(int x)
  2184. {
  2185. return await Task.Factory.StartNew(() => x);
  2186. }
  2187. public static async Task G()
  2188. {
  2189. Printer(Get(111), Get(222), Get(333), await F(Get(444)), Get(555));
  2190. }
  2191. public static void Main()
  2192. {
  2193. Task t = G();
  2194. t.Wait();
  2195. }
  2196. }";
  2197. var expected = @"
  2198. > 111
  2199. > 222
  2200. > 333
  2201. > 444
  2202. > 555
  2203. 111
  2204. 222
  2205. 333
  2206. 444
  2207. 555
  2208. ";
  2209. CompileAndVerify(source, expected);
  2210. }
  2211. [Fact]
  2212. public void SpillCall2()
  2213. {
  2214. var source = @"
  2215. using System;
  2216. using System.Collections.Generic;
  2217. using System.Threading.Tasks;
  2218. class Test
  2219. {
  2220. public static void Printer(int a, int b, int c, int d, int e)
  2221. {
  2222. foreach (var x in new List<int>() { a, b, c, d, e })
  2223. {
  2224. Console.WriteLine(x);
  2225. }
  2226. }
  2227. public static int Get(int x)
  2228. {
  2229. Console.WriteLine(""> "" + x);
  2230. return x;
  2231. }
  2232. public static async Task<int> F(int x)
  2233. {
  2234. return await Task.Factory.StartNew(() => x);
  2235. }
  2236. public static async Task G()
  2237. {
  2238. Printer(Get(111), await F(Get(222)), Get(333), await F(Get(444)), Get(555));
  2239. }
  2240. public static void Main()
  2241. {
  2242. Task t = G();
  2243. t.Wait();
  2244. }
  2245. }";
  2246. var expected = @"
  2247. > 111
  2248. > 222
  2249. > 333
  2250. > 444
  2251. > 555
  2252. 111
  2253. 222
  2254. 333
  2255. 444
  2256. 555
  2257. ";
  2258. CompileAndVerify(source, expected);
  2259. }
  2260. [Fact]
  2261. public void SpillCall3()
  2262. {
  2263. var source = @"
  2264. using System;
  2265. using System.Collections.Generic;
  2266. using System.Threading.Tasks;
  2267. class Test
  2268. {
  2269. public static void Printer(int a, int b, int c, int d, int e, int f)
  2270. {
  2271. foreach (var x in new List<int>(){a, b, c, d, e, f})
  2272. {
  2273. Console.WriteLine(x);
  2274. }
  2275. }
  2276. public static async Task<int> F(int x)
  2277. {
  2278. return await Task.Factory.StartNew(() => x);
  2279. }
  2280. public static async Task G()
  2281. {
  2282. Printer(1, await F(2), 3, await F(await F(await F(await F(4)))), await F(5), 6);
  2283. }
  2284. public static void Main()
  2285. {
  2286. Task t = G();
  2287. t.Wait();
  2288. }
  2289. }";
  2290. var expected = @"
  2291. 1
  2292. 2
  2293. 3
  2294. 4
  2295. 5
  2296. 6
  2297. ";
  2298. CompileAndVerify(source, expected);
  2299. }
  2300. [Fact]
  2301. public void SpillCall4()
  2302. {
  2303. var source = @"
  2304. using System;
  2305. using System.Collections.Generic;
  2306. using System.Threading.Tasks;
  2307. class Test
  2308. {
  2309. public static void Printer(int a, int b)
  2310. {
  2311. foreach (var x in new List<int>(){a, b})
  2312. {
  2313. Console.WriteLine(x);
  2314. }
  2315. }
  2316. public static async Task<int> F(int x)
  2317. {
  2318. return await Task.Factory.StartNew(() => x);
  2319. }
  2320. public static async Task G()
  2321. {
  2322. Printer(1, await F(await F(2)));
  2323. }
  2324. public static void Main()
  2325. {
  2326. Task t = G();
  2327. t.Wait();
  2328. }
  2329. }";
  2330. var expected = @"
  2331. 1
  2332. 2
  2333. ";
  2334. CompileAndVerify(source, expected);
  2335. }
  2336. [Fact]
  2337. public void Array01()
  2338. {
  2339. var source = @"
  2340. using System;
  2341. using System.Threading;
  2342. using System.Threading.Tasks;
  2343. class TestCase
  2344. {
  2345. public async Task<T> GetVal<T>(T t)
  2346. {
  2347. await Task.Delay(1);
  2348. return t;
  2349. }
  2350. public async void Run<T>(T t) where T : struct
  2351. {
  2352. int tests = 0;
  2353. try
  2354. {
  2355. tests++;
  2356. int[] arr = new int[await GetVal(4)];
  2357. if (arr.Length == 4)
  2358. Driver.Count++;
  2359. //multidimensional
  2360. tests++;
  2361. decimal[,] arr2 = new decimal[await GetVal(4), await GetVal(4)];
  2362. if (arr2.Rank == 2 && arr2.Length == 16)
  2363. Driver.Count++;
  2364. arr2 = new decimal[4, await GetVal(4)];
  2365. if (arr2.Rank == 2 && arr2.Length == 16)
  2366. Driver.Count++;
  2367. tests++;
  2368. arr2 = new decimal[await GetVal(4), 4];
  2369. if (arr2.Rank == 2 && arr2.Length == 16)
  2370. Driver.Count++;
  2371. //jagged array
  2372. tests++;
  2373. decimal?[][] arr3 = new decimal?[await GetVal(4)][];
  2374. if (arr3.Rank == 2 && arr3.Length == 4)
  2375. Driver.Count++;
  2376. }
  2377. finally
  2378. {
  2379. Driver.Result = Driver.Count - tests;
  2380. //When test complete, set the flag.
  2381. Driver.CompletedSignal.Set();
  2382. }
  2383. }
  2384. }
  2385. class Driver
  2386. {
  2387. public static int Result = -1;
  2388. public static int Count = 0;
  2389. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2390. static void Main()
  2391. {
  2392. var t = new TestCase();
  2393. t.Run(6);
  2394. CompletedSignal.WaitOne();
  2395. // 0 - success
  2396. // 1 - failed (test completed)
  2397. // -1 - failed (test incomplete - deadlock, etc)
  2398. Console.WriteLine(Driver.Result);
  2399. }
  2400. }";
  2401. var expected = @"
  2402. 0
  2403. ";
  2404. CompileAndVerify(source, expected);
  2405. }
  2406. [Fact]
  2407. public void Array02()
  2408. {
  2409. var source = @"
  2410. using System;
  2411. using System.Threading;
  2412. using System.Threading.Tasks;
  2413. class TestCase
  2414. {
  2415. public async Task<T> GetVal<T>(T t)
  2416. {
  2417. await Task.Delay(1);
  2418. return t;
  2419. }
  2420. public async void Run<T>(T t) where T : struct
  2421. {
  2422. int tests = 0;
  2423. try
  2424. {
  2425. tests++;
  2426. int[] arr = new int[await GetVal(4)];
  2427. if (arr.Length == 4)
  2428. Driver.Count++;
  2429. tests++;
  2430. arr[0] = await GetVal(4);
  2431. if (arr[0] == 4)
  2432. Driver.Count++;
  2433. tests++;
  2434. arr[0] += await GetVal(4);
  2435. if (arr[0] == 8)
  2436. Driver.Count++;
  2437. tests++;
  2438. arr[1] += await (GetVal(arr[0]));
  2439. if (arr[1] == 8)
  2440. Driver.Count++;
  2441. tests++;
  2442. arr[1] += await (GetVal(arr[await GetVal(0)]));
  2443. if (arr[1] == 16)
  2444. Driver.Count++;
  2445. tests++;
  2446. arr[await GetVal(2)]++;
  2447. if (arr[2] == 1)
  2448. Driver.Count++;
  2449. }
  2450. finally
  2451. {
  2452. Driver.Result = Driver.Count - tests;
  2453. //When test complete, set the flag.
  2454. Driver.CompletedSignal.Set();
  2455. }
  2456. }
  2457. }
  2458. class Driver
  2459. {
  2460. public static int Result = -1;
  2461. public static int Count = 0;
  2462. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2463. static void Main()
  2464. {
  2465. var t = new TestCase();
  2466. t.Run(6);
  2467. CompletedSignal.WaitOne();
  2468. // 0 - success
  2469. // 1 - failed (test completed)
  2470. // -1 - failed (test incomplete - deadlock, etc)
  2471. Console.WriteLine(Driver.Result);
  2472. }
  2473. }";
  2474. var expected = @"
  2475. 0
  2476. ";
  2477. CompileAndVerify(source, expected);
  2478. }
  2479. [Fact]
  2480. public void Array03()
  2481. {
  2482. var source = @"
  2483. using System;
  2484. using System.Threading;
  2485. using System.Threading.Tasks;
  2486. class TestCase
  2487. {
  2488. public async Task<T> GetVal<T>(T t)
  2489. {
  2490. await Task.Delay(1);
  2491. return t;
  2492. }
  2493. public async void Run<T>(T t) where T : struct
  2494. {
  2495. int tests = 0;
  2496. try
  2497. {
  2498. int[,] arr = new int[await GetVal(4), await GetVal(4)];
  2499. tests++;
  2500. arr[0, 0] = await GetVal(4);
  2501. if (arr[0, await (GetVal(0))] == 4)
  2502. Driver.Count++;
  2503. tests++;
  2504. arr[0, 0] += await GetVal(4);
  2505. if (arr[0, 0] == 8)
  2506. Driver.Count++;
  2507. tests++;
  2508. arr[1, 1] += await (GetVal(arr[0, 0]));
  2509. if (arr[1, 1] == 8)
  2510. Driver.Count++;
  2511. tests++;
  2512. arr[1, 1] += await (GetVal(arr[0, await GetVal(0)]));
  2513. if (arr[1, 1] == 16)
  2514. Driver.Count++;
  2515. tests++;
  2516. arr[2, await GetVal(2)]++;
  2517. if (arr[2, 2] == 1)
  2518. Driver.Count++;
  2519. }
  2520. finally
  2521. {
  2522. Driver.Result = Driver.Count - tests;
  2523. //When test complete, set the flag.
  2524. Driver.CompletedSignal.Set();
  2525. }
  2526. }
  2527. }
  2528. class Driver
  2529. {
  2530. public static int Result = -1;
  2531. public static int Count = 0;
  2532. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2533. static void Main()
  2534. {
  2535. var t = new TestCase();
  2536. t.Run(6);
  2537. CompletedSignal.WaitOne();
  2538. // 0 - success
  2539. // 1 - failed (test completed)
  2540. // -1 - failed (test incomplete - deadlock, etc)
  2541. Console.WriteLine(Driver.Result);
  2542. }
  2543. }";
  2544. var expected = @"
  2545. 0
  2546. ";
  2547. CompileAndVerify(source, expected);
  2548. }
  2549. [Fact]
  2550. public void Array04()
  2551. {
  2552. var source = @"
  2553. using System.Threading;
  2554. using System.Threading.Tasks;
  2555. using System;
  2556. struct MyStruct<T>
  2557. {
  2558. T t { get; set; }
  2559. public T this[T index]
  2560. {
  2561. get
  2562. {
  2563. return t;
  2564. }
  2565. set
  2566. {
  2567. t = value;
  2568. }
  2569. }
  2570. }
  2571. struct TestCase
  2572. {
  2573. public async void Run()
  2574. {
  2575. try
  2576. {
  2577. MyStruct<int> ms = new MyStruct<int>();
  2578. var x = ms[index: await Foo()];
  2579. }
  2580. finally
  2581. {
  2582. Driver.CompletedSignal.Set();
  2583. }
  2584. }
  2585. public async Task<int> Foo()
  2586. {
  2587. await Task.Delay(1);
  2588. return 1;
  2589. }
  2590. }
  2591. class Driver
  2592. {
  2593. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2594. static void Main()
  2595. {
  2596. var t = new TestCase();
  2597. t.Run();
  2598. CompletedSignal.WaitOne();
  2599. }
  2600. }";
  2601. CompileAndVerify(source, "");
  2602. }
  2603. [Fact]
  2604. public void ArrayAssign()
  2605. {
  2606. var source = @"
  2607. using System;
  2608. using System.Threading.Tasks;
  2609. class TestCase
  2610. {
  2611. static int[] arr = new int[4];
  2612. static async Task Run()
  2613. {
  2614. arr[0] = await Task.Factory.StartNew(() => 42);
  2615. }
  2616. static void Main()
  2617. {
  2618. Task task = Run();
  2619. task.Wait();
  2620. Console.WriteLine(arr[0]);
  2621. }
  2622. }";
  2623. var expected = @"
  2624. 42
  2625. ";
  2626. CompileAndVerify(source, expected);
  2627. }
  2628. [Fact]
  2629. public void CaptureThis()
  2630. {
  2631. var source = @"
  2632. using System.Threading;
  2633. using System.Threading.Tasks;
  2634. using System;
  2635. struct TestCase
  2636. {
  2637. public async Task<int> Run()
  2638. {
  2639. return await Foo();
  2640. }
  2641. public async Task<int> Foo()
  2642. {
  2643. return await Task.Factory.StartNew(() => 42);
  2644. }
  2645. }
  2646. class Driver
  2647. {
  2648. static void Main()
  2649. {
  2650. var t = new TestCase();
  2651. var task = t.Run();
  2652. task.Wait();
  2653. Console.WriteLine(task.Result);
  2654. }
  2655. }";
  2656. var expected = @"
  2657. 42
  2658. ";
  2659. CompileAndVerify(source, expected);
  2660. }
  2661. [Fact]
  2662. public void CaptureThis2()
  2663. {
  2664. var source = @"
  2665. using System.Collections.Generic;
  2666. using System.Threading;
  2667. using System.Threading.Tasks;
  2668. using System;
  2669. struct TestCase
  2670. {
  2671. public IEnumerable<int> Run()
  2672. {
  2673. yield return Foo();
  2674. }
  2675. public int Foo()
  2676. {
  2677. return 42;
  2678. }
  2679. }
  2680. class Driver
  2681. {
  2682. static void Main()
  2683. {
  2684. var t = new TestCase();
  2685. foreach (var x in t.Run())
  2686. {
  2687. Console.WriteLine(x);
  2688. }
  2689. }
  2690. }";
  2691. var expected = @"
  2692. 42
  2693. ";
  2694. CompileAndVerify(source, expected);
  2695. }
  2696. [Fact]
  2697. public void spillArrayLocal()
  2698. {
  2699. var source = @"
  2700. using System;
  2701. using System.Threading;
  2702. using System.Threading.Tasks;
  2703. class TestCase
  2704. {
  2705. public async Task<T> GetVal<T>(T t)
  2706. {
  2707. await Task.Delay(1);
  2708. return t;
  2709. }
  2710. public async void Run<T>(T t) where T : struct
  2711. {
  2712. int[] arr = new int[2] { -1, 42 };
  2713. int tests = 0;
  2714. try
  2715. {
  2716. tests++;
  2717. int t1 = arr[await GetVal(1)];
  2718. if (t1 == 42)
  2719. Driver.Count++;
  2720. }
  2721. finally
  2722. {
  2723. Driver.Result = Driver.Count - tests;
  2724. //When test complete, set the flag.
  2725. Driver.CompletedSignal.Set();
  2726. }
  2727. }
  2728. }
  2729. class Driver
  2730. {
  2731. public static int Result = -1;
  2732. public static int Count = 0;
  2733. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2734. static void Main()
  2735. {
  2736. var t = new TestCase();
  2737. t.Run(6);
  2738. CompletedSignal.WaitOne();
  2739. // 0 - success
  2740. // 1 - failed (test completed)
  2741. // -1 - failed (test incomplete - deadlock, etc)
  2742. Console.WriteLine(Driver.Result);
  2743. }
  2744. }";
  2745. var expected = @"
  2746. 0
  2747. ";
  2748. CompileAndVerify(source, expected);
  2749. }
  2750. [Fact]
  2751. public void SpillArrayCompoundAssignmentLValue()
  2752. {
  2753. var source = @"
  2754. using System;
  2755. using System.Threading;
  2756. using System.Threading.Tasks;
  2757. class Driver
  2758. {
  2759. static int[] arr;
  2760. static async Task Run()
  2761. {
  2762. arr = new int[1];
  2763. arr[0] += await Task.Factory.StartNew(() => 42);
  2764. }
  2765. static void Main()
  2766. {
  2767. Run().Wait();
  2768. Console.WriteLine(arr[0]);
  2769. }
  2770. }";
  2771. var expected = @"
  2772. 42
  2773. ";
  2774. CompileAndVerify(source, expected);
  2775. }
  2776. [Fact]
  2777. public void SpillArrayCompoundAssignmentLValueAwait()
  2778. {
  2779. var source = @"
  2780. using System;
  2781. using System.Threading;
  2782. using System.Threading.Tasks;
  2783. class Driver
  2784. {
  2785. static int[] arr;
  2786. static async Task Run()
  2787. {
  2788. arr = new int[1];
  2789. arr[await Task.Factory.StartNew(() => 0)] += await Task.Factory.StartNew(() => 42);
  2790. }
  2791. static void Main()
  2792. {
  2793. Run().Wait();
  2794. Console.WriteLine(arr[0]);
  2795. }
  2796. }";
  2797. var expected = @"
  2798. 42
  2799. ";
  2800. CompileAndVerify(source, expected);
  2801. }
  2802. [Fact]
  2803. public void SpillArrayCompoundAssignmentLValueAwait2()
  2804. {
  2805. var source = @"
  2806. using System;
  2807. using System.Threading;
  2808. using System.Threading.Tasks;
  2809. struct S1
  2810. {
  2811. public int x;
  2812. }
  2813. struct S2
  2814. {
  2815. public S1 s1;
  2816. }
  2817. class Driver
  2818. {
  2819. static async Task<int> Run()
  2820. {
  2821. var arr = new S2[1];
  2822. arr[await Task.Factory.StartNew(() => 0)].s1.x += await Task.Factory.StartNew(() => 42);
  2823. return arr[await Task.Factory.StartNew(() => 0)].s1.x;
  2824. }
  2825. static void Main()
  2826. {
  2827. var t = Run();
  2828. t.Wait();
  2829. Console.WriteLine(t.Result);
  2830. }
  2831. }";
  2832. var expected = @"
  2833. 42
  2834. ";
  2835. CompileAndVerify(source, expected);
  2836. }
  2837. [Fact]
  2838. public void DoubleSpillArrayCompoundAssignment()
  2839. {
  2840. var source = @"
  2841. using System;
  2842. using System.Threading;
  2843. using System.Threading.Tasks;
  2844. struct S1
  2845. {
  2846. public int x;
  2847. }
  2848. struct S2
  2849. {
  2850. public S1 s1;
  2851. }
  2852. class Driver
  2853. {
  2854. static async Task<int> Run()
  2855. {
  2856. var arr = new S2[1];
  2857. arr[await Task.Factory.StartNew(() => 0)].s1.x += (arr[await Task.Factory.StartNew(() => 0)].s1.x += await Task.Factory.StartNew(() => 42));
  2858. return arr[await Task.Factory.StartNew(() => 0)].s1.x;
  2859. }
  2860. static void Main()
  2861. {
  2862. var t = Run();
  2863. t.Wait();
  2864. Console.WriteLine(t.Result);
  2865. }
  2866. }";
  2867. var expected = @"
  2868. 42
  2869. ";
  2870. CompileAndVerify(source, expected);
  2871. }
  2872. [Fact]
  2873. public void array05()
  2874. {
  2875. var source = @"
  2876. using System;
  2877. using System.Threading;
  2878. using System.Threading.Tasks;
  2879. class TestCase
  2880. {
  2881. public async Task<T> GetVal<T>(T t)
  2882. {
  2883. await Task.Delay(1);
  2884. return t;
  2885. }
  2886. public async void Run()
  2887. {
  2888. int tests = 0;
  2889. try
  2890. {
  2891. //jagged array
  2892. tests++;
  2893. int[][] arr1 = new[]
  2894. {
  2895. new []{await GetVal(2),await GetVal(3)},
  2896. new []{4,await GetVal(5),await GetVal(6)}
  2897. };
  2898. if (arr1[0][1] == 3 && arr1[1][1] == 5 && arr1[1][2] == 6)
  2899. Driver.Count++;
  2900. tests++;
  2901. int[][] arr2 = new[]
  2902. {
  2903. new []{await GetVal(2),await GetVal(3)},
  2904. await Foo()
  2905. };
  2906. if (arr2[0][1] == 3 && arr2[1][1] == 2)
  2907. Driver.Count++;
  2908. }
  2909. finally
  2910. {
  2911. Driver.Result = Driver.Count - tests;
  2912. //When test complete, set the flag.
  2913. Driver.CompletedSignal.Set();
  2914. }
  2915. }
  2916. public async Task<int[]> Foo()
  2917. {
  2918. await Task.Delay(1);
  2919. return new int[] { 1, 2, 3 };
  2920. }
  2921. }
  2922. class Driver
  2923. {
  2924. public static int Result = -1;
  2925. public static int Count = 0;
  2926. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2927. static void Main()
  2928. {
  2929. var t = new TestCase();
  2930. t.Run();
  2931. CompletedSignal.WaitOne();
  2932. // 0 - success
  2933. // 1 - failed (test completed)
  2934. // -1 - failed (test incomplete - deadlock, etc)
  2935. Console.WriteLine(Driver.Result);
  2936. }
  2937. }";
  2938. var expected = @"
  2939. 0
  2940. ";
  2941. CompileAndVerify(source, expected);
  2942. }
  2943. [Fact]
  2944. public void array06()
  2945. {
  2946. var source = @"
  2947. using System;
  2948. using System.Threading;
  2949. using System.Threading.Tasks;
  2950. class TestCase
  2951. {
  2952. public async Task<T> GetVal<T>(T t)
  2953. {
  2954. await Task.Delay(1);
  2955. return t;
  2956. }
  2957. public async void Run()
  2958. {
  2959. int tests = 0;
  2960. try
  2961. {
  2962. //jagged array
  2963. tests++;
  2964. int[,] arr1 =
  2965. {
  2966. {await GetVal(2),await GetVal(3)},
  2967. {await GetVal(5),await GetVal(6)}
  2968. };
  2969. if (arr1[0, 1] == 3 && arr1[1, 0] == 5 && arr1[1, 1] == 6)
  2970. Driver.Count++;
  2971. tests++;
  2972. int[,] arr2 =
  2973. {
  2974. {await GetVal(2),3},
  2975. {4,await GetVal(5)}
  2976. };
  2977. if (arr2[0, 1] == 3 && arr2[1, 1] == 5)
  2978. Driver.Count++;
  2979. }
  2980. finally
  2981. {
  2982. Driver.Result = Driver.Count - tests;
  2983. //When test complete, set the flag.
  2984. Driver.CompletedSignal.Set();
  2985. }
  2986. }
  2987. }
  2988. class Driver
  2989. {
  2990. public static int Result = -1;
  2991. public static int Count = 0;
  2992. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  2993. static void Main()
  2994. {
  2995. var t = new TestCase();
  2996. t.Run();
  2997. CompletedSignal.WaitOne();
  2998. Console.WriteLine(Driver.Result);
  2999. }
  3000. }";
  3001. var expected = @"
  3002. 0
  3003. ";
  3004. CompileAndVerify(source, expected);
  3005. }
  3006. [Fact]
  3007. public void array07()
  3008. {
  3009. var source = @"
  3010. using System;
  3011. using System.Threading;
  3012. using System.Threading.Tasks;
  3013. class TestCase
  3014. {
  3015. public async Task<T> GetVal<T>(T t)
  3016. {
  3017. await Task.Delay(1);
  3018. return t;
  3019. }
  3020. public async void Run()
  3021. {
  3022. int tests = 0;
  3023. try
  3024. {
  3025. //jagged array
  3026. tests++;
  3027. int[][] arr1 = new[]
  3028. {
  3029. new []{await GetVal(2),await Task.Run<int>(async()=>{await Task.Delay(1);return 3;})},
  3030. new []{await GetVal(5),4,await Task.Run<int>(async()=>{await Task.Delay(1);return 6;})}
  3031. };
  3032. if (arr1[0][1] == 3 && arr1[1][1] == 4 && arr1[1][2] == 6)
  3033. Driver.Count++;
  3034. tests++;
  3035. dynamic arr2 = new[]
  3036. {
  3037. new []{await GetVal(2),3},
  3038. await Foo()
  3039. };
  3040. if (arr2[0][1] == 3 && arr2[1][1] == 2)
  3041. Driver.Count++;
  3042. }
  3043. finally
  3044. {
  3045. Driver.Result = Driver.Count - tests;
  3046. //When test complete, set the flag.
  3047. Driver.CompletedSignal.Set();
  3048. }
  3049. }
  3050. public async Task<int[]> Foo()
  3051. {
  3052. await Task.Delay(1);
  3053. return new int[] { 1, 2, 3 };
  3054. }
  3055. }
  3056. class Driver
  3057. {
  3058. public static int Result = -1;
  3059. public static int Count = 0;
  3060. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3061. static void Main()
  3062. {
  3063. var t = new TestCase();
  3064. t.Run();
  3065. CompletedSignal.WaitOne();
  3066. // 0 - success
  3067. // 1 - failed (test completed)
  3068. // -1 - failed (test incomplete - deadlock, etc)
  3069. Console.WriteLine(Driver.Result);
  3070. }
  3071. }";
  3072. var expected = @"
  3073. 0
  3074. ";
  3075. CompileAndVerify(source, expected);
  3076. }
  3077. [Fact]
  3078. public void AssignToAwait()
  3079. {
  3080. var source = @"
  3081. using System;
  3082. using System.Threading;
  3083. using System.Threading.Tasks;
  3084. class S
  3085. {
  3086. public int x = -1;
  3087. }
  3088. class Test
  3089. {
  3090. static S _s = new S();
  3091. public static async Task<S> GetS()
  3092. {
  3093. return await Task.Factory.StartNew(() => _s);
  3094. }
  3095. public static async Task Run()
  3096. {
  3097. (await GetS()).x = 42;
  3098. Console.WriteLine(_s.x);
  3099. }
  3100. }
  3101. class Driver
  3102. {
  3103. static void Main()
  3104. {
  3105. Test.Run().Wait();
  3106. }
  3107. }";
  3108. var expected = @"
  3109. 42
  3110. ";
  3111. CompileAndVerify(source, expected);
  3112. }
  3113. [Fact]
  3114. public void AssignAwaitToAwait()
  3115. {
  3116. var source = @"
  3117. using System;
  3118. using System.Threading.Tasks;
  3119. class S
  3120. {
  3121. public int x = -1;
  3122. }
  3123. class Test
  3124. {
  3125. static S _s = new S();
  3126. public static async Task<S> GetS()
  3127. {
  3128. return await Task.Factory.StartNew(() => _s);
  3129. }
  3130. public static async Task Run()
  3131. {
  3132. (await GetS()).x = await Task.Factory.StartNew(() => 42);
  3133. Console.WriteLine(_s.x);
  3134. }
  3135. }
  3136. class Driver
  3137. {
  3138. static void Main()
  3139. {
  3140. Test.Run().Wait();
  3141. }
  3142. }";
  3143. var expected = @"
  3144. 42
  3145. ";
  3146. CompileAndVerify(source, expected);
  3147. }
  3148. [Fact]
  3149. public void ReuseFields()
  3150. {
  3151. var source = @"
  3152. using System.Threading.Tasks;
  3153. class Test
  3154. {
  3155. static void F1(int x, int y)
  3156. {
  3157. }
  3158. async static Task<int> F2()
  3159. {
  3160. return await Task.Factory.StartNew(() => 42);
  3161. }
  3162. public static async void Run()
  3163. {
  3164. int x = 1;
  3165. F1(x, await F2());
  3166. int y = 2;
  3167. F1(y, await F2());
  3168. int z = 3;
  3169. F1(z, await F2());
  3170. }
  3171. public static void Main()
  3172. {
  3173. Run();
  3174. }
  3175. }";
  3176. var reference = CreateCompilationWithMscorlib45(source, references: new MetadataReference[] { SystemRef_v4_0_30319_17929 }).EmitToImageReference();
  3177. var comp = CreateCompilationWithMscorlib45("", new[] { reference }, compOptions: TestOptions.DllAlwaysImportInternals);
  3178. var testClass = comp.GlobalNamespace.GetMember<NamedTypeSymbol>("Test");
  3179. var stateMachineClass = (NamedTypeSymbol)testClass.GetMembers().Single(s => s.Name.StartsWith("<Run>"));
  3180. IEnumerable<IGrouping<TypeSymbol, FieldSymbol>> spillFieldsByType = stateMachineClass.GetMembers().Where(m => m.Kind == SymbolKind.Field && m.Name.StartsWith("<>7__wrap")).Cast<FieldSymbol>().GroupBy(x => x.Type);
  3181. Assert.Equal(1, spillFieldsByType.Count());
  3182. Assert.Equal(1, spillFieldsByType.Single(x => x.Key == comp.GetSpecialType(SpecialType.System_Int32)).Count());
  3183. }
  3184. [Fact]
  3185. public void NextedExpressionInArrayInitializer()
  3186. {
  3187. var source = @"
  3188. using System;
  3189. using System.Threading.Tasks;
  3190. class Test
  3191. {
  3192. public static async Task<int[,]> Run()
  3193. {
  3194. return new int[,] {
  3195. {1, 2, 21 + (await Task.Factory.StartNew(() => 21)) },
  3196. };
  3197. }
  3198. public static void Main()
  3199. {
  3200. var t = Run();
  3201. t.Wait();
  3202. foreach (var xs in t.Result)
  3203. {
  3204. Console.WriteLine(xs);
  3205. }
  3206. }
  3207. }";
  3208. var expected = @"
  3209. 1
  3210. 2
  3211. 42
  3212. ";
  3213. CompileAndVerify(source, expected);
  3214. }
  3215. [Fact]
  3216. public void Basic02()
  3217. {
  3218. var source = @"
  3219. using System;
  3220. using System.Collections.Generic;
  3221. using System.Text;
  3222. using System.Threading;
  3223. using System.Threading.Tasks;
  3224. class TestCase
  3225. {
  3226. static int test = 0;
  3227. static int count = 0;
  3228. public static async Task Run()
  3229. {
  3230. try
  3231. {
  3232. test++;
  3233. var f = new Func<int, object>(checked(await Bar()));
  3234. var x = f(1);
  3235. if ((string)x != ""1"")
  3236. count--;
  3237. }
  3238. finally
  3239. {
  3240. Driver.Result = test - count;
  3241. Driver.CompleteSignal.Set();
  3242. }
  3243. }
  3244. static async Task<Converter<int, string>> Bar()
  3245. {
  3246. count++;
  3247. await Task.Delay(1);
  3248. return delegate(int p1) { return p1.ToString(); };
  3249. }
  3250. }
  3251. class Driver
  3252. {
  3253. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3254. public static int Result = -1;
  3255. public static void Main()
  3256. {
  3257. TestCase.Run();
  3258. CompleteSignal.WaitOne();
  3259. Console.Write(Result);
  3260. }
  3261. }";
  3262. CompileAndVerify(source, expectedOutput:"0");
  3263. }
  3264. // [Fact]
  3265. // public void Argument02()
  3266. // {
  3267. // var source = @"
  3268. //using System;
  3269. //using System.Runtime.InteropServices;
  3270. //using System.Threading;
  3271. //using System.Threading.Tasks;
  3272. //
  3273. //[ComImport]
  3274. //[Guid(""09133803-EF59-4467-9135-255A65B606C2"")]
  3275. //interface IA
  3276. //{
  3277. // void Foo(ref long x, long? y);
  3278. //}
  3279. //
  3280. //class TestCase
  3281. //{
  3282. // static int count = 0;
  3283. // public async Task Run()
  3284. // {
  3285. // int test = 0;
  3286. //
  3287. // test = 2;
  3288. // IA a = null;
  3289. // long x;
  3290. // a.Foo(await Bar(), await Bar());
  3291. //
  3292. // Driver.Result = test - count;
  3293. // Driver.CompleteSignal.Set();
  3294. // }
  3295. // async Task<int> Bar()
  3296. // {
  3297. // await Task.Delay(1);
  3298. // count++;
  3299. // return 2;
  3300. // }
  3301. //}
  3302. //class Driver
  3303. //{
  3304. // static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3305. // public static int Result = -1;
  3306. // public static void Main()
  3307. // {
  3308. // TestCase tc = new TestCase();
  3309. // tc.Run();
  3310. // CompleteSignal.WaitOne();
  3311. //
  3312. // Console.WriteLine(Result);
  3313. // }
  3314. //}
  3315. //";
  3316. // CompileAndVerify(source, expectedOutput: "0");
  3317. // }
  3318. [Fact]
  3319. public void Argument03()
  3320. {
  3321. var source = @"
  3322. using System;
  3323. using System.Runtime.InteropServices;
  3324. using System.Text;
  3325. using System.Threading;
  3326. using System.Threading.Tasks;
  3327. class TestCase
  3328. {
  3329. static StringBuilder sb = new StringBuilder();
  3330. public async Task Run()
  3331. {
  3332. try
  3333. {
  3334. Bar(__arglist(One(), await Two()));
  3335. if (sb.ToString() == ""OneTwo"")
  3336. Driver.Result = 0;
  3337. }
  3338. finally
  3339. {
  3340. Driver.CompleteSignal.Set();
  3341. }
  3342. }
  3343. int One()
  3344. {
  3345. sb.Append(""One"");
  3346. return 1;
  3347. }
  3348. async Task<int> Two()
  3349. {
  3350. await Task.Delay(1);
  3351. sb.Append(""Two"");
  3352. return 2;
  3353. }
  3354. void Bar(__arglist)
  3355. {
  3356. var ai = new ArgIterator(__arglist);
  3357. while (ai.GetRemainingCount() > 0)
  3358. Console.WriteLine( __refvalue(ai.GetNextArg(), int));
  3359. }
  3360. }
  3361. class Driver
  3362. {
  3363. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3364. public static int Result = -1;
  3365. public static void Main()
  3366. {
  3367. TestCase tc = new TestCase();
  3368. tc.Run();
  3369. CompleteSignal.WaitOne();
  3370. Console.WriteLine(Result);
  3371. }
  3372. }";
  3373. var expected = @"
  3374. 1
  3375. 2
  3376. 0
  3377. ";
  3378. CompileAndVerify(source, expectedOutput: expected);
  3379. }
  3380. [Fact]
  3381. public void ObjectInit02()
  3382. {
  3383. var source = @"
  3384. using System;
  3385. using System.Collections;
  3386. using System.Threading;
  3387. using System.Threading.Tasks;
  3388. struct TestCase : IEnumerable
  3389. {
  3390. int X;
  3391. public async Task Run()
  3392. {
  3393. int test = 0;
  3394. int count = 0;
  3395. try
  3396. {
  3397. test++;
  3398. var x = new TestCase { X = await Bar() };
  3399. if (x.X == 1)
  3400. count++;
  3401. }
  3402. finally
  3403. {
  3404. Driver.Result = test - count;
  3405. Driver.CompleteSignal.Set();
  3406. }
  3407. }
  3408. async Task<int> Bar()
  3409. {
  3410. await Task.Delay(1);
  3411. return 1;
  3412. }
  3413. public IEnumerator GetEnumerator()
  3414. {
  3415. throw new System.NotImplementedException();
  3416. }
  3417. }
  3418. class Driver
  3419. {
  3420. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3421. public static int Result = -1;
  3422. public static void Main()
  3423. {
  3424. TestCase tc = new TestCase();
  3425. tc.Run();
  3426. CompleteSignal.WaitOne();
  3427. Console.WriteLine(Result);
  3428. }
  3429. }";
  3430. var expected = @"
  3431. 0
  3432. ";
  3433. CompileAndVerify(source, expectedOutput: expected);
  3434. }
  3435. [Fact]
  3436. public void Generic01()
  3437. {
  3438. var source = @"
  3439. using System;
  3440. using System.Collections.Generic;
  3441. using System.Text;
  3442. using System.Threading;
  3443. using System.Threading.Tasks;
  3444. class TestCase
  3445. {
  3446. static int test = 0;
  3447. static int count = 0;
  3448. public static async Task Run()
  3449. {
  3450. try
  3451. {
  3452. test++;
  3453. Qux(async () => { return 1; });
  3454. await Task.Delay(50);
  3455. }
  3456. finally
  3457. {
  3458. Driver.Result = test - count;
  3459. Driver.CompleteSignal.Set();
  3460. }
  3461. }
  3462. static async void Qux<T>(Func<Task<T>> x)
  3463. {
  3464. var y = await x();
  3465. if ((int)(object)y == 1)
  3466. count++;
  3467. }
  3468. }
  3469. class Driver
  3470. {
  3471. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3472. public static int Result = -1;
  3473. public static void Main()
  3474. {
  3475. TestCase.Run();
  3476. CompleteSignal.WaitOne();
  3477. Console.WriteLine(Result);
  3478. }
  3479. }";
  3480. var expected = @"
  3481. 0
  3482. ";
  3483. CompileAndVerify(source, expectedOutput: expected);
  3484. }
  3485. [Fact]
  3486. public void Ref01()
  3487. {
  3488. var source = @"
  3489. using System;
  3490. using System.Threading;
  3491. using System.Threading.Tasks;
  3492. class BaseTestCase
  3493. {
  3494. public void FooRef(ref decimal d, int x, out decimal od)
  3495. {
  3496. od = d;
  3497. d++;
  3498. }
  3499. public async Task<T> GetVal<T>(T t)
  3500. {
  3501. await Task.Delay(1);
  3502. return t;
  3503. }
  3504. }
  3505. class TestCase : BaseTestCase
  3506. {
  3507. public async void Run()
  3508. {
  3509. int tests = 0;
  3510. try
  3511. {
  3512. decimal d = 1;
  3513. decimal od;
  3514. tests++;
  3515. base.FooRef(ref d, await base.GetVal(4), out od);
  3516. if (d == 2 && od == 1) Driver.Count++;
  3517. }
  3518. finally
  3519. {
  3520. Driver.Result = Driver.Count - tests;
  3521. //When test complete, set the flag.
  3522. Driver.CompletedSignal.Set();
  3523. }
  3524. }
  3525. }
  3526. class Driver
  3527. {
  3528. public static int Result = -1;
  3529. public static int Count = 0;
  3530. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3531. static void Main()
  3532. {
  3533. var t = new TestCase();
  3534. t.Run();
  3535. CompletedSignal.WaitOne();
  3536. // 0 - success
  3537. // 1 - failed (test completed)
  3538. // -1 - failed (test incomplete - deadlock, etc)
  3539. Console.WriteLine(Driver.Result);
  3540. }
  3541. }";
  3542. CompileAndVerify(source, "0");
  3543. }
  3544. [Fact]
  3545. public void Struct02()
  3546. {
  3547. var source = @"
  3548. using System;
  3549. using System.Threading;
  3550. using System.Threading.Tasks;
  3551. struct TestCase
  3552. {
  3553. private Task<int> t;
  3554. public async void Run()
  3555. {
  3556. int tests = 0;
  3557. try
  3558. {
  3559. tests++;
  3560. t = Task.Run(async () => { await Task.Delay(1); return 1; });
  3561. var x = await t;
  3562. if (x == 1) Driver.Count++;
  3563. tests++;
  3564. t = Task.Run(async () => { await Task.Delay(1); return 1; });
  3565. var x2 = await this.t;
  3566. if (x2 == 1) Driver.Count++;
  3567. }
  3568. finally
  3569. {
  3570. Driver.Result = Driver.Count - tests;
  3571. //When test complete, set the flag.
  3572. Driver.CompletedSignal.Set();
  3573. }
  3574. }
  3575. }
  3576. class Driver
  3577. {
  3578. public static int Result = -1;
  3579. public static int Count = 0;
  3580. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3581. static void Main()
  3582. {
  3583. var t = new TestCase();
  3584. t.Run();
  3585. CompletedSignal.WaitOne();
  3586. // 0 - success
  3587. // 1 - failed (test completed)
  3588. // -1 - failed (test incomplete - deadlock, etc)
  3589. Console.Write(Driver.Result);
  3590. }
  3591. }";
  3592. CompileAndVerify(source, "0");
  3593. }
  3594. [Fact]
  3595. public void StackSpill_Operator_Compound02()
  3596. {
  3597. var source = @"
  3598. using System;
  3599. using System.Threading;
  3600. using System.Threading.Tasks;
  3601. class TestCase
  3602. {
  3603. public async Task<T> GetVal<T>(T t)
  3604. {
  3605. await Task.Delay(1);
  3606. return t;
  3607. }
  3608. public async void Run()
  3609. {
  3610. int tests = 0;
  3611. try
  3612. {
  3613. tests++;
  3614. int[] x = new int[] { 1, 2, 3, 4 };
  3615. x[await GetVal(0)] += await GetVal(4);
  3616. if (x[0] == 5)
  3617. Driver.Count++;
  3618. }
  3619. finally
  3620. {
  3621. Driver.Result = Driver.Count - tests;
  3622. //When test complete, set the flag.
  3623. Driver.CompletedSignal.Set();
  3624. }
  3625. }
  3626. }
  3627. class Driver
  3628. {
  3629. public static int Result = -1;
  3630. public static int Count = 0;
  3631. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3632. static void Main()
  3633. {
  3634. var t = new TestCase();
  3635. t.Run();
  3636. CompletedSignal.WaitOne();
  3637. // 0 - success
  3638. // 1 - failed (test completed)
  3639. // -1 - failed (test incomplete - deadlock, etc)
  3640. Console.WriteLine(Driver.Result);
  3641. }
  3642. }";
  3643. CompileAndVerify(source, "0");
  3644. }
  3645. [Fact]
  3646. public void OperatorHoist()
  3647. {
  3648. var source = @"
  3649. using System;
  3650. using System.Threading;
  3651. using System.Threading.Tasks;
  3652. class TestCase
  3653. {
  3654. public async Task<T> GetVal<T>(T t)
  3655. {
  3656. await Task.Delay(1);
  3657. return t;
  3658. }
  3659. public async void Run()
  3660. {
  3661. int tests = 0;
  3662. try
  3663. {
  3664. tests++;
  3665. int[] x = new int[] { 1, 2, 3, 4 };
  3666. x[await GetVal(0)] += await GetVal(4);
  3667. if (x[0] == 5)
  3668. Driver.Count++;
  3669. }
  3670. finally
  3671. {
  3672. Driver.Result = Driver.Count - tests;
  3673. //When test complete, set the flag.
  3674. Driver.CompletedSignal.Set();
  3675. }
  3676. }
  3677. }
  3678. class Driver
  3679. {
  3680. public static int Result = -1;
  3681. public static int Count = 0;
  3682. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3683. static void Main()
  3684. {
  3685. var t = new TestCase();
  3686. t.Run();
  3687. CompletedSignal.WaitOne();
  3688. // 0 - success
  3689. // 1 - failed (test completed)
  3690. // -1 - failed (test incomplete - deadlock, etc)
  3691. Console.WriteLine(Driver.Result);
  3692. }
  3693. }";
  3694. CompileAndVerify(source, "0");
  3695. }
  3696. [Fact]
  3697. public void Delegate10()
  3698. {
  3699. var source = @"
  3700. using System.Threading;
  3701. using System.Threading.Tasks;
  3702. using System;
  3703. delegate Task MyDel<U>(out U u);
  3704. class MyClass<T>
  3705. {
  3706. public static Task Meth(out T t)
  3707. {
  3708. t = default(T);
  3709. return Task.Run(async () => { await Task.Delay(1); TestCase.Count++; });
  3710. }
  3711. public MyDel<T> myDel;
  3712. public event MyDel<T> myEvent;
  3713. public async Task TrigerEvent(T p)
  3714. {
  3715. try
  3716. {
  3717. await myEvent(out p);
  3718. }
  3719. catch
  3720. {
  3721. TestCase.Count += 5;
  3722. }
  3723. }
  3724. }
  3725. struct TestCase
  3726. {
  3727. public static int Count = 0;
  3728. private int tests;
  3729. public async void Run()
  3730. {
  3731. tests = 0;
  3732. try
  3733. {
  3734. tests++;
  3735. MyClass<string> ms = new MyClass<string>();
  3736. ms.myDel = MyClass<string>.Meth;
  3737. string str = """";
  3738. await ms.myDel(out str);
  3739. tests++;
  3740. ms.myEvent += MyClass<string>.Meth;
  3741. await ms.TrigerEvent(str);
  3742. }
  3743. finally
  3744. {
  3745. Driver.Result = TestCase.Count - this.tests;
  3746. //When test complete, set the flag.
  3747. Driver.CompletedSignal.Set();
  3748. }
  3749. }
  3750. }
  3751. class Driver
  3752. {
  3753. public static int Result = -1;
  3754. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3755. static void Main()
  3756. {
  3757. var t = new TestCase();
  3758. t.Run();
  3759. CompletedSignal.WaitOne();
  3760. // 0 - success
  3761. // 1 - failed (test completed)
  3762. // -1 - failed (test incomplete - deadlock, etc)
  3763. Console.WriteLine(Driver.Result);
  3764. }
  3765. }";
  3766. CompileAndVerify(source, "0");
  3767. }
  3768. [Fact]
  3769. public void Unhoisted_Used_Param()
  3770. {
  3771. var source = @"
  3772. using System.Collections.Generic;
  3773. struct Test
  3774. {
  3775. public static IEnumerable<int> F(int x)
  3776. {
  3777. x = 2;
  3778. yield return 1;
  3779. }
  3780. public static void Main()
  3781. {
  3782. F(1);
  3783. }
  3784. }";
  3785. CompileAndVerify(source, "");
  3786. }
  3787. [Fact]
  3788. public void AwaitSwitch()
  3789. {
  3790. var source = @"
  3791. using System;
  3792. using System.Threading;
  3793. using System.Threading.Tasks;
  3794. class TestCase
  3795. {
  3796. public async void Run()
  3797. {
  3798. int test = 0;
  3799. int result = 0;
  3800. try
  3801. {
  3802. test++;
  3803. switch (await ((Func<Task<int>>)(async () => { await Task.Delay(1); return 5; }))())
  3804. {
  3805. case 1:
  3806. case 2: break;
  3807. case 5: result++; break;
  3808. default: break;
  3809. }
  3810. }
  3811. finally
  3812. {
  3813. Driver.Result = test - result;
  3814. Driver.CompleteSignal.Set();
  3815. }
  3816. }
  3817. }
  3818. class Driver
  3819. {
  3820. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3821. public static int Result = -1;
  3822. public static void Main()
  3823. {
  3824. TestCase tc = new TestCase();
  3825. tc.Run();
  3826. CompleteSignal.WaitOne();
  3827. Console.WriteLine(Result);
  3828. }
  3829. }";
  3830. CompileAndVerify(source, "0");
  3831. }
  3832. [Fact]
  3833. public void Return07()
  3834. {
  3835. var source = @"
  3836. using System;
  3837. using System.Threading;
  3838. using System.Threading.Tasks;
  3839. class TestCase
  3840. {
  3841. unsafe struct S
  3842. {
  3843. public int value;
  3844. public S* next;
  3845. }
  3846. public async void Run()
  3847. {
  3848. int test = 0;
  3849. int result = 0;
  3850. try
  3851. {
  3852. Func<Task<dynamic>> func, func2 = null;
  3853. test++;
  3854. S s = new S();
  3855. S s1 = new S();
  3856. unsafe
  3857. {
  3858. S* head = &s;
  3859. s.next = &s1;
  3860. func = async () => { (*(head->next)).value = 1; result++; return head->next->value; };
  3861. func2 = async () => (*(head->next));
  3862. }
  3863. var x = await func();
  3864. if (x != 1)
  3865. result--;
  3866. var xx = await func2();
  3867. if (xx.value != 1)
  3868. result--;
  3869. }
  3870. finally
  3871. {
  3872. Driver.Result = test - result;
  3873. Driver.CompleteSignal.Set();
  3874. }
  3875. }
  3876. }
  3877. class Driver
  3878. {
  3879. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  3880. public static int Result = -1;
  3881. public static void Main()
  3882. {
  3883. TestCase tc = new TestCase();
  3884. tc.Run();
  3885. CompleteSignal.WaitOne();
  3886. Console.WriteLine(Result);
  3887. }
  3888. }";
  3889. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  3890. CompileAndVerify(source, "0", compOptions: compOptions);
  3891. }
  3892. [Fact]
  3893. public void Inference()
  3894. {
  3895. var source = @"
  3896. using System;
  3897. using System.Collections.Generic;
  3898. using System.Threading;
  3899. using System.Threading.Tasks;
  3900. struct Test
  3901. {
  3902. public Task<string> Foo
  3903. {
  3904. get { return Task.Run<string>(async () => { await Task.Delay(1); return ""abc""; }); }
  3905. }
  3906. }
  3907. class TestCase<U>
  3908. {
  3909. public static async Task<object> GetValue(object x)
  3910. {
  3911. await Task.Delay(1);
  3912. return x;
  3913. }
  3914. public static T GetValue1<T>(T t) where T : Task<U>
  3915. {
  3916. return t;
  3917. }
  3918. public async void Run()
  3919. {
  3920. int tests = 0;
  3921. Test t = new Test();
  3922. tests++;
  3923. var x1 = await TestCase<string>.GetValue(await t.Foo);
  3924. if (x1 == ""abc"")
  3925. Driver.Count++;
  3926. tests++;
  3927. var x2 = await TestCase<string>.GetValue1(t.Foo);
  3928. if (x2 == ""abc"")
  3929. Driver.Count++;
  3930. Driver.Result = Driver.Count - tests;
  3931. //When test completes, set the flag.
  3932. Driver.CompletedSignal.Set();
  3933. }
  3934. }
  3935. class Driver
  3936. {
  3937. public static int Result = -1;
  3938. public static int Count = 0;
  3939. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3940. static void Main()
  3941. {
  3942. var t = new TestCase<int>();
  3943. t.Run();
  3944. CompletedSignal.WaitOne();
  3945. // 0 - success
  3946. // 1 - failed (test completed)
  3947. // -1 - failed (test incomplete - deadlock, etc)
  3948. Console.WriteLine(Driver.Result);
  3949. }
  3950. }";
  3951. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  3952. CompileAndVerify(source, "0", compOptions: compOptions);
  3953. }
  3954. [Fact]
  3955. public void Operator05()
  3956. {
  3957. var source = @"
  3958. using System.Threading;
  3959. using System.Threading.Tasks;
  3960. using System;
  3961. class TestCase
  3962. {
  3963. public static int Count = 0;
  3964. public async void Run()
  3965. {
  3966. int tests = 0;
  3967. var x1 = ((await Foo1()) is object);
  3968. var x2 = ((await Foo2()) as string);
  3969. if (x1 == true)
  3970. tests++;
  3971. if (x2 == ""string"")
  3972. tests++;
  3973. Driver.Result = TestCase.Count - tests;
  3974. //When test complete, set the flag.
  3975. Driver.CompletedSignal.Set();
  3976. }
  3977. public async Task<int> Foo1()
  3978. {
  3979. await Task.Delay(1);
  3980. TestCase.Count++;
  3981. int i = 0;
  3982. return i;
  3983. }
  3984. public async Task<object> Foo2()
  3985. {
  3986. await Task.Delay(1);
  3987. TestCase.Count++;
  3988. return ""string"";
  3989. }
  3990. }
  3991. class Driver
  3992. {
  3993. public static int Result = -1;
  3994. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  3995. static void Main()
  3996. {
  3997. var t = new TestCase();
  3998. t.Run();
  3999. CompletedSignal.WaitOne();
  4000. // 0 - success
  4001. // 1 - failed (test completed)
  4002. // -1 - failed (test incomplete - deadlock, etc)
  4003. Console.Write(Driver.Result);
  4004. }
  4005. }";
  4006. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  4007. CompileAndVerify(source, "0", compOptions: compOptions);
  4008. }
  4009. [Fact]
  4010. public void Property21()
  4011. {
  4012. var source = @"
  4013. using System.Threading;
  4014. using System.Threading.Tasks;
  4015. using System;
  4016. class Base
  4017. {
  4018. public virtual int MyProp { get; private set; }
  4019. }
  4020. class TestClass : Base
  4021. {
  4022. async Task<int> getBaseMyProp() { await Task.Delay(1); return base.MyProp; }
  4023. async public void Run()
  4024. {
  4025. Driver.Result = await getBaseMyProp();
  4026. Driver.CompleteSignal.Set();
  4027. }
  4028. }
  4029. class Driver
  4030. {
  4031. public static AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  4032. public static void Main()
  4033. {
  4034. TestClass tc = new TestClass();
  4035. tc.Run();
  4036. CompleteSignal.WaitOne();
  4037. Console.WriteLine(Result);
  4038. }
  4039. public static int Result = -1;
  4040. }";
  4041. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  4042. CompileAndVerify(source, "0", compOptions: compOptions);
  4043. }
  4044. [Fact]
  4045. public void AnonType32()
  4046. {
  4047. var source =
  4048. @"using System;
  4049. using System.Collections.Generic;
  4050. using System.Linq;
  4051. using System.Threading;
  4052. using System.Threading.Tasks;
  4053. class TestCase
  4054. {
  4055. public async void Run()
  4056. {
  4057. int tests = 0;
  4058. try
  4059. {
  4060. tests++;
  4061. try
  4062. {
  4063. var tmp = await (new { task = Task.Run<string>(async () => { await Task.Delay(1); return """"; }) }).task;
  4064. throw new Exception(tmp);
  4065. }
  4066. catch (Exception ex)
  4067. {
  4068. if (ex.Message == """")
  4069. Driver.Count++;
  4070. }
  4071. }
  4072. finally
  4073. {
  4074. Driver.Result = Driver.Count - tests;
  4075. //When test complete, set the flag.
  4076. Driver.CompletedSignal.Set();
  4077. }
  4078. }
  4079. }
  4080. class Driver
  4081. {
  4082. public static int Result = -1;
  4083. public static int Count = 0;
  4084. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4085. static void Main()
  4086. {
  4087. var t = new TestCase();
  4088. t.Run();
  4089. CompletedSignal.WaitOne();
  4090. // 0 - success
  4091. // 1 - failed (test completed)
  4092. // -1 - failed (test incomplete - deadlock, etc)
  4093. Console.WriteLine(Driver.Result);
  4094. }
  4095. }";
  4096. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  4097. CompileAndVerify(source, "0", compOptions: compOptions);
  4098. }
  4099. [Fact]
  4100. public void Init19()
  4101. {
  4102. var source = @"
  4103. using System;
  4104. using System.Collections.Generic;
  4105. using System.Threading;
  4106. using System.Threading.Tasks;
  4107. class ObjInit
  4108. {
  4109. public int async;
  4110. public Task t;
  4111. public long l;
  4112. }
  4113. class TestCase
  4114. {
  4115. private T Throw<T>(T i)
  4116. {
  4117. MethodCount++;
  4118. throw new OverflowException();
  4119. }
  4120. private async Task<T> GetVal<T>(T x)
  4121. {
  4122. await Task.Delay(1);
  4123. Throw(x);
  4124. return x;
  4125. }
  4126. public Task<long> MyProperty { get; set; }
  4127. public async void Run()
  4128. {
  4129. int tests = 0;
  4130. Task<int> t = Task.Run<int>(async () => { await Task.Delay(1); throw new FieldAccessException(); return 1; });
  4131. //object type init
  4132. tests++;
  4133. try
  4134. {
  4135. MyProperty = Task.Run<long>(async () => { await Task.Delay(1); throw new DataMisalignedException(); return 1; });
  4136. var obj = new ObjInit()
  4137. {
  4138. async = await t,
  4139. t = GetVal((Task.Run(async () => { await Task.Delay(1); }))),
  4140. l = await MyProperty
  4141. };
  4142. await obj.t;
  4143. }
  4144. catch (FieldAccessException)
  4145. {
  4146. Driver.Count++;
  4147. }
  4148. catch
  4149. {
  4150. Driver.Count--;
  4151. }
  4152. Driver.Result = Driver.Count - tests;
  4153. //When test complete, set the flag.
  4154. Driver.CompletedSignal.Set();
  4155. }
  4156. public int MethodCount = 0;
  4157. }
  4158. class Driver
  4159. {
  4160. public static int Result = -1;
  4161. public static int Count = 0;
  4162. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4163. static void Main()
  4164. {
  4165. var t = new TestCase();
  4166. t.Run();
  4167. CompletedSignal.WaitOne();
  4168. // 0 - success
  4169. // 1 - failed (test completed)
  4170. // -1 - failed (test incomplete - deadlock, etc)
  4171. Console.WriteLine(Driver.Result);
  4172. }
  4173. }";
  4174. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  4175. CompileAndVerify(source, "0", compOptions: compOptions);
  4176. }
  4177. [Fact]
  4178. public void Conformance_OverloadResolution_1Class_Generic_regularMethod05()
  4179. {
  4180. var source = @"
  4181. using System.Threading;
  4182. using System.Threading.Tasks;
  4183. using System;
  4184. struct Test<U, V, W>
  4185. {
  4186. //Regular methods
  4187. public int Foo(Func<Task<U>> f) { return 1; }
  4188. public int Foo(Func<Task<V>> f) { return 2; }
  4189. public int Foo(Func<Task<W>> f) { return 3; }
  4190. }
  4191. class TestCase
  4192. {
  4193. //where there is a conversion between types (int->double)
  4194. public void Run()
  4195. {
  4196. Test<decimal, string, dynamic> test = new Test<decimal, string, dynamic>();
  4197. int rez = 0;
  4198. // Pick double
  4199. Driver.Tests++;
  4200. rez = test.Foo(async () => { return 1.0; });
  4201. if (rez == 3) Driver.Count++;
  4202. //pick int
  4203. Driver.Tests++;
  4204. rez = test.Foo(async delegate() { return 1; });
  4205. if (rez == 1) Driver.Count++;
  4206. // The best overload is Func<Task<object>>
  4207. Driver.Tests++;
  4208. rez = test.Foo(async () => { return """"; });
  4209. if (rez == 2) Driver.Count++;
  4210. Driver.Tests++;
  4211. rez = test.Foo(async delegate() { return """"; });
  4212. if (rez == 2) Driver.Count++;
  4213. }
  4214. }
  4215. class Driver
  4216. {
  4217. public static int Count = 0;
  4218. public static int Tests = 0;
  4219. static int Main()
  4220. {
  4221. var t = new TestCase();
  4222. t.Run();
  4223. var ret = Driver.Tests - Driver.Count;
  4224. Console.WriteLine(ret);
  4225. return ret;
  4226. }
  4227. }";
  4228. var compOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimize: false, concurrentBuild: false, allowUnsafe: true);
  4229. CompileAndVerify(source, "0", compOptions: compOptions);
  4230. }
  4231. [Fact]
  4232. public void Dynamic()
  4233. {
  4234. var source = @"
  4235. using System;
  4236. using System.Threading.Tasks;
  4237. class Test
  4238. {
  4239. public static async Task<dynamic> F1(dynamic d)
  4240. {
  4241. return await d;
  4242. }
  4243. public static async Task<int> F2(Task<int> d)
  4244. {
  4245. return await d;
  4246. }
  4247. public static async Task<int> Run()
  4248. {
  4249. int a = await F1(Task.Factory.StartNew(() => 21));
  4250. int b = await F2(Task.Factory.StartNew(() => 21));
  4251. return a + b;
  4252. }
  4253. static void Main()
  4254. {
  4255. var t = Run();
  4256. t.Wait();
  4257. Console.WriteLine(t.Result);
  4258. }
  4259. }";
  4260. CompileAndVerify(source, "42");
  4261. }
  4262. [Fact]
  4263. [WorkItem(638261, "DevDiv")]
  4264. public void Await15()
  4265. {
  4266. var source = @"
  4267. using System;
  4268. using System.Collections.Generic;
  4269. using System.Threading;
  4270. using System.Threading.Tasks;
  4271. struct DynamicClass
  4272. {
  4273. public async Task<dynamic> Foo<T>(T t)
  4274. {
  4275. await Task.Delay(1);
  4276. return t;
  4277. }
  4278. public async Task<Task<dynamic>> Bar(int i)
  4279. {
  4280. await Task.Delay(1);
  4281. return Task.Run<dynamic>(async () => { await Task.Delay(1); return i; });
  4282. }
  4283. }
  4284. class TestCase
  4285. {
  4286. public async void Run()
  4287. {
  4288. int tests = 0;
  4289. DynamicClass dc = new DynamicClass();
  4290. dynamic d = 123;
  4291. try
  4292. {
  4293. tests++;
  4294. var x1 = await dc.Foo("""");
  4295. if (x1 == """") Driver.Count++;
  4296. tests++;
  4297. var x2 = await await dc.Bar(d);
  4298. if (x2 == 123) Driver.Count++;
  4299. tests++;
  4300. var x3 = await await dc.Bar(await dc.Foo(234));
  4301. if (x3 == 234) Driver.Count++;
  4302. }
  4303. finally
  4304. {
  4305. Driver.Result = Driver.Count - tests;
  4306. //When test complete, set the flag.
  4307. Driver.CompletedSignal.Set();
  4308. }
  4309. }
  4310. }
  4311. class Driver
  4312. {
  4313. public static int Result = -1;
  4314. public static int Count = 0;
  4315. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4316. static void Main()
  4317. {
  4318. var t = new TestCase();
  4319. t.Run();
  4320. CompletedSignal.WaitOne();
  4321. // 0 - success
  4322. // 1 - failed (test completed)
  4323. // -1 - failed (test incomplete - deadlock, etc)
  4324. Console.WriteLine(Driver.Result);
  4325. }
  4326. }";
  4327. CompileAndVerify(source, "0");
  4328. }
  4329. [Fact]
  4330. public void Await01()
  4331. {
  4332. // The legacy compiler allows this; we don't. This kills conformance_await_dynamic_await01.
  4333. var source = @"
  4334. using System;
  4335. using System.Threading;
  4336. using System.Threading.Tasks;
  4337. class DynamicMembers
  4338. {
  4339. public dynamic Prop { get; set; }
  4340. }
  4341. class Driver
  4342. {
  4343. static void Main()
  4344. {
  4345. DynamicMembers dc2 = new DynamicMembers();
  4346. dc2.Prop = (Func<Task<int>>)(async () => { await Task.Delay(1); return 1; });
  4347. var rez2 = dc2.Prop();
  4348. }
  4349. }";
  4350. CompileAndVerify(source, "");
  4351. }
  4352. [Fact]
  4353. public void Await40()
  4354. {
  4355. var source = @"
  4356. using System;
  4357. using System.Threading;
  4358. using System.Threading.Tasks;
  4359. class C1
  4360. {
  4361. public async Task<int> Method(int x)
  4362. {
  4363. await Task.Delay(1);
  4364. return x;
  4365. }
  4366. }
  4367. class C2
  4368. {
  4369. public int Status;
  4370. public C2(int x = 5)
  4371. {
  4372. this.Status = x;
  4373. }
  4374. public C2(int x, int y)
  4375. {
  4376. this.Status = x + y;
  4377. }
  4378. public int Bar(int x)
  4379. {
  4380. return x;
  4381. }
  4382. }
  4383. class TestCase
  4384. {
  4385. public async void Run()
  4386. {
  4387. int tests = 0;
  4388. try
  4389. {
  4390. tests++;
  4391. dynamic c = new C1();
  4392. C2 cc = new C2(x: await c.Method(1));
  4393. if (cc.Status == 1)
  4394. Driver.Count++;
  4395. tests++;
  4396. dynamic f = (Func<Task<dynamic>>)(async () => { await Task.Delay(1); return 4; });
  4397. cc = new C2(await c.Method(2), await f());
  4398. if (cc.Status == 6)
  4399. Driver.Count++;
  4400. tests++;
  4401. var x = new C2(2).Bar(await c.Method(1));
  4402. if (cc.Status == 6 && x == 1)
  4403. Driver.Count++;
  4404. }
  4405. finally
  4406. {
  4407. Driver.Result = Driver.Count - tests;
  4408. //When test complete, set the flag.
  4409. Driver.CompletedSignal.Set();
  4410. }
  4411. }
  4412. }
  4413. class Driver
  4414. {
  4415. public static int Result = -1;
  4416. public static int Count = 0;
  4417. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4418. static void Main()
  4419. {
  4420. var t = new TestCase();
  4421. t.Run();
  4422. CompletedSignal.WaitOne();
  4423. // 0 - success
  4424. // 1 - failed (test completed)
  4425. // -1 - failed (test incomplete - deadlock, etc)
  4426. Console.WriteLine(Driver.Result);
  4427. }
  4428. }";
  4429. CompileAndVerify(source, "0");
  4430. }
  4431. [Fact]
  4432. public void Await43()
  4433. {
  4434. var source = @"
  4435. using System.Threading;
  4436. using System.Threading.Tasks;
  4437. using System;
  4438. struct MyClass
  4439. {
  4440. public static Task operator *(MyClass c, int x)
  4441. {
  4442. return Task.Run(async delegate
  4443. {
  4444. await Task.Delay(1);
  4445. TestCase.Count++;
  4446. });
  4447. }
  4448. public static Task operator +(MyClass c, long x)
  4449. {
  4450. return Task.Run(async () =>
  4451. {
  4452. await Task.Delay(1);
  4453. TestCase.Count++;
  4454. });
  4455. }
  4456. }
  4457. class TestCase
  4458. {
  4459. public static int Count = 0;
  4460. private int tests;
  4461. public async void Run()
  4462. {
  4463. this.tests = 0;
  4464. dynamic dy = Task.Run<MyClass>(async () => { await Task.Delay(1); return new MyClass(); });
  4465. try
  4466. {
  4467. this.tests++;
  4468. await (await dy * 5);
  4469. this.tests++;
  4470. dynamic d = new MyClass();
  4471. dynamic dd = Task.Run<long>(async () => { await Task.Delay(1); return 1L; });
  4472. await (d + await dd);
  4473. }
  4474. catch (Exception ex)
  4475. {
  4476. Console.WriteLine(ex);
  4477. Console.WriteLine(ex.StackTrace);
  4478. }
  4479. finally
  4480. {
  4481. Driver.Result = TestCase.Count - this.tests;
  4482. //When test complete, set the flag.
  4483. Driver.CompletedSignal.Set();
  4484. }
  4485. }
  4486. }
  4487. class Driver
  4488. {
  4489. public static int Result = -1;
  4490. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4491. static void Main()
  4492. {
  4493. var t = new TestCase();
  4494. t.Run();
  4495. CompletedSignal.WaitOne();
  4496. // 0 - success
  4497. // 1 - failed (test completed)
  4498. // -1 - failed (test incomplete - deadlock, etc)
  4499. Console.WriteLine(Driver.Result);
  4500. }
  4501. }";
  4502. CompileAndVerify(source, "0");
  4503. }
  4504. [Fact]
  4505. public void Await44()
  4506. {
  4507. var source = @"
  4508. using System.Threading;
  4509. using System.Threading.Tasks;
  4510. using System;
  4511. class MyClass
  4512. {
  4513. public static implicit operator Task(MyClass c)
  4514. {
  4515. return Task.Run(async delegate
  4516. {
  4517. await Task.Delay(1);
  4518. TestCase.Count++;
  4519. });
  4520. }
  4521. }
  4522. class TestCase
  4523. {
  4524. public static int Count = 0;
  4525. private int tests;
  4526. public async void Run()
  4527. {
  4528. this.tests = 0;
  4529. dynamic mc = new MyClass();
  4530. try
  4531. {
  4532. tests++;
  4533. Task t1 = mc;
  4534. await t1;
  4535. tests++;
  4536. dynamic t2 = (Task)mc;
  4537. await t2;
  4538. }
  4539. finally
  4540. {
  4541. Driver.Result = TestCase.Count - this.tests;
  4542. //When test complete, set the flag.
  4543. Driver.CompletedSignal.Set();
  4544. }
  4545. }
  4546. }
  4547. class Driver
  4548. {
  4549. public static int Result = -1;
  4550. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4551. static void Main()
  4552. {
  4553. var t = new TestCase();
  4554. t.Run();
  4555. CompletedSignal.WaitOne();
  4556. // 0 - success
  4557. // 1 - failed (test completed)
  4558. // -1 - failed (test incomplete - deadlock, etc)
  4559. Console.WriteLine(Driver.Result);
  4560. }
  4561. }";
  4562. CompileAndVerify(source, "0");
  4563. }
  4564. [Fact]
  4565. public void ThisShouldProbablyCompileToVerifiableCode()
  4566. {
  4567. var source = @"
  4568. using System;
  4569. class Driver
  4570. {
  4571. public static bool Run()
  4572. {
  4573. dynamic dynamicThing = false;
  4574. return true && dynamicThing;
  4575. }
  4576. static void Main()
  4577. {
  4578. Console.WriteLine(Run());
  4579. }
  4580. }";
  4581. CompileAndVerify(source, "False");
  4582. }
  4583. [Fact]
  4584. public void Async_Conformance_Awaiting_indexer23()
  4585. {
  4586. var source = @"
  4587. using System.Threading;
  4588. using System.Threading.Tasks;
  4589. using System;
  4590. struct MyStruct<T> where T : Task<Func<int>>
  4591. {
  4592. T t { get; set; }
  4593. public T this[T index]
  4594. {
  4595. get
  4596. {
  4597. return t;
  4598. }
  4599. set
  4600. {
  4601. t = value;
  4602. }
  4603. }
  4604. }
  4605. struct TestCase
  4606. {
  4607. public static int Count = 0;
  4608. private int tests;
  4609. public async void Run()
  4610. {
  4611. this.tests = 0;
  4612. MyStruct<Task<Func<int>>> ms = new MyStruct<Task<Func<int>>>();
  4613. try
  4614. {
  4615. ms[index: null] = Task.Run<Func<int>>(async () => { await Task.Delay(1); Interlocked.Increment(ref TestCase.Count); return () => (123); });
  4616. this.tests++;
  4617. var x = await ms[index: await Foo(null)];
  4618. if (x() == 123)
  4619. this.tests++;
  4620. }
  4621. finally
  4622. {
  4623. Driver.Result = TestCase.Count - this.tests;
  4624. //When test complete, set the flag.
  4625. Driver.CompletedSignal.Set();
  4626. }
  4627. }
  4628. public async Task<Task<Func<int>>> Foo(Task<Func<int>> d)
  4629. {
  4630. await Task.Delay(1);
  4631. Interlocked.Increment(ref TestCase.Count);
  4632. return d;
  4633. }
  4634. }
  4635. class Driver
  4636. {
  4637. public static int Result = -1;
  4638. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4639. static void Main()
  4640. {
  4641. var t = new TestCase();
  4642. t.Run();
  4643. CompletedSignal.WaitOne();
  4644. // 0 - success
  4645. // 1 - failed (test completed)
  4646. // -1 - failed (test incomplete - deadlock, etc)
  4647. Console.WriteLine(Driver.Result);
  4648. }
  4649. }";
  4650. CompileAndVerify(source, "0");
  4651. }
  4652. [Fact]
  4653. public void Async_StackSpill_Argument_Generic04()
  4654. {
  4655. var source = @"
  4656. using System;
  4657. using System.Threading.Tasks;
  4658. public class mc<T>
  4659. {
  4660. async public System.Threading.Tasks.Task<dynamic> Foo<V>(T t, V u) { await Task.Delay(1); return u; }
  4661. }
  4662. class Test
  4663. {
  4664. static async Task<int> Foo()
  4665. {
  4666. dynamic mc = new mc<string>();
  4667. var rez = await mc.Foo<string>(null, await ((Func<Task<string>>)(async () => { await Task.Delay(1); return ""Test""; }))());
  4668. if (rez == ""Test"")
  4669. return 0;
  4670. return 1;
  4671. }
  4672. static void Main()
  4673. {
  4674. Console.WriteLine(Foo().Result);
  4675. }
  4676. }";
  4677. CompileAndVerify(source, "0");
  4678. }
  4679. [Fact]
  4680. public void AsyncStackSpill_assign01()
  4681. {
  4682. var source = @"
  4683. using System;
  4684. using System.Threading;
  4685. using System.Threading.Tasks;
  4686. struct TestCase
  4687. {
  4688. private int val;
  4689. public async Task<T> GetVal<T>(T t)
  4690. {
  4691. await Task.Delay(1);
  4692. return t;
  4693. }
  4694. public async void Run()
  4695. {
  4696. int tests = 0;
  4697. try
  4698. {
  4699. tests++;
  4700. int[] x = new int[] { 1, 2, 3, 4 };
  4701. val = x[await GetVal(0)] += await GetVal(4);
  4702. if (x[0] == 5 && val == await GetVal(5))
  4703. Driver.Count++;
  4704. }
  4705. finally
  4706. {
  4707. Driver.Result = Driver.Count - tests;
  4708. //When test complete, set the flag.
  4709. Driver.CompletedSignal.Set();
  4710. }
  4711. }
  4712. }
  4713. class Driver
  4714. {
  4715. public static int Result = -1;
  4716. public static int Count = 0;
  4717. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4718. static void Main()
  4719. {
  4720. var t = new TestCase();
  4721. t.Run();
  4722. CompletedSignal.WaitOne();
  4723. // 0 - success
  4724. // 1 - failed (test completed)
  4725. // -1 - failed (test incomplete - deadlock, etc)
  4726. Console.WriteLine(Driver.Result);
  4727. }
  4728. }";
  4729. CompileAndVerify(source, "0");
  4730. }
  4731. [Fact]
  4732. public void Conformance_Exceptions_Async_Await_Names()
  4733. {
  4734. var source = @"
  4735. using System;
  4736. class TestCase
  4737. {
  4738. public void Run()
  4739. {
  4740. Driver.Tests++;
  4741. try
  4742. {
  4743. throw new ArgumentException();
  4744. }
  4745. catch (Exception await)
  4746. {
  4747. if (await is ArgumentException)
  4748. Driver.Count++;
  4749. }
  4750. Driver.Tests++;
  4751. try
  4752. {
  4753. throw new ArgumentException();
  4754. }
  4755. catch (Exception async)
  4756. {
  4757. if (async is ArgumentException)
  4758. Driver.Count++;
  4759. }
  4760. }
  4761. }
  4762. class Driver
  4763. {
  4764. public static int Tests;
  4765. public static int Count;
  4766. static void Main()
  4767. {
  4768. TestCase t = new TestCase();
  4769. t.Run();
  4770. Console.WriteLine(Tests - Count);
  4771. }
  4772. }";
  4773. CompileAndVerify(source, "0");
  4774. }
  4775. [Fact]
  4776. public void MyTask_08()
  4777. {
  4778. var source = @"
  4779. using System;
  4780. using System.Threading;
  4781. using System.Threading.Tasks;
  4782. //Implementation of you own async pattern
  4783. public class MyTask
  4784. {
  4785. public async void Run()
  4786. {
  4787. int tests = 0;
  4788. try
  4789. {
  4790. tests++;
  4791. var myTask = new MyTask();
  4792. var x = await myTask;
  4793. if (x == 123) Driver.Count++;
  4794. }
  4795. finally
  4796. {
  4797. Driver.Result = Driver.Count - tests;
  4798. //When test complete, set the flag.
  4799. Driver.CompletedSignal.Set();
  4800. }
  4801. }
  4802. }
  4803. public class MyTaskAwaiter : System.Runtime.CompilerServices.INotifyCompletion
  4804. {
  4805. public void OnCompleted(Action continuationAction)
  4806. {
  4807. }
  4808. public int GetResult()
  4809. {
  4810. return 123;
  4811. }
  4812. public bool IsCompleted { get { return true; } }
  4813. }
  4814. public static class Extension
  4815. {
  4816. public static MyTaskAwaiter GetAwaiter(this MyTask my)
  4817. {
  4818. return new MyTaskAwaiter();
  4819. }
  4820. }
  4821. //-------------------------------------
  4822. class Driver
  4823. {
  4824. public static int Result = -1;
  4825. public static int Count = 0;
  4826. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4827. static void Main()
  4828. {
  4829. new MyTask().Run();
  4830. CompletedSignal.WaitOne();
  4831. // 0 - success
  4832. // 1 - failed (test completed)
  4833. // -1 - failed (test incomplete - deadlock, etc)
  4834. Console.WriteLine(Driver.Result);
  4835. }
  4836. }";
  4837. CompileAndVerify(source, "0");
  4838. }
  4839. [Fact]
  4840. public void MyTask_16()
  4841. {
  4842. var source = @"
  4843. using System;
  4844. using System.Threading;
  4845. using System.Threading.Tasks;
  4846. //Implementation of you own async pattern
  4847. public class MyTask
  4848. {
  4849. public MyTaskAwaiter GetAwaiter()
  4850. {
  4851. return new MyTaskAwaiter();
  4852. }
  4853. public async void Run()
  4854. {
  4855. int tests = 0;
  4856. try
  4857. {
  4858. tests++;
  4859. var myTask = new MyTask();
  4860. var x = await myTask;
  4861. if (x == 123) Driver.Count++;
  4862. }
  4863. finally
  4864. {
  4865. Driver.Result = Driver.Count - tests;
  4866. //When test complete, set the flag.
  4867. Driver.CompletedSignal.Set();
  4868. }
  4869. }
  4870. }
  4871. public class MyTaskBaseAwaiter : System.Runtime.CompilerServices.INotifyCompletion
  4872. {
  4873. public void OnCompleted(Action continuationAction)
  4874. {
  4875. }
  4876. public int GetResult()
  4877. {
  4878. return 123;
  4879. }
  4880. public bool IsCompleted { get { return true; } }
  4881. }
  4882. public class MyTaskAwaiter : MyTaskBaseAwaiter
  4883. {
  4884. }
  4885. //-------------------------------------
  4886. class Driver
  4887. {
  4888. public static int Result = -1;
  4889. public static int Count = 0;
  4890. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4891. static void Main()
  4892. {
  4893. new MyTask().Run();
  4894. CompletedSignal.WaitOne();
  4895. // 0 - success
  4896. // 1 - failed (test completed)
  4897. // -1 - failed (test incomplete - deadlock, etc)
  4898. Console.WriteLine(Driver.Result);
  4899. }
  4900. }";
  4901. CompileAndVerify(source, "0");
  4902. }
  4903. [Fact]
  4904. public void InitCollection_04()
  4905. {
  4906. var source = @"
  4907. using System;
  4908. using System.Collections;
  4909. using System.Collections.Generic;
  4910. using System.Threading;
  4911. using System.Threading.Tasks;
  4912. struct PrivateCollection : IEnumerable
  4913. {
  4914. public List<int> lst; //public so we can check the values
  4915. public void Add(int x)
  4916. {
  4917. if (lst == null)
  4918. lst = new List<int>();
  4919. lst.Add(x);
  4920. }
  4921. public IEnumerator GetEnumerator()
  4922. {
  4923. return lst as IEnumerator;
  4924. }
  4925. }
  4926. class TestCase
  4927. {
  4928. public async Task<T> GetValue<T>(T x)
  4929. {
  4930. await Task.Delay(1);
  4931. return x;
  4932. }
  4933. public async void Run()
  4934. {
  4935. int tests = 0;
  4936. try
  4937. {
  4938. tests++;
  4939. var myCol = new PrivateCollection() {
  4940. await GetValue(1),
  4941. await GetValue(2)
  4942. };
  4943. if (myCol.lst[0] == 1 && myCol.lst[1] == 2)
  4944. Driver.Count++;
  4945. }
  4946. finally
  4947. {
  4948. Driver.Result = Driver.Count - tests;
  4949. //When test completes, set the flag.
  4950. Driver.CompletedSignal.Set();
  4951. }
  4952. }
  4953. public int Foo { get; set; }
  4954. }
  4955. class Driver
  4956. {
  4957. public static int Result = -1;
  4958. public static int Count = 0;
  4959. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  4960. static void Main()
  4961. {
  4962. var t = new TestCase();
  4963. t.Run();
  4964. CompletedSignal.WaitOne();
  4965. // 0 - success
  4966. // 1 - failed (test completed)
  4967. // -1 - failed (test incomplete - deadlock, etc)
  4968. Console.WriteLine(Driver.Result);
  4969. }
  4970. }";
  4971. CompileAndVerify(source, "0");
  4972. }
  4973. [Fact]
  4974. public void Return07_2()
  4975. {
  4976. var source = @"
  4977. using System;
  4978. using System.Threading;
  4979. using System.Threading.Tasks;
  4980. class TestCase
  4981. {
  4982. unsafe struct S
  4983. {
  4984. public int value;
  4985. public S* next;
  4986. }
  4987. public async void Run()
  4988. {
  4989. int test = 0;
  4990. int result = 0;
  4991. Func<Task<dynamic>> func, func2 = null;
  4992. try
  4993. {
  4994. test++;
  4995. S s = new S();
  4996. S s1 = new S();
  4997. unsafe
  4998. {
  4999. S* head = &s;
  5000. s.next = &s1;
  5001. func = async () => { (*(head->next)).value = 1; result++; return head->next->value; };
  5002. func2 = async () => (*(head->next));
  5003. }
  5004. var x = await func();
  5005. if (x != 1)
  5006. result--;
  5007. var xx = await func2();
  5008. if (xx.value != 1)
  5009. result--;
  5010. }
  5011. finally
  5012. {
  5013. Driver.Result = test - result;
  5014. Driver.CompleteSignal.Set();
  5015. }
  5016. }
  5017. }
  5018. class Driver
  5019. {
  5020. static public AutoResetEvent CompleteSignal = new AutoResetEvent(false);
  5021. public static int Result = -1;
  5022. public static void Main()
  5023. {
  5024. TestCase tc = new TestCase();
  5025. tc.Run();
  5026. CompleteSignal.WaitOne();
  5027. Console.WriteLine(Result);
  5028. }
  5029. }";
  5030. var compOptions= new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true);
  5031. CompileAndVerify(source, "0", compOptions: compOptions);
  5032. }
  5033. [Fact]
  5034. [WorkItem(625282, "DevDiv")]
  5035. public void Generic05()
  5036. {
  5037. var source = @"
  5038. using System;
  5039. using System.Collections.Generic;
  5040. using System.Threading;
  5041. using System.Threading.Tasks;
  5042. class TestCase
  5043. {
  5044. public T Foo<T>(T x, T y, int z)
  5045. {
  5046. return x;
  5047. }
  5048. public T GetVal<T>(T t)
  5049. {
  5050. return t;
  5051. }
  5052. public IEnumerable<T> Run<T>(T t)
  5053. {
  5054. dynamic d = GetVal(t);
  5055. yield return Foo(t, d, 3);
  5056. }
  5057. }
  5058. class Driver
  5059. {
  5060. static void Main()
  5061. {
  5062. var t = new TestCase();
  5063. t.Run(6);
  5064. }
  5065. }";
  5066. CompileAndVerify(source, new[] { CSharpRef, SystemCoreRef });
  5067. }
  5068. [Fact]
  5069. public void RefExpr()
  5070. {
  5071. var source = @"
  5072. using System;
  5073. using System.Threading.Tasks;
  5074. class MyClass
  5075. {
  5076. public int Field;
  5077. }
  5078. class TestCase
  5079. {
  5080. public static int Foo(ref int x, int y)
  5081. {
  5082. return x + y;
  5083. }
  5084. public async Task<int> Run()
  5085. {
  5086. return Foo(
  5087. ref (new MyClass() { Field = 21 }.Field),
  5088. await Task.Factory.StartNew(() => 21));
  5089. }
  5090. }
  5091. static class Driver
  5092. {
  5093. static void Main()
  5094. {
  5095. var t = new TestCase().Run();
  5096. t.Wait();
  5097. Console.WriteLine(t.Result);
  5098. }
  5099. }";
  5100. CompileAndVerify(source, "42");
  5101. }
  5102. [Fact]
  5103. public void ManagedPointerSpillAssign03()
  5104. {
  5105. var source = @"
  5106. using System;
  5107. using System.Threading;
  5108. using System.Threading.Tasks;
  5109. class TestCase
  5110. {
  5111. public async Task<T> GetVal<T>(T t)
  5112. {
  5113. await Task.Delay(1);
  5114. return t;
  5115. }
  5116. class PrivClass
  5117. {
  5118. internal struct ValueT
  5119. {
  5120. public int Field;
  5121. }
  5122. internal ValueT[] arr = new ValueT[3];
  5123. }
  5124. private PrivClass myClass;
  5125. public async void Run()
  5126. {
  5127. int tests = 0;
  5128. this.myClass = new PrivClass();
  5129. try
  5130. {
  5131. tests++;
  5132. this.myClass.arr[0].Field = await GetVal(4);
  5133. if (myClass.arr[0].Field == 4)
  5134. Driver.Count++;
  5135. tests++;
  5136. this.myClass.arr[0].Field += await GetVal(4);
  5137. if (myClass.arr[0].Field == 8)
  5138. Driver.Count++;
  5139. tests++;
  5140. this.myClass.arr[await GetVal(1)].Field += await GetVal(4);
  5141. if (myClass.arr[1].Field == 4)
  5142. Driver.Count++;
  5143. tests++;
  5144. this.myClass.arr[await GetVal(1)].Field++;
  5145. if (myClass.arr[1].Field == 5)
  5146. Driver.Count++;
  5147. }
  5148. finally
  5149. {
  5150. Driver.Result = Driver.Count - tests;
  5151. //When test complete, set the flag.
  5152. Driver.CompletedSignal.Set();
  5153. }
  5154. }
  5155. }
  5156. class Driver
  5157. {
  5158. public static int Result = -1;
  5159. public static int Count = 0;
  5160. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  5161. static void Main()
  5162. {
  5163. var t = new TestCase();
  5164. t.Run();
  5165. CompletedSignal.WaitOne();
  5166. // 0 - success
  5167. // 1 - failed (test completed)
  5168. // -1 - failed (test incomplete - deadlock, etc)
  5169. Console.WriteLine(Driver.Result);
  5170. }
  5171. }";
  5172. CompileAndVerify(source, "0");
  5173. }
  5174. [Fact]
  5175. public void SacrificialRead()
  5176. {
  5177. var source = @"
  5178. using System;
  5179. using System.Threading.Tasks;
  5180. class C
  5181. {
  5182. static void F1(ref int x, int y, int z)
  5183. {
  5184. x += y + z;
  5185. }
  5186. static int F0()
  5187. {
  5188. Console.WriteLine(-1);
  5189. return 0;
  5190. }
  5191. static async Task<int> F2()
  5192. {
  5193. int[] x = new int[1] { 21 };
  5194. x = null;
  5195. F1(ref x[0], F0(), await Task.Factory.StartNew(() => 21));
  5196. return x[0];
  5197. }
  5198. public static void Main()
  5199. {
  5200. var t = F2();
  5201. try
  5202. {
  5203. t.Wait();
  5204. }
  5205. catch(Exception e)
  5206. {
  5207. Console.WriteLine(0);
  5208. return;
  5209. }
  5210. Console.WriteLine(-1);
  5211. }
  5212. }";
  5213. CompileAndVerify(source, "0");
  5214. }
  5215. [Fact]
  5216. public void RefThisStruct()
  5217. {
  5218. var source = @"
  5219. using System;
  5220. using System.Threading.Tasks;
  5221. struct s1
  5222. {
  5223. public int X;
  5224. public async void Foo1()
  5225. {
  5226. Bar(ref this, await Task<int>.FromResult(42));
  5227. }
  5228. public void Foo2()
  5229. {
  5230. Bar(ref this, 42);
  5231. }
  5232. public void Bar(ref s1 x, int y)
  5233. {
  5234. x.X = 42;
  5235. }
  5236. }
  5237. class c1
  5238. {
  5239. public int X;
  5240. public async void Foo1()
  5241. {
  5242. Bar(this, await Task<int>.FromResult(42));
  5243. }
  5244. public void Foo2()
  5245. {
  5246. Bar(this, 42);
  5247. }
  5248. public void Bar(c1 x, int y)
  5249. {
  5250. x.X = 42;
  5251. }
  5252. }
  5253. class C
  5254. {
  5255. public static void Main()
  5256. {
  5257. {
  5258. s1 s;
  5259. s.X = -1;
  5260. s.Foo1();
  5261. Console.WriteLine(s.X);
  5262. }
  5263. {
  5264. s1 s;
  5265. s.X = -1;
  5266. s.Foo2();
  5267. Console.WriteLine(s.X);
  5268. }
  5269. {
  5270. c1 c = new c1();
  5271. c.X = -1;
  5272. c.Foo1();
  5273. Console.WriteLine(c.X);
  5274. }
  5275. {
  5276. c1 c = new c1();
  5277. c.X = -1;
  5278. c.Foo2();
  5279. Console.WriteLine(c.X);
  5280. }
  5281. }
  5282. }";
  5283. var expected = @"
  5284. -1
  5285. 42
  5286. 42
  5287. 42
  5288. ";
  5289. CompileAndVerify(source, expected);
  5290. }
  5291. [Fact]
  5292. public void AsyncStateMachineIL_Struct_TaskT()
  5293. {
  5294. var source = @"
  5295. using System;
  5296. using System.Threading.Tasks;
  5297. class Test
  5298. {
  5299. public static async Task<int> F()
  5300. {
  5301. return await Task.Factory.StartNew(() => 42);
  5302. }
  5303. public static void Main()
  5304. {
  5305. var t = F();
  5306. t.Wait();
  5307. Console.WriteLine(t.Result);
  5308. }
  5309. }";
  5310. var expected = @"
  5311. 42
  5312. ";
  5313. var c = CompileAndVerify(source, expectedOutput: expected);
  5314. c.VerifyIL("Test.F", @"
  5315. {
  5316. // Code size 49 (0x31)
  5317. .maxstack 2
  5318. .locals init (Test.<F>d__1 V_0,
  5319. System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> V_1)
  5320. IL_0000: ldloca.s V_0
  5321. IL_0002: call ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Create()""
  5322. IL_0007: stfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5323. IL_000c: ldloca.s V_0
  5324. IL_000e: ldc.i4.m1
  5325. IL_000f: stfld ""int Test.<F>d__1.<>1__state""
  5326. IL_0014: ldloc.0
  5327. IL_0015: ldfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5328. IL_001a: stloc.1
  5329. IL_001b: ldloca.s V_1
  5330. IL_001d: ldloca.s V_0
  5331. IL_001f: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Start<Test.<F>d__1>(ref Test.<F>d__1)""
  5332. IL_0024: ldloca.s V_0
  5333. IL_0026: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5334. IL_002b: call ""System.Threading.Tasks.Task<int> System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Task.get""
  5335. IL_0030: ret
  5336. }
  5337. ");
  5338. c.VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext", @"
  5339. {
  5340. // Code size 188 (0xbc)
  5341. .maxstack 3
  5342. .locals init (int V_0,
  5343. int V_1,
  5344. System.Runtime.CompilerServices.TaskAwaiter<int> V_2,
  5345. System.Exception V_3)
  5346. IL_0000: ldarg.0
  5347. IL_0001: ldfld ""int Test.<F>d__1.<>1__state""
  5348. IL_0006: stloc.0
  5349. .try
  5350. {
  5351. IL_0007: ldloc.0
  5352. IL_0008: brfalse.s IL_000e
  5353. IL_000a: ldloc.0
  5354. IL_000b: ldc.i4.1
  5355. IL_000c: beq.s IL_0062
  5356. IL_000e: call ""System.Threading.Tasks.TaskFactory System.Threading.Tasks.Task.Factory.get""
  5357. IL_0013: ldsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5358. IL_0018: dup
  5359. IL_0019: brtrue.s IL_002e
  5360. IL_001b: pop
  5361. IL_001c: ldnull
  5362. IL_001d: ldftn ""int Test.<F>b__0(object)""
  5363. IL_0023: newobj ""System.Func<int>..ctor(object, System.IntPtr)""
  5364. IL_0028: dup
  5365. IL_0029: stsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5366. IL_002e: callvirt ""System.Threading.Tasks.Task<int> System.Threading.Tasks.TaskFactory.StartNew<int>(System.Func<int>)""
  5367. IL_0033: callvirt ""System.Runtime.CompilerServices.TaskAwaiter<int> System.Threading.Tasks.Task<int>.GetAwaiter()""
  5368. IL_0038: stloc.2
  5369. IL_0039: ldloca.s V_2
  5370. IL_003b: call ""bool System.Runtime.CompilerServices.TaskAwaiter<int>.IsCompleted.get""
  5371. IL_0040: brtrue.s IL_007e
  5372. IL_0042: ldarg.0
  5373. IL_0043: ldc.i4.1
  5374. IL_0044: dup
  5375. IL_0045: stloc.0
  5376. IL_0046: stfld ""int Test.<F>d__1.<>1__state""
  5377. IL_004b: ldarg.0
  5378. IL_004c: ldloc.2
  5379. IL_004d: stfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5380. IL_0052: ldarg.0
  5381. IL_0053: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5382. IL_0058: ldloca.s V_2
  5383. IL_005a: ldarg.0
  5384. IL_005b: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Test.<F>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Test.<F>d__1)""
  5385. IL_0060: leave.s IL_00bb
  5386. IL_0062: ldarg.0
  5387. IL_0063: ldfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5388. IL_0068: stloc.2
  5389. IL_0069: ldarg.0
  5390. IL_006a: ldflda ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5391. IL_006f: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5392. IL_0075: ldarg.0
  5393. IL_0076: ldc.i4.m1
  5394. IL_0077: dup
  5395. IL_0078: stloc.0
  5396. IL_0079: stfld ""int Test.<F>d__1.<>1__state""
  5397. IL_007e: ldloca.s V_2
  5398. IL_0080: call ""int System.Runtime.CompilerServices.TaskAwaiter<int>.GetResult()""
  5399. IL_0085: ldloca.s V_2
  5400. IL_0087: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5401. IL_008d: stloc.1
  5402. IL_008e: leave.s IL_00a7
  5403. }
  5404. catch System.Exception
  5405. {
  5406. IL_0090: stloc.3
  5407. IL_0091: ldarg.0
  5408. IL_0092: ldc.i4.s -2
  5409. IL_0094: stfld ""int Test.<F>d__1.<>1__state""
  5410. IL_0099: ldarg.0
  5411. IL_009a: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5412. IL_009f: ldloc.3
  5413. IL_00a0: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetException(System.Exception)""
  5414. IL_00a5: leave.s IL_00bb
  5415. }
  5416. IL_00a7: ldarg.0
  5417. IL_00a8: ldc.i4.s -2
  5418. IL_00aa: stfld ""int Test.<F>d__1.<>1__state""
  5419. IL_00af: ldarg.0
  5420. IL_00b0: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5421. IL_00b5: ldloc.1
  5422. IL_00b6: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetResult(int)""
  5423. IL_00bb: ret
  5424. }
  5425. ");
  5426. c.VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.SetStateMachine", @"
  5427. {
  5428. // Code size 13 (0xd)
  5429. .maxstack 2
  5430. IL_0000: ldarg.0
  5431. IL_0001: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5432. IL_0006: ldarg.1
  5433. IL_0007: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)""
  5434. IL_000c: ret
  5435. }
  5436. ");
  5437. }
  5438. [Fact]
  5439. public void AsyncStateMachineIL_Class_TaskT()
  5440. {
  5441. var source = @"
  5442. using System;
  5443. using System.Threading.Tasks;
  5444. class Test
  5445. {
  5446. public static async Task<int> F()
  5447. {
  5448. return await Task.Factory.StartNew(() => 42);
  5449. }
  5450. public static void Main()
  5451. {
  5452. var t = F();
  5453. t.Wait();
  5454. Console.WriteLine(t.Result);
  5455. }
  5456. }";
  5457. var expected = @"
  5458. 42
  5459. ";
  5460. var c = CompileAndVerify(source, expectedOutput: expected, compOptions: TestOptions.DebugExe, emitPdb: true);
  5461. c.VerifyIL("Test.F", @"
  5462. {
  5463. // Code size 54 (0x36)
  5464. .maxstack 2
  5465. .locals init (Test.<F>d__1 V_0,
  5466. System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> V_1,
  5467. System.Threading.Tasks.Task<int> V_2)
  5468. IL_0000: newobj ""Test.<F>d__1..ctor()""
  5469. IL_0005: stloc.0
  5470. IL_0006: ldloc.0
  5471. IL_0007: call ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Create()""
  5472. IL_000c: stfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5473. IL_0011: ldloc.0
  5474. IL_0012: ldc.i4.m1
  5475. IL_0013: stfld ""int Test.<F>d__1.<>1__state""
  5476. IL_0018: ldloc.0
  5477. IL_0019: ldfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5478. IL_001e: stloc.1
  5479. IL_001f: ldloca.s V_1
  5480. IL_0021: ldloca.s V_0
  5481. IL_0023: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Start<Test.<F>d__1>(ref Test.<F>d__1)""
  5482. IL_0028: ldloc.0
  5483. IL_0029: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5484. IL_002e: call ""System.Threading.Tasks.Task<int> System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Task.get""
  5485. IL_0033: stloc.2
  5486. IL_0034: ldloc.2
  5487. IL_0035: ret
  5488. }
  5489. ");
  5490. c.VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext", @"
  5491. {
  5492. // Code size 213 (0xd5)
  5493. .maxstack 3
  5494. .locals init (int V_0, //CS$524$0000
  5495. int V_1, //CS$523$0001
  5496. int V_2,
  5497. System.Runtime.CompilerServices.TaskAwaiter<int> V_3,
  5498. int V_4,
  5499. Test.<F>d__1 V_5,
  5500. System.Exception V_6)
  5501. ~IL_0000: ldarg.0
  5502. IL_0001: ldfld ""int Test.<F>d__1.<>1__state""
  5503. IL_0006: stloc.0
  5504. .try
  5505. {
  5506. ~IL_0007: ldloc.0
  5507. IL_0008: brfalse.s IL_0012
  5508. IL_000a: br.s IL_000c
  5509. IL_000c: ldloc.0
  5510. IL_000d: ldc.i4.1
  5511. IL_000e: beq.s IL_0014
  5512. IL_0010: br.s IL_0016
  5513. IL_0012: br.s IL_0016
  5514. IL_0014: br.s IL_0070
  5515. -IL_0016: nop
  5516. -IL_0017: call ""System.Threading.Tasks.TaskFactory System.Threading.Tasks.Task.Factory.get""
  5517. IL_001c: ldsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5518. IL_0021: dup
  5519. IL_0022: brtrue.s IL_0037
  5520. IL_0024: pop
  5521. IL_0025: ldnull
  5522. IL_0026: ldftn ""int Test.<F>b__0(object)""
  5523. IL_002c: newobj ""System.Func<int>..ctor(object, System.IntPtr)""
  5524. IL_0031: dup
  5525. IL_0032: stsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5526. IL_0037: callvirt ""System.Threading.Tasks.Task<int> System.Threading.Tasks.TaskFactory.StartNew<int>(System.Func<int>)""
  5527. IL_003c: callvirt ""System.Runtime.CompilerServices.TaskAwaiter<int> System.Threading.Tasks.Task<int>.GetAwaiter()""
  5528. IL_0041: stloc.3
  5529. IL_0042: ldloca.s V_3
  5530. IL_0044: call ""bool System.Runtime.CompilerServices.TaskAwaiter<int>.IsCompleted.get""
  5531. IL_0049: brtrue.s IL_008c
  5532. IL_004b: ldarg.0
  5533. IL_004c: ldc.i4.1
  5534. IL_004d: dup
  5535. IL_004e: stloc.0
  5536. IL_004f: stfld ""int Test.<F>d__1.<>1__state""
  5537. IL_0054: ldarg.0
  5538. IL_0055: ldloc.3
  5539. IL_0056: stfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5540. IL_005b: ldarg.0
  5541. IL_005c: stloc.s V_5
  5542. IL_005e: ldarg.0
  5543. IL_005f: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5544. IL_0064: ldloca.s V_3
  5545. IL_0066: ldloca.s V_5
  5546. IL_0068: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Test.<F>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Test.<F>d__1)""
  5547. IL_006d: nop
  5548. IL_006e: leave.s IL_00d4
  5549. IL_0070: ldarg.0
  5550. IL_0071: ldfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5551. IL_0076: stloc.3
  5552. IL_0077: ldarg.0
  5553. IL_0078: ldflda ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5554. IL_007d: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5555. IL_0083: ldarg.0
  5556. IL_0084: ldc.i4.m1
  5557. IL_0085: dup
  5558. IL_0086: stloc.0
  5559. IL_0087: stfld ""int Test.<F>d__1.<>1__state""
  5560. IL_008c: ldloca.s V_3
  5561. IL_008e: call ""int System.Runtime.CompilerServices.TaskAwaiter<int>.GetResult()""
  5562. IL_0093: stloc.s V_4
  5563. IL_0095: ldloca.s V_3
  5564. IL_0097: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5565. IL_009d: ldloc.s V_4
  5566. IL_009f: stloc.2
  5567. IL_00a0: ldloc.2
  5568. IL_00a1: stloc.1
  5569. IL_00a2: leave.s IL_00bf
  5570. }
  5571. catch System.Exception
  5572. {
  5573. ~IL_00a4: stloc.s V_6
  5574. IL_00a6: nop
  5575. IL_00a7: ldarg.0
  5576. IL_00a8: ldc.i4.s -2
  5577. IL_00aa: stfld ""int Test.<F>d__1.<>1__state""
  5578. IL_00af: ldarg.0
  5579. IL_00b0: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5580. IL_00b5: ldloc.s V_6
  5581. IL_00b7: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetException(System.Exception)""
  5582. IL_00bc: nop
  5583. IL_00bd: leave.s IL_00d4
  5584. }
  5585. -IL_00bf: ldarg.0
  5586. IL_00c0: ldc.i4.s -2
  5587. IL_00c2: stfld ""int Test.<F>d__1.<>1__state""
  5588. ~IL_00c7: ldarg.0
  5589. IL_00c8: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int> Test.<F>d__1.<>t__builder""
  5590. IL_00cd: ldloc.1
  5591. IL_00ce: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.SetResult(int)""
  5592. IL_00d3: nop
  5593. IL_00d4: ret
  5594. }
  5595. ", sequencePoints: "Test+<F>d__1.MoveNext");
  5596. c.VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.SetStateMachine", @"
  5597. {
  5598. // Code size 1 (0x1)
  5599. .maxstack 0
  5600. IL_0000: ret
  5601. }
  5602. ");
  5603. }
  5604. [Fact]
  5605. public void IL_Task()
  5606. {
  5607. var source = @"
  5608. using System;
  5609. using System.Threading.Tasks;
  5610. class Test
  5611. {
  5612. public static async Task F()
  5613. {
  5614. await Task.Factory.StartNew(() => 42);
  5615. Console.WriteLine(42);
  5616. }
  5617. public static void Main()
  5618. {
  5619. var t = F();
  5620. t.Wait();
  5621. }
  5622. }";
  5623. var expected = @"
  5624. 42
  5625. ";
  5626. CompileAndVerify(source, expectedOutput: expected).VerifyIL("Test.F", @"
  5627. {
  5628. // Code size 49 (0x31)
  5629. .maxstack 2
  5630. .locals init (Test.<F>d__1 V_0,
  5631. System.Runtime.CompilerServices.AsyncTaskMethodBuilder V_1)
  5632. IL_0000: ldloca.s V_0
  5633. IL_0002: call ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Create()""
  5634. IL_0007: stfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5635. IL_000c: ldloca.s V_0
  5636. IL_000e: ldc.i4.m1
  5637. IL_000f: stfld ""int Test.<F>d__1.<>1__state""
  5638. IL_0014: ldloc.0
  5639. IL_0015: ldfld ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5640. IL_001a: stloc.1
  5641. IL_001b: ldloca.s V_1
  5642. IL_001d: ldloca.s V_0
  5643. IL_001f: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Test.<F>d__1>(ref Test.<F>d__1)""
  5644. IL_0024: ldloca.s V_0
  5645. IL_0026: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5646. IL_002b: call ""System.Threading.Tasks.Task System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Task.get""
  5647. IL_0030: ret
  5648. }
  5649. ").VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext", @"
  5650. {
  5651. // Code size 194 (0xc2)
  5652. .maxstack 3
  5653. .locals init (int V_0,
  5654. System.Runtime.CompilerServices.TaskAwaiter<int> V_1,
  5655. System.Exception V_2)
  5656. IL_0000: ldarg.0
  5657. IL_0001: ldfld ""int Test.<F>d__1.<>1__state""
  5658. IL_0006: stloc.0
  5659. .try
  5660. {
  5661. IL_0007: ldloc.0
  5662. IL_0008: brfalse.s IL_000e
  5663. IL_000a: ldloc.0
  5664. IL_000b: ldc.i4.1
  5665. IL_000c: beq.s IL_0062
  5666. IL_000e: call ""System.Threading.Tasks.TaskFactory System.Threading.Tasks.Task.Factory.get""
  5667. IL_0013: ldsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5668. IL_0018: dup
  5669. IL_0019: brtrue.s IL_002e
  5670. IL_001b: pop
  5671. IL_001c: ldnull
  5672. IL_001d: ldftn ""int Test.<F>b__0(object)""
  5673. IL_0023: newobj ""System.Func<int>..ctor(object, System.IntPtr)""
  5674. IL_0028: dup
  5675. IL_0029: stsfld ""System.Func<int> Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5676. IL_002e: callvirt ""System.Threading.Tasks.Task<int> System.Threading.Tasks.TaskFactory.StartNew<int>(System.Func<int>)""
  5677. IL_0033: callvirt ""System.Runtime.CompilerServices.TaskAwaiter<int> System.Threading.Tasks.Task<int>.GetAwaiter()""
  5678. IL_0038: stloc.1
  5679. IL_0039: ldloca.s V_1
  5680. IL_003b: call ""bool System.Runtime.CompilerServices.TaskAwaiter<int>.IsCompleted.get""
  5681. IL_0040: brtrue.s IL_007e
  5682. IL_0042: ldarg.0
  5683. IL_0043: ldc.i4.1
  5684. IL_0044: dup
  5685. IL_0045: stloc.0
  5686. IL_0046: stfld ""int Test.<F>d__1.<>1__state""
  5687. IL_004b: ldarg.0
  5688. IL_004c: ldloc.1
  5689. IL_004d: stfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5690. IL_0052: ldarg.0
  5691. IL_0053: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5692. IL_0058: ldloca.s V_1
  5693. IL_005a: ldarg.0
  5694. IL_005b: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Test.<F>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Test.<F>d__1)""
  5695. IL_0060: leave.s IL_00c1
  5696. IL_0062: ldarg.0
  5697. IL_0063: ldfld ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5698. IL_0068: stloc.1
  5699. IL_0069: ldarg.0
  5700. IL_006a: ldflda ""System.Runtime.CompilerServices.TaskAwaiter<int> Test.<F>d__1.<>u__$awaiter2""
  5701. IL_006f: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5702. IL_0075: ldarg.0
  5703. IL_0076: ldc.i4.m1
  5704. IL_0077: dup
  5705. IL_0078: stloc.0
  5706. IL_0079: stfld ""int Test.<F>d__1.<>1__state""
  5707. IL_007e: ldloca.s V_1
  5708. IL_0080: call ""int System.Runtime.CompilerServices.TaskAwaiter<int>.GetResult()""
  5709. IL_0085: pop
  5710. IL_0086: ldloca.s V_1
  5711. IL_0088: initobj ""System.Runtime.CompilerServices.TaskAwaiter<int>""
  5712. IL_008e: ldc.i4.s 42
  5713. IL_0090: call ""void System.Console.WriteLine(int)""
  5714. IL_0095: leave.s IL_00ae
  5715. }
  5716. catch System.Exception
  5717. {
  5718. IL_0097: stloc.2
  5719. IL_0098: ldarg.0
  5720. IL_0099: ldc.i4.s -2
  5721. IL_009b: stfld ""int Test.<F>d__1.<>1__state""
  5722. IL_00a0: ldarg.0
  5723. IL_00a1: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5724. IL_00a6: ldloc.2
  5725. IL_00a7: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetException(System.Exception)""
  5726. IL_00ac: leave.s IL_00c1
  5727. }
  5728. IL_00ae: ldarg.0
  5729. IL_00af: ldc.i4.s -2
  5730. IL_00b1: stfld ""int Test.<F>d__1.<>1__state""
  5731. IL_00b6: ldarg.0
  5732. IL_00b7: ldflda ""System.Runtime.CompilerServices.AsyncTaskMethodBuilder Test.<F>d__1.<>t__builder""
  5733. IL_00bc: call ""void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult()""
  5734. IL_00c1: ret
  5735. }
  5736. ");
  5737. }
  5738. [Fact]
  5739. public void IL_Void()
  5740. {
  5741. var source = @"
  5742. using System;
  5743. using System.Threading;
  5744. using System.Threading.Tasks;
  5745. class Test
  5746. {
  5747. static int i = 0;
  5748. public static async void F(AutoResetEvent handle)
  5749. {
  5750. await Task.Factory.StartNew(() => { Test.i = 42; });
  5751. handle.Set();
  5752. }
  5753. public static void Main()
  5754. {
  5755. var handle = new AutoResetEvent(false);
  5756. F(handle);
  5757. handle.WaitOne(1000 * 60);
  5758. Console.WriteLine(i);
  5759. }
  5760. }";
  5761. var expected = @"
  5762. 42
  5763. ";
  5764. CompileAndVerify(source, expectedOutput: expected).VerifyIL("Test.F", @"
  5765. {
  5766. // Code size 45 (0x2d)
  5767. .maxstack 2
  5768. .locals init (Test.<F>d__1 V_0,
  5769. System.Runtime.CompilerServices.AsyncVoidMethodBuilder V_1)
  5770. IL_0000: ldloca.s V_0
  5771. IL_0002: ldarg.0
  5772. IL_0003: stfld ""System.Threading.AutoResetEvent Test.<F>d__1.handle""
  5773. IL_0008: ldloca.s V_0
  5774. IL_000a: call ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Create()""
  5775. IL_000f: stfld ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder Test.<F>d__1.<>t__builder""
  5776. IL_0014: ldloca.s V_0
  5777. IL_0016: ldc.i4.m1
  5778. IL_0017: stfld ""int Test.<F>d__1.<>1__state""
  5779. IL_001c: ldloc.0
  5780. IL_001d: ldfld ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder Test.<F>d__1.<>t__builder""
  5781. IL_0022: stloc.1
  5782. IL_0023: ldloca.s V_1
  5783. IL_0025: ldloca.s V_0
  5784. IL_0027: call ""void System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start<Test.<F>d__1>(ref Test.<F>d__1)""
  5785. IL_002c: ret
  5786. }
  5787. ").VerifyIL("Test.<F>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext", @"
  5788. {
  5789. // Code size 198 (0xc6)
  5790. .maxstack 3
  5791. .locals init (int V_0,
  5792. System.Runtime.CompilerServices.TaskAwaiter V_1,
  5793. System.Exception V_2)
  5794. IL_0000: ldarg.0
  5795. IL_0001: ldfld ""int Test.<F>d__1.<>1__state""
  5796. IL_0006: stloc.0
  5797. .try
  5798. {
  5799. IL_0007: ldloc.0
  5800. IL_0008: brfalse.s IL_000e
  5801. IL_000a: ldloc.0
  5802. IL_000b: ldc.i4.1
  5803. IL_000c: beq.s IL_0062
  5804. IL_000e: call ""System.Threading.Tasks.TaskFactory System.Threading.Tasks.Task.Factory.get""
  5805. IL_0013: ldsfld ""System.Action Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5806. IL_0018: dup
  5807. IL_0019: brtrue.s IL_002e
  5808. IL_001b: pop
  5809. IL_001c: ldnull
  5810. IL_001d: ldftn ""void Test.<F>b__0(object)""
  5811. IL_0023: newobj ""System.Action..ctor(object, System.IntPtr)""
  5812. IL_0028: dup
  5813. IL_0029: stsfld ""System.Action Test.CS$<>9__CachedAnonymousMethodDelegate1""
  5814. IL_002e: callvirt ""System.Threading.Tasks.Task System.Threading.Tasks.TaskFactory.StartNew(System.Action)""
  5815. IL_0033: callvirt ""System.Runtime.CompilerServices.TaskAwaiter System.Threading.Tasks.Task.GetAwaiter()""
  5816. IL_0038: stloc.1
  5817. IL_0039: ldloca.s V_1
  5818. IL_003b: call ""bool System.Runtime.CompilerServices.TaskAwaiter.IsCompleted.get""
  5819. IL_0040: brtrue.s IL_007e
  5820. IL_0042: ldarg.0
  5821. IL_0043: ldc.i4.1
  5822. IL_0044: dup
  5823. IL_0045: stloc.0
  5824. IL_0046: stfld ""int Test.<F>d__1.<>1__state""
  5825. IL_004b: ldarg.0
  5826. IL_004c: ldloc.1
  5827. IL_004d: stfld ""System.Runtime.CompilerServices.TaskAwaiter Test.<F>d__1.<>u__$awaiter2""
  5828. IL_0052: ldarg.0
  5829. IL_0053: ldflda ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder Test.<F>d__1.<>t__builder""
  5830. IL_0058: ldloca.s V_1
  5831. IL_005a: ldarg.0
  5832. IL_005b: call ""void System.Runtime.CompilerServices.AsyncVoidMethodBuilder.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter, Test.<F>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter, ref Test.<F>d__1)""
  5833. IL_0060: leave.s IL_00c5
  5834. IL_0062: ldarg.0
  5835. IL_0063: ldfld ""System.Runtime.CompilerServices.TaskAwaiter Test.<F>d__1.<>u__$awaiter2""
  5836. IL_0068: stloc.1
  5837. IL_0069: ldarg.0
  5838. IL_006a: ldflda ""System.Runtime.CompilerServices.TaskAwaiter Test.<F>d__1.<>u__$awaiter2""
  5839. IL_006f: initobj ""System.Runtime.CompilerServices.TaskAwaiter""
  5840. IL_0075: ldarg.0
  5841. IL_0076: ldc.i4.m1
  5842. IL_0077: dup
  5843. IL_0078: stloc.0
  5844. IL_0079: stfld ""int Test.<F>d__1.<>1__state""
  5845. IL_007e: ldloca.s V_1
  5846. IL_0080: call ""void System.Runtime.CompilerServices.TaskAwaiter.GetResult()""
  5847. IL_0085: ldloca.s V_1
  5848. IL_0087: initobj ""System.Runtime.CompilerServices.TaskAwaiter""
  5849. IL_008d: ldarg.0
  5850. IL_008e: ldfld ""System.Threading.AutoResetEvent Test.<F>d__1.handle""
  5851. IL_0093: callvirt ""bool System.Threading.EventWaitHandle.Set()""
  5852. IL_0098: pop
  5853. IL_0099: leave.s IL_00b2
  5854. }
  5855. catch System.Exception
  5856. {
  5857. IL_009b: stloc.2
  5858. IL_009c: ldarg.0
  5859. IL_009d: ldc.i4.s -2
  5860. IL_009f: stfld ""int Test.<F>d__1.<>1__state""
  5861. IL_00a4: ldarg.0
  5862. IL_00a5: ldflda ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder Test.<F>d__1.<>t__builder""
  5863. IL_00aa: ldloc.2
  5864. IL_00ab: call ""void System.Runtime.CompilerServices.AsyncVoidMethodBuilder.SetException(System.Exception)""
  5865. IL_00b0: leave.s IL_00c5
  5866. }
  5867. IL_00b2: ldarg.0
  5868. IL_00b3: ldc.i4.s -2
  5869. IL_00b5: stfld ""int Test.<F>d__1.<>1__state""
  5870. IL_00ba: ldarg.0
  5871. IL_00bb: ldflda ""System.Runtime.CompilerServices.AsyncVoidMethodBuilder Test.<F>d__1.<>t__builder""
  5872. IL_00c0: call ""void System.Runtime.CompilerServices.AsyncVoidMethodBuilder.SetResult()""
  5873. IL_00c5: ret
  5874. }
  5875. ");
  5876. }
  5877. [Fact]
  5878. [WorkItem(564036, "DevDiv")]
  5879. public void InferFromAsyncLambda()
  5880. {
  5881. var source =
  5882. @"using System;
  5883. using System.Threading.Tasks;
  5884. class Program
  5885. {
  5886. public static T CallWithCatch<T>(Func<T> func)
  5887. {
  5888. Console.WriteLine(typeof(T).ToString());
  5889. return func();
  5890. }
  5891. private static async Task LoadTestDataAsync()
  5892. {
  5893. await CallWithCatch(async () => await LoadTestData());
  5894. }
  5895. private static async Task LoadTestData()
  5896. {
  5897. await Task.Run(() => { });
  5898. }
  5899. public static void Main(string[] args)
  5900. {
  5901. Task t = LoadTestDataAsync();
  5902. t.Wait(1000);
  5903. }
  5904. }";
  5905. var expected = @"System.Threading.Tasks.Task";
  5906. CompileAndVerify(source, expectedOutput: expected);
  5907. }
  5908. [Fact]
  5909. [WorkItem(620987, "DevDiv")]
  5910. public void PrematureNull()
  5911. {
  5912. var source =
  5913. @"using System;
  5914. using System.Collections.Generic;
  5915. using System.Diagnostics;
  5916. using System.Linq;
  5917. using System.Text;
  5918. using System.Threading;
  5919. using System.Threading.Tasks;
  5920. class Program
  5921. {
  5922. public static void Main(string[] args)
  5923. {
  5924. try
  5925. {
  5926. var ar = FindReferencesInDocumentAsync(""Document"");
  5927. ar.Wait(1000 * 60);
  5928. Console.WriteLine(ar.Result);
  5929. }
  5930. catch (Exception ex)
  5931. {
  5932. Console.WriteLine(ex);
  5933. }
  5934. }
  5935. internal static async Task<string> GetTokensWithIdentifierAsync()
  5936. {
  5937. Console.WriteLine(""in GetTokensWithIdentifierAsync"");
  5938. return ""GetTokensWithIdentifierAsync"";
  5939. }
  5940. protected static async Task<string> FindReferencesInTokensAsync(
  5941. string document,
  5942. string tokens)
  5943. {
  5944. Console.WriteLine(""in FindReferencesInTokensAsync"");
  5945. if (tokens == null) throw new NullReferenceException(""tokens"");
  5946. Console.WriteLine(""tokens were fine"");
  5947. if (document == null) throw new NullReferenceException(""document"");
  5948. Console.WriteLine(""document was fine"");
  5949. return ""FindReferencesInTokensAsync"";
  5950. }
  5951. public static async Task<string> FindReferencesInDocumentAsync(
  5952. string document)
  5953. {
  5954. Console.WriteLine(""in FindReferencesInDocumentAsync"");
  5955. if (document == null) throw new NullReferenceException(""document"");
  5956. var nonAliasReferences = await FindReferencesInTokensAsync(
  5957. document,
  5958. await GetTokensWithIdentifierAsync()
  5959. ).ConfigureAwait(true);
  5960. return ""done!"";
  5961. }
  5962. }";
  5963. var expected =
  5964. @"in FindReferencesInDocumentAsync
  5965. in GetTokensWithIdentifierAsync
  5966. in FindReferencesInTokensAsync
  5967. tokens were fine
  5968. document was fine
  5969. done!";
  5970. CompileAndVerify(source, expectedOutput: expected);
  5971. }
  5972. [Fact]
  5973. [WorkItem(621705, "DevDiv")]
  5974. public void GenericAsyncLambda()
  5975. {
  5976. var source =
  5977. @"using System;
  5978. using System.Diagnostics;
  5979. using System.Threading;
  5980. using System.Threading.Tasks;
  5981. class G<T>
  5982. {
  5983. T t;
  5984. public G(T t, Func<T, Task<T>> action)
  5985. {
  5986. var tt = action(t);
  5987. var completed = tt.Wait(1000 * 60);
  5988. Debug.Assert(completed);
  5989. this.t = tt.Result;
  5990. }
  5991. public override string ToString()
  5992. {
  5993. return t.ToString();
  5994. }
  5995. }
  5996. class Test
  5997. {
  5998. static G<U> M<U>(U t)
  5999. {
  6000. return new G<U>(t, async x =>
  6001. {
  6002. return await IdentityAsync(x);
  6003. }
  6004. );
  6005. }
  6006. static async Task<V> IdentityAsync<V>(V x)
  6007. {
  6008. await Task.Delay(1);
  6009. return x;
  6010. }
  6011. public static void Main()
  6012. {
  6013. var g = M(12);
  6014. Console.WriteLine(g);
  6015. }
  6016. }";
  6017. var expected =
  6018. @"12";
  6019. CompileAndVerify(source, expectedOutput: expected);
  6020. }
  6021. [Fact]
  6022. [WorkItem(602028, "DevDiv")]
  6023. public void BetterConversionFromAsyncLambda()
  6024. {
  6025. var source =
  6026. @"using System.Threading;
  6027. using System.Threading.Tasks;
  6028. using System;
  6029. class TestCase
  6030. {
  6031. public static int Foo(Func<Task<double>> f) { return 12; }
  6032. public static int Foo(Func<Task<object>> f) { return 13; }
  6033. public static void Main()
  6034. {
  6035. Console.WriteLine(Foo(async delegate() { return 14; }));
  6036. }
  6037. }
  6038. ";
  6039. var expected =
  6040. @"12";
  6041. CompileAndVerify(source, expectedOutput: expected);
  6042. }
  6043. [Fact]
  6044. [WorkItem(602206, "DevDiv")]
  6045. public void ExtensionAddMethod()
  6046. {
  6047. var source =
  6048. @"using System;
  6049. using System.Collections.Generic;
  6050. using System.Threading;
  6051. using System.Threading.Tasks;
  6052. static public class Extension
  6053. {
  6054. static public void Add<T>(this Stack<T> stack, T item)
  6055. {
  6056. Console.WriteLine(""Add "" + item.ToString());
  6057. stack.Push(item);
  6058. }
  6059. }
  6060. class TestCase
  6061. {
  6062. AutoResetEvent handle = new AutoResetEvent(false);
  6063. private async Task<T> GetVal<T>(T x)
  6064. {
  6065. await Task.Delay(1);
  6066. Console.WriteLine(""GetVal "" + x.ToString());
  6067. return x;
  6068. }
  6069. public async void Run()
  6070. {
  6071. try
  6072. {
  6073. Stack<int> stack = new Stack<int>() { await GetVal(1), 2, 3 }; // CS0117
  6074. }
  6075. finally
  6076. {
  6077. handle.Set();
  6078. }
  6079. }
  6080. public static void Main(string[] args)
  6081. {
  6082. var tc = new TestCase();
  6083. tc.Run();
  6084. tc.handle.WaitOne(1000 * 60);
  6085. }
  6086. }";
  6087. var expected =
  6088. @"GetVal 1
  6089. Add 1
  6090. Add 2
  6091. Add 3";
  6092. CompileAndVerify(source, expectedOutput: expected);
  6093. }
  6094. [Fact]
  6095. [WorkItem(748527, "DevDiv")]
  6096. public void Bug748527()
  6097. {
  6098. var source = @"using System.Threading.Tasks;
  6099. using System;
  6100. namespace A
  6101. {
  6102. public struct TestClass
  6103. {
  6104. async public System.Threading.Tasks.Task<int> IntRet(int IntI)
  6105. {
  6106. return await ((Func<Task<int>>)(async ()=> { await Task.Yield(); return IntI ; } ))() ;
  6107. }
  6108. }
  6109. public class B
  6110. {
  6111. async public static System.Threading.Tasks.Task<int> MainMethod()
  6112. {
  6113. int MyRet = 0;
  6114. TestClass TC = new TestClass();
  6115. if (( await ((Func<Task<int>>)(async ()=> { await Task.Yield(); return (await(new TestClass().IntRet( await ((Func<Task<int>>)(async ()=> { await Task.Yield(); return 3 ; } ))() ))) ; } ))() ) != await ((Func<Task<int>>)(async ()=> { await Task.Yield(); return 3 ; } ))() )
  6116. {
  6117. MyRet = 1;
  6118. }
  6119. return await ((Func<Task<int>>)(async ()=> {await Task.Yield(); return MyRet;}))();
  6120. }
  6121. static void Main ()
  6122. {
  6123. MainMethod();
  6124. return;
  6125. }
  6126. }
  6127. }";
  6128. var expectedOutput = "";
  6129. CompileAndVerify(source, expectedOutput: expectedOutput);
  6130. }
  6131. [Fact]
  6132. [WorkItem(602216, "DevDiv")]
  6133. public void AsyncMethodOnlyWritesToEnclosingStruct()
  6134. {
  6135. var source =
  6136. @"public struct GenC<T> where T : struct
  6137. {
  6138. public T? valueN;
  6139. public async void Test(T t)
  6140. {
  6141. valueN = t;
  6142. }
  6143. }
  6144. public class Test
  6145. {
  6146. public static void Main()
  6147. {
  6148. int test = 12;
  6149. GenC<int> _int = new GenC<int>();
  6150. _int.Test(test);
  6151. System.Console.WriteLine(_int.valueN ?? 1);
  6152. }
  6153. }";
  6154. var expected =
  6155. @"1";
  6156. CompileAndVerify(source, expectedOutput: expected);
  6157. }
  6158. [Fact]
  6159. [WorkItem(602246, "DevDiv")]
  6160. public void Bug602246()
  6161. {
  6162. var source =
  6163. @"using System;
  6164. using System.Threading.Tasks;
  6165. public class TestCase
  6166. {
  6167. public static async Task<T> Run<T>(T t)
  6168. {
  6169. await Task.Delay(1);
  6170. Func<Func<Task<T>>, Task<T>> f = async (x) => { return await x(); };
  6171. var rez = await f(async () => { await Task.Delay(1); return t; });
  6172. return rez;
  6173. }
  6174. public static void Main()
  6175. {
  6176. var t = TestCase.Run<int>(12);
  6177. if (!t.Wait(1000 * 60)) throw new Exception();
  6178. Console.Write(t.Result);
  6179. }
  6180. }";
  6181. var expected =
  6182. @"12";
  6183. CompileAndVerify(source, expectedOutput: expected);
  6184. }
  6185. [WorkItem(628654, "DevDiv")]
  6186. [Fact]
  6187. public void AsyncWithDynamic01()
  6188. {
  6189. var source = @"
  6190. using System;
  6191. using System.Threading.Tasks;
  6192. class Program
  6193. {
  6194. static void Main()
  6195. {
  6196. Foo<int>().Wait();
  6197. }
  6198. static async Task Foo<T>()
  6199. {
  6200. Console.WriteLine(""{0}"" as dynamic, await Task.FromResult(new T[] { }));
  6201. }
  6202. }";
  6203. var expected = @"
  6204. System.Int32[]
  6205. ";
  6206. CompileAndVerify(source, expectedOutput: expected);
  6207. }
  6208. [WorkItem(640282, "DevDiv")]
  6209. [Fact]
  6210. public void CustomAsyncWithDynamic01()
  6211. {
  6212. var source = @"
  6213. using System;
  6214. using System.Threading;
  6215. using System.Threading.Tasks;
  6216. class MyTask
  6217. {
  6218. public dynamic GetAwaiter()
  6219. {
  6220. return new MyTaskAwaiter<Action>();
  6221. }
  6222. public async void Run<T>()
  6223. {
  6224. int tests = 0;
  6225. tests++;
  6226. dynamic myTask = new MyTask();
  6227. var x = await myTask;
  6228. if (x == 123) Driver.Count++;
  6229. Driver.Result = Driver.Count - tests;
  6230. //When test complete, set the flag.
  6231. Driver.CompletedSignal.Set();
  6232. }
  6233. }
  6234. class MyTaskAwaiter<U>
  6235. {
  6236. public void OnCompleted(U continuationAction)
  6237. {
  6238. }
  6239. public int GetResult()
  6240. {
  6241. return 123;
  6242. }
  6243. public dynamic IsCompleted { get { return true; } }
  6244. }
  6245. class Driver
  6246. {
  6247. public static int Result = -1;
  6248. public static int Count = 0;
  6249. public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
  6250. public static void Main()
  6251. {
  6252. new MyTask().Run<int>();
  6253. CompletedSignal.WaitOne();
  6254. // 0 - success
  6255. // 1 - failed (test completed)
  6256. // -1 - failed (test incomplete - deadlock, etc)
  6257. Console.WriteLine(Driver.Result);
  6258. }
  6259. }";
  6260. var expected = @"0";
  6261. CompileAndVerify(source, expectedOutput: expected);
  6262. }
  6263. [WorkItem(840843, "DevDiv")]
  6264. [Fact]
  6265. public void MissingAsyncMethodBuilder()
  6266. {
  6267. var source = @"
  6268. class C
  6269. {
  6270. async void M() {}
  6271. }
  6272. ";
  6273. var comp = CSharpTestBaseBase.CreateCompilation(source, new[] { MscorlibRef }, OptionsDll); // NOTE: 4.0, not 4.5, so it's missing the async helpers.
  6274. // CONSIDER: It would be nice if we didn't squiggle the whole method body, but this is a corner case.
  6275. comp.VerifyEmitDiagnostics(
  6276. // (4,16): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
  6277. Diagnostic(ErrorCode.WRN_AsyncLacksAwaits, "M"),
  6278. // (5,5): error CS0518: Predefined type 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder' is not defined or imported
  6279. Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, @"{}").WithArguments("System.Runtime.CompilerServices.AsyncVoidMethodBuilder"),
  6280. // (5,5): error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder.SetException'
  6281. Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"{}").WithArguments("System.Runtime.CompilerServices.AsyncVoidMethodBuilder", "SetException"));
  6282. }
  6283. [Fact]
  6284. [WorkItem(868822, "DevDiv")]
  6285. public void AsyncDelegates()
  6286. {
  6287. var source =
  6288. @"using System;
  6289. using System.Threading.Tasks;
  6290. class Program
  6291. {
  6292. static void Main(string[] args)
  6293. {
  6294. test1();
  6295. test2();
  6296. }
  6297. static void test1()
  6298. {
  6299. Invoke(async delegate
  6300. {
  6301. if (0.ToString().Length == 0)
  6302. {
  6303. await Task.Yield();
  6304. }
  6305. else
  6306. {
  6307. System.Console.WriteLine(0.ToString());
  6308. }
  6309. });
  6310. }
  6311. static string test2()
  6312. {
  6313. return Invoke(async delegate
  6314. {
  6315. if (0.ToString().Length == 0)
  6316. {
  6317. await Task.Yield();
  6318. return 1.ToString();
  6319. }
  6320. else
  6321. {
  6322. System.Console.WriteLine(2.ToString());
  6323. return null;
  6324. }
  6325. });
  6326. }
  6327. static void Invoke(Action method)
  6328. {
  6329. method();
  6330. }
  6331. static void Invoke(Func<Task> method)
  6332. {
  6333. method().Wait();
  6334. }
  6335. static TResult Invoke<TResult>(Func<TResult> method)
  6336. {
  6337. return method();
  6338. }
  6339. internal static TResult Invoke<TResult>(Func<Task<TResult>> method)
  6340. {
  6341. if (method != null)
  6342. {
  6343. return Invoke1(async delegate
  6344. {
  6345. await Task.Yield();
  6346. return await method();
  6347. });
  6348. }
  6349. return default(TResult);
  6350. }
  6351. internal static TResult Invoke1<TResult>(Func<Task<TResult>> method)
  6352. {
  6353. return method().Result;
  6354. }
  6355. }
  6356. ";
  6357. var expected =
  6358. @"0
  6359. 2";
  6360. CompileAndVerify(source, expectedOutput: expected);
  6361. }
  6362. }
  6363. }