Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1638482pxp; Thu, 17 Mar 2022 13:18:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZyA5hRlbx8hHBQQ7ddGSUkQ12+7AISKGhDA8VL0MdGwEzrQnql7Sdx5QXKHZUETEw+Cxw X-Received: by 2002:a17:902:6b8b:b0:14d:66c4:f704 with SMTP id p11-20020a1709026b8b00b0014d66c4f704mr6873641plk.53.1647548339493; Thu, 17 Mar 2022 13:18:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647548339; cv=none; d=google.com; s=arc-20160816; b=ATPYQ+VVjF83hT+yhAM0aAaijSmG7j8Oe5LK2c/BMIQ2okmx9HRkmtwr+ninEaiYvo 1SY++xbvwypkCIVh3JH64dUnba1Fg9DsnSwi/LRmAyStipaBBYrtVa10lxXUgCO0T04s e4t5JNuGVTh9YIpiQ0X2r2UszPO+9N9gsvi2NCzf7hU61Bb52NI7iD+P9G/4Wvc6Kcn/ LihGZgZrXjVIv6wVcgvbBq50mSTET//YnZ7Q5Ymyo9r2ffKazWALG+yaFxPVn+5bZ2q9 7ZLEaBgBgGLOukpNjA9m51LB5r6qEKINuiRJK15B5Ov/aUE6Vk9E+0e1y3G/acdSNieU OL1w== 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=2i9lI+bhIb1qXX1XWU2CfzEAHeZ3Rg3vrC4fPPfMfAI=; b=gXcfxwtYCZ9FGmq38+3nfKVBNJdXnRHBOv6gTvodHPdrnxeywx28Ej7C9L7Oc4qtz6 I7zh2ENj4oHHCDwFn6+xYVUTw0OKTI+o34aSKg1dksVIorAvZjc2lG87x1iz2TMklnqu kHOUmEnNAYQTRGtAnyB/W6bNl1aFSEc269VofilAg+4JwnB4+bxzP3ySstBl6m6RWBDZ JZDFwYnC+ZkB6MqwvLMzpshdGbUhcbuFEVyf7nH/4tZXsY/2Syw50tJ38MFRs5xGqK5L cdestKaMSLd04eSVwfdOfw3EwM8JzGWnvLsPlGfy/qgmVGbQF2YW7Yt92IMko+dYPAcB SyAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JwxwYQCp; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d22-20020a170902b71600b00153b2d16465si56142pls.109.2022.03.17.13.18.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 13:18:59 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JwxwYQCp; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AFBB915B9AB; Thu, 17 Mar 2022 13:00:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237299AbiCQSNU (ORCPT + 99 others); Thu, 17 Mar 2022 14:13:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237325AbiCQSM6 (ORCPT ); Thu, 17 Mar 2022 14:12:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 167B121DF1F; Thu, 17 Mar 2022 11:11:42 -0700 (PDT) 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 B0EC961663; Thu, 17 Mar 2022 18:11:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DA00C340EE; Thu, 17 Mar 2022 18:11:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1647540701; bh=ApspYmKimTsDaiKmY2HBxsKKPe+ifO/dur0fE3PhxaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JwxwYQCpujUbn3iSxMbqq1IT3VjQDchj8PsKL7f1Gda6BxP1wQjgohMpTeASsXvZu DT/OcpPIbFK7PfWLSMcDcA4h3tw/iQBXq6/bBufdhjnML8Pu6b2W2a7if65KUEA2Qc d+tXlywh3msUCFgD0d+xcKNUzQleDukjCgZMgJXDrm5mmmMoQZp5zIQ3RtA4mwZ5u/ V+2ysz9RHvVQdIEbZ8xpcg1gbib2QR7Xp35wEP6fNyM3sxyfND9bvO95AOELWDPN3h oMekcEVoFIwPBWlDbNS5+kTq7tT6A7qIXEesWzUZz15aKWk5ZdJCrIGL6tpQbP+okF 4VtV4e0NPS8wA== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Gary Guo , Alex Gaynor , Wedson Almeida Filho Subject: [PATCH v5 07/20] rust: add `build_error` crate Date: Thu, 17 Mar 2022 19:09:55 +0100 Message-Id: <20220317181032.15436-8-ojeda@kernel.org> In-Reply-To: <20220317181032.15436-1-ojeda@kernel.org> References: <20220317181032.15436-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 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.35.1