aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/libexpr/nixexpr.cc7
-rw-r--r--src/libexpr/nixexpr.hh3
-rw-r--r--tests/unit/libexpr/expr-print.cc34
-rw-r--r--tests/unit/meson.build1
4 files changed, 44 insertions, 1 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index bc53ca053..08d4b279b 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -50,6 +50,13 @@ void ExprVar::show(const SymbolTable & symbols, std::ostream & str) const
str << symbols[name];
}
+void ExprInheritFrom::show(SymbolTable const & symbols, std::ostream & str) const
+{
+ str << "(/* expanded inherit (expr) */ ";
+ 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<const StaticEnv> & env);
+ void show(SymbolTable const & symbols, std::ostream & str) const override;
+ void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & 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..aa25d00df
--- /dev/null
+++ b/tests/unit/libexpr/expr-print.cc
@@ -0,0 +1,34 @@
+#include <sstream>
+#include <string_view>
+
+#include <gtest/gtest.h>
+
+#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<ExprVar>(state.symbols.create("stdenv"));
+ ExprInheritFrom const eInheritFrom(noPos, 0, inheritSource);
+ test(eInheritFrom, "(/* expanded inherit (expr) */ stdenv)");
+}
+
+}
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 17d089f18..a6719a060 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -186,6 +186,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',
)