fail_lifetime.carbon 1.1 KB

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