From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 91705 invoked by alias); 9 Feb 2020 12:51:27 -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 91694 invoked by uid 89); 9 Feb 2020 12:51:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=91465, sees, grok X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 09 Feb 2020 12:51:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581252684; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o0DZJjqRQqTNO1rdgUaDNGyxWKC/smhsUG6cftlKTgQ=; b=GVn78TrwU9yNWgDTe2HhNhvZLyF500OL1KW+xniX3PqryYznclNKWoKLlLzKGyZSqRBeOH kU7MU2ZzKWVRfLkYOIuwjvBEJsZ0kmXRBq3imuzjSo2HEZbyhEt74SSd3s5GWw3mBd5dAF /3LDoXpSxNheZLL2AaUhg7N4NXt1EtQ= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-0XEk0iw1MlmJEhRQuR9oBA-1; Sun, 09 Feb 2020 07:51:17 -0500 Received: by mail-wr1-f72.google.com with SMTP id 50so3153265wrc.2 for ; Sun, 09 Feb 2020 04:51:17 -0800 (PST) Return-Path: Received: from [172.19.106.44] (static-186-95-145-212.ipcom.comunitel.net. [212.145.95.186]) by smtp.gmail.com with ESMTPSA id i16sm11918914wmb.36.2020.02.09.04.51.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 09 Feb 2020 04:51:14 -0800 (PST) Subject: Re: [PATCH] c++: Fix ICE with template codes in check_narrowing [PR91465] To: Marek Polacek , GCC Patches References: <20200207003019.45538-1-polacek@redhat.com> From: Jason Merrill Message-ID: Date: Sun, 09 Feb 2020 12:51:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: <20200207003019.45538-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2020-02/txt/msg00498.txt.bz2 On 2/6/20 7:30 PM, Marek Polacek wrote: > In ed4f2c001a883b2456fc607a33f1c59f9c4ee65d I changed the call to > fold_non_dependent_expr in check_narrowing to maybe_constant_value. > That was the wrong thing to do as these tests show: check_narrowing > bails out for dependent expressions but we can still have template > codes like CAST_EXPR that don't have anything dependent in it so are > considered non-dependent. But cxx_eval_* don't grok template codes, > so we need to call fold_non_dependent_expr instead which knows what > to do with template codes. (I fully accept a "told you so".) > > I'm passing tf_none to it, otherwise we'd emit a bogus error for > constexpr-ex4.C: there INIT is "A::operator int(&a)" and while > instantiating this CALL_EXPR (in a template) we call finish_call_expr > and that sees a BASELINK and so emits a new dummy object for 'this', > and then we complain about the wrong number of arguments, because now > we basically have two 'this's. Which is exactly the problem I saw > recently in c++/92948. Yeah, the problem continues to be that build_converted_constant_expr is breaking the boundary between template and non-template codes: convert_like_real produces trees that aren't suitable for later substitution, so substituting them breaks. Perhaps if we're looking at a non-dependent constant expression in a template, build_converted_constant_expr should instantiate_non_dependent_expr, pass the result to convert_like, and then if successful throw away the result in favor of an IMPLICIT_CONV_EXPR. > Bootstrapped/regtested on x86_64-linux, ok for trunk and 9? > PR c++/91465 - ICE with template codes in check_narrowing. > * typeck2.c (check_narrowing): Call fold_non_dependent_expr > instead of maybe_constant_value. > > * g++.dg/cpp0x/pr91465.C: New test. > * g++.dg/cpp1z/pr91465.C: New test. > --- > gcc/cp/typeck2.c | 4 +++- > gcc/testsuite/g++.dg/cpp0x/pr91465.C | 16 ++++++++++++++++ > gcc/testsuite/g++.dg/cpp1z/pr91465.C | 10 ++++++++++ > 3 files changed, 29 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr91465.C > create mode 100644 gcc/testsuite/g++.dg/cpp1z/pr91465.C > > diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c > index 371b203c29b..8f8e9703ac8 100644 > --- a/gcc/cp/typeck2.c > +++ b/gcc/cp/typeck2.c > @@ -981,7 +981,9 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, > return ok; > } > > - init = maybe_constant_value (init); > + init = fold_non_dependent_expr (init, tf_none); > + if (init == error_mark_node) > + return ok; > > /* If we were asked to only check constants, return early. */ > if (const_only && !TREE_CONSTANT (init)) > diff --git a/gcc/testsuite/g++.dg/cpp0x/pr91465.C b/gcc/testsuite/g++.dg/cpp0x/pr91465.C > new file mode 100644 > index 00000000000..e2021aa13e1 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/pr91465.C > @@ -0,0 +1,16 @@ > +// PR c++/91465 - ICE with template codes in check_narrowing. > +// { dg-do compile { target c++11 } } > + > +enum class D { X }; > +enum class S { Z }; > + > +D foo(S) { return D{}; } > +D foo(double) { return D{}; } > + > +template > +struct Bar { > + D baz(S s) > + { > + return D{foo(s)}; > + } > +}; > diff --git a/gcc/testsuite/g++.dg/cpp1z/pr91465.C b/gcc/testsuite/g++.dg/cpp1z/pr91465.C > new file mode 100644 > index 00000000000..5b1205349d0 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/pr91465.C > @@ -0,0 +1,10 @@ > +// PR c++/91465 - ICE with template codes in check_narrowing. > +// { dg-do compile { target c++17 } } > + > +enum class E { Z }; > + > +template > +void foo(F) > +{ > + E{char(0)}; > +} > > base-commit: cb273d81a45092ceee793f0357526e291f03c7b7 >