From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [63.128.21.74]) by sourceware.org (Postfix) with ESMTP id 806B93ADC02F for ; Tue, 17 Mar 2020 21:36:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 806B93ADC02F Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-450-6DCrJbYFPZqY5BifqNoRCw-1; Tue, 17 Mar 2020 17:36:11 -0400 X-MC-Unique: 6DCrJbYFPZqY5BifqNoRCw-1 Received: by mail-qk1-f199.google.com with SMTP id v14so23277208qkj.3 for ; Tue, 17 Mar 2020 14:36:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=DYM4wDhv7hk+Sp6TUopwupkk26rXxL9UL9RXaMmixlo=; b=bC/i97PytAGeD5Cx+I4sSgXk1YwTmW4kgBGI+tCke/OsBOMrW13X98PV1g+9D9OcCX kDLpAU5PzoqgUcCxMIhI0S7sAAGCIX1KypmWeh2NvIR6X9/+Drm6YpEmhybDfPSNqfF6 JyYTZvIxqFkmen3Jfnb48Q5xMCzTgfr+MdxIknOsirAU3l4EHSkuaRrA7tzPgMuWY7G2 L6jzuKcPjLpyGRXWNCewxIE+s0SODv+iBg1oUgrNebttsJyidbKgpKZWGHJLxuKZHCQU EAxV7pzerXcKVxx17pdT8HI8f2A0M72Tr9pHGjXR0tstt7ABJZtpNYHPOcZb4Nl1ATI4 +yjg== X-Gm-Message-State: ANhLgQ1lP77L2C08IjJ+sVgDOid4qGnVWlPXVik4bI4pWaid88KWzPnB W5U7dJTVPM3KjQvLo3bZARbzVv7N6m40lj9F3EEzbNL5g/F6Gdh4AV3/hhfEAHKkrZbGdgfHNhR I8Vq1PkLM8MYR9N+nqA== X-Received: by 2002:a0c:eb8b:: with SMTP id x11mr1297377qvo.86.1584480971080; Tue, 17 Mar 2020 14:36:11 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtO4Z4Tnj0AhNcbeWUT0H7poOkSeiA+/r3fLvrVIQChzurrbLGCQHfSRQ20ezeUJNstS8Tw+g== X-Received: by 2002:a0c:eb8b:: with SMTP id x11mr1297350qvo.86.1584480970584; Tue, 17 Mar 2020 14:36:10 -0700 (PDT) Received: from [192.168.1.148] (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id g2sm2792734qkb.27.2020.03.17.14.36.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Mar 2020 14:36:09 -0700 (PDT) Subject: Re: [PATCH] c++: Diagnose a deduction guide in a wrong scope [PR91759] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: <20200317084902.GX2156@tucnak> <10abd205-1301-d32f-ad47-8d2c6b5d8d34@redhat.com> <20200317210615.GK2156@tucnak> From: Jason Merrill Message-ID: Date: Tue, 17 Mar 2020 17:36:09 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: <20200317210615.GK2156@tucnak> Content-Language: en-US X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.2 required=5.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Tue, 17 Mar 2020 21:36:15 -0000 On 3/17/20 5:06 PM, Jakub Jelinek wrote: > On Tue, Mar 17, 2020 at 03:54:57PM -0400, Jason Merrill via Gcc-patches wrote: >> How about always diagnosing this here, and moving the deduction guide code >> in grokfndecl up above set_decl_namespace to avoid a duplicate diagnostic? > > I've tried that as: > --- gcc/cp/decl.c.jj 2020-03-16 22:56:46.787172869 +0100 > +++ gcc/cp/decl.c 2020-03-17 21:29:17.812732717 +0100 > @@ -9506,6 +9506,27 @@ grokfndecl (tree ctype, > inlinep &= ~8; > } > > + if (deduction_guide_p (decl)) > + { > + if (!DECL_NAMESPACE_SCOPE_P (decl)) > + { > + error_at (location, "deduction guide %qD must be declared at " > + "namespace scope", decl); > + return NULL_TREE; > + } > + tree type = TREE_TYPE (DECL_NAME (decl)); > + if (CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type)) > + { > + error_at (location, "deduction guide %qD must be declared in the " > + "same scope as %qT", decl, type); > + inform (location_of (type), " declared here"); > + return NULL_TREE; > + } > + if (funcdef_flag) > + error_at (location, > + "deduction guide %qD must not have a function body", decl); > + } > + > /* If this decl has namespace scope, set that up. */ > if (in_namespace) > set_decl_namespace (decl, in_namespace, friendp); > @@ -9636,20 +9657,8 @@ grokfndecl (tree ctype, > } > } > > - if (deduction_guide_p (decl)) > - { > - if (!DECL_NAMESPACE_SCOPE_P (decl)) > - { > - error_at (location, "deduction guide %qD must be declared at " > - "namespace scope", decl); > - return NULL_TREE; > - } > - if (funcdef_flag) > - error_at (location, > - "deduction guide %qD must not have a function body", decl); > - } > - else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) > - && !grok_op_properties (decl, /*complain=*/true)) > + if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) > + && !grok_op_properties (decl, /*complain=*/true)) > return NULL_TREE; > else if (UDLIT_OPER_P (DECL_NAME (decl))) > { > --- gcc/testsuite/g++.dg/cpp1z/class-deduction9.C.jj 2020-01-12 11:54:37.126402652 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/class-deduction9.C 2020-03-17 21:29:53.842207083 +0100 > @@ -10,7 +10,7 @@ namespace N { > } > > template > -N::A(T) -> N::A; // { dg-error "should have been declared inside .N" } > +N::A(T) -> N::A; // { dg-error "must be declared in the same scope as" } > > namespace N { > template > --- gcc/testsuite/g++.dg/cpp1z/class-deduction72.C.jj 2020-03-17 21:26:00.566610331 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/class-deduction72.C 2020-03-17 21:26:00.566610331 +0100 > @@ -0,0 +1,11 @@ > +// PR c++/91759 > +// { dg-do compile { target c++17 } } > + > +namespace N { > + template > + struct X{ X(int); }; // { dg-message "declared here" } > +} > + > +using N::X; > + > +X(int) -> X; // { dg-error "must be declared in the same scope as" } > > but that fails with > Excess errors: > /usr/src/gcc/obj/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_pair.h:459:40: error: deduction guide 'pair(_T1, _T2)-> std::pair<_T1, _T2>' must be declared in the same scope as 'std::pair<_T1, _T2>' > /usr/src/gcc/obj/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:1873:5: error: deduction guide 'vector(_InputIterator, _InputIterator, _Allocator)-> std::vector<_ValT, _Allocator>' must be declared in the same scope as 'std::vector<_Tp, _Alloc>' > on various testcases. > > I've tried next: > --- gcc/cp/decl.c.jj 2020-03-16 22:56:46.787172869 +0100 > +++ gcc/cp/decl.c 2020-03-17 22:00:13.897648589 +0100 > @@ -9506,6 +9506,36 @@ grokfndecl (tree ctype, > inlinep &= ~8; > } > > + if (deduction_guide_p (decl)) > + { > + if (!DECL_NAMESPACE_SCOPE_P (decl)) > + { > + error_at (location, "deduction guide %qD must be declared at " > + "namespace scope", decl); > + return NULL_TREE; > + } > + tree type = TREE_TYPE (DECL_NAME (decl)); > + tree ctx = NULL_TREE; > + if (in_namespace) > + ctx = ORIGINAL_NAMESPACE (in_namespace); > + else if (!ctype) > + ctx = current_decl_namespace (); > + if (ctx) > + ctx = FROB_CONTEXT (ctx); > + if (SCOPE_FILE_SCOPE_P (ctx)) > + ctx = global_namespace; > + if (CP_TYPE_CONTEXT (type) != ctx) > + { > + error_at (location, "deduction guide %qD must be declared in the " > + "same scope as %qT", decl, type); > + inform (location_of (type), " declared here"); > + return NULL_TREE; > + } > + if (funcdef_flag) > + error_at (location, > + "deduction guide %qD must not have a function body", decl); > + } > + > /* If this decl has namespace scope, set that up. */ > if (in_namespace) > set_decl_namespace (decl, in_namespace, friendp); > @@ -9636,20 +9666,8 @@ grokfndecl (tree ctype, > } > } > > - if (deduction_guide_p (decl)) > - { > - if (!DECL_NAMESPACE_SCOPE_P (decl)) > - { > - error_at (location, "deduction guide %qD must be declared at " > - "namespace scope", decl); > - return NULL_TREE; > - } > - if (funcdef_flag) > - error_at (location, > - "deduction guide %qD must not have a function body", decl); > - } > - else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) > - && !grok_op_properties (decl, /*complain=*/true)) > + if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) > + && !grok_op_properties (decl, /*complain=*/true)) > return NULL_TREE; > else if (UDLIT_OPER_P (DECL_NAME (decl))) > { > --- gcc/testsuite/g++.dg/cpp1z/class-deduction72.C.jj 2020-03-17 21:26:00.566610331 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/class-deduction72.C 2020-03-17 21:26:00.566610331 +0100 > @@ -0,0 +1,11 @@ > +// PR c++/91759 > +// { dg-do compile { target c++17 } } > + > +namespace N { > + template > + struct X{ X(int); }; // { dg-message "declared here" } > +} > + > +using N::X; > + > +X(int) -> X; // { dg-error "must be declared in the same scope as" } > --- gcc/testsuite/g++.dg/cpp1z/class-deduction9.C.jj 2020-01-12 11:54:37.126402652 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/class-deduction9.C 2020-03-17 21:29:53.842207083 +0100 > @@ -10,7 +10,7 @@ namespace N { > } > > template > -N::A(T) -> N::A; // { dg-error "should have been declared inside .N" } > +N::A(T) -> N::A; // { dg-error "must be declared in the same scope as" } > > namespace N { > template > > but that FAILs the class-deduction9.C test with the adjustment, > reports the older "should have been"... error instead, because the computed > ctx is the N namespace, which is also what set_decl_namespace sets, but then > it fails some lookup and emits this message. Let's go with your earlier patch, then. Thanks. Jason