But what if you mix an index option with a SN. For example, close a losing SPX option and replace the position with 1000 SPY?
That link is about Canadian tax law The profile page for the OP says he is in Palm Desert, California
No, it's not illegal. It is not illegal to execute that type of trade. Depending on the specific facts and circumstances, it may not produce the tax effects that you are hoping for. And if you do not properly report the transactions on your tax return, e.g., if you claim a tax loss that you are not really allowed to claim, then you are violaing federal tax law. The trade itself is not "illegal."
That will not generate a wash sale under US tax law. An SPX option is a cash-settled index option. It does not have an underlying security. Shares of SPY are not the same security as an SPX option. And an SPX option is not a contract to buy or sell SPY. It is something else.
There's nothing illegal about making any trade. If your intent behind the trade is tax avoidance, we have clear rules to help avoid that. That's not 'illegal' that's called good tax planning and leveraging the laws allowed. It would be illegal to hide something you need to report, like transactions that are identical between otherwise separate accounts...