Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4659992iob; Sun, 8 May 2022 20:59:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyX7zJX70Z50hMDuajgADOSd+LQo4k0ooBjn5CIuiHfHoY+GjsDvRL714MXe5ho0d+34o1I X-Received: by 2002:a63:78ca:0:b0:398:ae5:6515 with SMTP id t193-20020a6378ca000000b003980ae56515mr11914314pgc.345.1652068752256; Sun, 08 May 2022 20:59:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652068752; cv=none; d=google.com; s=arc-20160816; b=fxJpAG1EgX7ecNXQIKTAq3WDBRr2oNSzon7VKEut6CEssMP2CsQ043jwY2ycuQOifL 8S9dH6hC0TZ/GqoJNzQ8xzi7Or1dFi5QLGv3fAbsNKbN2J1gxed8S0qyh8SOTYDWYSCA R+4IoN1oDjHotj0LMGsF7usabk79kJr0Lcsk1poVes/QXn/uk4YFr2BlRLVeyGW4lqX2 2kwqw1BC0mhYnIsIbUO5kwFmUHljhMi0gTi6tO4qO5YdIY0gzrlsyTI8MOJxIP7VYUl6 ttloMrAMyct6Yzc/8I7f44Y8p4hQhhn8jdqoq9ZHQW1WG5BKv+q9N54/1BCBbvc/c1Kj 5byA== 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=86gz7RjdC6b27eLGT/LHBxmT0xfQikRmiC0jGt1TIFI=; b=qQsYweU9nB6TRQvN0PH1tB6TFXOhL3dUVpQmtImblPi60FUskYHzNsB8grFaSn7LG/ JpWgiMd6e/Hzt/vmyxzU8QxducqjVah27XISRNbN+N3XA+ub3RoVsiDtGAepPhwFPRWt h7zEgSDi5j66c739oNpDctUHR1i47BbLmP7YlL0r5JZsdIBk7VO4MHYSsfVqNz/zz22d DXx3arr8z4G//f6DnsyiHaIxNHSDOE2b+vWNfd+cL97XohnXDa5nmJBhJL45DyyS/sMU 7vjVeIn8rHiTgtpxpwnV1ku/+OeqkGz+qRVWqud3e1PoRnhGG1uJTWPUrnstSDSl2HrN UxDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=szB9nM9B; 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 na16-20020a17090b4c1000b001cb8365e641si14909116pjb.52.2022.05.08.20.59.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 20:59:12 -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=szB9nM9B; 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 6A8D510637F; Sun, 8 May 2022 20:57:55 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1445625AbiEGFbg (ORCPT + 99 others); Sat, 7 May 2022 01:31:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445629AbiEGFa2 (ORCPT ); Sat, 7 May 2022 01:30:28 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD5226D4E2; Fri, 6 May 2022 22:26:32 -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 ams.source.kernel.org (Postfix) with ESMTPS id 86596B819F9; Sat, 7 May 2022 05:26:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D0E4C385AE; Sat, 7 May 2022 05:26:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651901190; bh=ZP4+oz4+9HeN/nOKgAqEjc07ZfqXpMkkeI6k5cbKVJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=szB9nM9B6RyvNf+yDoqXr4LUZdhZN8t9ERVzZkhvfLyl325UrmFuYZHKs43T5og8y wDbSWkgX408MRE6P/mvbEituswtzvA1RMlBTS8OPb9P5uhHWpsZPeE/N6ZQaly8oR9 UfyOVQXwM2Ar7xGrxvKliQEV4TODRxW9y8WCROfrF0ywtrVoB4PgcBKwtcsq0MGt0G 3/8/7OVLxKtIDttZArI4j9lFVPALaWVqsKJBVIjALGzCkO/Cw2c0HlYsQpxOr8x9Sr 9wbaZ0d+OCE7cwUpOYBDW1AFr17ym+AWQcjLh5yqVgwSz0jm/cKARLvX+ZCFLaHi8v TaJz/E9M2oebA== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Jarkko Sakkinen , Miguel Ojeda , Gary Guo , Alex Gaynor , Wedson Almeida Filho Subject: [PATCH v6 09/23] rust: add `build_error` crate Date: Sat, 7 May 2022 07:24:07 +0200 Message-Id: <20220507052451.12890-10-ojeda@kernel.org> In-Reply-To: <20220507052451.12890-1-ojeda@kernel.org> References: <20220507052451.12890-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 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.3