From patchwork Fri Dec 2 16:14:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28981 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp948389wrr; Fri, 2 Dec 2022 08:19:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf4rCuAE9CywEF1PIampCP6NPhyys2EyXT6DsiGdMeSSh59s9hVL4JasQ262UKTAWSCoohhf X-Received: by 2002:aa7:dd0e:0:b0:46b:9dee:1e09 with SMTP id i14-20020aa7dd0e000000b0046b9dee1e09mr13166008edv.390.1669997984421; Fri, 02 Dec 2022 08:19:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997984; cv=none; d=google.com; s=arc-20160816; b=EXvSOeZ/78oZrutjMdgKZ1WjaxZWDQyQMPWAFEw4ZfEEoVgKBEPR6KvhOQWaneRSNN 17LOc1iJTXQGfMUl88Ch3kURK/4XDfwQg4Hk4/RZLPfTbhP+UXgqZZSJDBNATaMk2qiN VTEi5fhB2v8zH7/Tc8R9O0UHm+HXW+84WLhvzN1BccJLP2kkApZfbI1n9L9WoxiSonS9 RfpsuRsLMagD62zgRsJ0y2S2otvhOCqBREIdILRmm2qox6bdOLgvA7xyFSIv3OtNg0vr LlUoJXbJkWQ43TPwqSoaD5ta5zjEhsC2gbDBUuvIuhY9m/s2luW0fmkjoZAU3GPiHAZP Ldsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2r8yRciLz/zLOdY+CcyixgkvhpUVu0vf9nzwOLVNSWs=; b=lkl4IVrQHaLrBbCnFpQoC8S09q9hIWxEJ0BHKnTBKCz01jx1AHQ4SwuMbamfDVysyH 66fikLxbNmsN2bQr14l+QVMoR6gkpd2m4Z2th0o/n8menPSfrLlg/tDCqtfit58XpoZu wUJ9RSGsBhv5EMtLd+5XKjjgYyxwHukWzrEJwupmsKWOGan2QZ26ZzEvpfLo7Tw8h04E KDzYaEuGdxB8j55+LugJcpLS51j6QvGQK5Vvrvsp/wHNgXVlDbeYDyby+lKX4iKSZHhN lxMTeOpWwO6MNgty70G2Ra3fvxPu2PZNraE4B7WDRg3dL38AXxFalDzQfiiCW7HQCZjI GvCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q0pa2gxE; 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=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q14-20020a1709060e4e00b0078db89b2566si4830586eji.699.2022.12.02.08.19.20; Fri, 02 Dec 2022 08:19:44 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=Q0pa2gxE; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234033AbiLBQSL (ORCPT + 99 others); Fri, 2 Dec 2022 11:18:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234028AbiLBQRC (ORCPT ); Fri, 2 Dec 2022 11:17:02 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FD61E2546; Fri, 2 Dec 2022 08:16:38 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 32B19B821E8; Fri, 2 Dec 2022 16:16:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED068C433D6; Fri, 2 Dec 2022 16:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997796; bh=jbFFwN1XbpZoDSezKmpFYsV4hQRLNxhA6Wyw/KC6oWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q0pa2gxEprAPjTUOTcae3ubV+WpDQtNMpmX5UWyloHT7Ohb740+tEAt+1aFtikVvK GYfKMwPTEc7I1xtnY/TESBfUVk80EMTIm8F7+YUUwr0imfpqC2gX3k3VfWBCw93fiC Y3JxWSe1z5DXZCdONVynHKb6WST83Jy4KPqhsyeq+DSTXLtj7WP4ns70iABhLxeb7A 6mcyBYdLz5ewUXFsWiEmF8rCe3aFlXGvDAN+ahag8qxToPCAUwCeX1nB3OSazRadcg i0GXhFfGYFBAaAMS8lSOFLoqpYYFZIE3Xx6WM+N7zyForeoHiqqlu6wW2eORgLo3fl FtcjKdk8zm7zQ== From: ojeda@kernel.org To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 24/28] rust: static_assert: add `static_assert!` macro Date: Fri, 2 Dec 2022 17:14:55 +0100 Message-Id: <20221202161502.385525-25-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119806630440798?= X-GMAIL-MSGID: =?utf-8?q?1751119806630440798?= From: Miguel Ojeda Add the `static_assert!` macro, which is a compile-time assert, similar to the C11 `_Static_assert` and C++11 `static_assert` declarations [1,2]. Do so in a new module, called `static_assert`. For instance: static_assert!(42 > 24); static_assert!(core::mem::size_of::() == 1); const X: &[u8] = b"bar"; static_assert!(X[1] == b'a'); const fn f(x: i32) -> i32 { x + 2 } static_assert!(f(40) == 42); Link: https://en.cppreference.com/w/c/language/_Static_assert [1] Link: https://en.cppreference.com/w/cpp/language/static_assert [2] Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Signed-off-by: Miguel Ojeda --- rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ rust/kernel/static_assert.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 rust/kernel/static_assert.rs diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index d6371c9c8453..9b83ef736298 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -26,6 +26,7 @@ mod allocator; pub mod error; pub mod prelude; pub mod print; +mod static_assert; #[doc(hidden)] pub mod std_vendor; pub mod str; diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index 345fc9075d1f..178fe8e6cb6d 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -19,6 +19,8 @@ pub use macros::{module, vtable}; pub use super::{dbg, pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn}; +pub use super::static_assert; + pub use super::error::{code::*, Error, Result}; pub use super::{str::CStr, ThisModule}; diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs new file mode 100644 index 000000000000..3115ee0ba8e9 --- /dev/null +++ b/rust/kernel/static_assert.rs @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Static assert. + +/// Static assert (i.e. compile-time assert). +/// +/// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`]. +/// +/// The feature may be added to Rust in the future: see [RFC 2790]. +/// +/// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert +/// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert +/// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790 +/// +/// # Examples +/// +/// ``` +/// static_assert!(42 > 24); +/// static_assert!(core::mem::size_of::() == 1); +/// +/// const X: &[u8] = b"bar"; +/// static_assert!(X[1] == b'a'); +/// +/// const fn f(x: i32) -> i32 { +/// x + 2 +/// } +/// static_assert!(f(40) == 42); +/// ``` +#[macro_export] +macro_rules! static_assert { + ($condition:expr) => { + const _: () = core::assert!($condition); + }; +}