[Bug c++/80783] New: Inconsistent constexpr diagnostic for some cstdlib functions

2017-05-16 Thread victor.nawothnig at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80783

Bug ID: 80783
   Summary: Inconsistent constexpr diagnostic for some cstdlib
functions
   Product: gcc
   Version: unknown
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: victor.nawothnig at gmail dot com
  Target Milestone: ---

A set of functions from cstdlib suffers from incorrect constexpr diagnostic
"call to non-constexpr function"

#include 
constexpr size_t f() { exit(0); return 1; }
/* Bad: Silently compiles */

When f() is being evaluated in a constant expression, the diagnostic properly
triggers again, this reduces the impact of this bug:

#include 
constexpr size_t f() { exit(0); return 1; }
int arr[f()];
/* Good: triggers diagnostic */

Interestingly the error seems to be tied to specific function signatures,
rather than the standard library headers. Given the following (without
including any headers):

extern "C" void free(int);
constexpr void f() { free(0); }
/* Good: triggers diagnostic, free has a non-standard signature */

extern "C" void free(void *);
constexpr void f() { free(0); }
/* Bad: silently compiles, free has a standard signature */

It also is limited to only a particular set of cstdlib functionsincluding but
not limited to: free, malloc, exit, atoi.  I have not found any other affected
headers yet, but I did not do an exhaustive search.

[Bug c++/80783] missing constexpr diagnostic for functions that cannot be constexpr

2017-05-18 Thread victor.nawothnig at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80783

--- Comment #2 from Victor Nawothnig  ---
Except all the following examples violate the core constant expression
constraint ([expr.const] 2.3), but GCC diagnoses only some:

```
$ echo '#include  \n constexpr int f() { atoi(""); return 1; }' |
g++-6 -x c++ -c -
: In function 'constexpr int f()':
:2:26: error: call to non-constexpr function 'int atoi(const char*)'
$ echo '#include  \n constexpr int f() { atexit(0); return 1; }' |
g++-6 -x c++ -c -
: In function 'constexpr int f()':
:2:28: error: call to non-constexpr function 'int atexit(void (*)())'
$ echo 'void g(); constexpr int f() { g(); return 1; }' | g++-6 -x c++ -c -
: In function 'constexpr int f()':
:1:32: error: call to non-constexpr function 'void g()'
$ echo '#include  \n constexpr int f() { calloc(0,0); return 1; }' |
g++-6 -x c++ -c -
(success)
$ echo '#include  \n constexpr int f() { malloc(0); return 1; }' |
g++-6 -x c++ -c -
(success)
$ echo '#include  \n constexpr int f() { free(0); return 1; }' | g++-6
-x c++ -c -
(success)
```


And regarding your last argument, here is to show that this is not the case. It
seems like something about the signature influences how GCC applies its
constexpr diagnostic.
```
$ echo 'extern "C" void free(void *); constexpr int f() { free(0); return 1; }'
| g++-6 -x c++ -c -
(success)
$ echo 'extern "C" int free(double); constexpr int f() { free(0); return 1; }'
| g++-6 -x c++ -c -
: In function 'constexpr int f()':
:1:54: error: call to non-constexpr function 'int free(double)'
```
```
$ echo 'extern "C" void exit (int); constexpr int f() { exit(0); return 1; }' |
g++-6 -x c++ -c -
(success)
$ echo 'extern "C" void exit (double); constexpr int f() { exit(0); return 1;
}' | g++-6 -x c++ -c -
: In function 'constexpr int f()':
:1:56: error: call to non-constexpr function 'void exit(double)'
```

That test came from trying to narrow it down to either the libstdc++-v3 or g++.

[Bug c++/80783] missing constexpr diagnostic for functions that cannot be constexpr

2017-05-18 Thread victor.nawothnig at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80783

--- Comment #3 from Victor Nawothnig  ---
Created attachment 41384
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41384&action=edit
Examples

All testcases and their results attached for better readability