From 011fbeca4702e0f878f029c02dcff3cde86bea09 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 24 Jun 2024 14:33:12 -0600 Subject: [PATCH] add an impl of Expr::show for ExprInheritFrom that doesn't crash ExprVar::show() assumes it has a name. dynamic inherits do not necessarily (ever?) have a name. Change-Id: If10893188e307431da17f0c1bd0787adc74f7141 --- src/libexpr/nixexpr.cc | 11 +++++++++++ src/libexpr/nixexpr.hh | 3 ++- tests/unit/libexpr/expr-print.cc | 34 ++++++++++++++++++++++++++++++++ tests/unit/meson.build | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/unit/libexpr/expr-print.cc diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index bc53ca053..53e61cb30 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -50,6 +50,17 @@ void ExprVar::show(const SymbolTable & symbols, std::ostream & str) const str << symbols[name]; } +void ExprInheritFrom::show(SymbolTable const & symbols, std::ostream & str) const +{ + if (name) { + str << symbols[name]; + } else { + str << "("; + this->fromExpr->show(symbols, str); + str << ")"; + } +} + void ExprSelect::show(const SymbolTable & symbols, std::ostream & str) const { str << "("; diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh index 703a32e8f..25ba94595 100644 --- a/src/libexpr/nixexpr.hh +++ b/src/libexpr/nixexpr.hh @@ -154,7 +154,8 @@ struct ExprInheritFrom : ExprVar this->fromWith = nullptr; } - void bindVars(EvalState & es, const std::shared_ptr & env); + void show(SymbolTable const & symbols, std::ostream & str) const override; + void bindVars(EvalState & es, const std::shared_ptr & env) override; }; struct ExprSelect : Expr diff --git a/tests/unit/libexpr/expr-print.cc b/tests/unit/libexpr/expr-print.cc new file mode 100644 index 000000000..c6365d0f5 --- /dev/null +++ b/tests/unit/libexpr/expr-print.cc @@ -0,0 +1,34 @@ +#include +#include + +#include + +#include "tests/libexpr.hh" + +#include "nixexpr.hh" +#include "ref.hh" + +namespace nix +{ + +using namespace testing; +struct ExprPrintingTests : LibExprTest +{ + void test(Expr const & expr, std::string_view expected) + { + std::stringstream out; + expr.show(state.symbols, out); + ASSERT_EQ(out.str(), expected); + } +}; + +TEST_F(ExprPrintingTests, ExprInheritFrom) +{ + // ExprInheritFrom has its own show() impl. + // If it uses its parent class's impl it will crash. + auto inheritSource = make_ref(state.symbols.create("stdenv")); + ExprInheritFrom const eInheritFrom(noPos, 0, inheritSource); + test(eInheritFrom, "(stdenv)"); +} + +} diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 2b5471526..e2ca95629 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -185,6 +185,7 @@ libexpr_tests_sources = files( 'libexpr/primops.cc', 'libexpr/search-path.cc', 'libexpr/trivial.cc', + 'libexpr/expr-print.cc', 'libexpr/value/context.cc', 'libexpr/value/print.cc', )