From patchwork Thu May 12 13:55:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 53873 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13B663836427 for ; Thu, 12 May 2022 13:56:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 13B663836427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652363761; bh=Hc455fUYzVwV8riB9GNoyPaA739LBG6/74DA0yumjYA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hfiJAVroNMmLTAZafHZsS6MxskutPNFr+p47+Az1TgZuLzen6qHlX/JrtdJJI669E 8tQWrEsauFCEyls5Xrc+l9/xiL6W+7NBcu6hVzn+RHupzQTA4L8yDwZwxKcjuohZWh WwHyYAH4Ayz8i5FcKfy7+tyZl4gComL6A65/aSbY= 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 6BD46384B0CC for ; Thu, 12 May 2022 13:55:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BD46384B0CC Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-vTUK4WmgNdW5notrMmENvA-1; Thu, 12 May 2022 09:55:27 -0400 X-MC-Unique: vTUK4WmgNdW5notrMmENvA-1 Received: by mail-qv1-f71.google.com with SMTP id s19-20020ad44b33000000b00456107e1120so4596583qvw.0 for ; Thu, 12 May 2022 06:55:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Hc455fUYzVwV8riB9GNoyPaA739LBG6/74DA0yumjYA=; b=YrV1qRmfcZ2IRJHzlZGFAKbHT9Lj1dUADV+VTiKLIbLaqqncMeWoFD12Vz8jKI+G0d niif9LFUK7pBOkZ1fmWZn0LLTIFNfWsUTOQmHpycZk0bo2hPBCBnhGNHFb2agiHT9nvW /VoEyCvIblFIeHn6OiXXAW+XKazS28gy2juUByfrUsQWBZKQfCSq/mmLkIrRMjNgHfxq /ocYOO/IcQ0u1vTtDXJLSiRYLTA+Q/pKqideEZmiePoHrwJDpBgvdqOYMUVuzdYMN15+ cikgqLbzfvNk4SwoenLLuxgeOt1erSE5ntMYUUwm++GDHTBdVRWyAUzh7oETrctgDOJk oaSw== X-Gm-Message-State: AOAM5304lmSlHkSZivCVVR74mXw6dSKLZpj+0eZLRuA5E3fWZtb3xZJM VtfE1oBw+2DdpbjXDLOAHNwjZbb7lA/rgUZ4Q2UyIr/53A1t8QRklbxtPjkMeHzYWYzjYJG01Wz 2rOgj1mssmb+f4h9yq+1cwgFXHHULIGY+xOrrkrdsMb5vqPgkOK8HVWov7TKU67jYwHY= X-Received: by 2002:a37:68d7:0:b0:69f:fd74:7cbf with SMTP id d206-20020a3768d7000000b0069ffd747cbfmr22210632qkc.91.1652363726198; Thu, 12 May 2022 06:55:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5cIHPN9b5ngYH0I1plL1ps1wJLFhuZ8klLlXu0wt3r15fHw1qqqmc3D8lp9BsvMpHuRKLQA== X-Received: by 2002:a37:68d7:0:b0:69f:fd74:7cbf with SMTP id d206-20020a3768d7000000b0069ffd747cbfmr22210591qkc.91.1652363725603; Thu, 12 May 2022 06:55:25 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id b64-20020a378043000000b0069fc13ce1eesm3313381qkd.31.2022.05.12.06.55.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 May 2022 06:55:25 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros Date: Thu, 12 May 2022 09:55:23 -0400 Message-Id: <20220512135523.2899969-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.63.gef9b086d95 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.4 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_LOW, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" After r13-332-g88459c3965e2a2, it looks like we can safely remove the NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its semantics. And TMPL_ARGS_LEVEL should verify the level argument is sane in the one-dimensional vector case. This change uncovered a couple of latent bugs: in try_class_unification, we weren't correctly copying multidimensional targs, and in unify_pack_expansion it seems an inequality test needs to be reversed (not sure about this latter fix, the test was added in r8-6178-g2625472ffa519e FWIW). Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? Also tested on cmcstl2 and range-v3. gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. * pt.cc (try_class_unification): Correctly copy multidimensional targs. (unify_pack_expansion): Fix level comparison. --- gcc/cp/cp-tree.h | 5 +++-- gcc/cp/pt.cc | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b6961a796af..f681d32ac93 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { /* Nonzero if the template arguments is actually a vector of vectors, rather than just a vector. */ #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) /* The depth of a template argument vector. When called directly by @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { args is level 1, not level 0. */ #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ + : (gcc_checking_assert (LEVEL == 1), (ARGS))) /* Set the LEVELth level of the template ARGS to VAL. This macro does not work with single-level argument vectors. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2c7c5f8bb5d..75b21e5c88a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23398,8 +23398,6 @@ static tree try_class_unification (tree tparms, tree targs, tree parm, tree arg, bool explain_p) { - tree copy_of_targs; - if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) return NULL_TREE; else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, because there are two ways to unify base classes of S<0, 1, 2> with S. If we kept the already deduced knowledge, we would reject the possibility I=1. */ - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); + targs = copy_template_args (targs); + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) + targ = NULL_TREE; if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) { - if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p)) + if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p)) return NULL_TREE; return arg; } /* If unification failed, we're done. */ - if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), + if (unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) return NULL_TREE; @@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms, /* Determine the index and level of this parameter pack. */ template_parm_level_and_index (parm_pack, &level, &idx); - if (level < levels) + if (level > levels) continue; /* Keep track of the parameter packs and their corresponding