From patchwork Tue Feb 13 16:49:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 200521 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp666373dyb; Tue, 13 Feb 2024 08:49:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWfAoPxXlofYAolrHQKQxmuSdZd3oZmv5YENUF5a0i8Z+BdaLGcNa0/ZBen7l9WMji2MrfHXSoeIHkra7cgz4kRcfbOkg== X-Google-Smtp-Source: AGHT+IF4HWUmhMQ6ehtRWuJE+RJkBPHpajTvCOscWvKO1xZ9lDGalIenTDQc83OL9HGrnI0xBVLA X-Received: by 2002:a05:622a:20a:b0:42d:b20d:d6fc with SMTP id b10-20020a05622a020a00b0042db20dd6fcmr1498364qtx.3.1707842995257; Tue, 13 Feb 2024 08:49:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707842995; cv=pass; d=google.com; s=arc-20160816; b=RPYswky2Na/p4JJuJbRkAMi8OTDKAh/MMp56f1PhTtlD6fi/zuQiwLaXjOwih6HJnF R/BnbMdooWyKSZXxF3a1xW3S2od6/BUQbvwNjn9i1OBuDuF6679TXCsIqUKO/7d/iHdc RdvkPZdJxewIw336pNXuBlBn2YCLSgxTCrAKUb5rB+ZlDkd/W1qbITLjEo4DNUZNwq3h z/2vcaJRWczI5Pr/eOIQz/HRzYMnzPlyZOX38v07MC5BYDQsc6xQrL9QqbCE3ZmxKQtp w/fYMeowbe2Q/6+1dDmqcCLKETV7pR7LCgHJhocBrNCtUzpo73IUIpttxAwX11jxAHtM LE4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Mve/RxAbAZFGrDmjMa9GqqUM96pzdqzJjtkTE7qze+w=; fh=f4aINNEuLAyoj7tmGElB3o+xxiioWDedyoeaOWUv3OY=; b=tYcTDNGn1vWPigD97qS2aqZQ4+9G9/PmlQwrD+84pDbZ815h0/GbFEuQENmM+m2vLi U2eHEJ8u7al6c4+O+r9sw7cWCS90/hTq/jeOyoiZjIPfXu3Sqhxu0LIxYvuuEDNoYhC6 c6Qgycu7qA6FBd4HxckvARlGVltv02w/NzckDWyqeyrc+LuX3w6zduKmE74zW4yPpyIQ w1JPbudSBDE9FHEyw/rw6NuWE8nZnBfDLmmILG2crv653H+3lQqV1psgqzWThHmMlhKB Xt14Oz7+JWIvF/6cUrdMqHng3BSgohjklLD0238uLZtrl7CA6s7oH/T+h9HtHvENxGzI crlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XhOOJk+o; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCUVSiuJzt1FTQrQK67dvxTbGA908tb9luXUMmunFyTIFKtojsWWG8KO+t4ValXMSlhliV/YRNiDzROs8VTnpJuVEsMkLw== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o12-20020a05622a008c00b0042c6d7ee1a8si3430127qtw.722.2024.02.13.08.49.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 08:49:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XhOOJk+o; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9CA73857C43 for ; Tue, 13 Feb 2024 16:49:54 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id E226A38582B1 for ; Tue, 13 Feb 2024 16:49:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E226A38582B1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E226A38582B1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707842958; cv=none; b=DgzVFSMV4TTzSWDW9rkgHCc7pXQXDOkWWSMqfQyhD2/aGlqXvGFaDJ4+fAtbXkB9z+0hYTbj/aUFX96cfbB6Z2RDsRgkna1E29qkL2tj7G8+Rrqb01XnWHH7gwyhsjKbcgTuauHYzIOkuf+OUPJNws11Cnig7kPEyhgPq+eP+Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707842958; c=relaxed/simple; bh=sdBERQmmAht0asV6JQcLld7EQATjKVyiW+2wcyCX6dY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=O9H61GzXMOMYxCowrLLVn/UjxFqCn+avCO0gXBm5VRtNANb8jcDzqBG/ueE8vME2yra3pY5bLTk71RZRgVl2pQWTDxteS7gzDyZ+o5Ir+eetn0ZpVlQ1GuFt4yENcY/HH/WhNElMMQlWHi2YpXBkjBZjiDoFzmo/p5hyz5lOnIQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707842956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Mve/RxAbAZFGrDmjMa9GqqUM96pzdqzJjtkTE7qze+w=; b=XhOOJk+oa/x7U21B7Bg3ENEnVd+SQvdrjHPtASlYYZ5JX7f8GcqDNe9ThvwyXLu6zteICy +uSnWHgodXTVMZWtlSwZmitxdBwpxp3iX00+9LCh2ZJN5l83opAjBoX0Skq/+RwNqwoZNo +IhDLYRh9Ok0LeWuVBCN63MUqkgEriQ= Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-423-UGQndP9sNUaoV1ggZhv51Q-1; Tue, 13 Feb 2024 11:49:14 -0500 X-MC-Unique: UGQndP9sNUaoV1ggZhv51Q-1 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-602dae507caso78916387b3.0 for ; Tue, 13 Feb 2024 08:49:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707842953; x=1708447753; 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=Mve/RxAbAZFGrDmjMa9GqqUM96pzdqzJjtkTE7qze+w=; b=oTh094isxA7UvHdd78ajea+z9b+eUxodEjzN6muJfTfsMhZQrecyJzVK7T1wgRx09S dXp68wYEMCRIfzzlmo8FlNx89Cj21GTfID2RPe5ECiYYh6B37OFucSHuyjrEUxs6keCK b89U+ch17A+fgMrXiqDtvpNNJZ8IHStQNaaadSA8/Nobcive1bQfhwd6zMRqJ9bHXJsM Rp31767T5qkPtJk/SFfNYv1yW4P8BLbE183V7x8/9I+VIH5Uc2MUTXxhGtHr5iyRn3et TWuKysiMDk0gN1wsZsjGgegDnY0IzTNmM3m7aK56BI1oZ1eMHtgVvlbbz5EaaOdg943c 2D1A== X-Gm-Message-State: AOJu0Yzox4GRFll9cTh8rfhqkihohzPgWD/UqbiGYXrKFVx1JT2rRRgi 41PHErwIT0xksf4FqTrZ5MWzjMKDGThIAbCJ9T5FdkMUd4qvXmIJTyV8VaR/hpm2UOyUYKMVpvo DbhLtdmGsO8rDJWw468RvXsDFzPq0WqvnZodCzDI17EVE94alU3HQYSyxV03M5fpG5KqFE5PFSg r3/y/LVsZnPvHdHkAPI22gEji4ivzJdC9eN0MH X-Received: by 2002:a81:7107:0:b0:607:8f1d:5926 with SMTP id m7-20020a817107000000b006078f1d5926mr1573592ywc.15.1707842953338; Tue, 13 Feb 2024 08:49:13 -0800 (PST) X-Received: by 2002:a81:7107:0:b0:607:8f1d:5926 with SMTP id m7-20020a817107000000b006078f1d5926mr1573577ywc.15.1707842952996; Tue, 13 Feb 2024 08:49:12 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWPDk1wbK+BaZta3yYxx4qz/dQhMqs6gbnxrH91Pq4SPhG8w09UZI8/UEvvhDPoDkT7pUSFg3w8uZllRDvKB8IXNA== Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id x130-20020a81a088000000b0060781543694sm523295ywg.10.2024.02.13.08.49.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 08:49:12 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: implicitly_declare_fn and access checks [PR113908] Date: Tue, 13 Feb 2024 11:49:07 -0500 Message-ID: <20240213164907.3648858-1-ppalka@redhat.com> X-Mailer: git-send-email 2.44.0.rc0.46.g2996f11c1d MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 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, RCVD_IN_MSPIKE_H2, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790803176749668276 X-GMAIL-MSGID: 1790803176749668276 Bootstrapped and regtested on x86_64-pc-linux-gnu, are one of both of these fixes OK for trunk? -- >8 -- Here during ahead of time checking of the non-dependent new-expr we synthesize B's copy constructor, which should be defined as deleted due to A's inaccessible copy constructor. But enforce_access incorrectly decides to defer the (silent) access check for A::A(const A&) during synthesization since current_template_parms is still set (before r14-557 it checked processing_template_decl which got cleared from implicitly_declare_fn), which leads to the access check leaking out to the template context that needed the synthesization. This patch narrowly fixes this regression in two sufficient ways: 1. Clear current_template_parms alongside processing_template_decl in implicitly_declare_fn so that it's more independent of context. 2. Don't defer a silent access check when in a template context, since such deferred checks will be replayed noisily at instantiation time which may not be what the caller intended. We need to adjust cp_parser_make_typename_type since otherwise the diagnostic for typedef22.C regresses because cp_parser_invalid_type_name otherwise will emit "invalid combination of multiple type-specifiers" for what is an inaccessible qualified-id. PR c++/113908 gcc/cp/ChangeLog: * method.cc (implicitly_declare_fn): Clear current_template_parms alongside processing_template_decl. * parser.cc (cp_parser_make_typename_type): Temporarily defer access checks around the tf_none call to make_typename_type. * semantics.cc (enforce_access): Don't defer silent access checks from a template context. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent31.C: New test. --- gcc/cp/method.cc | 8 +++++++- gcc/cp/parser.cc | 2 ++ gcc/cp/semantics.cc | 1 + .../g++.dg/template/non-dependent31.C | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/non-dependent31.C diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 957496d3e18..17d42143299 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -3088,6 +3088,7 @@ implicitly_declare_fn (special_function_kind kind, tree type, tree this_parm; tree name; HOST_WIDE_INT saved_processing_template_decl; + tree saved_current_template_parms; bool deleted_p = false; bool constexpr_p = false; tree inherited_ctor = (kind == sfk_inheriting_constructor @@ -3132,6 +3133,10 @@ implicitly_declare_fn (special_function_kind kind, tree type, when not in a template. */ saved_processing_template_decl = processing_template_decl; processing_template_decl = 0; + /* Also clear CURRENT_TEMPLATE_PARMS so enforce_access immediately checks + access even in a template context. */ + saved_current_template_parms = current_template_parms; + current_template_parms = NULL_TREE; type = TYPE_MAIN_VARIANT (type); @@ -3341,8 +3346,9 @@ implicitly_declare_fn (special_function_kind kind, tree type, set_constraints (fn, new_ci); } - /* Restore PROCESSING_TEMPLATE_DECL. */ + /* Restore PROCESSING_TEMPLATE_DECL and CURRENT_TEMPLATE_PARMS. */ processing_template_decl = saved_processing_template_decl; + current_template_parms = saved_current_template_parms; if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL) fn = add_inherited_template_parms (fn, inherited_ctor); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index c4292c49ce2..29120995449 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -4350,8 +4350,10 @@ cp_parser_make_typename_type (cp_parser *parser, tree id, tree result; if (identifier_p (id)) { + push_deferring_access_checks (dk_deferred); result = make_typename_type (parser->scope, id, typename_type, /*complain=*/tf_none); + pop_to_parent_deferring_access_checks (); if (result == error_mark_node) cp_parser_diagnose_invalid_type_name (parser, id, id_location); return result; diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 3299e270446..0a18eaf9c2e 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -346,6 +346,7 @@ enforce_access (tree basetype_path, tree decl, tree diag_decl, tree cs = current_scope (); if (in_template_context + && (complain & tf_error) && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)) if (tree template_info = get_template_info (cs)) { diff --git a/gcc/testsuite/g++.dg/template/non-dependent31.C b/gcc/testsuite/g++.dg/template/non-dependent31.C new file mode 100644 index 00000000000..3fa68f40fe1 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/non-dependent31.C @@ -0,0 +1,18 @@ +// PR c++/113908 +// { dg-do compile { target c++11 } } + +struct A { + A(); +private: + A(const A&); +}; + +struct B { + A a; + + template + static void f() { new B(); } +}; + +template void B::f(); +static_assert(!__is_constructible(B, const B&), "");