From patchwork Mon Dec 17 15:37:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1014587 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-492654-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Gcu66OYz"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43JQKD5LzWz9sD4 for ; Tue, 18 Dec 2018 02:38:07 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=JdTjZ4/G6dYn6KTf2CtCbZbsF9nQHcPTnxA0XDcPjo4WD6W4U5 w8ya3zRbnWKQYffo3f5hOp3jkzm5XRHv8VPGq2VTSpvdnj7Si2KZS+A/pqVCije4 L8TpnRsCmRX82PwMUxkP9JJDRRwXXAuQJv/qHlzu7M9Wj0sK7s7cYlXaQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=W85fxlIZ59poeKHgVcT6AOr93aE=; b=Gcu66OYzEKB68Sd8y/fg rv6NXGJghVZBFDWFN85GDl5ZDMMWHdgYYbRehcfjNyMPPExVqXyhyD4hBmd0mWP+ qNRUn+UTO8T295VhYctkeNsTjF/PtCnympI+K1z3MJ36tMNrBGY5h3Id1S0gPUBt tAQcKbYu58R8GI5A0cev0EA= Received: (qmail 75550 invoked by alias); 17 Dec 2018 15:38:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 75535 invoked by uid 89); 17 Dec 2018 15:37:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 17 Dec 2018 15:37:55 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8BA6C89AD4; Mon, 17 Dec 2018 15:37:54 +0000 (UTC) Received: from localhost (unknown [10.33.36.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 355CE5D744; Mon, 17 Dec 2018 15:37:54 +0000 (UTC) Date: Mon, 17 Dec 2018 15:37:53 +0000 From: Jonathan Wakely To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , nathan@acm.org Subject: [PATCH] PR c++/52321 print note for static_cast to/from incomplete type Message-ID: <20181217153753.GA28463@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.10.1 (2018-07-13) If build_static_cast_1 returns false, and one or both type is a pointer/reference to an incomplete class, print a note saying so. This doesn't attempt to check whether the static_cast failed because the type is incomplete (which was checked inside build_static_cast_1 but not in the caller). It just informs the user that it is incomplete, which is probably the most likely reason. 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. Tested powerpc64le-linux. OK for trunk? commit c76d6fb5cfb37c2aed387def151d9f707306128a Author: Jonathan Wakely Date: Mon Dec 17 15:01:21 2018 +0000 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. 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" } +}