From patchwork Tue Jul 17 16:13:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 171471 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]) by ozlabs.org (Postfix) with SMTP id A348F2C00B5 for ; Wed, 18 Jul 2012 02:14:38 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1343146478; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:CC:Subject:References:In-Reply-To:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=2ZnF5wcwsqwNp5+CdzrJGZn6/tk=; b=gP2FSTE9jTtjjXrXUv2jz13cnVx7TXJTxEefjDDa4aiIAx2ZNeALYN+V3XjIx7 5fHD7ErtKD2al6cY2gEJ6dIqnmuEOYsLphgqbcm4ZDQEbVo5CJqOsr1o3QrYu82/ 0b/NA1Y8WCsbQaug3VBbo/01RZilKej5CpaQZ2id7EZPY= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=TSZymqLwt8B6Sl4GO7CS5Ci5S7jiGhmMZ78khd7Xgsr+sorCK5K8WObSMOsSVO HfxrGD9hkGchBbiLd5r5QNusOeVMqyidrRjECsO3x/pZYa7B7/pemGm518+xBkYF b97MUirU95ARnfmd72HNCEYiU1NBZyjvZKsuKSHmsgeSI=; Received: (qmail 10972 invoked by alias); 17 Jul 2012 16:14:30 -0000 Received: (qmail 10393 invoked by uid 22791); 17 Jul 2012 16:14:23 -0000 X-SWARE-Spam-Status: No, hits=-7.3 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_THREADED, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_NO, RCVD_IN_HOSTKARMA_YE, TW_CX, TW_FN, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from acsinet15.oracle.com (HELO acsinet15.oracle.com) (141.146.126.227) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 17 Jul 2012 16:14:06 +0000 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by acsinet15.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id q6HGE2e5008900 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 17 Jul 2012 16:14:03 GMT Received: from acsmt356.oracle.com (acsmt356.oracle.com [141.146.40.156]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id q6HGE1Lr016505 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 17 Jul 2012 16:14:02 GMT Received: from abhmt116.oracle.com (abhmt116.oracle.com [141.146.116.68]) by acsmt356.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id q6HGE1KQ013216; Tue, 17 Jul 2012 11:14:01 -0500 Received: from [192.168.1.4] (/79.53.14.201) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 17 Jul 2012 09:14:00 -0700 Message-ID: <50058F46.9020009@oracle.com> Date: Tue, 17 Jul 2012 18:13:58 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120601 Thunderbird/13.0 MIME-Version: 1.0 To: Jason Merrill CC: "gcc-patches@gcc.gnu.org" , Dodji Seketeli Subject: Re: [C++ RFC / Patch] PR 51213 ("access control under SFINAE") References: <4FDB4640.3050502@oracle.com> <4FFF55EF.3090508@redhat.com> <500141D9.6010808@oracle.com> <500451CB.5040903@redhat.com> <50055E76.8030500@oracle.com> <50057250.9080902@redhat.com> In-Reply-To: <50057250.9080902@redhat.com> X-IsSubscribed: yes 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 Hi, On 07/17/2012 04:10 PM, Jason Merrill wrote: > On 07/17/2012 08:45 AM, Paolo Carlini wrote: >> -check_default_argument (tree decl, tree arg) >> +check_default_argument (tree decl, tree arg, tsubst_flags_t complain) > > Hmm, I don't think substitution of default arguments can cause > deduction failure; it happens after we've chosen which function to > call. What was the motivation for the default argument changes? Eh, thanks for noticing. Honestly, I have no idea where that cruft was coming from, I removed it and everything seems fine. > >> + tmp = error_mark_node; > > Let's use a more informative name than "tmp" for these flags. Ok. > >> -void >> -perform_deferred_access_checks (void) >> +bool >> +perform_deferred_access_checks (tsubst_flags_t complain) > > Need to document what the return value means. Indeed. > > >> - if (complain & tf_error) >> - perform_or_defer_access_check (TYPE_BINFO (context), t, t); >> >> + if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, >> complain)) >> + return error_mark_node; > > These changes along with the enforce_access handling of cxx_dialect > break C++03 code that currently works, such as > > template > class A > { > typedef T I; > }; > > template > void f(typename T::I); > > template > void f(int); > > int main() > { > f > (1); > } > > Under the C++03 rules, we don't get access errors when generating > overload candidates, we get them when the function is instantiated > (i.e. in instantiate_decl). Ok, thanks. In fact today I added the enforce_access change very late, without really checking that it was consistent with the callers etc. The below variant makes more sense to me, afaics reduces correctly in the various C++ modes either to what we used to have before this work or to what I had in my first draft. > Hmm, now that I look at the code in instantiate_decl for > re-substituting to get additional errors, I guess I should have > factored that code out into a separate function and used it in the > access7 patch rather than add handling of FNDECL_RECHECK_ACCESS_P in > tsubst_decl. I see. Could you take care of this improvement? Otherwise the below passes testing, tries to implement all the changes you indicated. Thanks! Paolo. //////////////////////// Index: libstdc++-v3/testsuite/20_util/pair/noncopyable.cc =================================================================== --- libstdc++-v3/testsuite/20_util/pair/noncopyable.cc (revision 0) +++ libstdc++-v3/testsuite/20_util/pair/noncopyable.cc (revision 0) @@ -0,0 +1,39 @@ +// { dg-do compile } +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +// PR c++/51213 +class Uncopyable +{ + Uncopyable(const Uncopyable&); + public: + Uncopyable() = default; +}; + +struct ContainsUncopyable +{ + std::pair pv; +}; + +void foo() +{ + ContainsUncopyable c; +} Index: gcc/testsuite/g++.dg/cpp0x/sfinae37.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/sfinae37.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/sfinae37.C (revision 0) @@ -0,0 +1,22 @@ +// PR c++/51213 +// { dg-do compile { target c++11 } } + +class C { + typedef int type; +}; + +template +auto f(int) -> char; + +template +auto f(...) -> char (&)[2]; + +static_assert(sizeof(f(0)) == 2, "Ouch"); + +template +auto g(int) -> decltype(typename T::type(), char()); + +template +auto g(...) -> char (&)[2]; + +static_assert(sizeof(g(0)) == 2, "Ouch"); Index: gcc/testsuite/g++.dg/template/access7.C =================================================================== --- gcc/testsuite/g++.dg/template/access7.C (revision 189572) +++ gcc/testsuite/g++.dg/template/access7.C (working copy) @@ -14,5 +14,5 @@ typename A::T* f (A) { // { dg-error "this conte } void g () { - f (S ()); // { dg-message "required" } + f (S ()); // { dg-message "required|no match" } } Index: gcc/testsuite/g++.dg/template/sfinae10.C =================================================================== --- gcc/testsuite/g++.dg/template/sfinae10.C (revision 189572) +++ gcc/testsuite/g++.dg/template/sfinae10.C (working copy) @@ -81,19 +81,19 @@ struct Y { }; struct Z { private: - Z operator+(); // { dg-error "is private" } - Z operator-(); // { dg-error "is private" } - int operator*(); // { dg-error "is private" } - Z operator~(); // { dg-error "is private" } - bool operator!(); // { dg-error "is private" } - Z& operator++(); // { dg-error "is private" } - Z& operator--(); // { dg-error "is private" } - Z& operator++(int); // { dg-error "is private" } - Z& operator--(int); // { dg-error "is private" } + Z operator+(); // { dg-error "is private" "" { target c++98 } } + Z operator-(); // { dg-error "is private" "" { target c++98 } } + int operator*(); // { dg-error "is private" "" { target c++98 } } + Z operator~(); // { dg-error "is private" "" { target c++98 } } + bool operator!(); // { dg-error "is private" "" { target c++98 } } + Z& operator++(); // { dg-error "is private" "" { target c++98 } } + Z& operator--(); // { dg-error "is private" "" { target c++98 } } + Z& operator++(int); // { dg-error "is private" "" { target c++98 } } + Z& operator--(int); // { dg-error "is private" "" { target c++98 } } }; // has_unary_plus -DEFINE_PREFIX_UNARY_TRAIT(has_unary_plus, +); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(has_unary_plus, +); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_unary_plus::value)); STATIC_ASSERT((!has_unary_plus::value)); STATIC_ASSERT((has_unary_plus::value)); @@ -101,7 +101,7 @@ STATIC_ASSERT((has_unary_plus::value)); STATIC_ASSERT((!has_unary_plus::value)); // is_negatable -DEFINE_PREFIX_UNARY_TRAIT(is_negatable, -); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(is_negatable, -); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((is_negatable::value)); STATIC_ASSERT((!is_negatable::value)); STATIC_ASSERT((is_negatable::value)); @@ -109,7 +109,7 @@ STATIC_ASSERT((is_negatable::value)); STATIC_ASSERT((!is_negatable::value)); // is_dereferenceable -DEFINE_PREFIX_UNARY_TRAIT(is_dereferenceable, *); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(is_dereferenceable, *); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((!is_dereferenceable::value)); STATIC_ASSERT((is_dereferenceable::value)); STATIC_ASSERT((is_dereferenceable::value)); @@ -117,7 +117,7 @@ STATIC_ASSERT((is_dereferenceable::value)); STATIC_ASSERT((!is_dereferenceable::value)); // has_bitwise_not -DEFINE_PREFIX_UNARY_TRAIT(has_bitwise_not, ~); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(has_bitwise_not, ~); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_bitwise_not::value)); STATIC_ASSERT((!has_bitwise_not::value)); STATIC_ASSERT((has_bitwise_not::value)); @@ -125,7 +125,7 @@ STATIC_ASSERT((has_bitwise_not::value)); STATIC_ASSERT((!has_bitwise_not::value)); // has_truth_not -DEFINE_PREFIX_UNARY_TRAIT(has_truth_not, !); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(has_truth_not, !); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_truth_not::value)); STATIC_ASSERT((has_truth_not::value)); STATIC_ASSERT((has_truth_not::value)); @@ -133,7 +133,7 @@ STATIC_ASSERT((has_truth_not::value)); STATIC_ASSERT((!has_truth_not::value)); // has_preincrement -DEFINE_PREFIX_UNARY_TRAIT(has_preincrement, ++); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(has_preincrement, ++); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_preincrement::value)); STATIC_ASSERT((has_preincrement::value)); STATIC_ASSERT((!has_preincrement::value)); @@ -142,7 +142,7 @@ STATIC_ASSERT((has_preincrement::value)); STATIC_ASSERT((!has_preincrement::value)); // has_predecrement -DEFINE_PREFIX_UNARY_TRAIT(has_predecrement, --); // { dg-error "within this context" } +DEFINE_PREFIX_UNARY_TRAIT(has_predecrement, --); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_predecrement::value)); STATIC_ASSERT((has_predecrement::value)); STATIC_ASSERT((!has_predecrement::value)); @@ -151,7 +151,7 @@ STATIC_ASSERT((has_predecrement::value)); STATIC_ASSERT((!has_predecrement::value)); // has_postincrement -DEFINE_POSTFIX_UNARY_TRAIT(has_postincrement, ++); // { dg-error "within this context" } +DEFINE_POSTFIX_UNARY_TRAIT(has_postincrement, ++); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_postincrement::value)); STATIC_ASSERT((has_postincrement::value)); STATIC_ASSERT((!has_postincrement::value)); @@ -160,7 +160,7 @@ STATIC_ASSERT((has_postincrement::value)); STATIC_ASSERT((!has_postincrement::value)); // has_postdecrement -DEFINE_POSTFIX_UNARY_TRAIT(has_postdecrement, --); // { dg-error "within this context" } +DEFINE_POSTFIX_UNARY_TRAIT(has_postdecrement, --); // { dg-error "within this context" "" { target c++98 } } STATIC_ASSERT((has_postdecrement::value)); STATIC_ASSERT((has_postdecrement::value)); STATIC_ASSERT((!has_postdecrement::value)); @@ -169,13 +169,12 @@ STATIC_ASSERT((has_postdecrement::value)); STATIC_ASSERT((!has_postdecrement::value)); // Check for private members -STATIC_ASSERT((has_unary_plus::value)); // { dg-message "required from here" } -STATIC_ASSERT((is_negatable::value)); // { dg-message "required from here" } -STATIC_ASSERT((is_dereferenceable::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_bitwise_not::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_truth_not::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_preincrement::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_predecrement::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_postincrement::value)); // { dg-message "required from here" } -STATIC_ASSERT((has_postdecrement::value)); // { dg-message "required from here" } - +STATIC_ASSERT((!has_unary_plus::value)); +STATIC_ASSERT((!is_negatable::value)); +STATIC_ASSERT((!is_dereferenceable::value)); +STATIC_ASSERT((!has_bitwise_not::value)); +STATIC_ASSERT((!has_truth_not::value)); +STATIC_ASSERT((!has_preincrement::value)); +STATIC_ASSERT((!has_predecrement::value)); +STATIC_ASSERT((!has_postincrement::value)); +STATIC_ASSERT((!has_postdecrement::value)); Index: gcc/testsuite/g++.dg/template/sfinae6_neg.C =================================================================== --- gcc/testsuite/g++.dg/template/sfinae6_neg.C (revision 189572) +++ gcc/testsuite/g++.dg/template/sfinae6_neg.C (working copy) @@ -14,7 +14,7 @@ template struct enable_if { template typename enable_if()(create_a(), create_a()), 1), yes_type>::type - check_is_callable2(type, type, type); // { dg-error "within this context" } + check_is_callable2(type, type, type); // { dg-error "within this context" "" { target c++98 } } no_type check_is_callable2(...); @@ -52,7 +52,7 @@ struct F { void operator()(A, A); private: - void operator()(B, B); // { dg-error "is private" } + void operator()(B, B); // { dg-error "is private" "" { target c++98 } } }; -STATIC_ASSERT((is_callable2::value)); +STATIC_ASSERT((!is_callable2::value)); Index: gcc/cp/init.c =================================================================== --- gcc/cp/init.c (revision 189561) +++ gcc/cp/init.c (working copy) @@ -1876,9 +1876,9 @@ build_offset_ref (tree type, tree member, bool add (or any class derived from that class). */ if (address_p && DECL_P (t) && DECL_NONSTATIC_MEMBER_P (t)) - perform_or_defer_access_check (TYPE_BINFO (type), t, t); + perform_or_defer_access_check (TYPE_BINFO (type), t, t, tf_error); else - perform_or_defer_access_check (basebinfo, t, t); + perform_or_defer_access_check (basebinfo, t, t, tf_error); if (DECL_STATIC_FUNCTION_P (t)) return t; @@ -1891,7 +1891,7 @@ build_offset_ref (tree type, tree member, bool add /* We need additional test besides the one in check_accessibility_of_qualified_id in case it is a pointer to non-static member. */ - perform_or_defer_access_check (TYPE_BINFO (type), member, member); + perform_or_defer_access_check (TYPE_BINFO (type), member, member, tf_error); if (!address_p) { Index: gcc/cp/class.c =================================================================== --- gcc/cp/class.c (revision 189561) +++ gcc/cp/class.c (working copy) @@ -1189,7 +1189,7 @@ alter_access (tree t, tree fdecl, tree access) } else { - perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl); + perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl, tf_error); DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); return 1; } @@ -7138,7 +7138,7 @@ resolve_address_of_overloaded_function (tree targe && DECL_FUNCTION_MEMBER_P (fn)) { gcc_assert (access_path); - perform_or_defer_access_check (access_path, fn, fn); + perform_or_defer_access_check (access_path, fn, fn, tf_error); } if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) Index: gcc/cp/decl.c =================================================================== --- gcc/cp/decl.c (revision 189561) +++ gcc/cp/decl.c (working copy) @@ -3306,10 +3306,11 @@ make_typename_type (tree context, tree name, enum context, name, t); return error_mark_node; } - - if (complain & tf_error) - perform_or_defer_access_check (TYPE_BINFO (context), t, t); + if ((cxx_dialect >= cxx0x || complain & tf_error) + && !perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain)) + return error_mark_node; + /* If we are currently parsing a template and if T is a typedef accessed through CONTEXT then we need to remember and check access of T at template instantiation time. */ @@ -3378,8 +3379,10 @@ make_unbound_class_template (tree context, tree na return error_mark_node; } - if (complain & tf_error) - perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl); + if ((cxx_dialect >= cxx0x || complain & tf_error) + && !perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl, + complain)) + return error_mark_node; return tmpl; } @@ -6647,7 +6650,8 @@ register_dtor_fn (tree decl) gcc_assert (idx >= 0); cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx); /* Make sure it is accessible. */ - perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup); + perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup, + tf_error); } else { Index: gcc/cp/pt.c =================================================================== --- gcc/cp/pt.c (revision 189561) +++ gcc/cp/pt.c (working copy) @@ -8370,7 +8370,7 @@ perform_typedefs_access_check (tree tmpl, tree tar of the use of the typedef. */ input_location = iter->locus; perform_or_defer_access_check (TYPE_BINFO (type_scope), - type_decl, type_decl); + type_decl, type_decl, tf_error); } input_location = saved_location; } @@ -8877,7 +8877,7 @@ instantiate_class_template_1 (tree type) added to the template at parsing time. Let's get those and perform the access checks then. */ perform_typedefs_access_check (pattern, args); - perform_deferred_access_checks (); + perform_deferred_access_checks (tf_error); pop_nested_class (); maximum_field_alignment = saved_maximum_field_alignment; if (!fn_context) @@ -9870,10 +9870,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t com hash = hash_tmpl_and_args (gen_tmpl, argvec); spec = retrieve_specialization (gen_tmpl, argvec, hash); + r = spec; if (spec) { - r = spec; - break; + if (FNDECL_RECHECK_ACCESS_P (spec) && (complain & tf_error)) + /* Reinstantiate to get access errors. */; + else + break; } /* We can see more levels of arguments than parameters if @@ -9949,6 +9952,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t com if (type == error_mark_node) RETURN (error_mark_node); + if (r) + { + /* We're done reinstantiating for access errors. */ + gcc_assert (FNDECL_RECHECK_ACCESS_P (r)); + break; + } + /* We do NOT check for matching decls pushed separately at this point, as they may not represent instantiations of this template, and in any case are considered separate under the @@ -14294,6 +14304,7 @@ instantiate_template_1 (tree tmpl, tree orig_args, tree fndecl; tree gen_tmpl; tree spec; + bool access_ok = true; if (tmpl == error_mark_node) return error_mark_node; @@ -14341,7 +14352,13 @@ instantiate_template_1 (tree tmpl, tree orig_args, || fndecl == NULL_TREE); if (spec != NULL_TREE) - return spec; + { + if (FNDECL_RECHECK_ACCESS_P (spec) + && (complain & tf_error)) + /* Do the instantiation again, we're out of SFINAE context. */; + else + return spec; + } if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), complain)) @@ -14380,7 +14397,8 @@ instantiate_template_1 (tree tmpl, tree orig_args, /* Now we know the specialization, compute access previously deferred. */ push_access_scope (fndecl); - perform_deferred_access_checks (); + if (!perform_deferred_access_checks (complain)) + access_ok = false; pop_access_scope (fndecl); pop_deferring_access_checks (); @@ -14391,6 +14409,16 @@ instantiate_template_1 (tree tmpl, tree orig_args, if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl))) clone_function_decl (fndecl, /*update_method_vec_p=*/0); + if (!access_ok) + { + if (!(complain & tf_error)) + { + /* Remember to reinstantiate when we're out of SFINAE so the user + can see the errors. */ + FNDECL_RECHECK_ACCESS_P (fndecl) = true; + } + return error_mark_node; + } return fndecl; } Index: gcc/cp/semantics.c =================================================================== --- gcc/cp/semantics.c (revision 189561) +++ gcc/cp/semantics.c (working copy) @@ -223,7 +223,7 @@ pop_to_parent_deferring_access_checks (void) if (ptr->deferring_access_checks_kind == dk_no_deferred) { /* Check access. */ - perform_access_checks (checks); + perform_access_checks (checks, tf_error); } else { @@ -252,25 +252,30 @@ pop_to_parent_deferring_access_checks (void) /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node is the BINFO indicating the qualifying scope used to access the - DECL node stored in the TREE_VALUE of the node. */ + DECL node stored in the TREE_VALUE of the node. If CHECKS is empty + or we aren't in SFINAE context or all the checks succeed return TRUE, + otherwise FALSE. */ -void -perform_access_checks (VEC (deferred_access_check,gc)* checks) +bool +perform_access_checks (VEC (deferred_access_check,gc)* checks, + tsubst_flags_t complain) { int i; deferred_access_check *chk; location_t loc = input_location; + bool ok = true; if (!checks) - return; + return true; FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk) { input_location = chk->loc; - enforce_access (chk->binfo, chk->decl, chk->diag_decl); + ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain); } input_location = loc; + return (complain & tf_error) ? true : ok; } /* Perform the deferred access checks. @@ -287,19 +292,21 @@ pop_to_parent_deferring_access_checks (void) A::X A::a, x; // No error for `A::a', error for `x' We have to perform deferred access of `A::X', first with `A::a', - next with `x'. */ + next with `x'. Return value like perform_access_checks above. */ -void -perform_deferred_access_checks (void) +bool +perform_deferred_access_checks (tsubst_flags_t complain) { - perform_access_checks (get_deferred_access_checks ()); + return perform_access_checks (get_deferred_access_checks (), complain); } /* Defer checking the accessibility of DECL, when looked up in - BINFO. DIAG_DECL is the declaration to use to print diagnostics. */ + BINFO. DIAG_DECL is the declaration to use to print diagnostics. + Return value like perform_access_checks above. */ -void -perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl) +bool +perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl, + tsubst_flags_t complain) { int i; deferred_access *ptr; @@ -310,7 +317,7 @@ pop_to_parent_deferring_access_checks (void) /* Exit if we are in a context that no access checking is performed. */ if (deferred_access_no_check) - return; + return true; gcc_assert (TREE_CODE (binfo) == TREE_BINFO); @@ -319,8 +326,8 @@ pop_to_parent_deferring_access_checks (void) /* If we are not supposed to defer access checks, just check now. */ if (ptr->deferring_access_checks_kind == dk_no_deferred) { - enforce_access (binfo, decl, diag_decl); - return; + bool ok = enforce_access (binfo, decl, diag_decl, complain); + return (complain & tf_error) ? true : ok; } /* See if we are already going to perform this check. */ @@ -330,7 +337,7 @@ pop_to_parent_deferring_access_checks (void) if (chk->decl == decl && chk->binfo == binfo && chk->diag_decl == diag_decl) { - return; + return true; } } /* If not, record the check. */ @@ -341,6 +348,8 @@ pop_to_parent_deferring_access_checks (void) new_access->decl = decl; new_access->diag_decl = diag_decl; new_access->loc = input_location; + + return true; } /* Used by build_over_call in LOOKUP_SPECULATIVE mode: return whether DECL @@ -349,7 +358,7 @@ pop_to_parent_deferring_access_checks (void) bool speculative_access_check (tree binfo, tree decl, tree diag_decl, - bool complain) + tsubst_flags_t complain) { if (deferred_access_no_check) return true; @@ -359,8 +368,8 @@ speculative_access_check (tree binfo, tree decl, t if (!accessible_p (binfo, decl, true)) { /* Unless we're under maybe_explain_implicit_delete. */ - if (complain) - enforce_access (binfo, decl, diag_decl); + if (complain & tf_error) + enforce_access (binfo, decl, diag_decl, tf_error); return false; } @@ -1611,7 +1620,7 @@ finish_non_static_data_member (tree decl, tree obj tree access_type = TREE_TYPE (object); perform_or_defer_access_check (TYPE_BINFO (access_type), decl, - decl); + decl, tf_error); /* If the data member was named `C::M', convert `*this' to `C' first. */ @@ -1733,7 +1742,7 @@ check_accessibility_of_qualified_id (tree decl, && CLASS_TYPE_P (qualifying_type) && !dependent_type_p (qualifying_type)) perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl, - decl); + decl, tf_error); } /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the @@ -3336,7 +3345,7 @@ finish_id_expression (tree id_expression, { tree path = currently_open_derived_class (context); perform_or_defer_access_check (TYPE_BINFO (path), - decl, decl); + decl, decl, tf_error); } } Index: gcc/cp/parser.c =================================================================== --- gcc/cp/parser.c (revision 189561) +++ gcc/cp/parser.c (working copy) @@ -10515,7 +10515,7 @@ cp_parser_simple_declaration (cp_parser* parser, if (cp_parser_declares_only_class_p (parser)) shadow_tag (&decl_specifiers); /* Perform any deferred access checks. */ - perform_deferred_access_checks (); + perform_deferred_access_checks (tf_error); } /* Consume the `;'. */ @@ -12416,7 +12416,7 @@ cp_parser_template_id (cp_parser *parser, FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk) perform_or_defer_access_check (chk->binfo, chk->decl, - chk->diag_decl); + chk->diag_decl, tf_error); } /* Return the stored value. */ return check_value->value; @@ -15751,7 +15751,7 @@ cp_parser_init_declarator (cp_parser* parser, /* Perform the access control checks for the declarator and the decl-specifiers. */ - perform_deferred_access_checks (); + perform_deferred_access_checks (true); /* Restore the saved value. */ if (TREE_CODE (decl) == FUNCTION_DECL) @@ -21017,7 +21017,7 @@ cp_parser_function_definition_from_specifiers_and_ did not check, check them now. We must wait until we are in the scope of the function to perform the checks, since the function might be a friend. */ - perform_deferred_access_checks (); + perform_deferred_access_checks (tf_error); if (!success_p) { @@ -21311,7 +21311,7 @@ static void cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks) { ++processing_template_parmlist; - perform_access_checks (checks); + perform_access_checks (checks, tf_error); --processing_template_parmlist; } @@ -22760,7 +22760,7 @@ cp_parser_pre_parsed_nested_name_specifier (cp_par FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk) perform_or_defer_access_check (chk->binfo, chk->decl, - chk->diag_decl); + chk->diag_decl, tf_error); } /* Set the scope from the stored value. */ parser->scope = check_value->value; @@ -24018,7 +24018,7 @@ cp_parser_objc_method_definition_list (cp_parser* if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END)) { - perform_deferred_access_checks (); + perform_deferred_access_checks (tf_error); stop_deferring_access_checks (); meth = cp_parser_function_definition_after_declarator (parser, false); Index: gcc/cp/call.c =================================================================== --- gcc/cp/call.c (revision 189561) +++ gcc/cp/call.c (working copy) @@ -5515,7 +5515,8 @@ build_op_delete_call (enum tree_code code, tree ad /* If the FN is a member function, make sure that it is accessible. */ if (BASELINK_P (fns)) - perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn); + perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn, + complain); /* Core issue 901: It's ok to new a type with deleted delete. */ if (DECL_DELETED_FN (fn) && alloc_fn) @@ -5573,19 +5574,23 @@ build_op_delete_call (enum tree_code code, tree ad the declaration to use in the error diagnostic. */ bool -enforce_access (tree basetype_path, tree decl, tree diag_decl) +enforce_access (tree basetype_path, tree decl, tree diag_decl, + tsubst_flags_t complain) { gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO); if (!accessible_p (basetype_path, decl, true)) { - if (TREE_PRIVATE (decl)) - error ("%q+#D is private", diag_decl); - else if (TREE_PROTECTED (decl)) - error ("%q+#D is protected", diag_decl); - else - error ("%q+#D is inaccessible", diag_decl); - error ("within this context"); + if (cxx_dialect < cxx0x || complain & tf_error) + { + if (TREE_PRIVATE (decl)) + error ("%q+#D is private", diag_decl); + else if (TREE_PROTECTED (decl)) + error ("%q+#D is protected", diag_decl); + else + error ("%q+#D is inaccessible", diag_decl); + error ("within this context"); + } return false; } @@ -6513,11 +6518,12 @@ build_over_call (struct z_candidate *cand, int fla if (flags & LOOKUP_SPECULATIVE) { if (!speculative_access_check (cand->access_path, access_fn, fn, - complain & tf_error)) + complain)) return error_mark_node; } - else - perform_or_defer_access_check (cand->access_path, access_fn, fn); + else if (!perform_or_defer_access_check (cand->access_path, access_fn, + fn, complain)) + return error_mark_node; } /* If we're checking for implicit delete, don't bother with argument Index: gcc/cp/cp-tree.h =================================================================== --- gcc/cp/cp-tree.h (revision 189561) +++ gcc/cp/cp-tree.h (working copy) @@ -78,6 +78,7 @@ c-common.h, not after. CONVERT_EXPR_VBASE_PATH (in CONVERT_EXPR) OVL_ARG_DEPENDENT (in OVERLOAD) PACK_EXPANSION_LOCAL_P (in *_PACK_EXPANSION) + TINFO_RECHECK_ACCESS_P (in TEMPLATE_INFO) 1: IDENTIFIER_VIRTUAL_P (in IDENTIFIER_NODE) TI_PENDING_TEMPLATE_FLAG. TEMPLATE_PARMS_FOR_INLINE. @@ -725,6 +726,14 @@ typedef struct qualified_typedef_usage_s qualified DEF_VEC_O (qualified_typedef_usage_t); DEF_VEC_ALLOC_O (qualified_typedef_usage_t,gc); +/* Non-zero if this template specialization has access violations that + should be rechecked when the function is instantiated outside argument + deduction. */ +#define TINFO_RECHECK_ACCESS_P(NODE) \ + (TREE_LANG_FLAG_0 (TEMPLATE_INFO_CHECK (NODE))) +#define FNDECL_RECHECK_ACCESS_P(NODE) \ + (TINFO_RECHECK_ACCESS_P (DECL_TEMPLATE_INFO (NODE))) + struct GTY(()) tree_template_info { struct tree_common common; VEC(qualified_typedef_usage_t,gc) *typedefs_needing_access_checking; @@ -4901,7 +4910,8 @@ extern bool can_convert_arg (tree, tree, tree, i tsubst_flags_t); extern bool can_convert_arg_bad (tree, tree, tree, int, tsubst_flags_t); -extern bool enforce_access (tree, tree, tree); +extern bool enforce_access (tree, tree, tree, + tsubst_flags_t); extern void push_defarg_context (tree); extern void pop_defarg_context (void); extern tree convert_default_arg (tree, tree, tree, int, @@ -5497,10 +5507,13 @@ extern void stop_deferring_access_checks (void); extern void pop_deferring_access_checks (void); extern VEC (deferred_access_check,gc)* get_deferred_access_checks (void); extern void pop_to_parent_deferring_access_checks (void); -extern void perform_access_checks (VEC (deferred_access_check,gc)*); -extern void perform_deferred_access_checks (void); -extern void perform_or_defer_access_check (tree, tree, tree); -extern bool speculative_access_check (tree, tree, tree, bool); +extern bool perform_access_checks (VEC (deferred_access_check,gc)*, + tsubst_flags_t); +extern bool perform_deferred_access_checks (tsubst_flags_t); +extern bool perform_or_defer_access_check (tree, tree, tree, + tsubst_flags_t); +extern bool speculative_access_check (tree, tree, tree, + tsubst_flags_t); extern int stmts_are_full_exprs_p (void); extern void init_cp_semantics (void); extern tree do_poplevel (tree); Index: gcc/cp/search.c =================================================================== --- gcc/cp/search.c (revision 189561) +++ gcc/cp/search.c (working copy) @@ -1254,8 +1254,10 @@ lookup_member (tree xbasetype, tree name, int prot && !really_overloaded_fn (rval)) { tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval; - if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) - perform_or_defer_access_check (basetype_path, decl, decl); + if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) + && !perform_or_defer_access_check (basetype_path, decl, decl, + complain)) + rval = error_mark_node; } if (errstr && protect) Index: gcc/cp/friend.c =================================================================== --- gcc/cp/friend.c (revision 189561) +++ gcc/cp/friend.c (working copy) @@ -166,7 +166,7 @@ add_friend (tree type, tree decl, bool complain) ctx = DECL_CONTEXT (decl); if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx)) - perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl); + perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl, tf_error); maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);