From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684432775; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dqSk7lWtMMIKmt3djmAon/e2+/6JrGEYCZtWj93eXoY=; b=OliQVJBX+tUvDpg+ORy6G0iMCwlfnQ9xXw8PZ0Wltkkn2oJ3H9QEvsMcEwb+g2cXBp6uow Jhuk6oy/q2bjRuOL7QwiGn4CFwyekqpjr/LVyaYhQd3+gSnmhriaiVl1St25MO0shCHcjq RKHHfUn6Bbx8WGqSwvPgO7zn8Qtar+g= 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) From: Patrick Palka 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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; -- 2.41.0.rc0.4.g004e0f790f