experimental_continuation9.carbon 607 B

123456789101112131415161718192021
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // Test that the read from x triggers an error because x is dead.
  5. // This test also demonstrates how by-reference free-variable capture
  6. // is dangerous and can happen inside continuations.
  7. fn capture() -> __Continuation {
  8. var x: i32 = 1;
  9. __continuation k {
  10. var y: i32 = x;
  11. }
  12. return k;
  13. }
  14. fn main() -> i32 {
  15. var k: __Continuation = capture();
  16. __run k; // error, lifetime of x is over
  17. return 0;
  18. }