Ensure function_refs are copyable even from non-const references
authorDavid Blaikie <dblaikie@gmail.com>
Wed, 12 Nov 2014 02:06:08 +0000 (02:06 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 12 Nov 2014 02:06:08 +0000 (02:06 +0000)
commitd9078385de7188165707668ccce250dcca19a834
tree4f2bc43b96f201d65164bee248f3b3c8799a44f4
parent6a222ec8935e94defb7e096d8ce4069e69b42433
Ensure function_refs are copyable even from non-const references

A subtle bug was found where attempting to copy a non-const function_ref
lvalue would actually invoke the generic forwarding constructor (as it
was a closer match - being T& rather than the const T& of the implicit
copy constructor). In the particular case this lead to a dangling
function_ref member (since it had referenced the function_ref passed by
value to its ctor, rather than the outer function_ref that was still
alive)

SFINAE the converting constructor to not be considered if the copy
constructor is available and demonstrate that this causes the copy to
refer to the original functor, not to the function_ref it was copied
from. (without the code change, the test would fail as Y would be
referencing X and Y() would see the result of the mutation to X, ie: 2)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221753 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/ADT/STLExtras.h
unittests/ADT/CMakeLists.txt
unittests/ADT/FunctionRefTest.cpp [new file with mode: 0644]