This is an archive of the discontinued LLVM Phabricator instance.

Allow incomplete template types in unique_function arguments
ClosedPublic

Authored by math-fehr on May 17 2021, 7:42 AM.

Details

Summary

We can't declare unique_function that has in its arguments a reference to
a template type with an incomplete argument.
For instance, we can't declare unique_function<void(SmallVectorImpl<A>&)>
when A is forward declared.

This is because SFINAE will trigger a hard error in this case, when instantiating
IsSizeLessThanThresholdT with the incomplete type.

This patch specialize AdjustedParamT for references to remove this error.

Diff Detail

Event Timeline

math-fehr created this revision.May 17 2021, 7:42 AM
math-fehr requested review of this revision.May 17 2021, 7:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2021, 7:42 AM
math-fehr retitled this revision from Add SFINAE in unique_function to reference incomplete types in arguments to Allow incomplete template types in unique_function arguments.May 18 2021, 9:09 AM
math-fehr edited the summary of this revision. (Show Details)

Update tests and comments

Remove unrelated change made by clang-format

yrouban accepted this revision.May 18 2021, 7:21 PM

lgtm

llvm/include/llvm/ADT/FunctionExtras.h
109

If it was defined as a new template AdjustedParamTBase then the AdjustedParamT could be

template<typename T> using AdjustedParamT = typename AdjustedParamTBase<T>::type;

So we would not need further changes adding '::type' to AdjustedParamT.

This revision is now accepted and ready to land.May 18 2021, 7:21 PM
DaniilSuchkov added inline comments.May 18 2021, 9:44 PM
llvm/include/llvm/ADT/FunctionExtras.h
110–114

std::is_reference is true not only for l-value references, i.e. if you try adding static_assert(!std::is_reference<T>::value); after this declaration, it'll fail. So it'd be probably better to keep that.
Also please add static_assert(!std::is_lvalue_reference<T>::value); to make the intentions clear.

DaniilSuchkov added inline comments.May 18 2021, 9:57 PM
llvm/include/llvm/ADT/FunctionExtras.h
110–114

So it'd be probably better to keep that.

Probably adding a specialization for T && would be a better idea. In that case I would recommend adding static_assert(!std::is_reference<T>::value); to the original definition (instead of static_assert(!std::is_lvalue_reference<T>::value); recommended above). And a test case with T && would be nice too.

math-fehr updated this revision to Diff 346479.May 19 2021, 8:54 AM

Address comments.

  • Rename AdjustedParamT to AdjustedParamTBase and define AdjustedParamT alias
  • Specialize also for rvalues
  • Add static assert to make intentions clear
  • Add rvalue tests, and renamed class to better express meaning
math-fehr marked 3 inline comments as done.May 19 2021, 9:05 AM

Thanks, I updated the patch with the comments.
Also, I do not have commit access, so could someone land it for me?

I'm ok with the patch and will land it. Lets wait for @DaniilSuchkov's resolution.

yrouban closed this revision.May 21 2021, 12:15 AM

I have just committed the patch as
https://reviews.llvm.org/rGe3eaff10b29d8bb85a0d09e6bb72fbdb7cc9c3ea
Sorry I forget to add the 'Differential Revision:' line to the comment.