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

clang fails to diagnose non-constexpr function call in return statement of constexpr member function

Open tommat01 opened this issue 4 years ago • 1 comments

Example:

struct A { int f() const; };

struct B {
  A a;
  constexpr int should_error_but_will_not() const {
    return a.f(); // gcc will error here, clang will not
  }
  constexpr int does_error() const {
    constexpr int i = a.f(); // both error here
    return i;
  }
};

Here are the outputs from g++9, g++11, and clang-16:

$ g++-9 -std=c++17 input.cpp
        .file   "input.cpp"
../input.cpp: In member function ‘constexpr int B::should_error_but_will_not() const’:
../input.cpp:6:15: error: call to non-‘constexpr’ function ‘int A::f() const’
    6 |     return a.f(); // gcc will error here, clang will not
      |            ~~~^~
../input.cpp: In member function ‘constexpr int B::does_error() const’:
../input.cpp:9:26: error: call to non-‘constexpr’ function ‘int A::f() const’
    9 |     constexpr int i = a.f(); // both error here
      |                       ~~~^~
$ g++-11 -std=c++17 input.cpp
        .file   "input.cpp"
../input.cpp: In member function ‘constexpr int B::should_error_but_will_not() const’:
../input.cpp:6:15: error: call to non-‘constexpr’ function ‘int A::f() const’
    6 |     return a.f(); // gcc will error here, clang will not
      |            ~~~^~
../input.cpp:1:16: note: ‘int A::f() const’ declared here
    1 | struct A { int f() const; };
      |                ^
../input.cpp: In member function ‘constexpr int B::does_error() const’:
../input.cpp:9:26: error: call to non-‘constexpr’ function ‘int A::f() const’
    9 |     constexpr int i = a.f(); // both error here
      |                       ~~~^~
../input.cpp:1:16: note: ‘int A::f() const’ declared here
    1 | struct A { int f() const; };
      |                ^
$ clang++-16 -std=c++17 input.cpp
../input.cpp:9:19: error: constexpr variable 'i' must be initialized by a constant expression
    constexpr int i = a.f(); // both error here
                  ^   ~~~~~
../input.cpp:9:23: note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
    constexpr int i = a.f(); // both error here
                      ^
1 error generated.

Clang will successfully error if any of the following are done:

  • make a global rather than a member
  • f() is a non-member function
  • make should_error_but_will_not a free function rather than a member function

tommat01 avatar Dec 04 '22 10:12 tommat01

We are not required to diagnose this and now gcc only generates a warning: https://godbolt.org/z/41Tav9fsh

Once we use it in a context that requires a constant expression both gcc and clang will produce a hard error.

Although I do think it would be good to diagnose cases that a function marked constexpr could never be invoked in a constexpr context.

shafik avatar Jan 31 '23 19:01 shafik