aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-08-08 17:05:54 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-08-08 17:24:48 +0100
commit92eef6c33d49137707e5b3962822c6dded1f3c79 (patch)
tree64afc1dfa0d48ff24e387ff7d23e897e8146cca2 /gcc
parentb9840366b6e4f2bba8f3c67a4ee805d9f4f3c1c5 (diff)
downloadgcc-92eef6c33d49137707e5b3962822c6dded1f3c79.zip
gcc-92eef6c33d49137707e5b3962822c6dded1f3c79.tar.gz
gcc-92eef6c33d49137707e5b3962822c6dded1f3c79.tar.bz2
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
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h11
-rw-r--r--gcc/testsuite/rust/compile/issue-1447.rs28
2 files changed, 39 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index de542ca..0c44f28 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 0000000..e0543e6
--- /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,
+ }
+ }
+}