Fix crash when replacing a lexical reference

LT-13729

Change-Id: Id3f2bcac98956e7728a709f4192142398ea555e0