test: Add a test for variance in trait matching.

Closes #5781.