The IRS instructions are confusing. They assume you have replacement shares. The examples they use all have shares left after the sale.
The key is to realize that you are not substantially identical to yourself; your twin brother is substantially identical to yourself.
A wash sale occurs when you sell or otherwise dispose of stock or securities (including a contract or option to acquire or sell stock or securities) at a loss and, within 30 days before or after the sale or disposition, you:
Buy substantially identical stock or securities,
I bought 100 shares. I sold 100 shares. None of these shares are substantially identical; they are the same shares.
You buy 100 shares of X stock for $1,000. You sell these shares for $750 and within 30 days from the sale you buy 100 shares of the same stock for $800. Because you bought substantially identical stock, you cannot deduct your loss of $250 on the sale. However, you add the disallowed loss of $250 to the cost of the new stock, $800, to obtain your basis in the new stock, which is $1,050.
The shares bought later are not the same shares you already sold. However, they are substantially identical.
Now when something about the law seems confusing, I like to read the law for myself. So let's all read along with 26 USC 1091
(a) Disallowance of loss deduction
In the case of any loss claimed to have been sustained from any sale or other disposition of shares of stock or securities where it appears that, within a period beginning 30 days before the date of such sale or disposition and ending 30 days after such date, the taxpayer has acquired (by purchase or by an exchange on which the entire amount of gain or loss was recognized by law), or has entered into a contract or option so to acquire, substantially identical stock or securities, then no deduction shall be allowed under section 165 unless the taxpayer is a dealer in stock or securities and the loss is sustained in a transaction made in the ordinary course of such business.
The key is the appearance, just as it is in a step transaction. However, the IRS leaves out the word appear or appearance in the instructions. Why they don't follow the law, I don't know.
I would never apply the words substantially identical to myself. I am who I am. There may be someone else who appears to be
substantially identical to me, but I am not that person.