about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/nixexpr.hh
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-13T20·20+0100
committertazjin <mail@tazj.in>2020-07-13T21·06+0000
commitfa161e9a380c530363c3eb72c9917e4db88287e0 (patch)
tree93a4a12d21ef5fb0b0fef6052e9ecc2fa2919707 /third_party/nix/src/libexpr/nixexpr.hh
parentafd1367337300f0411d1e6eee6bb6b53bbaf113c (diff)
refactor(3p/nix/libexpr): Remove the nix::Symbol default constructor r/1280
Having a default constructor for this causes a variety of annoying
situations across the codebase in which this is initialised to an
unexpected value, leading to constant guarding against those
conditions.

It turns out there's actually no intrinsic reason that this default
constructor needs to exist. The biggest one was addressed in CL/1138
and this commit cleans up the remaining bits.

Change-Id: I4a847f50bc90e72f028598196592a7d8730a4e01
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1139
Reviewed-by: isomer <isomer@tvl.fyi>
Tested-by: BuildkiteCI
Diffstat (limited to 'third_party/nix/src/libexpr/nixexpr.hh')
-rw-r--r--third_party/nix/src/libexpr/nixexpr.hh24
1 files changed, 17 insertions, 7 deletions
diff --git a/third_party/nix/src/libexpr/nixexpr.hh b/third_party/nix/src/libexpr/nixexpr.hh
index 7ec6f7f17f39..3bf9e4a1a77a 100644
--- a/third_party/nix/src/libexpr/nixexpr.hh
+++ b/third_party/nix/src/libexpr/nixexpr.hh
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <map>
+#include <optional>
 #include <variant>
 
 #include "libexpr/symbol-table.hh"
@@ -21,20 +22,28 @@ MakeError(RestrictedPathError, Error);
 /* Position objects. */
 
 struct Pos {
-  Symbol file;
+  std::optional<Symbol> file;
   unsigned int line, column;
-  Pos() : line(0), column(0){};
-  Pos(const Symbol& file, unsigned int line, unsigned int column)
+  Pos(const std::optional<Symbol>& file, unsigned int line, unsigned int column)
       : file(file), line(line), column(column){};
+
+  // TODO(tazjin): remove this - empty pos is never useful
+  Pos() : file(std::nullopt), line(0), column(0){};
+
   operator bool() const { return line != 0; }
+
   bool operator<(const Pos& p2) const {
+    if (!file.has_value()) {
+      return true;
+    }
+
     if (!line) {
       return p2.line;
     }
     if (!p2.line) {
       return false;
     }
-    int d = ((std::string)file).compare((std::string)p2.file);
+    int d = ((std::string)file.value()).compare((std::string)p2.file.value());
     if (d < 0) {
       return true;
     }
@@ -75,7 +84,6 @@ struct Expr {
   virtual void bindVars(const StaticEnv& env);
   virtual void eval(EvalState& state, Env& env, Value& v);
   virtual Value* maybeThunk(EvalState& state, Env& env);
-  virtual void setName(Symbol& name);
 };
 
 std::ostream& operator<<(std::ostream& str, const Expr& e);
@@ -219,8 +227,9 @@ struct Formals {
 };
 
 struct ExprLambda : Expr {
+ public:
   Pos pos;
-  Symbol name;
+  std::optional<Symbol> name;
   Symbol arg;
   bool matchAttrs;
   Formals* formals;
@@ -233,10 +242,11 @@ struct ExprLambda : Expr {
         formals(formals),
         body(body) {
     if (!arg.empty() && formals &&
-        formals->argNames.find(arg) != formals->argNames.end())
+        formals->argNames.find(arg) != formals->argNames.end()) {
       throw ParseError(
           format("duplicate formal function argument '%1%' at %2%") % arg %
           pos);
+    }
   };
   void setName(Symbol& name);
   std::string showNamePos() const;