aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/rust
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2025-02-20 17:01:28 +0000
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-24 13:07:10 +0100
commit051e54e5fcf0b0b852fef0c7f0e53e62b19a4176 (patch)
tree813e78e8e7c4e52ccbd751236aa419b8a360db26 /gcc/testsuite/rust
parent0287030e7704094133feaba8e3675c52f2439ec2 (diff)
downloadgcc-051e54e5fcf0b0b852fef0c7f0e53e62b19a4176.zip
gcc-051e54e5fcf0b0b852fef0c7f0e53e62b19a4176.tar.gz
gcc-051e54e5fcf0b0b852fef0c7f0e53e62b19a4176.tar.bz2
gccrs: derive(Hash): Add implementation.
gcc/rust/ChangeLog: * Make-lang.in: Compile it. * expand/rust-derive.cc (DeriveVisitor::derive): Call it. * expand/rust-derive-hash.cc: New file. * expand/rust-derive-hash.h: New file. gcc/testsuite/ChangeLog: * rust/compile/derive-hash1.rs: New test. * rust/compile/nr2/exclude: Add testcase to exclusion list.
Diffstat (limited to 'gcc/testsuite/rust')
-rw-r--r--gcc/testsuite/rust/compile/derive-hash1.rs91
-rw-r--r--gcc/testsuite/rust/compile/nr2/exclude1
2 files changed, 92 insertions, 0 deletions
diff --git a/gcc/testsuite/rust/compile/derive-hash1.rs b/gcc/testsuite/rust/compile/derive-hash1.rs
new file mode 100644
index 0000000..80e1e2d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/derive-hash1.rs
@@ -0,0 +1,91 @@
+#![feature(intrinsics)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub mod core {
+ pub mod intrinsics {
+ #[lang = "discriminant_kind"]
+ pub trait DiscriminantKind {
+ #[lang = "discriminant_type"]
+ type Discriminant;
+ }
+
+ extern "rust-intrinsic" {
+ pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
+ }
+ }
+
+ pub mod hash {
+ pub trait Hasher {}
+
+ pub trait Hash {
+ /// Feeds this value into the given [`Hasher`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::collections::hash_map::DefaultHasher;
+ /// use std::hash::{Hash, Hasher};
+ ///
+ /// let mut hasher = DefaultHasher::new();
+ /// 7920.hash(&mut hasher);
+ /// println!("Hash is {:x}!", hasher.finish());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn hash<H: Hasher>(&self, state: &mut H);
+
+ /// Feeds a slice of this type into the given [`Hasher`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::collections::hash_map::DefaultHasher;
+ /// use std::hash::{Hash, Hasher};
+ ///
+ /// let mut hasher = DefaultHasher::new();
+ /// let numbers = [6, 28, 496, 8128];
+ /// Hash::hash_slice(&numbers, &mut hasher);
+ /// println!("Hash is {:x}!", hasher.finish());
+ /// ```
+ #[stable(feature = "hash_slice", since = "1.3.0")]
+ fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
+ where
+ Self: Sized,
+ {
+ // for piece in data {
+ // piece.hash(state);
+ // }
+ }
+ }
+ }
+}
+
+impl core::hash::Hash for i32 {
+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {}
+}
+
+impl core::hash::Hash for i64 {
+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {}
+}
+
+// for the discriminant value
+impl core::hash::Hash for isize {
+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {}
+}
+
+#[derive(Hash)]
+struct Foo { // { dg-warning "never constructed" }
+ a: i32,
+ b: i32,
+}
+
+#[derive(Hash)]
+struct Bar(i32, i64); // { dg-warning "never constructed" }
+
+#[derive(Hash)]
+enum Baz {
+ A,
+ B(i32),
+ C { a: i64 }
+}
diff --git a/gcc/testsuite/rust/compile/nr2/exclude b/gcc/testsuite/rust/compile/nr2/exclude
index 26b830e..345b1d5 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -77,4 +77,5 @@ for-loop2.rs
issue-3403.rs
derive-eq-invalid.rs
derive-partialeq1.rs
+derive-hash1.rs
# please don't delete the trailing newline