From patchwork Thu Nov 7 10:48:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 2007954 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=EHo1txVw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xkf2p34Cpz1xxq for ; Thu, 7 Nov 2024 21:49:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D28D3858D34 for ; Thu, 7 Nov 2024 10:49:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id C53493858D20 for ; Thu, 7 Nov 2024 10:48:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C53493858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C53493858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::433 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730976543; cv=none; b=bfcDeSsolz0zhV8mf5Wb50R/YDwF8sxPWFRq1jserHUHw/96aL42XlBPBA+weezIqoTW9v5Lhs4b0umBqJZZx1b+6Hm/4tCxuatk7HfQ5cWor70BAOXw+PM1mOI+Hpn092sXgG1XsvMxal3cCtH2Y1Z/zl2ulGGdYnRzXdEitWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730976543; c=relaxed/simple; bh=aTaH9+mKSgk74r/UTy0Ecat1vQ920MPAuw1EPshkxLg=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=IYF7UHCf3l1WNa5PzMxpPc6ZWoNjL+o46bxRmjZ+2MbxAqp0d7XViBQ5tCUBx4wXAtNi6B1zW4KOFrjguM5/ZnbewMUmppxtZoN5b++Y5ZRyKeSQ7+kiSqseD4GgifRgE2lRBU/CEIo+r586OnrxcCv+QiSaip+jCXrMgW0W/vU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-71e7131931dso21599b3a.3 for ; Thu, 07 Nov 2024 02:48:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730976537; x=1731581337; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Dh3nIWJbXEoGrA60GsKcfaCPE9N7psnN1+qPeHUhuHU=; b=EHo1txVwrNzCXdafP0QnVTfrgrTbQTvlZOuEHi3dIdx1VtBw82VPmHFP0M4Jg22K4p BiC00jyvHrVF5Y0w1VQHvS3/nw93N5ID3+yBxnW/RtQnAIZOy/c2g+oRhCxZCXr46vZL 2jSUtEHZSIgnn2Tdz5fX+ZT9Nxfnxyj6E1TZ/QEck35HxxZnNE7JaAfTdOKkyQ6cCC+0 u0uoCMcs5NEPCpAcxdDflsakgjGsUiSQbchZp+ADssHAheG3age7SON3tDlb3PxEPNtR iqRqvf4Da5FQD1CV/QcaFLQEOHDTPPX4kYsvMRn+WsrMy+ru8MbBsY0UA8V4eASG833W 0Rhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730976537; x=1731581337; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Dh3nIWJbXEoGrA60GsKcfaCPE9N7psnN1+qPeHUhuHU=; b=iyHAZqs9BHmIdyPyogQHquGCBdAo2KpA7fjqGXbQgFDtk5A8SLc1lVJrnNrNC0H1qz o/TnvM9yX4nZKiiw/Q6UNBJp5P5DLXUzMCfN4YDLMbrvOcdg2D0/e3xnH7IhsbXhp1Wx 4ACPnn5ZBg3qu6BHCmiM9Vd6aD30aoehE3KSjvndOivbz91lPWmbEuIxAtcIt4605Udg QMLPPxQ3uUmxOMfek33Txi6zO/hGp0coXRM+ymcoDXzPP9Z8OmfelvDSytZYzcOfI1O5 x66BnF2xn6qGG/BXQYrX53TSQaON/Pgo3xrPBLHhisCeQ0uzPQgr2Pz/lzC6ZSpy7xGK +bag== X-Gm-Message-State: AOJu0Ywv101AGHEU+u4vWKxuYtTB2sPYiLiA8ouNZkMVeI1e++C7zAZh wJ0dpo4SsynlVJ8vd2L5g4p3A54ECJZienWvBjjjLd9WHs3MY5peQgDU/w== X-Google-Smtp-Source: AGHT+IEp5VgTAtniJERt8MczPPINim2OEnJzUaS3p92tRhMIArtLoc+RtjEFEMue0GvGb1lsawNsRw== X-Received: by 2002:a05:6a00:3e02:b0:71e:5033:c6 with SMTP id d2e1a72fcca58-7240cfafab6mr282113b3a.5.1730976536664; Thu, 07 Nov 2024 02:48:56 -0800 (PST) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72407a17ae4sm1221263b3a.140.2024.11.07.02.48.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2024 02:48:56 -0800 (PST) Message-ID: <672c9b18.050a0220.2a5143.2862@mx.google.com> X-Google-Original-Message-ID: Date: Thu, 7 Nov 2024 21:48:52 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH] c++: Disallow decomposition of lambda bases [PR90321] MIME-Version: 1.0 Content-Disposition: inline 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and lightly regtested on x86_64-pc-linux-gnu (so far just dg.exp), OK for trunk if full regtest succeeds? -- >8 -- Decomposition of lambda closure types is not allowed by [dcl.struct.bind] p6, since members of a closure have no name. r244909 made this an error, but missed the case where a lambda is used as a base. This patch moves the check to find_decomp_class_base to handle this case. As a drive-by improvement, we also slightly improve the diagnostics to indicate why a base class was being inspected. Ideally the diagnostic would point directly at the relevant base, but there doesn't seem to be an easy way to get this location just from the binfo so I don't worry about that here. PR c++/90321 gcc/cp/ChangeLog: * decl.cc (find_decomp_class_base): Check for decomposing a lambda closure type. Report base class chains if needed. (cp_finish_decomp): Remove no-longer-needed check. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/decomp62.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/decl.cc | 20 ++++++++++++++------ gcc/testsuite/g++.dg/cpp1z/decomp62.C | 12 ++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp62.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 0e4533c6fab..87480dca1ac 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -9268,6 +9268,14 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, static tree find_decomp_class_base (location_t loc, tree type, tree ret) { + if (LAMBDA_TYPE_P (type)) + { + error_at (loc, "cannot decompose lambda closure type %qT", type); + inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)), + "lambda declared here"); + return error_mark_node; + } + bool member_seen = false; for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) if (TREE_CODE (field) != FIELD_DECL @@ -9310,9 +9318,14 @@ find_decomp_class_base (location_t loc, tree type, tree ret) for (binfo = TYPE_BINFO (type), i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) { + auto_diagnostic_group d; tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret); if (t == error_mark_node) - return error_mark_node; + { + inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)), + "in base class of %qT", type); + return error_mark_node; + } if (t != NULL_TREE && t != ret) { if (ret == type) @@ -9768,11 +9781,6 @@ cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p) error_at (loc, "cannot decompose non-array non-class type %qT", type); goto error_out; } - else if (LAMBDA_TYPE_P (type)) - { - error_at (loc, "cannot decompose lambda closure type %qT", type); - goto error_out; - } else if (processing_template_decl && complete_type (type) == error_mark_node) goto error_out; else if (processing_template_decl && !COMPLETE_TYPE_P (type)) diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp62.C b/gcc/testsuite/g++.dg/cpp1z/decomp62.C new file mode 100644 index 00000000000..b0ce10570c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp62.C @@ -0,0 +1,12 @@ +// PR c++/90321 +// { dg-do compile { target c++17 } } + +template struct hack : F { }; +template hack(F) -> hack; + +int main() +{ + auto f = [x = 1, y = 2]() { }; + auto [a, b] = hack { f }; // { dg-error "cannot decompose lambda closure type" } + return b; +}