<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/97077>97077</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-Wunneeded-internal-declaration doesn't consider concept restraints
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bjadamson
</td>
</tr>
</table>
<pre>
The following program demonstrates that a constraint requires the implementation of operator== for a type when emitting a -unneeded-internal-declaration warning with the following program.
Commenting out the line the compiler states is unnecessary causes a compiler error, and keeping it persists the warning.
```C++
#include <cassert>
#include <concepts>
#include <type_traits>
namespace
{
template <typename T>
concept EqualityComparable = requires(T a, T b)
{
{ a == b } -> std::convertible_to<bool>;
};
template <EqualityComparable T, EqualityComparable U>
auto constexpr
MY_subtract(T const lhs, U const rhs) noexcept
{
auto const result = lhs.value - rhs.value;
return result;
}
struct CustomType final
{
int value;
};
// The line in question:
bool operator==(CustomType const& a, CustomType const& b) noexcept { return a.value == b.value; }
} // namespace
int main(int, char**)
{
CustomType a{10}, b{3};
auto result = MY_subtract(a, b);
assert(7 == result);
}
```
Removing the line from the program (as suggested by the warning) yields the following compiler warning:
```bash
error: no matching function for call to 'MY_subtract'
33 | auto result = MY_subtract(a, b);
| ^~~~~~~~~~~
test/core_value.cxx:11:1: note: candidate template ignored: constraints not satisfied [with T = CustomType, U = CustomType]
11 | MY_subtract(T const lhs, U const rhs) noexcept
| ^
test/core_value.cxx:9:11: note: because '(anonymous namespace)::CustomType' does not satisfy 'EqualityComparable'
9 | template <EqualityComparable T, EqualityComparable U>
| ^
test/core_value.cxx:6:7: note: because 'a == b' would be invalid: invalid operands to binary expression ('(anonymous namespace)::CustomType' and '(anonymous namespace)::CustomType')
6 | { a == b } -> std::convertible_to<bool>;
```
However keeping the line in the program yields the following error:
```bash
[1/2] Building CXX object test/CMakeFiles/core-tests.dir/core_value.cxx.o
test/core_value.cxx:25:6: warning: function 'operator==' is not needed and will not be emitted [-Wunneeded-internal-declaration]
25 | bool operator==(CustomType const& a, CustomType const& b) noexcept { return a.value == b.value; }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV02P4jwS_jXmUgIlTkPgwKGBRnuZy4rRzp5aTlIQzzh2xnaguexvX5XzQRp6ZjTzvtKLonzYLlc99fG4EM7Jk0Zcs_mGzXcT0fjS2HX2VRSickZPMlNc14cS4WiUMhepT1Bbc7KiggIro523wqMDXwoPAvJ2RGoPFr830oYpBFnVCivUXnhpNJgjmBqt8MayZMeSHRyNBQH-WiNcStSAlfSetAmYNlojFlhMpfZotVDTAnMlbLvXRVhNCy_Sl0HXg6UzFu1Y9Nzet6YiO2jaND4IKKkxvOSmqqVCC84HUNIB6c7ROWGvkIvGoQsou3VorbGMb0HoAr4h1rSt9FCjddL5Fntn4Dsr2CJqry3jG7raUZ5InaumQGDJNhfOofUseflw1ugca-9-ME2efKVIjFeEuxYVulrk2A2mnXKPVa2EH6RpHRwG4U4fvHxvhJL-ujVVLazIFAnshmgzvjyAIJccIGN8dacEgKUbENBFPQOW7mDKkhdwvmDJM0tI0Rmtl5nCV29Yss2MUWRG0nsp3d3eH0z_wL4DmfPB-OcBnGi8aXMX32rbjn3676trMm9F7gOoMA2qdLTb5-7T0ucKtME38s4D2tvGYNE1ygdnqdLNzkI1CFPaoX0fMAFY9I3VncQ73CPQztsm97BtnDfVgQrnKLVQDyZQLb5X8OhAxveM7-HQF4PU8L1BR_VFQQlrKAx3Vcv4cqQ-wGR80Yb_o4ls7KuQCR1S0bmjT4vBI3ADne6gM_M-g8OdYFZCasaXUnuyIC-FZfw5XI95OLJPsHQTR6SJbyFj6SYZe6gL4ih87zNDtFJ8NZZoK5cv0x5SF8vRqhuyngvGcP6NlTkTmwwEdbSmCl89_5JuB645ndB5LCC7jumGXH2VqAp3R4oDefUL-wDfWZMJV7ZDLcslz6ANVMLnJW1zbHQe-JeYOxdKgTf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