From patchwork Thu Jun 23 14:48:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 55331 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 E2AB8385626F for ; Thu, 23 Jun 2022 14:48:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2AB8385626F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1655995737; bh=iHDObEyuHx7TYyD2CHCmcP1BiGunsq8fV8MUYy4gRZM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=y8ImWUdUwZaZuodZbJ+NTjffAd8Q2h3UWo7C4FMt5JHtuW84/8akebFDIyzgKQP4E 5NzL9+2drNn7E++ScupIIjMH6uaiaizW+uDPZRZ7jKoZ27rrZMxzcaV7XDuiZPj0a0 7lJ0MflxhdArbnTc7UOXA8p7XtJsrQ3Z2ZifcsDE= 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 33DEE38582B1 for ; Thu, 23 Jun 2022 14:48:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 33DEE38582B1 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-674-AG3BWOwUMMafgIfvu8yVzw-1; Thu, 23 Jun 2022 10:48:25 -0400 X-MC-Unique: AG3BWOwUMMafgIfvu8yVzw-1 Received: by mail-qv1-f72.google.com with SMTP id b2-20020a0cb3c2000000b004703a79581dso13818899qvf.1 for ; Thu, 23 Jun 2022 07:48:25 -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=iHDObEyuHx7TYyD2CHCmcP1BiGunsq8fV8MUYy4gRZM=; b=E0U3/tnt/6nF5/2tPLHZNE0a6zmAWihbceX+WmdpWjjspnvBhsph2P5ZHBi6Gqgz0Q PU5p52v902dwNRgNCf9FgE1KokcZFRjVDHL/SfzQg7QbBJ7x0lv3qCkTqLPuHdDbyXB3 dW8Qk126XqA/3ogI71kZDxZgzsNZnlIzeqrgrFNkTYU8JF9NDZIrfCtFHEKfEZphZJBI eeC2duWB4Mirq6xJ3tWWXqA8tMlHigXDvB5bVJrmlR4/mWycrIeauiLJEEfFpDMvqeyH BzZHhzQC/0IyTjOG8D+vdo/KltxJuJJZwbqgchSyT5aH/SrYUdX1/woaqaAQPbQPtZCF S/UQ== X-Gm-Message-State: AJIora/0DnnnG3vhjBDA4+HA4gzeZUPx2HcpaqVi09Q1cAPK1570/j2b dknkIyeki/W0RudLT2wu9CfRT2g2DlRSidry+rhpHXtwv3PxpDIDus3rYmlxx1hMPc14hOs868G +xkVZ4CZWEWpUxysQB/O6d0H39EYhsx6iGbblzwL1ErLZPdtIWPMpbQ4aW5oRWG5fRaQ= X-Received: by 2002:a05:622a:83:b0:305:29cd:4fdd with SMTP id o3-20020a05622a008300b0030529cd4fddmr8504140qtw.50.1655995705188; Thu, 23 Jun 2022 07:48:25 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s6G5G0mT5x8C+JS9BsrtZIvcSfsKM7xGziUs2TcpBs8r3y9NVCni+Vf7CuksFcep78DbV/Kg== X-Received: by 2002:a05:622a:83:b0:305:29cd:4fdd with SMTP id o3-20020a05622a008300b0030529cd4fddmr8504109qtw.50.1655995704872; Thu, 23 Jun 2022 07:48:24 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id a1-20020ac84d81000000b00316dc1ffbb9sm2286863qtw.32.2022.06.23.07.48.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 07:48:24 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: context completion in lookup_template_class [PR105982] Date: Thu, 23 Jun 2022 10:48:22 -0400 Message-Id: <20220623144822.170716-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.0.rc1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.6 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" The below testcase demonstrates that completion of the substituted context during lookup_template_class can end up registering the desired specialization for us in more cases than r13-1045-gcb7fd1ea85feea anticipated. In particular, it can happen for a non-dependent specialization of a nested class as well. During overload resolution with A's guides, we substitute the deduced argument T=int into the TYPENAME_TYPE B::C (*). This substitution calls lookup_template_class for A::B with T=int, which completes A for the first time, which recursively registers the desired specialization of B for us. The parent call to lookup_template_class then tries registering the same specialization, which leads to a crash. This patch fixes this by making lookup_template_class check the specializations table after completion of the context iff necessary, i.e. when the call to complete_type actually has an effect. (*): Note that the TYPE_CONTEXT of this TYPENAME_TYPE is just the RECORD_TYPE B instead of TYPENAME_TYPE A::B. Is this expected? I'd think maybe_dependent_member_ref would rewrite the reference to B in terms of another TYPENAME_TYPE. This is why the testcase needs to use 'typename B::C' instead of just 'B' -- maybe_dependent_member_ref rewrites the use of B in the latter case but not the former. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/105982 gcc/cp/ChangeLog: * pt.cc (lookup_template_class): After completion of the substituted context, check the table again iff the type was previously incomplete and complete_type made it complete. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction111.C: New test. --- gcc/cp/pt.cc | 21 +++++++++++-------- .../g++.dg/cpp1z/class-deduction111.C | 10 +++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction111.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 28edc6ae988..eeae867a816 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10089,16 +10089,19 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, { context = tsubst_aggr_type (context, arglist, complain, in_decl, true); - context = complete_type (context); - if (is_dependent_type && arg_depth > 1) + /* Try completing the enclosing context if it's not already so. */ + if (context != error_mark_node + && !COMPLETE_TYPE_P (context)) { - /* If this is a dependent nested specialization such as - A::B [with T=int, U=U], then completion of A - could have caused to register the desired specialization - of B already, so check the table again (33959). */ - entry = type_specializations->find_with_hash (&elt, hash); - if (entry) - return entry->spec; + context = complete_type (context); + if (COMPLETE_TYPE_P (context)) + { + /* Completion could have caused us to register the desired + specialization for us, so check the table again. */ + entry = type_specializations->find_with_hash (&elt, hash); + if (entry) + return entry->spec; + } } } } diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction111.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction111.C new file mode 100644 index 00000000000..2406529ea5a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction111.C @@ -0,0 +1,10 @@ +// PR c++/105982 +// { dg-do compile { target c++17 } } + +template +struct A { + struct B { struct C { }; }; + A(T, typename B::C); +}; + +A a(0, {});