[llvm] [Support][JSON] Fix ObjectKey ownership and Value assignment bugs (PR #214104)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 01:50:40 PDT 2026


================
@@ -161,6 +161,53 @@ TEST(JSONTest, Object) {
   EXPECT_EQ(R"({"a":1,"c":3})", s(std::move(O)));
 }
 
+TEST(JSONTest, ObjectKeyCopyAssignment) {
+  ObjectKey Destination(std::string("owned"));
+  ObjectKey Source("borrowed");
+
+  Destination = Source;
+
+  // Assignment itself updates Data correctly.
+  EXPECT_EQ("borrowed", Destination.str());
+
+  // Copying Destination must preserve its current logical value.
+  // Without Owned.reset() in copy assignment, this incorrectly becomes "owned".
+  ObjectKey Copy = Destination;
+  EXPECT_EQ("borrowed", Copy.str());
+}
+
+TEST(JSONTest, CopyValueAfterErasingOwnedObjectKey) {
+  Object OriginalObject;
+  OriginalObject.try_emplace(std::string("removed"), 1);
+  OriginalObject.try_emplace(std::string("remaining"), 2);
+
+  ASSERT_TRUE(OriginalObject.erase("removed"));
+  EXPECT_EQ(nullptr, OriginalObject.get("removed"));
+  EXPECT_EQ(1u, OriginalObject.size());
+
+  Value Original = std::move(OriginalObject);
+  Value Copy = Original;
+
+  const Object *CopiedObject = Copy.getAsObject();
+  ASSERT_NE(nullptr, CopiedObject);
+
+  // An erased owned key must not reappear after copying the JSON value.
+  EXPECT_EQ(nullptr, CopiedObject->get("removed"));
+  EXPECT_EQ(std::optional<int64_t>(2), CopiedObject->getInteger("remaining"));
+  EXPECT_EQ(1u, CopiedObject->size());
+}
+
+TEST(JSONTest, AssignFromNestedValue) {
----------------
jh7370 wrote:

Could you make a second version of this test case that performs a move assignment please (and rename this to `CopyAssignFromNestedValue`). I imagine it could share much of the body with the current test.

Also, let's move it up to be next to the exist Move test for the class.

https://github.com/llvm/llvm-project/pull/214104


More information about the llvm-commits mailing list