Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp441380rwl; Wed, 9 Aug 2023 17:40:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGX5TfxFLZUtUwgjAuxojtsuQuLjh0iiNazQY7S52DzRaHEUmby0mdyhuYkwTceGmVHY19T X-Received: by 2002:a05:6a00:2997:b0:67b:77e7:f467 with SMTP id cj23-20020a056a00299700b0067b77e7f467mr576424pfb.16.1691628044196; Wed, 09 Aug 2023 17:40:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691628044; cv=none; d=google.com; s=arc-20160816; b=o1XUxdU76ztxmcFFRDjGAitOjDR44keJ4H+9/ER1PxjjyzMhJia4N3sIhxVhxJRanO YOrTbWUp1tLPX9GS2evbvZQNqQSXfJF2I3KyQtLThxz+15wjYtBihgJVEpuQOFTJXFee kXkk+KrBZtcotXDGUx/nxhMXDe8NeJHIj2YloC3Fk6w9XIAb+GwpaGVbNFAOOYacVn3t 2JjCXkJSTLowyeCJhYZJjU8q5/JYrW9tATRw+xQkEagea/0snoAfJ8xp1Tr36X+InSaU tRTpt3TD/1FQFm6f4XFaBGNngVncVV6N8pkyQCpgN7/oPHFDpbabUWu2RUNYuu+o94B4 An2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=BIUl3FxuenbYgegtww4zsQx4goyBh2oZ6RD1qE04nf0=; fh=pv8r68/7WM0830qdBJBQIYCJwof2LS98xa42gAHT4EY=; b=JNMOEjK1jUxevSVkhVRBqJ5MfFk5sPvhBLiKCo+cwt08Ju2TVzphFZSq8Aw3nCkdew b6f1RlyVKQrLbw37PfTO3d8uvjgCSfXYxz4vlMKvkptAr2PEYSBzRx0wV2A+ne1rbPkN HPc8NmDUlM5JBJ1/i6hXlTwMfM6Hb/608lZJqSyCoC1tS9Sl9puuUthabIg36/7AvFvO d90lhSjsYHJfhMNeYtlRTMx11s6HnAMmM+RggsdzjJ6177XGzuHH74wLqB2FnPE4qLru 0lnChaY7LlE7Y5bRSy6ppWWXhZLeT34/kve46Vcic37nO3Di5x+EwJrfi5VwqDrtF8zc 35bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=YOX5rrcH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b36-20020a630c24000000b00563d9ff5158si377596pgl.350.2023.08.09.17.40.32; Wed, 09 Aug 2023 17:40:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=YOX5rrcH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231402AbjHIXW2 (ORCPT + 99 others); Wed, 9 Aug 2023 19:22:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231337AbjHIXW1 (ORCPT ); Wed, 9 Aug 2023 19:22:27 -0400 Received: from mail-yb1-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBD89E7F; Wed, 9 Aug 2023 16:22:26 -0700 (PDT) Received: by mail-yb1-xb32.google.com with SMTP id 3f1490d57ef6-d44c2ca78ceso2137190276.0; Wed, 09 Aug 2023 16:22:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691623346; x=1692228146; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=BIUl3FxuenbYgegtww4zsQx4goyBh2oZ6RD1qE04nf0=; b=YOX5rrcHgZWY2PAuT84zdMK5Yo/XpX16IfVAZhlkaZrKsZXwKG64Js58zCjXWXaTIH PZrdehNDUIQ4TQguPx1elh2OcQF+y315TIYCyNzgJ3NHUh2+xPmCsPrvLd2BVxdP9sGR 5oRCEf+xHeM0b0kFaftTOX8s+ou0dNYRWqMkiXMw5LjZN1cJ3H5wnuZQkTlgxSiPjVSN 0VyJvWLNwsGL6SXH0ieyGbucyBUrrFT6WKlpqXZxTfYh3fe+VbKuc4mCm3shUwWkMRH3 CBNPQ28F+3l/osELuaK8AWaq4pogM48Xuur+/+o/mYykeXjNCjtS/mfBIfyy3NyaN2eL 7SIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691623346; x=1692228146; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BIUl3FxuenbYgegtww4zsQx4goyBh2oZ6RD1qE04nf0=; b=j2FSn8LfvfVrS4QTmTD1Aqh8JKQPggYx+1GkZPbssp+pflOHHVPuyF/fQdgJGacjd0 jMEDTNlXIMpecGbEqVFsCUzQD0vCphlY76SZxd0oerAuzX37QSvUTWV7untVCjIOF0Ux EiuJXEJjedJwBESXF9nA1Si+UrnGTEZ3M14cPRCXJKBNUvif9dXNB9Hsqjt4hIkxTqqu ZD6lGb/dB5MIH7/WfH58YCBE1mRpgtqcmV4kjgJ7N2YTp5L0Jtzr7R2guB/ju1WAINmp kWCp+FYjMwWU7L1qH/aAZ4KmsbL3uwmvTsu9bWHZzMtln4Yjqk14pedTddYpIQNzQizV lacA== X-Gm-Message-State: AOJu0YxMlxs+xAZrWNpU08CGr4bJ1eFlqzBSk8+vJ3nZjIyHzb9TCm9g 5OeV+B2V0f0B8P7mvNrZDOWh9Ta9IAwQZhNRDQs= X-Received: by 2002:a25:adc6:0:b0:cb2:7e6:191c with SMTP id d6-20020a25adc6000000b00cb207e6191cmr499022ybe.20.1691623346002; Wed, 09 Aug 2023 16:22:26 -0700 (PDT) MIME-Version: 1.0 References: <20230628171108.1150742-1-gary@garyguo.net> In-Reply-To: <20230628171108.1150742-1-gary@garyguo.net> From: Miguel Ojeda Date: Thu, 10 Aug 2023 01:22:15 +0200 Message-ID: Subject: Re: [PATCH] rust: macros: add `paste!` proc macro To: Gary Guo Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 28, 2023 at 7:12=E2=80=AFPM Gary Guo wrote: > > This macro provides a flexible way to concatenated identifiers together > and it allows the resulting identifier to be used to declare new items, > which `concat_idents!` does not allow. It also allows identifiers to be > transformed before concatenated. > > The `concat_idents!` example > > let x_1 =3D 42; > let x_2 =3D concat_idents!(x, _1); > assert!(x_1 =3D=3D x_2); > > can be written with `paste!` macro like this: > > let x_1 =3D 42; > let x_2 =3D paste!([]); > assert!(x_1 =3D=3D x_2); > > However `paste!` macro is more flexible because it can be used to create > a new variable: > > let x_1 =3D 42; > paste!(let [] =3D [];); > assert!(x_1 =3D=3D x_2); > > While this is not possible with `concat_idents!`. > > This macro is similar to the `paste!` crate [1], but this is a fresh > implementation to avoid vendoring large amount of code directly. Also, I > have augmented it to provide a way to specify span of the resulting > token, allowing precise control. > > For example, this code is broken because the variable is declared inside > the macro, so Rust macro hygiene rules prevents access from the outside: > > macro_rules! m { > ($id: ident) =3D> { > // The resulting token has hygiene of the macro. > paste!(let [<$id>] =3D 1;) > } > } > > m!(a); > let _ =3D a; > > In this versionn of `paste!` macro I added a `span` modifier to allow > this: > > macro_rules! m { > ($id: ident) =3D> { > // The resulting token has hygiene of `$id`. > paste!(let [<$id:span>] =3D 1;) > } > } > > m!(a); > let _ =3D a; > > Link: http://docs.rs/paste/ [1] > Signed-off-by: Gary Guo Applied to `rust-next` with the SPDX license identifier added as discussed and the typo fixed -- thanks everyone! Cheers, Miguel