Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp944317rwb; Thu, 10 Nov 2022 09:11:27 -0800 (PST) X-Google-Smtp-Source: AMsMyM65jwu+aD8VG8sGXN/kJPYTyNTaOo7qoSKenWzwFX6AYjQDl6HpmUXl2kiG3+Y+vV6imx7t X-Received: by 2002:a62:4e47:0:b0:56d:6e51:60ee with SMTP id c68-20020a624e47000000b0056d6e5160eemr2943620pfb.25.1668100287158; Thu, 10 Nov 2022 09:11:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668100287; cv=none; d=google.com; s=arc-20160816; b=YS/nYwufSul5LT0vikEp2pwA1KRBxX1HSBOnFrXzFpGnRhbLEHMFo1x4qpQkLfBD7/ cQJ4I0AvKgqAXjMURhiPXIM74MEojrkSeeA0aLrJ1QIZqxAe/055m0c0EFkD7TZvMUdy bDByutHwnuY5g2/yXK3PxjtCXGYggWWIWx6/UibuTcv9l0PrUoBE6nst+nwxuEp+Ova3 TgAkIIVqndRVEJ+me5DrkllEsdItH3FGLozZUimzPGjyU0bVF2jEW2QAwSWW30jmdgKA nLxNDM7zQJXQ/iTqYZV41ZjmKjacgeHwM0OzPevmw55qJ/1GOagGRXZc4hvjkJNy4jEU K37g== 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=cpMPROnt17I0XhcytYVQ8mrRPasrp+0HmNjZCbV7+H8=; b=hg4PId90O8nE1iM+bbKfPhc0cWwedOosDsPNpvlA/c1KSXUr/EJ3SON75oH2hVuvRS jchYFauqa6ykANswytU+HZsOx/Ie9mAO/lV+MTdMxGG5Ck1iHVmT5nWFNLDAFYaTBfIf y5HQUC59UrM29dIWtJ4t3Qu3+FN+QMlxirMv3O+jw3ng3an+EWq/fkHrxQp7xH/hQYYW A/I2KZ29rNyTBTv2aEMr3Qa7jkjiJ0GhPpAiEwcQUkrePbee/CHzFDbZm0+S0RaekAU/ 0CRZLBSDwccVtkfm00OfbTSqNmjxVxQQUN8yj5Pt/ivHknyQcDBITRAEJ097TQ/3buyV jwwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cMjyLtNa; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j13-20020a056a00130d00b0056dea1fd0f9si22028350pfu.318.2022.11.10.09.11.09; Thu, 10 Nov 2022 09:11:27 -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=cMjyLtNa; 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 S232546AbiKJQpM (ORCPT + 92 others); Thu, 10 Nov 2022 11:45:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232140AbiKJQoh (ORCPT ); Thu, 10 Nov 2022 11:44:37 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9718CFC; Thu, 10 Nov 2022 08:43:31 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 0004A61BD5; Thu, 10 Nov 2022 16:43:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B550C433B5; Thu, 10 Nov 2022 16:43:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098610; bh=uX6/pkir0Xey7grEQ8c5/5PlojfkXX4KXlDb5cWi7gY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cMjyLtNaHUBK7dWmm62B3GEJtoez4W1kGeKCONcxIW4KI1Fr5ul0oNhPPT6QyLkMt /N9DSfUgnyLEUE9TVJhm3z9qxsqOsIV+V0lETiUplg/oOLpOPCNy5cnnOHUcL8PLda Glu2x4PrtYoVx5/I7L/0gZMPnzUA9oxVQngc0MRZYRYj8MaSDc6arELfBB64goFDYn KLFOd38XtLy1qaNTLx7SgwHd2usIS+uq8A/t/3yN/3DF1AKPSAnBdnJ6O5/FU8ez9m dAT++p5ProfJfhhISfzrkrr+B7im6pUHOogZnyxVKX+j8z/I+f2ApU9flmrb2/HPnW A1zfDRLtKAJPg== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v1 24/28] rust: static_assert: add `static_assert!` macro Date: Thu, 10 Nov 2022 17:41:36 +0100 Message-Id: <20221110164152.26136-25-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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); + }; +} -- 2.38.1