Skip to content

Calling nullptr function pointer in a constexpr function results in poor diagnostic #59872

@tbaederr

Description

@tbaederr

See: https://godbolt.org/z/EY7f6M5bE

constexpr int foo(int (*bla)(void)) {
  return bla();
}

static_assert(foo(nullptr) == 1);

Output:

<source>:5:15: error: static assertion expression is not an integral constant expression
static_assert(foo(nullptr) == 1);
              ^~~~~~~~~~~~~~~~~
<source>:2:10: note: subexpression not valid in a constant expression
  return bla();
         ^
<source>:5:15: note: in call to 'foo(nullptr)'
static_assert(foo(nullptr) == 1);
              ^
1 error generated.

"subexpression not valid in a constant expression" is a pretty poor diagnostic for this case. When passing a non-constexpr function to foo(), clang manages to print a proper diagnostic:

<source>:9:15: error: static assertion expression is not an integral constant expression
static_assert(foo(f) == 1);
              ^~~~~~~~~~~
<source>:6:10: note: non-constexpr function 'f' cannot be used in a constant expression
  return bla();
         ^
<source>:9:15: note: in call to 'foo(&f)'
static_assert(foo(f) == 1);
              ^
<source>:2:5: note: declared here
int f() { return 1; }
    ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions