Hi,
it seems to me that in order to fix this accepts-invalid (for the
testcase we don't reject the declaration 'B b;') we have simply to
propagate from bases to derived the CLASSTYPE_READONLY_FIELDS_NEED_INIT
and CLASSTYPE_REF_FIELDS_NEED_INIT flags. Thus, the few class.c lines in
the patch below. Then I think we can also make the error messages in
diagnose_uninitialized_cst_or_ref_member_1 more clear and explicitly
mention the base to which the problematic member belongs.
Tested x86_64-linux.
Thanks,
Paolo.
PS: I struggled a bit with "%qT" vs "%q#T": I would slightly prefer
simply talking about, eg, "base class 'A'", we do that in other cases,
but here we are already using # which automatically picks class vs
struct. Thus I think we should use # for the base too... or remove all
the #s and just talk about, eg, "class 'B'" and "base class 'A'" for
these structs too?
//////////////
/cp
2013-10-06 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/58126
* class.c (check_bases): Propagate CLASSTYPE_READONLY_FIELDS_NEED_INIT
and CLASSTYPE_REF_FIELDS_NEED_INIT from bases to derived.
* init.c (diagnose_uninitialized_cst_or_ref_member_1): Extend error
messages about uninitialized const and references members to mention
the base class.
/testsuite
2013-10-06 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/58126
* g++.dg/init/uninitialized1.C: New.
Index: cp/class.c
===================================================================
--- cp/class.c (revision 203228)
+++ cp/class.c (working copy)
@@ -1517,6 +1517,12 @@ check_bases (tree t,
|= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
|| TYPE_HAS_COMPLEX_DFLT (basetype));
+ SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
+ (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
+ | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
+ SET_CLASSTYPE_REF_FIELDS_NEED_INIT
+ (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
+ | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
/* A standard-layout class is a class that:
...
Index: cp/init.c
===================================================================
--- cp/init.c (revision 203228)
+++ cp/init.c (working copy)
@@ -2120,11 +2120,24 @@ diagnose_uninitialized_cst_or_ref_member_1 (tree t
++ error_count;
if (complain)
{
- if (using_new)
- error ("uninitialized reference member in %q#T "
- "using %<new%> without new-initializer", origin);
+ if (DECL_CONTEXT (field) == origin)
+ {
+ if (using_new)
+ error ("uninitialized reference member in %q#T "
+ "using %<new%> without new-initializer", origin);
+ else
+ error ("uninitialized reference member in %q#T", origin);
+ }
else
- error ("uninitialized reference member in %q#T", origin);
+ {
+ if (using_new)
+ error ("uninitialized reference member in base %q#T "
+ "of %q#T using %<new%> without new-initializer",
+ DECL_CONTEXT (field), origin);
+ else
+ error ("uninitialized reference member in base %q#T "
+ "of %q#T", DECL_CONTEXT (field), origin);
+ }
inform (DECL_SOURCE_LOCATION (field),
"%qD should be initialized", field);
}
@@ -2135,11 +2148,24 @@ diagnose_uninitialized_cst_or_ref_member_1 (tree t
++ error_count;
if (complain)
{
- if (using_new)
- error ("uninitialized const member in %q#T "
- "using %<new%> without new-initializer", origin);
+ if (DECL_CONTEXT (field) == origin)
+ {
+ if (using_new)
+ error ("uninitialized const member in %q#T "
+ "using %<new%> without new-initializer", origin);
+ else
+ error ("uninitialized const member in %q#T", origin);
+ }
else
- error ("uninitialized const member in %q#T", origin);
+ {
+ if (using_new)
+ error ("uninitialized const member in base %q#T "
+ "of %q#T using %<new%> without new-initializer",
+ DECL_CONTEXT (field), origin);
+ else
+ error ("uninitialized const member in base %q#T "
+ "of %q#T", DECL_CONTEXT (field), origin);
+ }
inform (DECL_SOURCE_LOCATION (field),
"%qD should be initialized", field);
}
Index: testsuite/g++.dg/init/uninitialized1.C
===================================================================
--- testsuite/g++.dg/init/uninitialized1.C (revision 0)
+++ testsuite/g++.dg/init/uninitialized1.C (working copy)
@@ -0,0 +1,12 @@
+// PR c++/58126
+
+struct A {
+ const int value1;
+ int& value2;
+};
+
+struct B : A { };
+
+A a; // { dg-error "uninitialized const member in 'struct A'|uninitialized
reference member in 'struct A'" }
+
+B b; // { dg-error "uninitialized const member in base 'struct A' of 'struct
B'|uninitialized reference member in base 'struct A' of 'struct B'" }