Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3278216pxv; Sun, 4 Jul 2021 13:30:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRsniIoXXs7H4K9yD+DIbCZTHd4EHaI6uRKQAOKncSerR8GglHeYglbvL20+CDCjVEoH26 X-Received: by 2002:a5e:a80a:: with SMTP id c10mr9048072ioa.110.1625430606732; Sun, 04 Jul 2021 13:30:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625430606; cv=none; d=google.com; s=arc-20160816; b=BvUNo1HjW0iXK9JsFOSPn27dTDaKaCLwCbU0U4PwpqqzvhkyPO9oAEvyqXeETTa35g PIC3OOwKVG4XjFCxFALy3+si7EyXlUz2hKsbQQW1dMruse91v9Tx+9No9UKs31zj/uYW 3PJ0RUPY4cr2491rAsMlpqgsfzM0Acyyvni6aAkV4Yo9W8yZStEujOJkyNoMXvWiyXOu a5NNqllgSx6edaSVfFcqxPZ44tS9Ki5vE1dYykZmoyzDhBghV/sNES9OHjQmV4baRX2W S9XFYL4r5xNrMgsf/qAJEwFviRFZTL9dU/KkT62w4iUWqANfUD316gZz0VzubS166zIe 3O3Q== 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=IXwdVEpZKKMhbEQS/cwmP+8p0Xkzca9rRxL31MhmDvA=; b=YidYTX8EFLKCy6aFREAUaFFRIUPeFs/rxBrv3g96YSrEkhSxvJ41sexCbgAo94XHxd UxN84hzZFRfIE7i3RIMJ/983JXKYY4+2zFTEnQFOVbaO8qLnsO98CpYQHKlvvZRDaD8q LQP6MDscadow0EyUeTwi1fOpY3EOJv119TvUzFAh7AyZPbEq09r173uP4ZSUSV3tWkIn nWHtv0dBDRys22zVqxti2zL5fGMoUpl268FKH5w+/DzpVvw991by/EdVrp3feXuxrn6J FK42A7Y48+O0fJ1R8P8A8jaaEEBjwF/eTWOj/fJaSWiscI1oWwb2jBHmPaX7T4HqSX3+ NX4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WRVLu3zf; 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 w3si4890489jap.29.2021.07.04.13.29.55; Sun, 04 Jul 2021 13:30:06 -0700 (PDT) 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=WRVLu3zf; 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 S229788AbhGDUbq (ORCPT + 99 others); Sun, 4 Jul 2021 16:31:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:37182 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230101AbhGDUbp (ORCPT ); Sun, 4 Jul 2021 16:31:45 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8E248613F1; Sun, 4 Jul 2021 20:29:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625430549; bh=Xw+YpR7+ODy7bTkSIEvVXZYPVVcCYE52n6l0fRjuwvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WRVLu3zfWVLBgG6vP5LqmmFgCn+ZErWJeo7M9r20iJk4uT4IdUaftmowuJy3xAXqg a21MbJ3zXD58OywxBip5oGWUFzI01C3GIl0FuY1watCL3ckD+ouJVJ+7MVo7UWC4pF Deq1wDfHekE0OR+dqmYyVDXuMEECAQEd9zsJhsSigJbxfcvzK89hEc0MbHWEMcok/U YN4KxFtIXlaHlmICKBi76QRHBWykEk9hZARn+aeoO88QzzEiGyom+qjOErhrKK3P7I 4VvHfuZ95sVFtEl3BHIufPwGygr6UZih8P2qrDnJAlwRBqc4ZBKoh8oYW2w4+flJ43 iqjZPfbOkVoTw== From: ojeda@kernel.org 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 , Alex Gaynor , Geoffrey Thomas , Finn Behrens , Adam Bratschi-Kaye , Wedson Almeida Filho , Boqun Feng , Sumera Priyadarsini , Michael Ellerman , Sven Van Asbroeck , Gary Guo , Boris-Chengbiao Zhou , Fox Chen , Ayaan Zaidi , Douglas Su , Yuki Okushi Subject: [PATCH 08/17] rust: add `build_error` crate Date: Sun, 4 Jul 2021 22:27:47 +0200 Message-Id: <20210704202756.29107-9-ojeda@kernel.org> In-Reply-To: <20210704202756.29107-1-ojeda@kernel.org> References: <20210704202756.29107-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: Miguel Ojeda 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: Geoffrey Thomas Signed-off-by: Geoffrey Thomas Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Sumera Priyadarsini Signed-off-by: Sumera Priyadarsini Co-developed-by: Michael Ellerman Signed-off-by: Michael Ellerman Co-developed-by: Sven Van Asbroeck Signed-off-by: Sven Van Asbroeck Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boris-Chengbiao Zhou Signed-off-by: Boris-Chengbiao Zhou Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Ayaan Zaidi Signed-off-by: Ayaan Zaidi Co-developed-by: Douglas Su Signed-off-by: Douglas Su Co-developed-by: Yuki Okushi Signed-off-by: Yuki Okushi Signed-off-by: Miguel Ojeda --- rust/build_error.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 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 00000000000..d47fa8393cb --- /dev/null +++ b/rust/build_error.rs @@ -0,0 +1,33 @@ +// 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] +#![feature(const_panic, core_panic)] + +/// 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) -> ! { + // Could also be `panic!(msg)` to avoid using unstable feature `core_panic`, + // but it is not allowed in Rust 2021, while `panic!("{}", msg)` could not + // yet be used in const context. + core::panicking::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.32.0