Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp5526568pxb; Mon, 14 Feb 2022 00:50:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzhEpY5KCA87qjj2AM8E+HIJ9N4U/SqSkB8zfZQGuZw2z0QRHE56wzhcDOPZppu/HG8AB84 X-Received: by 2002:a05:6402:885:: with SMTP id e5mr5850434edy.61.1644828630302; Mon, 14 Feb 2022 00:50:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644828630; cv=none; d=google.com; s=arc-20160816; b=CX4QsGVOxSgpnMY6AEHZU3VsXOYmSEDwfCylCz592ZLvGqir9eELeQKDoIpy9HFg8n Rinq3yMc7fI5LwUMQIkIb13qeTb5WASo0v7w2S54W9HZzvvd9PiV1S9wtOd71+jRcm6T PyNe8Ljaxe0b/JVyOyh8zGjxVYWiCTBC15TrQPMWZLHyG3mxuKsZ5rPqdVyqE+O4e2mn NGAOCttjiMKEMty0Eg6sb5Ei4ob/0XvMADTFKB0htGVTYhjLhzm72bqYjlm12/+OtmS8 ht8VqGdtXZOOI2uc4LkLUTgZTw9AcgditQSYJSHjQKHef8Z7wGY91fn/HmFq8BWjCzO4 Ktyw== 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=jWpmc5uhcXZbYtzoIrCKXDjjh1/yfoaAxut8HUt5AkSe0xJXOfjyB+YX0Vrh6OE57g ZY8h6XFL1mb17RC2GmbYMwnhWbkfr5xKznAvLFQgbiXdWpYc19ZAIxO1b4fT7MVKJAh8 Arduuq9sRyA/DaVKtXJhnC19AhUHZGIWMx/XgWDR7Xk1xoLqmglM7s3BviluWQs23Iy2 QlB+5yin1hIHiTj5iNsLNjiWt9ka6d2RJuVeQ2gnbX01XhMijzxH/ZQ69mSpX+jzjl7w sBhjxqx1acTLfQQM5cpOZ9jp+9bX0kCEplIh6NhwwL+/wkeFEu1eOeoohL5d9ac59MTH za5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ula3eARD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q27si19477624edw.658.2022.02.14.00.50.07; Mon, 14 Feb 2022 00:50:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ula3eARD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S235492AbiBLNFu (ORCPT + 99 others); Sat, 12 Feb 2022 08:05:50 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:55852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235590AbiBLNFn (ORCPT ); Sat, 12 Feb 2022 08:05:43 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62B342A27E; Sat, 12 Feb 2022 05:05:33 -0800 (PST) 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 DE177B8068E; Sat, 12 Feb 2022 13:05:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8C7DC340EE; Sat, 12 Feb 2022 13:05:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1644671130; bh=ApspYmKimTsDaiKmY2HBxsKKPe+ifO/dur0fE3PhxaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ula3eARDEsbD+LDEmd083knSVBcJGS4ripnfE1TuScBA5NfGpCmyPModVGIlovdc0 5c/1ILdwPkdWM46u1ztSwn4aCPiU9wAJj9tGGDfuxNBhREyDJcDKit4TxJyi5OvYLe u5/1kCqn/haubaco3WBDbKSQ6CCbrMQYbvZfCZYtvfptA3RaikYHqMOTzVDTFdZAPH geXE/0a/2XGqkpRyHn72TkiaY4kPQEUR65nWFRqDOCuuXqx0PRGM9k8F3d7sYDBTeJ oDzRvWvUreoKslvlVZHayo5WQ7cwOnr6tPSKuRNzFnFnSsVF/fvckEo18UMjg9xFP6 ST5fMiD7NcC/A== 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 v4 07/20] rust: add `build_error` crate Date: Sat, 12 Feb 2022 14:03:33 +0100 Message-Id: <20220212130410.6901-8-ojeda@kernel.org> In-Reply-To: <20220212130410.6901-1-ojeda@kernel.org> References: <20220212130410.6901-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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