llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Out-of-line definition does not match any declaration error message could be improved

Open llvmbot opened this issue 14 years ago • 5 comments

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.

llvmbot avatar Jun 21 '12 21:06 llvmbot

Got a ping on this internally, might be worth looking into

dwblaikie avatar Feb 20 '16 19:02 dwblaikie

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)

llvmbot avatar Feb 25 '14 05:02 llvmbot

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.

Ma-gi-cian avatar Apr 14 '26 11:04 Ma-gi-cian

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::test definition
  • Clang (trunk): Accepts the out-of-line Test::test definition

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.

hongtaihu avatar Nov 08 '26 14:11 hongtaihu