This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Improve template argument deduction diagnostic for ambiguous base classes
Needs ReviewPublic

Authored by erik.pilkington on Jul 13 2016, 11:48 AM.

Details

Reviewers
faisalv
rsmith
Summary

Previously, clang would give a terrible diagnostic for the following:

template <int N> struct B {};
struct D : B<0>, B<1> {};
template <int N> int f(B<N>) { return 0; } // error, ambiguous bases of 'D' could be deduced.
int main() { f(D()); }

This patch just introduces a new deduction failure kind: TDK_AmbiguousBaseClasses, and uses it to emit a better diagnostic.

Thanks!

Diff Detail

Event Timeline

erik.pilkington retitled this revision from to [Sema] Improve template argument deduction diagnostic for ambiguous base classes.
erik.pilkington updated this object.
erik.pilkington added reviewers: rsmith, faisalv.
erik.pilkington added a subscriber: cfe-commits.
rsmith added inline comments.Jul 13 2016, 12:49 PM
include/clang/Basic/DiagnosticSemaKinds.td
3227

ambiguous -> multiple, perhaps? (An "ambiguous base class" is something else.)

However, even that's not quite right. The rule is (from [temp.deduct.call]/5):

"If <deducing against all base classes> yields more than one possible deduced A, type deduction fails"

... so deduction failure only happens if the base classes deduce different template arguments, not just if multiple deductions succeed. In particular, if you multiply-inherit from the same base class, then deduction can succeed (even though the subsequent initialization of the parameter will fail due to ambiguity):

template<int> struct A {};
struct B : A<0> {};
struct C : A<1> {};
struct D : B, C {};
template<int N> void f(A<N>);
void f(...);
void g(D d) { f(d); }  // deduction should succeed, should choose f<0>, result is hard error due to ambiguity

So maybe we should word this more like the note_ovl_candidate_deduced_mismatch case, since the problem really is that we deduced two different values for the same template parameter.

include/clang/Sema/Sema.h
6450

TDK_InconsistentBaseClassDeduction maybe?

lib/Sema/SemaOverload.cpp
599–603

Please update the comments in include/clang/Sema/TemplateDeduction.h to say what Info.FirstArg and Info.SecondArg mean for TDK_AmbiguousBaseClasses.