From patchwork Mon Jun 19 19:14:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 777954 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ws0zR2KPfz9s74 for ; Tue, 20 Jun 2017 05:14:58 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="MKMiXwaI"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=Ui/zTxE23qBIoIdZ1phZyFR47414y/N6bdhmKvJ4BWgojX92Is 4gNSXVyUstooiWmos6STn1920s705qhmn8UFqIkNtk+Lob+jaOfXriP9wPayE9b7 dKRZrOcPEAa34tp+lczWmtkEZQw2/b352iZSs1iXN+f0TAHKTNMC5bNTo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=+EMSlDKDzCQnePge/THo8HXR0Ck=; b=MKMiXwaIgN4gnkBmdIto +yeGP+GVMMUoENyCjZUz45iAfUJiCBflrIS1RkEO8C5Uw8vPg4iyk5PfOrs0JhNG FuyVKo1w//EINF5BzjC79sq+2NOL/aEpLUD6DNm+Q4utVQENt86lsNFb2osln4kQ uZWsug7h4EoeUNiBsuCGl18= Received: (qmail 96884 invoked by alias); 19 Jun 2017 19:14:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 96857 invoked by uid 89); 19 Jun 2017 19:14:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-yb0-f174.google.com Received: from mail-yb0-f174.google.com (HELO mail-yb0-f174.google.com) (209.85.213.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 19 Jun 2017 19:14:34 +0000 Received: by mail-yb0-f174.google.com with SMTP id e201so27047252ybb.1 for ; Mon, 19 Jun 2017 12:14:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:from:subject:message-id:date :user-agent:mime-version:content-language; bh=BTI4zWlNJI/fLJCruZuP3/ck2MAGNP1GOAa4sZEaNr4=; b=Oru9P87CRGhtCRdxSEIX9bcZrEjJhGelmGgNe0RhoC7e3KS3FxfrLX9L1q1SOy8Xws 27iJf2f5wEoeoGH6+WuxOY8iuvajtWfFMNhfkZIEkrJvhxygxO6dfEo9DLbdmrmpLXKz q/1FseeePNhwBbUMoVxBJda9RbiVMwXS7hYsceXzDUVjhGhV9M0HyeZSH2w16V5o1oLi OHEBjmX5iuWTpPNZAPDD3ENmWtN4Tl9m2XYCOuabzH3I+AwNAtrGAAf4CalIaoK6qyKP CleR0LXOZ4gaybts49gMr6o7RCZjHJfA47buDf1hwBXe4xDDHMIhK0QVSNCnhl800S6S jZPw== X-Gm-Message-State: AKS2vOwapilkhMVrMGGlKBOhQmsYGsNOXz8ELzt9X9OJ3o57KcbtDuij qj2OU2SlnLmvDg== X-Received: by 10.37.215.66 with SMTP id o63mr19850382ybg.163.1497899677728; Mon, 19 Jun 2017 12:14:37 -0700 (PDT) Received: from ?IPv6:2620:10d:c0a3:20fb:7500:e7fb:4a6f:2254? ([2620:10d:c091:200::144]) by smtp.googlemail.com with ESMTPSA id t136sm4899100ywc.41.2017.06.19.12.14.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Jun 2017 12:14:37 -0700 (PDT) To: GCC Patches From: Nathan Sidwell Subject: [PR c++/81124] inline namespace checking Message-ID: <6c174d3b-d3c4-4272-26a2-f08d79f2a95b@acm.org> Date: Mon, 19 Jun 2017 15:14:35 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 This fixes 81124, where we used ovl_iterate to iterate over a lookup result. We should always use lkp_iterate in those circumstances. However, regular lookup is not what we want here. We don't want to follow using directives -- just look in the local inline hierarchy. Plus also ignore decls found by using declarations. Finally, I could also fix 79766, by moving the excessive-qualification check to after finding the decl. '::foo' is perfectly fine to discover foo in an inline child namespace. nathan 2017-06-19 Nathan Sidwell PR c++/81124 PR c++/79766 * name-lookup.c (set_decl_namespace): Don't follow using directives and ignore using decls. Only check overly-explicit scope after discovering decl. * g++.dg/lookup/pr79766.C: New. * g++.dg/lookup/pr81124.C: New. * g++.dg/template/explicit6.C: Adjust. * g++.old-deja/g++.other/decl5.C: Adjust. Index: cp/name-lookup.c =================================================================== --- cp/name-lookup.c (revision 249369) +++ cp/name-lookup.c (working copy) @@ -4266,8 +4266,6 @@ set_global_binding (tree name, tree val) void set_decl_namespace (tree decl, tree scope, bool friendp) { - tree old; - /* Get rid of namespace aliases. */ scope = ORIGINAL_NAMESPACE (scope); @@ -4277,41 +4275,49 @@ set_decl_namespace (tree decl, tree scop decl, scope); DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - /* Writing "int N::i" to declare a variable within "N" is invalid. */ - if (scope == current_namespace) - { - if (at_namespace_scope_p ()) - error ("explicit qualification in declaration of %qD", - decl); - return; - } + /* See whether this has been declared in the namespace or inline + children. */ + tree old = NULL_TREE; + { + name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN); + if (!lookup.search_qualified (scope, /*usings=*/false)) + /* No old declaration at all. */ + goto not_found; + old = lookup.value; + } - /* See whether this has been declared in the namespace. */ - old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false, - /*complain*/true, /*hidden*/true); - if (old == error_mark_node) - /* No old declaration at all. */ - goto complain; /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ if (TREE_CODE (old) == TREE_LIST) { + ambiguous: + DECL_CONTEXT (decl) = FROB_CONTEXT (scope); error ("reference to %qD is ambiguous", decl); print_candidates (old); return; } - if (!OVL_P (decl)) + + if (!DECL_DECLARES_FUNCTION_P (decl)) { - /* We might have found OLD in an inline namespace inside SCOPE. */ - if (TREE_CODE (decl) == TREE_CODE (old)) - DECL_CONTEXT (decl) = DECL_CONTEXT (old); /* Don't compare non-function decls with decls_match here, since it can't check for the correct constness at this - point. pushdecl will find those errors later. */ + point. pushdecl will find those errors later. */ + + /* We might have found it in an inline namespace child of SCOPE. */ + if (TREE_CODE (decl) == TREE_CODE (old)) + DECL_CONTEXT (decl) = DECL_CONTEXT (old); + + found: + /* Writing "N::i" to declare something directly in "N" is invalid. */ + if (CP_DECL_CONTEXT (decl) == current_namespace + && at_namespace_scope_p ()) + error ("explicit qualification in declaration of %qD", decl); return; } + /* Since decl is a function, old should contain a function decl. */ if (!OVL_P (old)) - goto complain; + goto not_found; + /* We handle these in check_explicit_instantiation_namespace. */ if (processing_explicit_instantiation) return; @@ -4325,53 +4331,48 @@ set_decl_namespace (tree decl, tree scop friends in any namespace. */ if (friendp && DECL_USE_TEMPLATE (decl)) return; - if (OVL_P (old)) + + tree found; + found = NULL_TREE; + + for (lkp_iterator iter (old); iter; ++iter) { - tree found = NULL_TREE; + if (iter.using_p ()) + continue; - for (ovl_iterator iter (old); iter; ++iter) - { - tree ofn = *iter; - /* Adjust DECL_CONTEXT first so decls_match will return true - if DECL will match a declaration in an inline namespace. */ - DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); - if (decls_match (decl, ofn)) - { - if (found && !decls_match (found, ofn)) - { - DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - error ("reference to %qD is ambiguous", decl); - print_candidates (old); - return; - } - found = ofn; - } - } - if (found) + tree ofn = *iter; + + /* Adjust DECL_CONTEXT first so decls_match will return true + if DECL will match a declaration in an inline namespace. */ + DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); + if (decls_match (decl, ofn)) { - if (!is_nested_namespace (scope, CP_DECL_CONTEXT (found), true)) - goto complain; - if (DECL_HIDDEN_FRIEND_P (found)) + if (found) { - pedwarn (DECL_SOURCE_LOCATION (decl), 0, - "%qD has not been declared within %qD", decl, scope); - inform (DECL_SOURCE_LOCATION (found), - "only here as a %"); + /* We found more than one matching declaration. */ + DECL_CONTEXT (decl) = FROB_CONTEXT (scope); + goto ambiguous; } - DECL_CONTEXT (decl) = DECL_CONTEXT (found); - return; + found = ofn; } } - else + + if (found) { - DECL_CONTEXT (decl) = DECL_CONTEXT (old); - if (decls_match (decl, old)) - return; + if (DECL_HIDDEN_FRIEND_P (found)) + { + pedwarn (DECL_SOURCE_LOCATION (decl), 0, + "%qD has not been declared within %qD", decl, scope); + inform (DECL_SOURCE_LOCATION (found), + "only here as a %"); + } + DECL_CONTEXT (decl) = DECL_CONTEXT (found); + goto found; } + not_found: /* It didn't work, go back to the explicit scope. */ DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - complain: error ("%qD should have been declared inside %qD", decl, scope); } Index: testsuite/g++.dg/lookup/pr79766.C =================================================================== --- testsuite/g++.dg/lookup/pr79766.C (nonexistent) +++ testsuite/g++.dg/lookup/pr79766.C (working copy) @@ -0,0 +1,29 @@ +// { dg-do compile { target c++11 } } +// PR 79766 qualified name to find inline namespace is ok + +namespace Y +{ + inline namespace X + { + void Q (); + } +} + +void Y::Q () // OK -> Y::X::Q +{ +} + +inline namespace Z +{ + void R (); +} + +void ::R () // OK -> Z::R +{ +} + +void S (); + +void ::S () // { dg-error "explicit qualification" } +{ +} Index: testsuite/g++.dg/lookup/pr81124.C =================================================================== --- testsuite/g++.dg/lookup/pr81124.C (nonexistent) +++ testsuite/g++.dg/lookup/pr81124.C (working copy) @@ -0,0 +1,11 @@ +// { dg-do compile { target c++11 } } +// c++/81124 ICE with inline namespace + +namespace std { +inline namespace { +int to_string(); +void to_string(int); +} +void to_string(); +} +int std::to_string(); Index: testsuite/g++.dg/template/explicit6.C =================================================================== --- testsuite/g++.dg/template/explicit6.C (revision 249364) +++ testsuite/g++.dg/template/explicit6.C (working copy) @@ -5,4 +5,4 @@ // Bug 19895: ICE on invalid struct A; -template A<>::A(); // { dg-error "(not a template)|(explicit qualification)" } +template A<>::A(); // { dg-error "(should have been)|(not a template)" } Index: testsuite/g++.old-deja/g++.other/decl5.C =================================================================== --- testsuite/g++.old-deja/g++.other/decl5.C (revision 249364) +++ testsuite/g++.old-deja/g++.other/decl5.C (working copy) @@ -53,8 +53,8 @@ namespace N { namespace NMS { - void NMS::fn(); // { dg-error "explicit qual" } - int NMS::i; // { dg-error "explicit qual" } + void NMS::fn(); // { dg-error "should have been" } + int NMS::i; // { dg-error "should have been" } struct NMS::D { // { dg-error "does not name a class" } int i; };