public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add missing coercion rule from array to slice
@ 2022-06-08 12:34 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:639b3d61ab124ab14e0935893abe437ed7f6ad52

commit 639b3d61ab124ab14e0935893abe437ed7f6ad52
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Thu Apr 21 20:14:43 2022 +0100

    Add missing coercion rule from array to slice
    
    Arrays are coercible into slices, this adds the missing type-resolution
    the rule which works for now. The other part of this fix is described in
    so that we reuse as much code as possible and handle complex coercion
    sites.
    
    Fixes #1129

Diff:
---
 gcc/rust/typecheck/rust-tyty-coercion.h    | 16 ++++++++++++++++
 gcc/testsuite/rust/compile/issue-1129-1.rs |  4 ++++
 gcc/testsuite/rust/compile/issue-1129-2.rs | 22 ++++++++++++++++++++++
 3 files changed, 42 insertions(+)

diff --git a/gcc/rust/typecheck/rust-tyty-coercion.h b/gcc/rust/typecheck/rust-tyty-coercion.h
index c24f17e6c94..e13c7f8d982 100644
--- a/gcc/rust/typecheck/rust-tyty-coercion.h
+++ b/gcc/rust/typecheck/rust-tyty-coercion.h
@@ -886,6 +886,22 @@ public:
 			      TyVar (base_resolved->get_ref ()));
   }
 
+  void visit (ArrayType &type) override
+  {
+    // check base type
+    auto base_resolved
+      = base->get_element_type ()->unify (type.get_element_type ());
+    if (base_resolved == nullptr)
+      {
+	BaseCoercionRules::visit (type);
+	return;
+      }
+
+    resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
+			      type.get_ident ().locus,
+			      TyVar (base_resolved->get_ref ()));
+  }
+
 private:
   BaseType *get_base () override { return base; }
 
diff --git a/gcc/testsuite/rust/compile/issue-1129-1.rs b/gcc/testsuite/rust/compile/issue-1129-1.rs
new file mode 100644
index 00000000000..a15903983f0
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1129-1.rs
@@ -0,0 +1,4 @@
+// { dg-additional-options "-w" }
+fn write_u8(i: u8) {
+    let x: &[u8] = &[i];
+}
diff --git a/gcc/testsuite/rust/compile/issue-1129-2.rs b/gcc/testsuite/rust/compile/issue-1129-2.rs
new file mode 100644
index 00000000000..25d30faf4aa
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1129-2.rs
@@ -0,0 +1,22 @@
+// { dg-additional-options "-w" }
+pub trait Hasher {
+    fn finish(&self) -> u64;
+    fn write(&mut self, bytes: &[u8]);
+    fn write_u8(&mut self, i: u8) {
+        self.write(&[i])
+    }
+}
+
+struct SipHasher;
+
+impl Hasher for SipHasher {
+    #[inline]
+    fn write(&mut self, msg: &[u8]) {
+        loop {}
+    }
+
+    #[inline]
+    fn finish(&self) -> u64 {
+        0
+    }
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:34 [gcc/devel/rust/master] Add missing coercion rule from array to slice Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).