From patchwork Fri Jul 6 23:50:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ville Voutilainen X-Patchwork-Id: 940736 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-481146-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="RQGoZBD9"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="PfYX8v06"; dkim-atps=neutral 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 41Ms163Cl9z9s1B for ; Sat, 7 Jul 2018 09:50:32 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=HB8qIIvTHCAaGJPWg3oPoDgpWwxAq5O0bR0+K4zadqm5AE e1D/na3uWjiTHIo+N0jEMZhmz3I1WmFOL6n8/rw7XzRZHYDTcWFiyEEcodYSYaH2 O8u2QcY8YOfJlz0cgn9oozIvAmzzHQ1Q2SC59LYSxy8BMtfgjLCnt5IDjz90M= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=3xsYtCASgZ8w+TUA322o+WEdpLM=; b=RQGoZBD9bx2pe7KhdIzh shgzw9pPKSFM5vaZAMl997wQCy9Sn1bW8fIkmA7CVoeV6UxUg4JhP0zzO/BZXVOA PGqqroB+oxd6HZUm+KSz8w5Ukb8d9kQ71QbVKpLa0SY5rEXh7liNBbZaBRqeOpSb QQjrlYKx87fPSyLHr2i5jxc= Received: (qmail 27598 invoked by alias); 6 Jul 2018 23:50:24 -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 27576 invoked by uid 89); 6 Jul 2018 23:50:23 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.1 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=name-lookup, namelookup, decl_p, DECL_P X-HELO: mail-oi0-f68.google.com Received: from mail-oi0-f68.google.com (HELO mail-oi0-f68.google.com) (209.85.218.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 06 Jul 2018 23:50:22 +0000 Received: by mail-oi0-f68.google.com with SMTP id c6-v6so26460063oiy.0 for ; Fri, 06 Jul 2018 16:50:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=Nm+uZ71Xgt+b9Ag9a9DVdPOxmhBGOwsIyAMav27PIWk=; b=PfYX8v06NF8ZwiWztxhFagsl07sbdV3nQrppBF0Cen7UYF8Ug1oITbTtnzf9shlAmB kgDWed+80YGe52ucuRISiwXjvAhFwW0BiPjeDeG8mQ4biNy0lsPoTelb+fnNseInNvZh i53ZwxPQFCtcYWJViEB8gSqDJcBuV91MXSshgaha9Q13Yjit7qtR06zeeLQ3NR/wk5sS o99o15FKjxxYsaezHYo6B7I652yX9IKPHI3vEpZg75TUIKyQDiOoeaziq7QNH5+fXuJA fV/5zP4jK9A7xOM3bwlrP79ncZuLrJokeEK7ZFZmytKVuMmLyoN7wV70NP7r7XgIEMIp vtOw== MIME-Version: 1.0 Received: by 2002:a9d:156:0:0:0:0:0 with HTTP; Fri, 6 Jul 2018 16:50:19 -0700 (PDT) From: Ville Voutilainen Date: Sat, 7 Jul 2018 02:50:19 +0300 Message-ID: Subject: [C++ PATCH] PR c++/79133 To: gcc-patches List , Jason Merrill , Nathan Sidwell Tested on Linux-PPC64. Ok for trunk, perhaps with the change that I move the test under cpp1y, since it's a c++14 test anyway? I considered pushing the captures into the parameter scope. I don't know how to do that; changing the pushdecl_outermost_localscope to a pushdecl doesn't seem to cut it; I guess that I should add a new function into name-lookup.[ch], but I wonder whether that makes sense, considering that this is lambda-only functionality. I also wonder whether it makes more sense than the solution in this patch, considering that we handle packs here as well and capturepack/parampack, capturepack/param, capture/parampack and capture/param clashes. Guidance welcome. This approach has the benefit that it, well, seems to work. :) 2018-07-07 Ville Voutilainen gcc/cp/ PR c++/79133 * lambda.c (start_lambda_function): Reject captures and parameters with the same name. testsuite/ PR c++/79133 * g++.dg/cpp0x/lambda/lambda-shadow3.C: New. diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c index 3776d6b..534434a 100644 --- a/gcc/cp/lambda.c +++ b/gcc/cp/lambda.c @@ -1424,7 +1424,28 @@ start_lambda_function (tree fco, tree lambda_expr) /* Push the proxies for any explicit captures. */ for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap; cap = TREE_CHAIN (cap)) - build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap)); + { + /* DR 2211: check that captures and parameters + do not have the same name. */ + for (tree parms = DECL_ARGUMENTS (fco); parms; + parms = TREE_CHAIN (parms)) + { + tree real_cap = TREE_VALUE (cap); + tree real_parms = parms; + if (PACK_EXPANSION_P (real_cap)) + real_cap = PACK_EXPANSION_PATTERN (real_cap); + if (PACK_EXPANSION_P (parms)) + real_parms = PACK_EXPANSION_PATTERN (parms); + if (DECL_P (real_cap) + && DECL_NAME (real_cap) != this_identifier + && DECL_NAME (real_cap) == DECL_NAME (real_parms)) + error_at (DECL_SOURCE_LOCATION (parms), + "capture %qE and lambda parameter %qE " + "have the same name", + cap, parms); + } + build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap)); + } return body; } diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-shadow3.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-shadow3.C new file mode 100644 index 0000000..b006470 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-shadow3.C @@ -0,0 +1,12 @@ +// { dg-do compile { target c++14 } } + +int main() { + int x = 42; + auto lambda = [x](int x) {}; // { dg-error "have the same name" } + auto lambda2 = [x=x](int x) {}; // { dg-error "have the same name" } + auto lambda3 = [x](auto... x) {}; // { dg-error "have the same name" } + auto lambda4 = [](auto... x) { + auto lambda5 = [x...](auto... x) {}; // { dg-error "have the same name" } + auto lambda6 = [x...](int x) {}; // { dg-error "have the same name" } + }; +}