From ad1650c749483fb9e986006d45d3a1769ec4d942 Mon Sep 17 00:00:00 2001 From: AJ Root Date: Sun, 30 Aug 2026 10:15:06 -0700 Subject: [PATCH 1/2] Stop ScansToCalls dereferencing its moved-from constructor parameter 057eb349 renamed the ScansToCalls constructor's parameter from `_func` to `func` to satisfy readability-identifier-naming. That name shadows the member of the same name, and the constructor body reads `func` after the mem-initializer list has already moved out of it: ScansToCalls(std::shared_ptr func, ...) : func(std::move(func)), ... { write_expr = Var::make(func->args.back().type, ...); } Unqualified `func` in the body names the parameter, which std::shared_ptr's move constructor is required to leave null, so this dereferences nullptr. The fault address is 0x28 -- offsetof(Function, args) plus the vector's _M_finish, which back() loads first. It fires on the first build_scan_func call regardless of the reduction, so every scan over a whole subtree crashed the compiler. Only correctness/cpp/setops reaches LowerScans with an actual Scan node, and that test was added by the same pull request, so the crash and its only witness landed together. Give the parameters names the members do not have, and say why, so the next naming pass does not reintroduce it. `_func` is not an option: the repo's .clang-tidy sets ParameterCase to lower_case, which is what prompted the rename in the first place. correctness/cpp/setops now passes both execute and validate against the golden as committed; the suite goes from 32 failures to 30. --- src/Lower/Scans.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 = From a1180683137a964dd97ac7fdd063459a8ac2bba4 Mon Sep 17 00:00:00 2001 From: AJ Root Date: Sun, 30 Aug 2026 10:39:06 -0700 Subject: [PATCH 2/2] Make internal error locations independent of the build environment The 24 error/*.bonsai goldens compare the "[internal] Error: :" line byte-for-byte, and both halves of it were environment-dependent. The path came from ErrorReport trimming __FILE__ with rfind("bonsai"), a hack that only ever worked because GitHub Actions checks out to .../work/bonsai/bonsai. In a clone named anything else containing "bonsai" it truncates in the wrong place: this checkout is bonsai-test, so every one of the 24 reported test/src/Parser/Parser.cpp instead of src/Parser/Parser.cpp. Have CMake inject the source root as BONSAI_SOURCE_DIR and strip exactly that prefix, which is an exact compare rather than a search. The definition is PUBLIC so it reaches bonsai_compiler and bonsai_test_runner, which include Error.h too, and BUILD_INTERFACE keeps a local absolute path out of anything installed. If it is ever missing, the fallback trims to the last /src/, /include/ or /tests/ rather than silently mangling the path. The line number was compiler-dependent in error/no-return alone. GCC expands __LINE__ inside a multi-line function-like macro invocation to the line the macro name is on, Clang to the line of the closing paren, and that internal_assert spanned two lines. The golden was blessed by CI, which is macOS and Clang, so it recorded 510 where GCC reports 509. Hoist the condition into a named bool so the invocation fits on one line, which both compilers agree about; the Voidifier trick only ever made the << message lazy, never the condition, so this is equivalent. The golden is re-blessed for the new line and the new stringified condition. ctest goes from 30 failures to 6. The remaining 6 are backends IR goldens that reproduce identically on the pre-merge commit. --- CMakeLists.txt | 12 ++++++++ include/Error.h | 45 +++++++++++++++++++++-------- src/Lower/TypeInference.cpp | 8 +++-- tests/bonsai/error/no-return.expect | 4 +-- 4 files changed, 53 insertions(+), 16 deletions(-) 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/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.