54020 notes that we were accepting a constexpr function that is
ill-formed, no diagnostic required, but it would be easy to give a
diagnostic for.
54086 suggests that the standard seems to allow constexpr and const
together.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 92be1e0a23c735f8031c4fa70e55fbc4de5be3be
Author: Jason Merrill <ja...@redhat.com>
Date: Wed Jul 25 09:48:58 2012 -0400
PR c++/54020
* semantics.c (potential_constant_expression_1) [COND_EXPR]: Call
maybe_constant_value.
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 6381949..b27e8ab 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -8497,10 +8497,17 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
case TRUTH_ORIF_EXPR:
tmp = boolean_false_node;
truth:
- if (TREE_OPERAND (t, 0) == tmp)
- return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
- else
- return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
+ {
+ tree op = TREE_OPERAND (t, 0);
+ if (!potential_constant_expression_1 (op, rval, flags))
+ return false;
+ if (!processing_template_decl)
+ op = maybe_constant_value (op);
+ if (tree_int_cst_equal (op, tmp))
+ return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
+ else
+ return true;
+ }
case PLUS_EXPR:
case MULT_EXPR:
@@ -8556,7 +8563,9 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
tmp = TREE_OPERAND (t, 0);
if (!potential_constant_expression_1 (tmp, rval, flags))
return false;
- else if (integer_zerop (tmp))
+ if (!processing_template_decl)
+ tmp = maybe_constant_value (tmp);
+ if (integer_zerop (tmp))
return potential_constant_expression_1 (TREE_OPERAND (t, 2),
want_rval, flags);
else if (TREE_CODE (tmp) == INTEGER_CST)
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-neg2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-neg2.C
new file mode 100644
index 0000000..793b4c3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-neg2.C
@@ -0,0 +1,27 @@
+// PR c++/54020
+// { dg-do compile { target c++11 } }
+
+// 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); } // { dg-error "nonconst_func" }
+
+// Correctly rejected (not sure why).
+constexpr int correct_error() { return nonconst_func(0); } // { dg-error "nonconst_func" }
+
+// Correctly rejected.
+constexpr int z = bogus(); // { dg-error "" }
+
+// This is also correctly rejected.
+constexpr int correct_failure() { return 0 ? 3 : nonconst_func(0); } // { dg-error "nonconst_func" }
commit 2b151043b8e3259d5ec3cb0d6da243317c2a7d68
Author: Jason Merrill <ja...@redhat.com>
Date: Wed Jul 25 10:18:34 2012 -0400
PR c++/54086
* decl.c (grokdeclarator): Allow const and constexpr together.
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index c37787b..047b2fe 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -9528,8 +9528,6 @@ grokdeclarator (const cp_declarator *declarator,
the object as `const'. */
if (constexpr_p && innermost_code != cdk_function)
{
- if (type_quals & TYPE_QUAL_CONST)
- error ("both %<const%> and %<constexpr%> cannot be used here");
if (type_quals & TYPE_QUAL_VOLATILE)
error ("both %<volatile%> and %<constexpr%> cannot be used here");
if (TREE_CODE (type) != REFERENCE_TYPE)
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-const1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-const1.C
new file mode 100644
index 0000000..6ee7225
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-const1.C
@@ -0,0 +1,7 @@
+// PR c++/54086
+// { dg-do compile { target c++11 } }
+
+static constexpr const char Data[] = {
+ 'D', 'A', 'T', 'A',
+};
+static constexpr const char *data_func() { return Data; }
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C
index 4ff398b..6c9d466 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C
@@ -18,8 +18,7 @@ extern constexpr int i2; // { dg-error "definition" }
// error: missing initializer
constexpr A1 a2; // { dg-error "uninitialized const" }
-// error: duplicate cv
-const constexpr A1 a3 = A1(); // { dg-error "both .const. and .constexpr. cannot" }
+const constexpr A1 a3 = A1();
volatile constexpr A1 a4 = A1(); // { dg-error "both .volatile. and .constexpr. cannot" }