constexpr.carbon 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/constexpr.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/constexpr.carbon
  12. // --- bool.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp inline '''
  15. constexpr bool b = true;
  16. ''';
  17. class C(B:! bool) {}
  18. fn F() -> C(Cpp.b);
  19. let x: C(true) = F();
  20. // --- int.carbon
  21. library "[[@TEST_NAME]]";
  22. import Cpp inline '''
  23. constexpr int i = 123;
  24. ''';
  25. class C(I:! i32) {}
  26. fn F() -> C(Cpp.i);
  27. let x: C(123) = F();
  28. // --- float.carbon
  29. library "[[@TEST_NAME]]";
  30. import Cpp inline '''
  31. constexpr float flt = 123.5;
  32. ''';
  33. class C(V:! f32) {}
  34. fn F() -> C(Cpp.flt);
  35. let x: C(123.5) = F();
  36. // --- function.carbon
  37. library "[[@TEST_NAME]]";
  38. import Cpp inline '''
  39. constexpr int f(int a, int b) { return a + b; }
  40. ''';
  41. let a: array(i32, Cpp.f(1, 2)) = (1, 2, 3);
  42. // --- fail_invalid_constant_eval.carbon
  43. library "[[@TEST_NAME]]";
  44. import Cpp inline '''
  45. constexpr int arr[1] = {0};
  46. constexpr int f(int a) {
  47. return arr[a];
  48. }
  49. ''';
  50. // CHECK:STDERR: fail_invalid_constant_eval.carbon:[[@LINE+4]]:19: error: failed to evaluate constexpr function call as a constant [CppConstexprEval]
  51. // CHECK:STDERR: let a: array(i32, Cpp.f(5)) = (1, 2, 3);
  52. // CHECK:STDERR: ^~~~~~~~
  53. // CHECK:STDERR:
  54. let a: array(i32, Cpp.f(5)) = (1, 2, 3);