From patchwork Thu May 18 17:59:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1783472 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=uylA4wxD; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QMd6R05w2z20dg for ; Fri, 19 May 2023 03:59:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA1CF3857724 for ; Thu, 18 May 2023 17:59:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA1CF3857724 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684432795; bh=dqSk7lWtMMIKmt3djmAon/e2+/6JrGEYCZtWj93eXoY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=uylA4wxD9mBnQWmBsm0CLpICPs4w/xfRfvddfurMdSQps9ZgTsBSTzMUpRFH9xH5Q QwbPhJOvA0/ydZXH2HBmXHuriV+R9NFbeb94OsXEKhYu1e9SDJ+0Ypaj/seiXtyLAg tAaFKUwjhFkEAa07Te/Zfmw9BdOtcuAeYLgh0UKY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 82CC83858CDB for ; Thu, 18 May 2023 17:59:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 82CC83858CDB Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-479-zBE07tfNN1mXYHt_j6hD7Q-1; Thu, 18 May 2023 13:59:32 -0400 X-MC-Unique: zBE07tfNN1mXYHt_j6hD7Q-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6238c64280fso24088686d6.2 for ; Thu, 18 May 2023 10:59:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432771; x=1687024771; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dqSk7lWtMMIKmt3djmAon/e2+/6JrGEYCZtWj93eXoY=; b=kn69bDsU24uy3aPlOfmwU1F89QZv+4prgRFxQAh2Z5yEHPpsal+w1p6tdjCQIzC9XD qXPMIrvAfPJiMh9wacQs2XMyinvuBsDHIgO3O/UTCldc0IXLbYAooC6eU6TpyGPhFlhY 13+lVIlaJ+3uOzvtXkzXyWXWFOakBjfO9jOYvS7d30lX+sJxKUixweJ29+5cugP2+Ibw QZVGI+/tg/6musgXy17s9CJYMa3aWvw+FY3iASiyszkvvhaCqJeyQeRuoOMQGgEcRgWY Cz1TkU0zBsXlCkpAqYbuyDAstWBsE8ydZA4bkM5TktImD2hTQ1vBMzYukLxAAuuxSq1N VDYg== X-Gm-Message-State: AC+VfDyyGfCW9OXctx26XPtrvRA/t1ITZMALCIlPXgVdiku9mW/iqIrD 6mVIQjCOSfRWNw/mzOfgM4Gr41fvR2YwtrR9xvSwdLGNigu4PnfNMBiti3BCkAEY/FgsL/O36xe 8m0wP6XjbDxG1GBxRQWlv1JNxWYbnKg3O7hC9JtaYriCxcwI8bQ5UMBTdnAzGJ6xbbd4g8zsSwR E= X-Received: by 2002:a05:6214:c45:b0:623:98ee:c727 with SMTP id r5-20020a0562140c4500b0062398eec727mr779920qvj.40.1684432771089; Thu, 18 May 2023 10:59:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7fyMxFvJshieQiwiPFcQsmxU0xmJFecuKtszhwOSMtxBZJheUaCOZg/NsB/6BQxyN0GIixMw== X-Received: by 2002:a05:6214:c45:b0:623:98ee:c727 with SMTP id r5-20020a0562140c4500b0062398eec727mr779890qvj.40.1684432770773; Thu, 18 May 2023 10:59:30 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id p10-20020a05620a112a00b007579371d70esm559699qkk.46.2023.05.18.10.59.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:59:30 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: scoped variable template-id of reference type [PR97340] Date: Thu, 18 May 2023 13:59:27 -0400 Message-ID: <20230518175927.4158045-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc0.4.g004e0f790f MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" lookup_and_finish_template_variable calls convert_from_reference, which means for a variable template-id of reference type the function returns an INDIRECT_REF instead of the bare VAR_DECL. But the downstream logic of two callers, tsubst_qualified_id and finish_class_member_access_expr, expect a DECL_P result and so we end up crashing when resolving the template-id's in the first testcase. (Note that these two callers eventually call convert_from_reference as appropriate, so this earlier call seems at best redundant.) This patch fixes this by pulling out the convert_from_reference call from lookup_and_finish_template_variable and into the callers that actually need it, which turns out to be tsubst_copy_and_build (without it we'd mishandle the second testcase). Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/97340 gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Don't call convert_from_reference. (tsubst_copy_and_build) : Call convert_from_reference on the result of lookup_and_finish_template_variable. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ80.C: New test. * g++.dg/cpp1y/var-templ81.C: New test. --- gcc/cp/pt.cc | 3 ++- gcc/testsuite/g++.dg/cpp1y/var-templ80.C | 22 ++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ81.C | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ80.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ81.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 351fc18b600..9e5b29f3099 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10394,7 +10394,7 @@ lookup_and_finish_template_variable (tree templ, tree targs, complain &= ~tf_partial; var = finish_template_variable (var, complain); mark_used (var); - return convert_from_reference (var); + return var; } /* If the set of template parameters PARMS contains a template parameter @@ -20462,6 +20462,7 @@ tsubst_copy_and_build (tree t, { tree r = lookup_and_finish_template_variable (templ, targs, complain); + r = convert_from_reference (r); r = maybe_wrap_with_location (r, EXPR_LOCATION (t)); RETURN (r); } diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ80.C b/gcc/testsuite/g++.dg/cpp1y/var-templ80.C new file mode 100644 index 00000000000..4439bee8292 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ80.C @@ -0,0 +1,22 @@ +// PR c++/97340 +// { dg-do compile { target c++14 } } + +template +struct A { + template + static constexpr const int& var = 0; +}; + +template +struct B { + static constexpr int x1 = A::template var; + static constexpr int y1 = A{}.template var; + + static constexpr int x2 = A::template var; + static constexpr int y2 = A{}.template var; + + static constexpr int x3 = A::template var; + static constexpr int y3 = A{}.template var; +}; + +template struct B; diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ81.C b/gcc/testsuite/g++.dg/cpp1y/var-templ81.C new file mode 100644 index 00000000000..f9d2e6b1eed --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ81.C @@ -0,0 +1,14 @@ +// Verify we don't ICE on an invalid use of unary * for a variable +// template-id of reference type. +// { dg-do compile { target c++14 } } + +template +static constexpr const int& var = 0; + +template +struct B { + static constexpr int x = *var; // { dg-error "argument of unary" } + static constexpr const int& y = *var; // { dg-error "argument of unary" } +}; + +template struct B;