[PATCH] D150020: Fix possible self assign issue for DIEValue

Wang, Xin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 7 18:59:38 PDT 2023


XinWang10 added inline comments.


================
Comment at: llvm/include/llvm/CodeGen/DIE.h:469
+      return *this;
     destroyVal();
     Ty = X.Ty;
----------------
skan wrote:
> XinWang10 wrote:
> > XinWang10 wrote:
> > > skan wrote:
> > > > https://stackoverflow.com/questions/12015156/what-is-wrong-with-checking-for-self-assignment-and-what-does-it-mean
> > > > 
> > > > Use copy-and-swap method?
> > > > ```
> > > > DIEValue &operator=(DIEValue X) {
> > > >   swap(*this, X);
> > > >   return *this;
> > > > }
> > > > ```
> > > This reported a lot of errors to me, I think swap will also call operator assign? 
> > > From my point of view, the logic here for assign is correct if we just remove destroyVal(), since the Val is on stack, we don't need to destroy it and it will be overwrite by X. But I'm not sure if there is some concern here.
> > > 
> > > When I read this code, another question occured to me, why do we need the explicit copy and assign constructor, this class don't maintain any dynamic memory. 
> > > I think if I remove the copy and assign constructor, it will also work for me.
> > > But I don't want to make that big change :)
> > Seems can pass check-all locally without copy and assign construction function.
> swap does not call assignment operator, and it swaps the members of two classes.
> It seems `copyVal` will dynamically allocate memory.
> 
> What's the error?
swap can not get a rvalue as parm.
copyVal calls construct which just calls it's constructor.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150020/new/

https://reviews.llvm.org/D150020



More information about the llvm-commits mailing list