fail_lifetime.carbon 885 B

1234567891011121314151617181920212223242526272829
  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. //
  5. // AUTOUPDATE
  6. // RUN: %{not} %{explorer-run}
  7. // RUN: %{not} %{explorer-run-trace}
  8. package ExplorerTest api;
  9. // Test that the read from x triggers an error because x is dead.
  10. // This test also demonstrates how by-reference free-variable capture
  11. // is dangerous and can happen inside continuations.
  12. fn capture() -> __Continuation {
  13. var x: i32 = 1;
  14. __continuation k {
  15. // CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/testdata/experimental_continuation/fail_lifetime.carbon:[[@LINE+1]]: undefined behavior: access to dead value 1
  16. var y: i32 = x;
  17. }
  18. return k;
  19. }
  20. fn Main() -> i32 {
  21. var k: __Continuation = capture();
  22. // error, lifetime of x is over
  23. __run k;
  24. return 0;
  25. }