https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52321

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Untested patch:

--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -7348,8 +7348,19 @@ build_static_cast (tree type, tree oexpr, tsubst_flags_t
complain)
     }

   if (complain & tf_error)
-    error ("invalid static_cast from type %qT to type %qT",
-           TREE_TYPE (expr), type);
+    {
+      error ("invalid static_cast from type %qT to type %qT",
+            TREE_TYPE (expr), type);
+      if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
+         || !COMPLETE_TYPE_P (TREE_TYPE (type)))
+       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
+               "class type %qT is incomplete", TREE_TYPE (type));
+      tree expr_type = TREE_TYPE (expr);
+      if ((TYPE_PTR_P (expr_type) || TYPE_REF_P (expr_type))
+         || !COMPLETE_TYPE_P (TREE_TYPE (expr_type)))
+       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (expr_type))),
+               "class type %qT is incomplete", TREE_TYPE (expr_type));
+    }
   return error_mark_node;
 }


For the original example this produces:

53231.cc: In function 'int main()':
53231.cc:5:31: error: invalid static_cast from type 'foo*' to type 'bar*'
    5 |   bar* b = static_cast<bar*>(f);
      |                               ^
53231.cc:1:7: note: class type 'foo' is incomplete
    1 | class foo;
      |       ^~~


And for the example from Bug 88503:

88503.cc: In function 'Derived* foo(Parent*)':
88503.cc:6:39: error: invalid static_cast from type 'Parent*' to type
'Derived*'
    6 |         return static_cast<Derived*>(p);
      |                                       ^
88503.cc:2:7: note: class type 'Derived' is incomplete
    2 | class Derived;
      |       ^~~~~~~
88503.cc:1:7: note: class type 'Parent' is incomplete
    1 | class Parent;
      |       ^~~~~~

Reply via email to