[llvm-branch-commits] [lld] [NFC][ELF] Remove Symbol's unused copy constructor (PR #210615)

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Aug 2 12:40:10 PDT 2026


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/210615

>From 529700dbfc9bce1a853332930ac1406751bbae59 Mon Sep 17 00:00:00 2001
From: Jessica Clarke <jrtc27 at jrtc27.com>
Date: Sun, 19 Jul 2026 15:32:12 +0100
Subject: [PATCH] [NFC][ELF] Remove Symbol's unused copy constructor

Now that we are no longer copying symbols via the copy constructor we
can remove it. Given we track symbol pointers in various data structures
it can be dangerous to have multiple objects for the same symbol that
would risk being viewed inconsistently, or even moved and the original
lost, though no such cases exist upstream that I'm aware of.

Copying a symbol entirely is also a weird thing to do, and can be
inefficient, so when copies are being made it's best to be explicit
about the members to copy. This also makes it clearer to understand what
members are relevant, rather than implicitly copying all the members,
most of which aren't relevant in practice.

Whilst the copy/move constructors/assignment operators are currently
implicitly deleted due to the atomic flags member, explicitly delete
them all so they remain deleted in case that ever changes.
---
 lld/ELF/Symbols.h | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h
index 0893776882dc6..947a160331610 100644
--- a/lld/ELF/Symbols.h
+++ b/lld/ELF/Symbols.h
@@ -78,9 +78,17 @@ class Symbol {
   // The file from which this symbol was created.
   InputFile *file;
 
-  // The default copy constructor is deleted due to atomic flags. Define one for
-  // places where no atomic is needed.
-  Symbol(const Symbol &o) { memcpy(static_cast<void *>(this), &o, sizeof(o)); }
+  // Although the copy/move constructors/assignment operators are deleted due
+  // to atomic flags, explicitly delete them in case that ever changes. Copying
+  // or moving symbols is not something we want to be able to do implicitly, so
+  // we can reason about pointers and references to symbols keeping their
+  // meanings. Even when we are copying a symbol we'll still be changing some
+  // aspects of it, so this just ensures we're explicit about what is and is
+  // not being copied.
+  Symbol(const Symbol &o) = delete;
+  Symbol(Symbol &&o) = delete;
+  Symbol &operator=(const Symbol &) = delete;
+  Symbol &operator=(Symbol &&) = delete;
 
 protected:
   const char *nameData;



More information about the llvm-branch-commits mailing list