From patchwork Thu Jun 1 21:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 70478 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 69F813857357 for ; Thu, 1 Jun 2023 21:43:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69F813857357 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685655781; bh=tlYnrDBJlcnbub+G0p7h6RqkiynS6+TUmqqbJykHa2k=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=SVTY+6YfOhrXZXd6CbTWlIVThn2Xhk0lkWDwyB7qES0cb9ImhoOoC3e+vElWCM13b Bmsr0fG2I9/RWL6L3wG0yhPbS0PyCCwNgizi7oC4W/JsTLexA/IVO4EapBxFFT9UGz kpyZ+4FhCf7p8DChs8iuEbWi75P9ZR2bNNCCgW84= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id C3F573858C50 for ; Thu, 1 Jun 2023 21:42:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C3F573858C50 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-250-PgiIMGYsP9y7Eai2w9_XZw-1; Thu, 01 Jun 2023 17:42:30 -0400 X-MC-Unique: PgiIMGYsP9y7Eai2w9_XZw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6262300393dso16653266d6.1 for ; Thu, 01 Jun 2023 14:42:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685655749; x=1688247749; 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=tlYnrDBJlcnbub+G0p7h6RqkiynS6+TUmqqbJykHa2k=; b=f4woUTw5dOpCClydFvWM4TDCGovahdwlVU3eKZvQH0HbjAeJZf+e0xlkJ4FnebI7qr fPZdhBdl//Iw/DL3YsQLed/rwbYIzKxMr9ZeC8ZU7yOWaW9RzCbgkODUc7/R16JpZlyj u48dSTd5/G1fLa9yKB8Mi7OqSAl48Di8QVrHZDeyXSxCu3a+NkVE0v5yUHUk8oOqDqr8 G1QtebFYZAJv3Fl6l4xI3OnsryQpjcCS2xyx38tbtmcQUVFrB1Tgq+MdIKqsjjy0vBDQ bp3NLkWCBoQZp33ZRft8W9RLuwXX7lfnIDGB9hMIcF/GLdrsZFzIQPMRxDuYzlSQIdCA umSg== X-Gm-Message-State: AC+VfDzNdZ2Tw90DLOHZXFrFMUGkzqmfftrZomvL5gcFK9WwqsE9RMbJ YqSRlIN/rv7RMq5gKvA8zczxWV/ho41pSGCI39J7W/fdSX37olD84tN78AElpfEnyxHSNRUfh/N sjt3NjwBY5bjydph6IXdpoQey5YnbAbi1nkUNvEzPlwpspTE4Liiap16uaiCM49HXCiU9E80hfk U= X-Received: by 2002:a05:6214:5094:b0:5ef:8b22:699e with SMTP id kk20-20020a056214509400b005ef8b22699emr10442999qvb.14.1685655748892; Thu, 01 Jun 2023 14:42:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5ga2cNTjZJy8X1tZAoYcKxQbNt4RE/sgUNgHt5mDWwFSv4eywG/9Gr+enE1O3Z3KyT8TGYeA== X-Received: by 2002:a05:6214:5094:b0:5ef:8b22:699e with SMTP id kk20-20020a056214509400b005ef8b22699emr10442976qvb.14.1685655748336; Thu, 01 Jun 2023 14:42:28 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id px22-20020a056214051600b0061b731bf3c2sm7858710qvb.80.2023.06.01.14.42.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 14:42:27 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: fix up caching of level lowered ttps Date: Thu, 1 Jun 2023 17:42:24 -0400 Message-ID: <20230601214224.1468391-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc1.10.g9e49351c30 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.8 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Due to level/depth mismatches between the template parameters of a level lowered ttp and the original ttp, the ttp comparison check added by r14-418-g0bc2a1dc327af9 never actually holds outside of erroneous cases. Moreover, it'd be good to cache the overall TEMPLATE_TEMPLATE_PARM instead of just the corresponding TEMPLATE_PARM_INDEX. It's tricky to cache all level lowered ttps since the result of level lowering may depend on more than just the depth of the arguments, e.g. for TT in template struct A { template class TT> void f(); } the substitution T=int yields a different level-lowerd ttp than T=char. But these kinds of ttps seem to be rare in practice, and "simple" ttps that don't depend on outer template parameters are easy enough to cache like so. Unfortunately, this means we're back to expecting a duplicate error in nontype12.C again since the ttp in question is not "simple" so caching of the (erroneous) lowered ttp doesn't happen. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? This reduces memory usage of range-v3's zip.cpp by 1%. gcc/cp/ChangeLog: * cp-tree.h (TEMPLATE_PARM_DESCENDANTS): Harden. (TEMPLATE_TYPE_DESCENDANTS): Define. (TEMPLATE_TEMPLATE_PARM_SIMPLE_P): Define. * pt.cc (reduce_template_parm_level): Revert r14-418-g0bc2a1dc327af9 change. (process_template_parm): Set TEMPLATE_TEMPLATE_PARM_SIMPLE_P appropriately. (uses_outer_template_parms): Determine the outer depth of a template template parm without relying on DECL_CONTEXT. (tsubst) : Cache lowering a simple template template parm. Consistently use 'code'. gcc/testsuite/ChangeLog: * g++.dg/template/nontype12.C: Expect a duplicate error again. --- gcc/cp/cp-tree.h | 10 +++++- gcc/cp/pt.cc | 37 +++++++++++++---------- gcc/testsuite/g++.dg/template/nontype12.C | 3 +- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index f08e5630a5c..cd762667bec 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -525,6 +525,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX]; 5: CLASS_TYPE_P (in RECORD_TYPE and UNION_TYPE) ENUM_FIXED_UNDERLYING_TYPE_P (in ENUMERAL_TYPE) AUTO_IS_DECLTYPE (in TEMPLATE_TYPE_PARM) + TEMPLATE_TEMPLATE_PARM_SIMPLE_P (in TEMPLATE_TEMPLATE_PARM) 6: TYPE_DEPENDENT_P_VALID Usage of DECL_LANG_FLAG_?: @@ -5991,7 +5992,7 @@ enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, TYPENAME_FLAG }; ((template_parm_index*)TEMPLATE_PARM_INDEX_CHECK (NODE)) #define TEMPLATE_PARM_IDX(NODE) (TEMPLATE_PARM_INDEX_CAST (NODE)->index) #define TEMPLATE_PARM_LEVEL(NODE) (TEMPLATE_PARM_INDEX_CAST (NODE)->level) -#define TEMPLATE_PARM_DESCENDANTS(NODE) (TREE_CHAIN (NODE)) +#define TEMPLATE_PARM_DESCENDANTS(NODE) (TREE_CHAIN (TEMPLATE_PARM_INDEX_CHECK (NODE))) #define TEMPLATE_PARM_ORIG_LEVEL(NODE) (TEMPLATE_PARM_INDEX_CAST (NODE)->orig_level) #define TEMPLATE_PARM_DECL(NODE) (TEMPLATE_PARM_INDEX_CAST (NODE)->decl) #define TEMPLATE_PARM_PARAMETER_PACK(NODE) \ @@ -6009,6 +6010,8 @@ enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, TYPENAME_FLAG }; (TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX (NODE))) #define TEMPLATE_TYPE_ORIG_LEVEL(NODE) \ (TEMPLATE_PARM_ORIG_LEVEL (TEMPLATE_TYPE_PARM_INDEX (NODE))) +#define TEMPLATE_TYPE_DESCENDANTS(NODE) \ + (TEMPLATE_PARM_DESCENDANTS (TEMPLATE_TYPE_PARM_INDEX (NODE))) #define TEMPLATE_TYPE_DECL(NODE) \ (TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (NODE))) #define TEMPLATE_TYPE_PARAMETER_PACK(NODE) \ @@ -6018,6 +6021,11 @@ enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, TYPENAME_FLAG }; #define CLASS_PLACEHOLDER_TEMPLATE(NODE) \ (DECL_INITIAL (TYPE_NAME (TEMPLATE_TYPE_PARM_CHECK (NODE)))) +/* True iff the template parameters of this TEMPLATE_TEMPLATE_PARM don't + depend on outer template parameters. */ +#define TEMPLATE_TEMPLATE_PARM_SIMPLE_P(NODE) \ + (TYPE_LANG_FLAG_5 (TEMPLATE_TEMPLATE_PARM_CHECK (NODE))) + /* Contexts in which auto deduction occurs. These flags are used to control diagnostics in do_auto_deduction. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 1a32f10b22b..15128cf3c7c 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -219,6 +219,7 @@ static tree enclosing_instantiation_of (tree tctx); static void instantiate_body (tree pattern, tree args, tree d, bool nested); static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree); static void mark_template_arguments_used (tree, tree); +static bool uses_outer_template_parms (tree); /* Make the current scope suitable for access checking when we are processing T. T can be FUNCTION_DECL for instantiated function @@ -4554,12 +4555,7 @@ reduce_template_parm_level (tree index, tree type, int levels, tree args, if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) != TEMPLATE_PARM_LEVEL (index) - levels) - || !(TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM - ? (comp_template_parms - (DECL_TEMPLATE_PARMS (TYPE_NAME (type)), - DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL - (TEMPLATE_PARM_DESCENDANTS (index))))) - : same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))) + || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))) { tree orig_decl = TEMPLATE_PARM_DECL (index); @@ -4722,6 +4718,10 @@ process_template_parm (tree list, location_t parm_loc, tree parm, DECL_ARTIFICIAL (decl) = 1; SET_DECL_TEMPLATE_PARM_P (decl); + if (TREE_CODE (parm) == TEMPLATE_DECL + && !uses_outer_template_parms (parm)) + TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true; + /* Build requirements for the type/template parameter. This must be done after SET_DECL_TEMPLATE_PARM_P or process_template_parm could fail. */ @@ -11047,7 +11047,11 @@ uses_template_parms_level (tree t, int level) static bool uses_outer_template_parms (tree decl) { - int depth = template_class_depth (CP_DECL_CONTEXT (decl)); + int depth; + if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) + depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1; + else + depth = template_class_depth (CP_DECL_CONTEXT (decl)); if (depth == 0) return false; if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level, @@ -16293,14 +16297,15 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) t = TYPE_MAIN_VARIANT (t); } - if (TREE_CODE (t) == TEMPLATE_TYPE_PARM - && (arg = TEMPLATE_TYPE_PARM_INDEX (t), - r = TEMPLATE_PARM_DESCENDANTS (arg)) - && (TEMPLATE_PARM_LEVEL (r) - == TEMPLATE_PARM_LEVEL (arg) - levels)) - /* Cache the simple case of lowering a type parameter. */ - r = TREE_TYPE (r); - else + if (tree d = TEMPLATE_TYPE_DESCENDANTS (t)) + if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels + && (code == TEMPLATE_TYPE_PARM + || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t))) + /* Cache lowering a type parameter or a simple template + template parameter. */ + r = TREE_TYPE (d); + + if (!r) { r = copy_type (t); TEMPLATE_TYPE_PARM_INDEX (r) @@ -16311,7 +16316,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) TYPE_POINTER_TO (r) = NULL_TREE; TYPE_REFERENCE_TO (r) = NULL_TREE; - if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) + if (code == TEMPLATE_TYPE_PARM) if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)) /* Propagate constraints on placeholders since they are only instantiated during satisfaction. */ diff --git a/gcc/testsuite/g++.dg/template/nontype12.C b/gcc/testsuite/g++.dg/template/nontype12.C index 6642ffd0a13..e37cf8f7646 100644 --- a/gcc/testsuite/g++.dg/template/nontype12.C +++ b/gcc/testsuite/g++.dg/template/nontype12.C @@ -4,8 +4,7 @@ template struct A { template int foo(); // { dg-error "double" "" { target c++17_down } } - template class> int bar(); // { dg-bogus {double.*C:7:[^\n]*double} } - // { dg-error "double" "" { target c++17_down } .-1 } + template class> int bar(); // { dg-error "double" "" { target c++17_down } } template struct X; // { dg-error "double" "" { target c++17_down } } };