This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Disallow ambiguous base-to-derived conversion in template argument deduction
ClosedPublic

Authored by erik.pilkington on Jun 23 2016, 10:37 AM.

Details

Summary

Previously, Clang would incorrectly accept the following:

template<int N> struct A {};
struct B : A<0>, A<1> {};
template<int N> int f(A<N>);
int main() { f(B()); }

The problem is that when considering the base classes of B, there are 2 possible ambiguous bases that could be deduced. Therefore, deduction ought to fail (temp.deduct.call p5).

There are 3 improvements that I want to make here as well:

  1. Introduce a TDK_AmbiguousBaseClasses so that this diagnostic isn't terrible.
  2. Reapply Richard's r267453, (with some modifications) which previously broke stdlibc++'s tuple implementation (PR27601, reverted in r270016). This refactors the handmade base class traversal I fixed here to instead use CXXRecordDecl::forallBases(), I believe that requiring matching number of arguments in the DeduceTemplateArguments at SemaTemplateDeduction.cpp:433 will fix it.
  3. A NFC patch to rename the 10 (!) overloads of DeduceTemplateArguments to have more descriptive names. These functions are *really* annoying to deal with because of that.

If it would be better to do a subset (or all) of these improvements here, I could always update the diff. Fixes PR28195.

Thanks!

Diff Detail

Repository
rL LLVM

Event Timeline

erik.pilkington retitled this revision from to [Sema] Disallow ambiguous base-to-derived conversion in template argument deduction.
erik.pilkington updated this object.
erik.pilkington added reviewers: rsmith, faisalv.
erik.pilkington added a subscriber: cfe-commits.
rsmith accepted this revision.Jun 28 2016, 2:24 PM
rsmith edited edge metadata.

Please also add the testcase from PR27601.

This revision is now accepted and ready to land.Jun 28 2016, 2:24 PM

Do you mean Faisal's example? He committed that in r270016.
Thanks for taking a look!

Well OK then, commit away! :)

This revision was automatically updated to reflect the committed changes.