PR c++/66297, DR 1684 - literal class and constexpr member fns

* constexpr.c (is_valid_constexpr_fn): Only complain about
	non-literal enclosing class in C++11.
	* class.c (finalize_literal_type_property): Likewise.

From-SVN: r248752
This commit is contained in:
Jason Merrill 2017-05-31 13:52:58 -04:00 committed by Jason Merrill
parent f0e95f2ca0
commit 0634d9fe36
9 changed files with 35 additions and 21 deletions

View File

@ -1,5 +1,10 @@
2017-05-31 Jason Merrill <jason@redhat.com>
PR c++/66297, DR 1684 - literal class and constexpr member fns
* constexpr.c (is_valid_constexpr_fn): Only complain about
non-literal enclosing class in C++11.
* class.c (finalize_literal_type_property): Likewise.
PR c++/80179 - ICE with initialized flexible array member.
* constexpr.c (verify_ctor_sanity): Handle flexible array members.

View File

@ -5758,7 +5758,9 @@ finalize_literal_type_property (tree t)
&& !TYPE_HAS_CONSTEXPR_CTOR (t))
CLASSTYPE_LITERAL_P (t) = false;
if (!CLASSTYPE_LITERAL_P (t))
/* C++14 DR 1684 removed this restriction. */
if (cxx_dialect < cxx14
&& !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
if (DECL_DECLARED_CONSTEXPR_P (fn)
&& TREE_CODE (fn) != TEMPLATE_DECL
@ -5766,12 +5768,11 @@ finalize_literal_type_property (tree t)
&& !DECL_CONSTRUCTOR_P (fn))
{
DECL_DECLARED_CONSTEXPR_P (fn) = false;
if (!DECL_GENERATED_P (fn) && !LAMBDA_TYPE_P (t))
{
error ("enclosing class of constexpr non-static member "
"function %q+#D is not a literal type", fn);
explain_non_literal_class (t);
}
if (!DECL_GENERATED_P (fn)
&& pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
"enclosing class of constexpr non-static member "
"function %q+#D is not a literal type", fn))
explain_non_literal_class (t);
}
}

View File

@ -209,16 +209,17 @@ is_valid_constexpr_fn (tree fun, bool complain)
}
}
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
/* C++14 DR 1684 removed this restriction. */
if (cxx_dialect < cxx14
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
&& !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
{
ret = false;
if (complain)
{
error ("enclosing class of constexpr non-static member "
"function %q+#D is not a literal type", fun);
explain_non_literal_class (DECL_CONTEXT (fun));
}
if (complain
&& pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
"enclosing class of constexpr non-static member "
"function %q+#D is not a literal type", fun))
explain_non_literal_class (DECL_CONTEXT (fun));
}
}
else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))

View File

@ -36,7 +36,7 @@ class debug_flag
{
public:
explicit debug_flag(bool);
constexpr bool is_on(); // { dg-error "enclosing class .* not a literal type" }
constexpr bool is_on(); // { dg-error "enclosing class .* not a literal type" "" { target c++11_only } }
private:
bool flag;
};

View File

@ -5,7 +5,7 @@ template <class T>
struct A
{
T t;
constexpr int f() const { return 42; } // { dg-error "enclosing class" }
constexpr int f() const { return 42; } // { dg-error "enclosing class" "" { target c++11_only } }
};
struct B { B(); operator int(); };
@ -13,7 +13,7 @@ struct B { B(); operator int(); };
constexpr A<int> ai = { 42 };
constexpr int i = ai.f();
constexpr int b = A<B>().f(); // { dg-error "non-constexpr function" }
constexpr int b = A<B>().f(); // { dg-error "" }
template <class T>
constexpr int f (T t) { return 42; } // { dg-error "parameter" }

View File

@ -16,7 +16,7 @@ int main()
struct complex // { dg-message "no constexpr constructor" }
{
complex(double r, double i) : re(r), im(i) { }
constexpr double real() const { return re; } // { dg-error "not a literal type" }
constexpr double real() const { return re; } // { dg-error "not a literal type" "" { target c++11_only } }
double imag() const { return im; }
private:
@ -25,7 +25,7 @@ private:
};
constexpr complex co1(0, 1); // { dg-error "not literal" }
constexpr double dd2 = co1.real(); // { dg-error "non-constexpr function" }
constexpr double dd2 = co1.real(); // { dg-error "" }
// --------------------

View File

@ -13,6 +13,6 @@ constexpr X X::g(X x) { return x; }
struct Y
{
Y() { }
constexpr Y f(Y y); // { dg-error "not a literal type" }
constexpr Y f(Y y) {} // { dg-error "constexpr" }
static constexpr Y g(Y y) {} // { dg-error "constexpr" }
};

View File

@ -45,7 +45,7 @@ constexpr int g(int x, int n) {
class debug_flag {
public:
explicit debug_flag(bool);
constexpr bool is_on(); // { dg-error "not a literal type" } debug_flag not literal type
constexpr bool is_on(); // { dg-error "not a literal type" "" { target c++11_only } } debug_flag not literal type
private:
bool flag;
};

View File

@ -0,0 +1,7 @@
// DR 1684
// { dg-do compile { target c++11 } }
struct A {
A(int);
constexpr int foo() { return 0; } // { dg-error "literal" "" { target c++11_only } }
};