From: Pierre-Emmanuel Patry <[email protected]>
gcc/testsuite/ChangeLog:
* rust/minicore/build_error.rs: New test.
* rust/minicore/build_error_core/lib.rs: New test.
* rust/minicore/compiler_builtins.rs: New test.
* rust/minicore/compiler_builtins_core/lib.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/46cd680e0d252a4169c5d87c6d2156d8d4dd578a
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5105
gcc/testsuite/rust/minicore/build_error.rs | 31 ++++
.../rust/minicore/build_error_core/lib.rs | 140 ++++++++++++++++++
.../rust/minicore/compiler_builtins.rs | 108 ++++++++++++++
.../minicore/compiler_builtins_core/lib.rs | 140 ++++++++++++++++++
4 files changed, 419 insertions(+)
create mode 100644 gcc/testsuite/rust/minicore/build_error.rs
create mode 100644 gcc/testsuite/rust/minicore/build_error_core/lib.rs
create mode 100644 gcc/testsuite/rust/minicore/compiler_builtins.rs
create mode 100644 gcc/testsuite/rust/minicore/compiler_builtins_core/lib.rs
diff --git a/gcc/testsuite/rust/minicore/build_error.rs
b/gcc/testsuite/rust/minicore/build_error.rs
new file mode 100644
index 000000000..fa24eeef9
--- /dev/null
+++ b/gcc/testsuite/rust/minicore/build_error.rs
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Build-time error.
+//!
+//! This crate provides a [const function][const-functions] `build_error`,
which will panic in
+//! compile-time if executed in [const context][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][const-functions],
but those
+//! functions could still be called in the runtime).
+//!
+//! For details on constant evaluation in Rust, please see the
[Reference][const-eval].
+//!
+//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
+//! [const-functions]:
https://doc.rust-lang.org/reference/const_eval.html#const-functions
+//! [const-context]:
https://doc.rust-lang.org/reference/const_eval.html#const-context
+
+#![no_std]
+
+/// Panics if executed in [const context][const-context], or triggers a build
error if not.
+///
+/// [const-context]:
https://doc.rust-lang.org/reference/const_eval.html#const-context
+#[inline(never)]
+#[cold]
+#[export_name = "rust_build_error"]
+#[track_caller]
+pub const fn build_error(msg: &'static str) -> ! {
+ panic!("{}", msg);
+}
diff --git a/gcc/testsuite/rust/minicore/build_error_core/lib.rs
b/gcc/testsuite/rust/minicore/build_error_core/lib.rs
new file mode 100644
index 000000000..750cf241d
--- /dev/null
+++ b/gcc/testsuite/rust/minicore/build_error_core/lib.rs
@@ -0,0 +1,140 @@
+#![feature(no_core)]
+#![feature(lang_items)]
+#![feature(fundamental)]
+#![feature(rustc_attrs)]
+#![feature(arbitrary_self_types)]
+#![feature(optin_builtin_traits)]
+#![feature(prelude_import)]
+#![feature(intrinsics)]
+#![feature(staged_api)]
+#![no_core]
+
+pub use clone::Clone;
+pub use marker::{Copy, Sized};
+
+#[macro_export]
+macro_rules! panic {
+ ($($arg:tt),*) => {
+ $(
+ let _ = $arg;
+ )*
+ $crate::panic_handler()
+ };
+}
+
+#[macro_export]
+#[rustc_builtin_macro]
+macro_rules! assert {
+ ($cond:expr $(,)?) => {{ /* compiler built-in */ }};
+ ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
+}
+
+pub mod ffi {
+ #[repr(u8)]
+ pub enum c_void {
+ __variant1,
+ }
+}
+
+#[rustc_builtin_macro]
+#[macro_export]
+macro_rules! concat {
+ ($($e:expr),* $(,)?) => {{ /* compiler built-in */ }};
+}
+
+#[rustc_builtin_macro]
+#[macro_export]
+macro_rules! stringify {
+ ($($t:tt)*) => {
+ /* compiler built-in */
+ };
+}
+
+pub const fn panic_handler() {}
+
+pub mod mem {
+
+ use crate::intrinsics;
+
+ #[inline(always)]
+ #[rustc_promotable]
+ pub const fn size_of<T>() -> usize {
+ intrinsics::size_of::<T>()
+ }
+}
+
+pub mod intrinsics {
+
+ extern "rust-intrinsic" {
+ #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
+ pub fn size_of<T>() -> usize;
+ }
+}
+
+pub mod marker {
+ use crate::Clone;
+ #[lang = "sized"]
+ #[fundamental] // for Default, for example, which requires that `[T]:
!Default` be evaluatable
+ #[rustc_specialization_trait]
+ pub trait Sized {
+ // Empty.
+ }
+
+ #[lang = "copy"]
+ #[rustc_unsafe_specialization_marker]
+ pub trait Copy: Clone {
+ // Empty.
+ }
+}
+
+pub mod clone {
+ use crate::Sized;
+ #[lang = "clone"]
+ pub trait Clone: Sized {
+ #[must_use = "cloning is often expensive and is not expected to have
side effects"]
+ fn clone(&self) -> Self;
+
+ /// Performs copy-assignment from `source`.
+ ///
+ /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in
functionality,
+ /// but can be overridden to reuse the resources of `a` to avoid
unnecessary
+ /// allocations.
+ #[inline]
+ fn clone_from(&mut self, source: &Self) {
+ *self = source.clone()
+ }
+ }
+}
+
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+pub unsafe auto trait Send {
+ // empty.
+}
+
+#[lang = "structural_peq"]
+pub trait StructuralPartialEq {
+ // Empty.
+}
+
+#[lang = "structural_teq"]
+pub trait StructuralEq {
+ // Empty.
+}
+
+#[lang = "receiver"]
+#[doc(hidden)]
+pub trait Receiver {
+ // Empty.
+}
+
+#[prelude_import]
+mod prelude {
+ pub use crate::assert;
+ pub use crate::clone::Clone;
+ pub use crate::concat;
+ pub use crate::marker::{Copy, Sized};
+ pub use crate::panic;
+ pub use crate::stringify;
+}
diff --git a/gcc/testsuite/rust/minicore/compiler_builtins.rs
b/gcc/testsuite/rust/minicore/compiler_builtins.rs
new file mode 100644
index 000000000..0d49cf3f4
--- /dev/null
+++ b/gcc/testsuite/rust/minicore/compiler_builtins.rs
@@ -0,0 +1,108 @@
+// { dg-additional-options "-frust-compat-version=1.54" }
+// SPDX-License-Identifier: GPL-2.0
+
+//! Our own `compiler_builtins`.
+//!
+//! Rust provides [`compiler_builtins`] as a port of LLVM's [`compiler-rt`].
+//! Since we do not need the vast majority of them, we avoid the dependency
+//! by providing this file.
+//!
+//! At the moment, some builtins are required that should not be. For instance,
+//! [`core`] has 128-bit integers functionality which we should not be
compiling
+//! in. We will work with upstream [`core`] to provide feature flags to disable
+//! the parts we do not need. For the moment, we define them to [`panic!`] at
+//! runtime for simplicity to catch mistakes, instead of performing surgery
+//! on `core.o`.
+//!
+//! In any case, all these symbols are weakened to ensure we do not override
+//! those that may be provided by the rest of the kernel.
+//!
+//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
+//! [`compiler-rt`]: https://compiler-rt.llvm.org/
+
+#![allow(internal_features)]
+#![feature(compiler_builtins)]
+#![compiler_builtins]
+#![no_builtins]
+#![no_std]
+
+macro_rules! define_panicking_intrinsics(
+ ($reason: tt, { $($ident: ident, )* }) => {
+ $(
+ #[doc(hidden)]
+ #[export_name = concat!("__rust", stringify!($ident))]
+ pub extern "C" fn $ident() {
+ panic!($reason);
+ }
+ )*
+ }
+);
+
+define_panicking_intrinsics!("`f32` should not be used", {
+ __addsf3,
+ __eqsf2,
+ __extendsfdf2,
+ __gesf2,
+ __lesf2,
+ __ltsf2,
+ __mulsf3,
+ __nesf2,
+ __truncdfsf2,
+ __unordsf2,
+});
+
+define_panicking_intrinsics!("`f64` should not be used", {
+ __adddf3,
+ __eqdf2,
+ __ledf2,
+ __ltdf2,
+ __muldf3,
+ __unorddf2,
+});
+
+define_panicking_intrinsics!("`i128` should not be used", {
+ __ashrti3,
+ __muloti4,
+ __multi3,
+});
+
+define_panicking_intrinsics!("`u128` should not be used", {
+ __ashlti3,
+ __lshrti3,
+ __udivmodti4,
+ __udivti3,
+ __umodti3,
+});
+
+#[cfg(target_arch = "arm")]
+define_panicking_intrinsics!("`f32` should not be used", {
+ __aeabi_fadd,
+ __aeabi_fmul,
+ __aeabi_fcmpeq,
+ __aeabi_fcmple,
+ __aeabi_fcmplt,
+ __aeabi_fcmpun,
+});
+
+#[cfg(target_arch = "arm")]
+define_panicking_intrinsics!("`f64` should not be used", {
+ __aeabi_dadd,
+ __aeabi_dmul,
+ __aeabi_dcmple,
+ __aeabi_dcmplt,
+ __aeabi_dcmpun,
+});
+
+#[cfg(target_arch = "arm")]
+define_panicking_intrinsics!("`u64` division/modulo should not be used", {
+ __aeabi_uldivmod,
+});
+
+#[cfg(target_arch = "powerpc")]
+define_panicking_intrinsics!("`u64` division/modulo should not be used", {
+ __udivdi3,
+ __umoddi3,
+});
+
+// NOTE: if you are adding a new intrinsic here, you should also add it to
+// `redirect-intrinsics` in `rust/Makefile`.
diff --git a/gcc/testsuite/rust/minicore/compiler_builtins_core/lib.rs
b/gcc/testsuite/rust/minicore/compiler_builtins_core/lib.rs
new file mode 100644
index 000000000..388ed778d
--- /dev/null
+++ b/gcc/testsuite/rust/minicore/compiler_builtins_core/lib.rs
@@ -0,0 +1,140 @@
+#![feature(no_core)]
+#![feature(lang_items)]
+#![feature(fundamental)]
+#![feature(rustc_attrs)]
+#![feature(arbitrary_self_types)]
+#![feature(optin_builtin_traits)]
+#![feature(prelude_import)]
+#![feature(intrinsics)]
+#![feature(staged_api)]
+#![no_core]
+
+pub use clone::Clone;
+pub use marker::{Copy, Sized};
+
+#[macro_export]
+macro_rules! panic {
+ ($($arg:tt),*) => {
+ $(
+ let _ = $arg;
+ )*
+ $crate::panic_handler();
+ };
+}
+
+#[macro_export]
+#[rustc_builtin_macro]
+macro_rules! assert {
+ ($cond:expr $(,)?) => {{ /* compiler built-in */ }};
+ ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
+}
+
+pub mod ffi {
+ #[repr(u8)]
+ pub enum c_void {
+ __variant1,
+ }
+}
+
+#[rustc_builtin_macro]
+#[macro_export]
+macro_rules! concat {
+ ($($e:expr),* $(,)?) => {{ /* compiler built-in */ }};
+}
+
+#[rustc_builtin_macro]
+#[macro_export]
+macro_rules! stringify {
+ ($($t:tt)*) => {
+ /* compiler built-in */
+ };
+}
+
+pub const fn panic_handler() {}
+
+pub mod mem {
+
+ use crate::intrinsics;
+
+ #[inline(always)]
+ #[rustc_promotable]
+ pub const fn size_of<T>() -> usize {
+ intrinsics::size_of::<T>()
+ }
+}
+
+pub mod intrinsics {
+
+ extern "rust-intrinsic" {
+ #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
+ pub fn size_of<T>() -> usize;
+ }
+}
+
+pub mod marker {
+ use crate::Clone;
+ #[lang = "sized"]
+ #[fundamental] // for Default, for example, which requires that `[T]:
!Default` be evaluatable
+ #[rustc_specialization_trait]
+ pub trait Sized {
+ // Empty.
+ }
+
+ #[lang = "copy"]
+ #[rustc_unsafe_specialization_marker]
+ pub trait Copy: Clone {
+ // Empty.
+ }
+}
+
+pub mod clone {
+ use crate::Sized;
+ #[lang = "clone"]
+ pub trait Clone: Sized {
+ #[must_use = "cloning is often expensive and is not expected to have
side effects"]
+ fn clone(&self) -> Self;
+
+ /// Performs copy-assignment from `source`.
+ ///
+ /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in
functionality,
+ /// but can be overridden to reuse the resources of `a` to avoid
unnecessary
+ /// allocations.
+ #[inline]
+ fn clone_from(&mut self, source: &Self) {
+ *self = source.clone()
+ }
+ }
+}
+
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+pub unsafe auto trait Send {
+ // empty.
+}
+
+#[lang = "structural_peq"]
+pub trait StructuralPartialEq {
+ // Empty.
+}
+
+#[lang = "structural_teq"]
+pub trait StructuralEq {
+ // Empty.
+}
+
+#[lang = "receiver"]
+#[doc(hidden)]
+pub trait Receiver {
+ // Empty.
+}
+
+#[prelude_import]
+mod prelude {
+ pub use crate::assert;
+ pub use crate::clone::Clone;
+ pub use crate::concat;
+ pub use crate::marker::{Copy, Sized};
+ pub use crate::panic;
+ pub use crate::stringify;
+}
--
2.56.1