Always make sure we have the right comparer for DocumentIds #53603
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I believe this fixes #52698, although I don't have a dump for that issue so I'm having to deduce what I think happened based on the stack alone. Unfortunately the stack also has some frames inlined so there's a bit more guessing than usual:
The stack shows that we ended up in Comparer.Compare and it threw; the only exception that it throws directly if the thing being compared isn't IComparable. There aren't many uses of ImmutableSortedDictionary in Roslyn, but the one specific one that makes sense to be under GetDocumentState is the one we have inside our TextDocumentStates type. And that one is one where we normally pass in a custom comparer to pass in DocumentIds, so it seems logical that we somehow ended up with a map that didn't have our comparer.
It turns out the Empty member we had didn't specify one, so if we started with the Empty member and then added stuff from there, we'd end up with such a map without a comparer. Why didn't we see this right away? Because it turns out we only have a few uses of .Empty, most of which are with source generated files. And even then we often are creating the source generated collections with something else.