From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 33285 invoked by alias); 17 Jan 2019 19:09:29 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 33266 invoked by uid 89); 17 Jan 2019 19:09:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_NUMSUBJECT,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=check_narrowing 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; Thu, 17 Jan 2019 19:09:26 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D96869FDF9 for ; Thu, 17 Jan 2019 19:09:24 +0000 (UTC) Received: from redhat.com (ovpn-121-44.rdu2.redhat.com [10.10.121.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 375E760BF1; Thu, 17 Jan 2019 19:09:23 +0000 (UTC) Date: Thu, 17 Jan 2019 19:09:00 -0000 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH for c++/78244 - narrowing conversion in template not detected, part 2 Message-ID: <20190117190921.GM19569@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-SW-Source: 2019-01/txt/msg01026.txt.bz2 This patch ought to fix the rest of 78244, a missing narrowing warning in decltype. As I explained in Bugzilla, there can be three scenarios: 1) decltype is in a template and it has no dependent expressions, which is the problematical case. finish_compound_literal just returns the compound literal without checking narrowing if processing_template_decl. But finish_decltype_type sees an expr that is not instantiation-dependent, so it just takes its unlowered_expr_type and returns it, without calling check_narrowing. 2) decltype is in a template and has dependent expressions This works, because while finish_compound_literal just returns the compound literal, finish_decltype_type sees an expr that is instantiation-dependent, so it creates a DECLTYPE_TYPE. tsubst_copy_and_build then calls RETURN (finish_compound_literal (type, r, complain, cl)); while substituting the CONSTRUCTOR. Now processing_template_decl is 0, so finish_compound_literal calls check_narrowing, so we detect it. 3) decltype is not in a template finish_compound_literal calls check_narrowing and the narrowing is detected. I think it's better not to blithely instantiate such decltypes just for the benefit of check_narrowing, but we can still try to check narrowing even in a template. check_narrowing calls instantiation_dependent_expression_p, so it does nothing when there are dependent expressions. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-01-17 Marek Polacek PR c++/78244 - narrowing conversion in template not detected. * semantics.c (finish_compound_literal): When processing a template, try to check narrowing. * g++.dg/cpp0x/Wnarrowing15.C: New test. * g++.dg/cpp1y/Wnarrowing1.C: New test. diff --git gcc/cp/semantics.c gcc/cp/semantics.c index bc9d53800f7..828f1578697 100644 --- gcc/cp/semantics.c +++ gcc/cp/semantics.c @@ -2797,6 +2797,14 @@ finish_compound_literal (tree type, tree compound_literal, if (processing_template_decl) { + /* If there are no dependent expressions, we can detect narrowing + conversions. */ + if (SCALAR_TYPE_P (type) + && CONSTRUCTOR_NELTS (compound_literal) == 1 + && !check_narrowing (type, + CONSTRUCTOR_ELT (compound_literal, 0)->value, + complain)) + return error_mark_node; TREE_TYPE (compound_literal) = type; /* Mark the expression as a compound literal. */ TREE_HAS_CONSTRUCTOR (compound_literal) = 1; diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C new file mode 100644 index 00000000000..4e7c17dcfca --- /dev/null +++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C @@ -0,0 +1,14 @@ +// PR c++/78244 +// { dg-do compile { target c++11 } } + +template +auto f1() -> decltype(int{2.0}, void()) { } // { dg-error "narrowing conversion" } + +template +auto f2() -> decltype(int{2.0}) { return 1; } // { dg-error "narrowing conversion" } + +template +auto f3() -> decltype(void(), int{2.0}) { return 1; } // { dg-error "narrowing conversion" } + +template +auto f4() -> decltype((int{2.0})) { return 1; } // { dg-error "narrowing conversion" } diff --git gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C new file mode 100644 index 00000000000..e1e499542f0 --- /dev/null +++ gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C @@ -0,0 +1,5 @@ +// PR c++/78244 +// { dg-do compile { target c++14 } } + +template +decltype(int{1.1}) v; // { dg-error "narrowing conversion" }