diff --git a/CMakeLists.txt b/CMakeLists.txt index b6f5f2767..e5fa930a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,18 @@ add_library(bonsai_library STATIC) add_library(bonsai::library ALIAS bonsai_library) target_compile_features(bonsai_library PUBLIC cxx_std_20) + +# include/Error.h trims this prefix off __FILE__, so that the "[internal] +# Error: :" lines checked in under tests/bonsai/error/*.expect do +# not depend on the name of the directory the repository was cloned into. +# PUBLIC because bonsai_compiler and bonsai_test_runner include Error.h too; +# BUILD_INTERFACE keeps a local absolute path out of anything installed. +target_compile_definitions( + bonsai_library + PUBLIC + "$" +) + target_link_libraries(bonsai_library PUBLIC ModernLLVM::LLVM) target_sources( diff --git a/include/Error.h b/include/Error.h index d6eae8450..24892c5a0 100644 --- a/include/Error.h +++ b/include/Error.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace bonsai { @@ -11,22 +12,42 @@ class Error final : public std::runtime_error { using std::runtime_error::runtime_error; }; +namespace detail { + +// __FILE__ is an absolute path into whatever directory the repository was +// cloned into. Trim it back to a repository-relative path, so that the +// "[internal] Error: :" lines that tests/bonsai/error/*.expect +// compares byte-for-byte do not depend on where the checkout lives. +inline std::string source_relative_path(const char *file) { + std::string f(file); + +#ifdef BONSAI_SOURCE_DIR + // The repository root, injected by the top-level CMakeLists.txt. + constexpr std::string_view root = BONSAI_SOURCE_DIR; + if (!root.empty() && f.size() > root.size() && + f.compare(0, root.size(), root) == 0 && f[root.size()] == '/') { + return f.substr(root.size() + 1); + } +#endif + + // For a translation unit built outside our CMake, or one whose __FILE__ + // has been rewritten (-ffile-prefix-map), fall back to the last top-level + // source directory. Printing the whole path is the honest last resort. + for (const std::string_view dir : {"/src/", "/include/", "/tests/"}) { + if (const size_t pos = f.rfind(dir); pos != std::string::npos) { + return f.substr(pos + 1); + } + } + return f; +} + +} // namespace detail + class ErrorReport { public: ErrorReport(const char *cond_str, const char *file, size_t line) { - // Print the file path proceeding the root directory (inclusive). - constexpr std::string_view ROOT_DIRECTORY = "bonsai"; - std::string f(file); - - // TODO(cgyurgyik): Fix this hack. - // Finds the last occurrence of `bonsai` to conform with Github Actions, - // where both the WORKSPACE and the REPOSITORY are named `bonsai`. - if (size_t pos = f.rfind(ROOT_DIRECTORY); pos != std::string::npos) { - f = f.substr(pos + ROOT_DIRECTORY.length() + 1); - } - stream << "[internal] Error: "; - stream << f << ":" << line << "\n"; + stream << detail::source_relative_path(file) << ":" << line << "\n"; if (cond_str) { stream << "\n--> " << cond_str << "\n"; } diff --git a/src/Lower/Scans.cpp b/src/Lower/Scans.cpp index b614a30a1..ae68e4cf8 100644 --- a/src/Lower/Scans.cpp +++ b/src/Lower/Scans.cpp @@ -75,10 +75,13 @@ build_scan_func(const std::vector &args, const std::string &func_name, std::optional op; Expr map_func; - ScansToCalls(std::shared_ptr func, - std::optional op, Expr map_func) - : func(std::move(func)), op(std::move(op)), - map_func(std::move(map_func)) { + // The parameters deliberately do not share the members' names. The + // body reads `func` after the mem-initializer has moved from it, so a + // parameter named `func` would shadow the member and be null there. + ScansToCalls(std::shared_ptr scan_func, + std::optional agg_op, Expr map) + : func(std::move(scan_func)), op(std::move(agg_op)), + map_func(std::move(map)) { write_expr = Var::make(func->args.back().type, func->args.back().name); write_loc = diff --git a/src/Lower/TypeInference.cpp b/src/Lower/TypeInference.cpp index 98da055d1..2f779c3ed 100644 --- a/src/Lower/TypeInference.cpp +++ b/src/Lower/TypeInference.cpp @@ -506,8 +506,12 @@ infer_types(const std::shared_ptr &fnotypes, coerce_return_types(std::move(ftypes->body), ftypes->ret_type); ftypes->body = infer_build_types(std::move(ftypes->body), ftypes->ret_type); - internal_assert(ftypes->ret_type.is() || - always_returns(ftypes->body)) + // Keep the internal_assert invocation on one line: GCC reports __LINE__ as + // the line the macro name is on and Clang as the line the closing paren is + // on, and tests/bonsai/error/no-return.expect records that number. + const bool returns_on_all_paths = + ftypes->ret_type.is() || always_returns(ftypes->body); + internal_assert(returns_on_all_paths) << "Function: " << ftypes->name << " does not return in all code paths."; diff --git a/tests/bonsai/error/no-return.expect b/tests/bonsai/error/no-return.expect index 277b8a2f1..d55276822 100644 --- a/tests/bonsai/error/no-return.expect +++ b/tests/bonsai/error/no-return.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/Lower/TypeInference.cpp:510 +[internal] Error: src/Lower/TypeInference.cpp:514 ---> ftypes->ret_type.is() || always_returns(ftypes->body) +--> returns_on_all_paths Function: foo does not return in all code paths.