Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1667371ioo; Sun, 22 May 2022 23:40:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxR00aHv/3kataR9cnpVYeucG2z/TsckFu0ueXoDyow27cxJyutasfeJclibytN4Jmf6mTf X-Received: by 2002:a63:844a:0:b0:3f9:f00e:a4b0 with SMTP id k71-20020a63844a000000b003f9f00ea4b0mr8544859pgd.335.1653288029415; Sun, 22 May 2022 23:40:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653288029; cv=none; d=google.com; s=arc-20160816; b=Yc6Ey0SWAM/hhgTvrns7LM02MoMUNS/clCuOc2MNnj+65OEfllvYn7iPmEz3wPghhY 4rV31TCpNtvTeNAMVisYMNXXF5i9XAYpa+KLagkhwaFS+znbCABnFsemTDfkOv4AsJk5 gm4nSSP0Zm5zBIDeLGuojJ+3QlgdeMv9cXQKSZTpI6qDyNFpe+9DowkXQSNRdbTMhaZk 0W7fSRUBI9oPvoZbYpACmcXOqF4NV8nVMu3esDzlpYa/+fFw+PTEOqCLUVS8F9E6vLAB oXVjNk1Jhn8xlKpvAHpmmCzwrQK3J4KOvBuxqwPzREfFSW626+ZlKVpQROPQBeknA2E5 oYfg== 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=dXolbcTDJGESHCVjqbMlNc+XxxneXj9MhpehUJ9OFT8=; b=b1h4D95JaSgljoKaJx20J22a4WFEtp6IYEJesIGUp8VvPSj43ByU3MARzp35Fg9xzt 0FAl6SBSQbM4iEKNHvE0Qwig77VyBEzWCFts8F8eG99LNSK2iu3ERHiEhC8Pty80JRWM Psarrtm8tqMHb1xqwX5FY9r14ed6lfpbk503nFIlSSVBQ9yekuCBvFG8tTiZZiZ7V2Ml Jzdb7hkH+dp6CAyLOyjvSWAOEhhTn2TLMtnL4B2T4+sSjw06ebyqH7B/CZmeDZOK14hB zc9TZ9C3ylHdsSNuckdcAudzmLlcT0wXQam0LGOGKnuZ6/Um2QVzYITgIk93me5wkKUo eu5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YWzPuA+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bd7-20020a056a00278700b0050ddce7e81bsi11201357pfb.374.2022.05.22.23.40.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:40:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YWzPuA+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9AB02632A; Sun, 22 May 2022 23:13:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348437AbiEWCEO (ORCPT + 99 others); Sun, 22 May 2022 22:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352311AbiEWCDv (ORCPT ); Sun, 22 May 2022 22:03:51 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3DE43982E; Sun, 22 May 2022 19:03:31 -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 3A72E60FD7; Mon, 23 May 2022 02:03:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E455C34113; Mon, 23 May 2022 02:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653271410; bh=vbNh5V+If7BMOjgvnKPpE36qR1+NfFcgaQxIO6y+Mq4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YWzPuA+9psavWwLq35M/pqJIXrSIU2Fgq/Gv4WznPVO2b+fZ07cdFAaBhN/0q4BB3 5TJKTq3eF9zw3bytBpdU1u4jGXXq3y1hjXq3/ppNykMkU1GKw5DmSYdD9zTUJflX5y hhGIbIeVBzCA+CvVRI0bdAHiMX66PHpNZbanitGGKp/BKIqDwtFTy94LfCNTwOZ4vY p5nXM7c85FzEJ6lLryTj5nXbicewYGquvuHGml1LN9+ojemV8jjXu99x4+3g5+45sE yRAzNEULjwggRK/V3CdXLoFOFQp8KoWysT/ZSdplp6SqUJNKR3jY/iXXCn7OJXju5q B7c/JA9qs294g== 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 v7 09/25] rust: add `build_error` crate Date: Mon, 23 May 2022 04:01:22 +0200 Message-Id: <20220523020209.11810-10-ojeda@kernel.org> In-Reply-To: <20220523020209.11810-1-ojeda@kernel.org> References: <20220523020209.11810-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.36.1