From 42a98e1676d10e57ae28d1ce715ef64643b3589c Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Sat, 19 Jun 2021 22:13:17 -0400
Subject: [PATCH] Fix composition with empty nodes.

---
 Machines/Utility/ROMCatalogue.cpp | 11 +++++++++--
 Machines/Utility/ROMCatalogue.hpp |  4 ++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/Machines/Utility/ROMCatalogue.cpp b/Machines/Utility/ROMCatalogue.cpp
index 643b72a9e..23bf6f1a8 100644
--- a/Machines/Utility/ROMCatalogue.cpp
+++ b/Machines/Utility/ROMCatalogue.cpp
@@ -27,8 +27,15 @@ Request::Request(Name name, bool optional) {
 }
 
 Request Request::append(Node::Type type, const Request &rhs) {
-	// Start with the easiest case: this is already an appropriate
-	// request, and so is the new thing.
+	// If either side is empty, act appropriately.
+	if(node.empty() && !rhs.node.empty()) {
+		return rhs;
+	}
+	if(rhs.node.empty()) {
+		return *this;
+	}
+
+	// Just copy in the RHS child nodes if types match.
 	if(node.type == type && rhs.node.type == type) {
 		Request new_request = *this;
 		new_request.node.children.insert(new_request.node.children.end(), rhs.node.children.begin(), rhs.node.children.end());
diff --git a/Machines/Utility/ROMCatalogue.hpp b/Machines/Utility/ROMCatalogue.hpp
index a9f457b95..5fabd093a 100644
--- a/Machines/Utility/ROMCatalogue.hpp
+++ b/Machines/Utility/ROMCatalogue.hpp
@@ -240,6 +240,10 @@ struct Request {
 			bool is_optional = false;
 			std::vector<Node> children;
 
+			bool empty() const {
+				return type == Type::One && name == Name::None;
+			}
+
 			void add_descriptions(std::vector<Description> &) const;
 			bool validate(Map &) const;
 			void visit(