From f4d458f3fab13daa8a8adbc7165ee4a7050db3f6 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 17 Dec 2018 21:49:58 +0000 Subject: [PATCH] PR c++/52321 print note for static_cast to/from incomplete type PR c++/52321 * typeck.c (build_static_cast): Print a note when the destination type or the operand is a pointer/reference to incomplete class type. From-SVN: r267219 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/typeck.c | 17 +++++++++++++-- gcc/testsuite/g++.dg/expr/static_cast8.C | 27 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/expr/static_cast8.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d12173e69e9..a7ec887d9c0 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-12-17 Jonathan Wakely + + PR c++/52321 + * typeck.c (build_static_cast): Print a note when the destination + type or the operand is a pointer/reference to incomplete class type. + 2018-12-16 Jakub Jelinek PR c++/88482 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index ac0c81155b5..47ddad16fc1 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -7348,8 +7348,21 @@ 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)) + && CLASS_TYPE_P (TREE_TYPE (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)) + expr_type = TREE_TYPE (expr_type); + if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type)) + inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)), + "class type %qT is incomplete", expr_type); + } return error_mark_node; } diff --git a/gcc/testsuite/g++.dg/expr/static_cast8.C b/gcc/testsuite/g++.dg/expr/static_cast8.C new file mode 100644 index 00000000000..dc4d2162d6c --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/static_cast8.C @@ -0,0 +1,27 @@ +// PR c++/52321 +struct A1; // { dg-message "note: class type 'A1' is incomplete" } +struct A2; // { dg-message "note: class type 'A2' is incomplete" } +struct B1; // { dg-message "note: class type 'B1' is incomplete" } +struct B2; // { dg-message "note: class type 'B2' is incomplete" } + +struct C { }; +extern C* c; + +void pointers(C* c, A2* a2, B1* b1) +{ + (void) static_cast(c); // { dg-error "invalid static_cast" } + (void) static_cast(a2); // { dg-error "invalid static_cast" } + (void) static_cast(b1); // { dg-error "invalid static_cast" } +} + +struct D1; // { dg-message "note: class type 'D1' is incomplete" } +struct D2; // { dg-message "note: class type 'D2' is incomplete" } +struct E1; // { dg-message "note: class type 'E1' is incomplete" } +struct E2; // { dg-message "note: class type 'E2' is incomplete" } + +void references(C& c, D2& d2, E1& e1) +{ + (void) static_cast(c); // { dg-error "invalid static_cast" } + (void) static_cast(d2); // { dg-error "invalid static_cast" } + (void) static_cast(e1); // { dg-error "invalid static_cast" } +} -- 2.30.2