Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2763188pxb; Mon, 17 Jan 2022 05:22:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSgOjfrzqoFpUFzwNhzPztUU3/IDkBtYb00Lk5WngI6LYqRmWFV0fHUnA3Po0zQLYdH3jg X-Received: by 2002:aa7:985d:0:b0:4bd:8a52:63f0 with SMTP id n29-20020aa7985d000000b004bd8a5263f0mr21064101pfq.56.1642425747291; Mon, 17 Jan 2022 05:22:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642425747; cv=none; d=google.com; s=arc-20160816; b=IRAt6f9QK5KoTiNtRQkc4xP+N7fa5I7r0eM2hQKxh+dAjb4bOm8gbtp5zsKUoq1dRP asAVLAht9kiTNut2nxnwKWtgVh1pX9Pqd9CUjOzpBK4xt9uWptv/iftD4zx2Q/7URSN6 1JUOV3ejNNUEcZ/98Q7ocg+SVwKSHRCr/nv1HjQrnhsWNmZ/OG4wYLFb3Dgq+Gn1xeoQ M9J6AnwvW5qgy+4x8i7RD4qjYh8qxnNrTxzOtI5TtRc8CjFWZRByCkdIXC4N+eqa5YH6 7Q57cAaaOK8EME6YZiWwJNsQABzDFsXE4wB1Xt1JCOMGhSGifQpzBWIgXs0BRuMcnulY 1L/Q== 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=fbk5Fva2Alu83KfQRmgxjLVVAF9pyweLw6h022az5eY=; b=BGD+5JMDt8xl527xZ5yUUvXXe94RtD1fHtaqV3xEI7zbKIL6cr/U6K1hn9XYOPGehd 29xNPx56Bpx34RTLQyJwIqZBP5vi2BNFl7xCQhEPVkxm+l55NeeNHSPPCTTcR5tFFDzu qbXg88jlQl4fYxNNXIEaMTRExug1DjWarjk1JIevL4j0C2dMK7lumlqa/WYCSIXQZhOv r1AzI3Ht3wkEWZ72oMJRlbn+oxLkGMrEpHlOycz08z+zL6mA7C6KuPdeZno7ZKJ4l0Is yZsdVQcZ9Lp2o69LS5GIY/wilncSeaeU17Xe9x3iDU0gj9h18gsDvLltc2kbLpfJ5+Re JPAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Om3bJIbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ot1si20838393pjb.32.2022.01.17.05.21.58; Mon, 17 Jan 2022 05:22:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Om3bJIbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S232439AbiAQFel (ORCPT + 99 others); Mon, 17 Jan 2022 00:34:41 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:47192 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232142AbiAQFe3 (ORCPT ); Mon, 17 Jan 2022 00:34:29 -0500 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 68E30B80DCF; Mon, 17 Jan 2022 05:34:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26EE9C36AEF; Mon, 17 Jan 2022 05:34:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642397666; bh=q4T/HWnrX6MkEzrYkY7huTX+IcB2Gc3XmNqnnQXC/gg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Om3bJIbmdNRZs8qjTT5TswBGLL5mkxGSGm0fAQSiR5aec7qi9q7IcpHkoJzU9yte+ +IuCyUCTduDfCTpB4hy3Tb7it42V+vUHwapvNZ8aWMR4W0iYULH7P7IgbF/0EzNVhx OsYz14KHAtOKKT/MeUymGMRc52jU9Tc4XOMRubm8c++7Z+10RfO21IZvGCxJvlpl8W luhs47wLhCLCf5RcBBxty+fRDwHmWb8tF5mLEGoXx5L9XI3TiGMh5DYEaMhVr46U4M 6hSqZPD0hA1VUe5l/5OxQ6k0+x85+ECq8EOYYxMqP1uafRbtc1F8TYI/tnlDNBHC61 74vbbURyPs2hg== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Gary Guo , Alex Gaynor , Wedson Almeida Filho Subject: [PATCH v3 07/19] rust: add `build_error` crate Date: Mon, 17 Jan 2022 06:33:37 +0100 Message-Id: <20220117053349.6804-8-ojeda@kernel.org> In-Reply-To: <20220117053349.6804-1-ojeda@kernel.org> References: <20220117053349.6804-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gary Guo The `build_error` crate provides the `build_error` function which is then used to provide the `build_error!` and the `build_assert!` macros. `build_assert!` is intended to be used when `static_assert!` cannot be used, e.g. when the condition refers to generic parameters or parameters of an inline function. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Gary Guo Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda --- rust/build_error.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 rust/build_error.rs diff --git a/rust/build_error.rs b/rust/build_error.rs new file mode 100644 index 000000000000..34e589149d3e --- /dev/null +++ b/rust/build_error.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Build-time error. +//! +//! This crate provides a function `build_error`, which will panic in +//! compile-time if executed in const context, and will cause a build error +//! if not executed at compile time and the optimizer does not optimise away the +//! call. +//! +//! It is used by `build_assert!` in the kernel crate, allowing checking of +//! conditions that could be checked statically, but could not be enforced in +//! Rust yet (e.g. perform some checks in const functions, but those +//! functions could still be called in the runtime). + +#![no_std] + +/// Panics if executed in const context, or triggers a build error if not. +#[inline(never)] +#[cold] +#[no_mangle] +#[track_caller] +pub const fn build_error(msg: &'static str) -> ! { + panic!("{}", msg); +} + +#[cfg(CONFIG_RUST_BUILD_ASSERT_WARN)] +#[link_section = ".gnu.warning.build_error"] +#[used] +static BUILD_ERROR_WARNING: [u8; 45] = *b"call to build_error present after compilation"; -- 2.34.1