public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Fix type inference on unit-structs
@ 2022-08-09 20:39 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-08-09 20:39 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:92eef6c33d49137707e5b3962822c6dded1f3c79

commit 92eef6c33d49137707e5b3962822c6dded1f3c79
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Mon Aug 8 17:05:54 2022 +0100

    Fix type inference on unit-structs
    
    When we initilize a structure that contains an identifier for the value
    which resolves to a generic unit-struct. The unit struct has no fields
    but it also contains a reference to a generic parameter which needs to be
    infered this updates the code for identifiers to act like a
    PathInExpression to infer the generic parameters when its unit struct only.
    
    Fixes #1447

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.h | 11 +++++++++++
 gcc/testsuite/rust/compile/issue-1447.rs      | 28 +++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index de542ca70e8..0c44f28e4ac 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -310,6 +310,17 @@ public:
       }
 
     infered = lookup->clone ();
+
+    // Generic unit structs look like an identifier but they actually need be
+    // handled as a path-in-expression so this gives us a chance to infer the
+    // generic parameters.
+    // see https://github.com/Rust-GCC/gccrs/issues/1447
+    bool is_unit_struct
+      = infered->get_kind () == TyTy::TypeKind::ADT && infered->is_unit ();
+    if (is_unit_struct && infered->needs_generic_substitutions ())
+      {
+	infered = SubstMapper::InferSubst (infered, expr.get_locus ());
+      }
   }
 
   void visit (HIR::LiteralExpr &expr) override
diff --git a/gcc/testsuite/rust/compile/issue-1447.rs b/gcc/testsuite/rust/compile/issue-1447.rs
new file mode 100644
index 00000000000..e0543e6247c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1447.rs
@@ -0,0 +1,28 @@
+// { dg-options "-w" }
+struct PhantomData<T>;
+
+struct Hasher<S> {
+    _marker: PhantomData<S>,
+}
+
+struct Sip24Rounds;
+
+struct SipHasher24 {
+    hasher: Hasher<Sip24Rounds>,
+}
+
+impl SipHasher24 {
+    pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher24 {
+        SipHasher24 {
+            hasher: Hasher::new_with_keys(),
+        }
+    }
+}
+
+impl<S> Hasher<S> {
+    fn new_with_keys() -> Hasher<S> {
+        Hasher {
+            _marker: PhantomData,
+        }
+    }
+}


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

only message in thread, other threads:[~2022-08-09 20:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-09 20:39 [gcc/devel/rust/master] Fix type inference on unit-structs 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).