Bug 54020 - [c++0x] incorrectly accepted constexpr functions
Summary: [c++0x] incorrectly accepted constexpr functions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: 4.8.0
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-18 19:34 UTC by Andy Lutomirski
Modified: 2012-07-25 15:04 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-07-25 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Lutomirski 2012-07-18 19:34:06 UTC
Tested on a somewhat recent trunk build as well as 4.7-some branch version.

// Preliminaries.
extern int nonconst_func(int);
constexpr int identity(int x) { return x; }
constexpr int zero() { return identity(0); }
constexpr int one() { return identity(1); }

// Correctly accepted.
constexpr int three = one() ? 3 : nonconst_func(0);

// Incorrectly accepted.  See [dcl.constexpr] #5:
//   For a constexpr function, if no function argument values exist
//   such that the function invocation sub-stitution would produce a
//   constant expression (5.19), the program is ill-formed; no diagnostic
//   required.
constexpr int bogus() { return zero () ? 3 : nonconst_func(0); }

// Correctly rejected (not sure why).
constexpr int correct_error() { return nonconst_func(0); }

// Correctly rejected.
constexpr int z = bogus();

// This is also correctly rejected.
constexpr int correct_failure() { return 0 ? 3 : nonconst_func(0); }
Comment 1 Jakub Jelinek 2012-07-19 06:40:41 UTC
... the program is ill-formed; no diagnostic required.
                               ^^^^^^^^^^^^^^^^^^^^^^^
GCC doesn't immediately see that the first operand of ?: is zero or non-zero in that function, so assumes it could be either zero or non-zero and doesn't diagnose, you get diagnostics only when you are actually using the function in some constexpr var initializer or other context where a constant expression is required.
Comment 2 Andy Lutomirski 2012-07-19 15:41:51 UTC
I clearly failed at reading comprehension yesterday.

Maybe this should be considered as more of an enhancement request (like PR54021): it would be nicer for the user if constexpr worked the same with an without optimization.  Otherwise there'll probably be reports of code that builds at -O2 but not -O0.
Comment 3 Paolo Carlini 2012-07-25 09:39:36 UTC
Let's add Jason in CC (about the -O0 vs -O2 thing, in particular)
Comment 4 Jason Merrill 2012-07-25 13:53:22 UTC
This testcase behaves the same at -O0 or -O2.  Seems like a simple enough enhancement, though.
Comment 5 Jason Merrill 2012-07-25 14:57:01 UTC
Author: jason
Date: Wed Jul 25 14:56:57 2012
New Revision: 189851

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189851
Log:
	PR c++/54020
	* semantics.c (potential_constant_expression_1) [COND_EXPR]: Call
	maybe_constant_value.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-neg2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
Comment 6 Jason Merrill 2012-07-25 15:04:27 UTC
Fixed for 4.8.