public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Fix type inference on unit-structs
Date: Tue,  9 Aug 2022 20:39:25 +0000 (GMT)	[thread overview]
Message-ID: <20220809203925.4C793385AC2C@sourceware.org> (raw)

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,
+        }
+    }
+}


                 reply	other threads:[~2022-08-09 20:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220809203925.4C793385AC2C@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).