This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Fix PR14211 Crash for explicit instantiation of overloaded template function within class template
ClosedPublic

Authored by hintonda on Feb 12 2016, 2:05 PM.

Details

Reviewers
rjmccall
lvoufo
Summary

Fix PR14211

Only add FunctionDecl's that are actual template methods to the set of
matches passed to getMostSpecializied() -- otherwise it will
assert/crash. If a match to a non-template method is found, save the
pointer and don't call getMostSpecialized().

Event Timeline

hintonda updated this revision to Diff 47856.Feb 12 2016, 2:05 PM
hintonda retitled this revision from to Fix PR14211 Crash for explicit instantiation of overloaded template function within class template.
hintonda updated this object.
hintonda added reviewers: lvoufo, rjmccall.
hintonda added a subscriber: cfe-commits.
hintonda retitled this revision from Fix PR14211 Crash for explicit instantiation of overloaded template function within class template to Fix PR14211 [Sema] Crash for explicit instantiation of overloaded template function within class template.Feb 13 2016, 8:03 AM
hintonda retitled this revision from Fix PR14211 [Sema] Crash for explicit instantiation of overloaded template function within class template to [Sema] Fix PR14211 Crash for explicit instantiation of overloaded template function within class template.
rjmccall added inline comments.Feb 15 2016, 9:57 AM
lib/Sema/SemaTemplate.cpp
8959

Please name this variable NonTemplateMatch instead of Specialization, then initialize the later Specialization variable to it if it's non-null. That's more self-documenting, and it avoids readability problems with the fact that this loop actually declares its own Specialization variable that would shadow this one.

Please also rename Matches to TemplateMatches.

8970–8976

This cast shouldn't be necessary. CXXMethodDecl is a subclass of FunctionDecl.

hintonda updated this revision to Diff 47999.Feb 15 2016, 10:14 AM
  • Renamed variables to make code self-documenting

Btw, I don't have commit access, so if accepted, could you please commit for me.

rjmccall added inline comments.Feb 15 2016, 1:35 PM
lib/Sema/SemaTemplate.cpp
8977

Could you add an assertion here that NonTemplateMatch is still null? That should definitely never trip.

Hmm, actually, it might trip in invalid code; you should include a test case like

template <class T, class U> class A { void foo(T) {} void foo(U) {} };
template void A<int, int>::foo(int);
hintonda added inline comments.Feb 15 2016, 1:50 PM
lib/Sema/SemaTemplate.cpp
8977

I'll add the new test, but we do check for null below, i.e., we assign NonTemplateMatch to the original Specialization pointer, and then check the matches. If we don't find one, we true on line 7883.

rjmccall added inline comments.Feb 15 2016, 3:27 PM
lib/Sema/SemaTemplate.cpp
8977

No, I mean an assertion that we're not re-assigning NonTemplateMatch, i.e. that we didn't find two different non-template functions like this.

hintonda updated this revision to Diff 48031.Feb 15 2016, 4:12 PM
  • Added assert and additional test
hintonda added inline comments.Feb 15 2016, 4:17 PM
lib/Sema/SemaTemplate.cpp
8977

Got it...

hintonda updated this revision to Diff 101482.Jun 5 2017, 5:19 PM

Fixes PR14211.

Since getMostSpecialized only handles FunctionTemplateDecl matches,
keep track of non-FunctionTemplateDecl matches and only call
getMostSpecialized if no non-FunctionTemplateDecl matches are found.

Added tests.

rjmccall added inline comments.Jun 5 2017, 5:35 PM
lib/Sema/SemaTemplate.cpp
8979

These continues that you're adding are already at the end of the loop body, so this code is really confusing.

I think the template-specialization check here is trying to do the same thing you're doing: treat non-template matches specially. But your way of handling it is better, so I would just remove the check and all the breaks and continues so that we add the match to either NonTemplateMatch or TemplateMatches. (Please do rename Matches to TemplateMatches, though.)

hintonda updated this revision to Diff 101503.Jun 5 2017, 7:34 PM
  • Addressed comments.
rjmccall accepted this revision.Jun 5 2017, 7:41 PM

Thanks, looks great. Do you still not have commit access?

This revision is now accepted and ready to land.Jun 5 2017, 7:41 PM

Great, thanks John.

Yes, that's correct -- I do not have commit access.

Since I don't have commit access, could you commit this for me?

thanks...
don

Sure thing, r304951.

hintonda closed this revision.Jun 8 2017, 7:32 AM