From patchwork Tue Feb 13 21:11:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 873187 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-473214-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ivXwAm8B"; 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 3zgx5w6CyPz9t3B for ; Wed, 14 Feb 2018 08:49:56 +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:reply-to:mime-version :content-type; q=dns; s=default; b=cqFNksHA3ceCCzFx9Qzabz+HBqIUN uUpeGKM/PfgCtlrIv4/s33wRKeD39qWoE7SDqvU+gUKRgFajSdzKNtyAYutQMbch IlFjDVW+wV3qLDScbLgORHjSvHmTtvjDm9wnPQGBcCYgo4wJPgg6Y2mB6XAM/Rnp ZDUzL+81ulpsdU= 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:reply-to:mime-version :content-type; s=default; bh=Xxc0UHKG7CWEOC2xT0fK2UnJMVg=; b=ivX wAm8BnApHLmobo0HEskm2pZNhdU0ZYzKy81BIpoKjo1RyMf29T+9U2je4cg+ThxP WshrVgoLlswZvy4Ckba8CT5XV25mWW2jhw1Q0D+coZLqMyHgoXm34xplwRwAALZU i52OhKqgrKVLk0wGqT44rYFv/xAkEY4Qx8wzhwXY= Received: (qmail 125498 invoked by alias); 13 Feb 2018 21:49:43 -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 125410 invoked by uid 89); 13 Feb 2018 21:49:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=Effective X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 13 Feb 2018 21:49:41 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9E2944084FEF for ; Tue, 13 Feb 2018 21:49:29 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-85.brq.redhat.com [10.40.204.85]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 586CF1007D67 for ; Tue, 13 Feb 2018 21:49:29 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id w1DLBU0v003472; Tue, 13 Feb 2018 22:11:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id w1DLBTXm003471; Tue, 13 Feb 2018 22:11:29 +0100 Date: Tue, 13 Feb 2018 22:11:29 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix -Weffc++ warning on return in assignment op in templates (PR c++/84364) Message-ID: <20180213211129.GV5867@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) X-IsSubscribed: yes Hi! Before r253599 check_return_expr would: if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))) || (retval != NULL_TREE && type_dependent_expression_p (retval))) return retval; when processing_template_decl, and thus not issue the warning, but now even type dependent expressions can make it into the assignment operator -Weffc++ warning checks. There are multiple problems, e.g. in template struct A { T &operator=(T &f) { return *this; // { dg-bogus "should return a reference to" } } }; already the: if (TREE_CODE (valtype) == REFERENCE_TYPE && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) test will fail, even when one can instantiate it with T = A. The bigger problem is: /* Returning '*this' is obviously OK. */ if (retval == current_class_ref) warn = false; when retval is type dependent expression, it will be usually? built with build_min and thus == comparison will not work. The following patch just does what we used to do, not warn with -Weffc++ for type dependent retval expressions, defer that to instantiation. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2018-02-13 Jakub Jelinek PR c++/84364 * typeck.c (check_return_expr): Don't emit -Weffc++ warning about return other than *this in assignment operators if retval is type dependent expression. * g++.dg/warn/effc4.C: New test. Jakub --- gcc/cp/typeck.c.jj 2018-02-10 00:15:36.702163326 +0100 +++ gcc/cp/typeck.c 2018-02-13 16:51:04.570204418 +0100 @@ -9232,7 +9232,8 @@ check_return_expr (tree retval, bool *no /* Effective C++ rule 15. See also start_function. */ if (warn_ecpp - && DECL_NAME (current_function_decl) == assign_op_identifier) + && DECL_NAME (current_function_decl) == assign_op_identifier + && !type_dependent_expression_p (retval)) { bool warn = true; --- gcc/testsuite/g++.dg/warn/effc4.C.jj 2018-02-13 16:53:16.399220269 +0100 +++ gcc/testsuite/g++.dg/warn/effc4.C 2018-02-13 16:55:10.472233986 +0100 @@ -0,0 +1,10 @@ +// PR c++/84364 +// { dg-do compile } +// { dg-options "-Weffc++" } + +template +struct A { + A &operator=(A& f) { + return *this; // { dg-bogus "should return a reference to" } + } +};