Out-of-line definition does not match any declaration error message could be improved
| Bugzilla Link | 13166 |
| Version | trunk |
| OS | All |
| Reporter | LLVM Bugzilla Contributor |
| CC | @dwblaikie,@zygoloid,@Weverything |
Extended Description
It would be nice if the Clang could produce a better error message for the following code:
struct Test {
typedef int T;
template <typename T>
void test(T value);
};
template <typename T>
void Test::test(T value) {}
Currently the error message only reads
test.cpp:10:12: error: out-of-line definition of 'test' does not match any declaration in 'Test'
void Test::test(T value) {}
^~~~
1 error generated.
Got a ping on this internally, might be worth looking into
Here's another minimal example of the same error message where GCC's description seems better.
Assume we have the following files (can happen in a real scenario when "a" is installed (like /usr/local/include) and "b" comes from a new version of the same software):
file.cpp
#include "file.h"
namespace Ns {
Cl::Cl(int a, int b) { a = 0; };
Cl::~Cl() {};
}
a/file.h
namespace Ns {
class Cl {
Cl(int a);
~Cl();
};
}
b/file.h
namespace Ns {
class Cl {
Cl(int a, int b);
~Cl();
};
}
The following message from clang doesn't tell much:
> clang++ -I./a -I./b -c file.cpp
file.cpp:4:9: error: out-of-line definition of 'Cl' does not match any declaration in 'Ns::Cl'
Cl::Cl(int a, int b) { a = 0; };
^~
1 error generated.
While the following one clearly shows the files where definitions come from and makes it evident right away that the order of includes was messed up:
> llvm-g++ -I./a -I./b -c file.cpp
file.cpp:4: error: prototype for ‘Ns::Cl::Cl(int, int)’ does not match any in class ‘Ns::Cl’
./a/file.h:2: error: candidates are: Ns::Cl::Cl(const Ns::Cl&)
./a/file.h:3: error: Ns::Cl::Cl(int)
I just got bit by this.
struct Expr;
struct StringLiteral;
struct Sema { void foo(StringLiteral*); };
void Sema::foo(Expr*) {}
We say:
<stdin>:1:90: error: out-of-line definition of 'foo' does not match any declaration in 'Sema'
g++ does better than this:
<stdin>:1:84: error: prototype for ‘void Sema::foo(Expr*)’ does not match any in class ‘Sema’
<stdin>:1:55: error: candidate is: void Sema::foo(StringLiteral*)
Did no one resolve this.
Bisected: the reported behavior no longer reproduces starting with https://github.com/llvm/llvm-project/commit/d1446017f3fdc2f6a9efba222008d20afa1e26cc.
Godbolt: https://godbolt.org/z/qEK819vbW
- Clang 10.0.1: Reports
out-of-line definition of 'test' does not match any declaration in 'Test'at line 9 - Clang 11.0.0: Accepts the out-of-line
Test::testdefinition - Clang (trunk): Accepts the out-of-line
Test::testdefinition
This is only an observed change in behavior. I have not checked whether this commit addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.
Prepared with assistance from Codex.