Merging template type diffing changes into 3.8 branch

Oops, send to old mailing list, trying again.

I would like to get the following changes merged into 3.8

r257831
r257838
r257853
r257861
r257869
r257870

These changes are for fixing issues with template type diffing, including a number of asserts from edge cases, at least one mistake in printing, and a lot of general refactoring. There is a large amount of lines changed, but it is confined to one code file and two tests.

Oops, send to old mailing list, trying again.

I would like to get the following changes merged into 3.8

r257831
r257838
r257853
r257861
r257869
r257870

r257871

Adding one more to the list. Looks like I missed a -Wparentheses warning
with my code.

These changes are for fixing issues with template type diffing, including a

SGTM. Richard S, let me know what you think.

These all LGTM for 3.8.

Thanks! Merged in r259422.