parse_and_execute_test.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include "explorer/parse_and_execute/parse_and_execute.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. namespace Carbon {
  8. namespace {
  9. using ::testing::MatchesRegex;
  10. TEST(ParseAndExecuteTest, Recursion) {
  11. llvm::vfs::InMemoryFileSystem fs;
  12. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
  13. llvm::MemoryBuffer::getFile("explorer/data/prelude.carbon");
  14. ASSERT_FALSE(prelude.getError()) << prelude.getError().message();
  15. ASSERT_TRUE(fs.addFile("prelude.carbon", /*ModificationTime=*/0,
  16. std::move(*prelude)));
  17. std::string source = R"(
  18. package Test api;
  19. fn Main() -> i32 {
  20. return
  21. )";
  22. // A high depth that's expected to complete in a few seconds.
  23. static constexpr int Depth = 50000;
  24. for (int i = 0; i < Depth; ++i) {
  25. source += "if true then\n";
  26. }
  27. source += "1\n";
  28. for (int i = 0; i < Depth; ++i) {
  29. source += "else 0\n";
  30. }
  31. source += R"(
  32. ;
  33. }
  34. )";
  35. ASSERT_TRUE(fs.addFile("test.carbon", /*ModificationTime=*/0,
  36. llvm::MemoryBuffer::getMemBuffer(source)));
  37. TraceStream trace_stream;
  38. auto err =
  39. ParseAndExecute(fs, "prelude.carbon", "test.carbon",
  40. /*parser_debug=*/false, &trace_stream, &llvm::nulls());
  41. ASSERT_FALSE(err.ok());
  42. // Don't expect any particular source location for the error.
  43. EXPECT_THAT(err.error().message(),
  44. MatchesRegex("RUNTIME ERROR:.* stack overflow: too many "
  45. "interpreter actions on stack"));
  46. }
  47. } // namespace
  48. } // namespace Carbon