experimental_continuation9.carbon 604 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 Int x = 1;
  9. __continuation k {
  10. var Int y = x;
  11. }
  12. return k;
  13. }
  14. fn main() -> Int {
  15. var __Continuation k = capture();
  16. __run k; // error, lifetime of x is over
  17. return 0;
  18. }