experimental_continuation9.carbon 645 B

1234567891011121314151617181920212223
  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. package ExecutableSemanticsTest api;
  5. // Test that the read from x triggers an error because x is dead.
  6. // This test also demonstrates how by-reference free-variable capture
  7. // is dangerous and can happen inside continuations.
  8. fn capture() -> __Continuation {
  9. var x: i32 = 1;
  10. __continuation k {
  11. var y: i32 = x;
  12. }
  13. return k;
  14. }
  15. fn main() -> i32 {
  16. var k: __Continuation = capture();
  17. __run k; // error, lifetime of x is over
  18. return 0;
  19. }