fail_lifetime.carbon 1.1 KB

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