Repository: intellij-rust/intellij-rust
Issue: Completion doesn't work when accessing a tuple item

## Environment

* **Intellij-Rust plugin version:** v0.2.96.2598-191-nightly
* **Rust toolchain version:** stable
* **IDE name and version:** CLion 2019.1
* **Operating system:** Linux

## Problem description

Completion doesn't work after `x.1.`.

## Steps to reproduce
```rust
fn main() {
    let x = (0, String::new());
    x.1./* cursor */
}
```
Pressing ctrl+space here doesn't provide any suggestions. If I type a letter, e.g. `x.1.p`, the completion works correctly after that.


Changed files:
src/main/kotlin/org/rust/lang/core/completion/RsCommonCompletionProvider.kt, src/test/kotlin/org/rust/lang/core/completion/RsCompletionTest.kt

diff --git a/src/main/kotlin/org/rust/lang/core/completion/RsCommonCompletionProvider.kt b/src/main/kotlin/org/rust/lang/core/completion/RsCommonCompletionProvider.kt
index 33136265f..d7d24be84 100644
--- a/src/main/kotlin/org/rust/lang/core/completion/RsCommonCompletionProvider.kt
+++ b/src/main/kotlin/org/rust/lang/core/completion/RsCommonCompletionProvider.kt
@@ -42,9 +42,7 @@ object RsCommonCompletionProvider : CompletionProvider<CompletionParameters>() {
         result: CompletionResultSet
     ) {
         // Use original position if possible to re-use caches of the real file
-        val position = CompletionUtil.getOriginalElement(parameters.position)
-            ?.takeIf { isAncestorTypesEquals(it, parameters.position) }
-            ?: parameters.position
+        val position = parameters.position.safeGetOriginalOrSafe()
         val element = position.parent as RsReferenceElement
         if (position !== element.referenceNameElement) return
 
@@ -98,7 +96,7 @@ object RsCommonCompletionProvider : CompletionProvider<CompletionParameters>() {
         result: CompletionResultSet,
         forSimplePath: Boolean
     ) {
-        val receiver = CompletionUtil.getOriginalOrSelf(element.receiver)
+        val receiver = element.receiver.safeGetOriginalOrSafe()
         val lookup = ImplLookup.relativeTo(receiver)
         val receiverTy = receiver.type
         val processResolveVariants = if (element is RsMethodCall) {
@@ -198,7 +196,13 @@ object RsCommonCompletionProvider : CompletionProvider<CompletionParameters>() {
     }
 }
 
-private fun isAncestorTypesEquals(psi1: PsiElement, psi2: PsiElement): Boolean =
+private fun <T: PsiElement> T.safeGetOriginalOrSafe(): T {
+    return CompletionUtil.getOriginalElement(this)
+        ?.takeIf { areAncestorTypesEquals(it, this) }
+        ?: this
+}
+
+private fun areAncestorTypesEquals(psi1: PsiElement, psi2: PsiElement): Boolean =
     psi1.ancestors.zip(psi2.ancestors).all { (a, b) -> a.javaClass == b.javaClass }
 
 private fun filterAssocTypes(
diff --git a/src/test/kotlin/org/rust/lang/core/completion/RsCompletionTest.kt b/src/test/kotlin/org/rust/lang/core/completion/RsCompletionTest.kt
index c11c437df..13b425050 100644
--- a/src/test/kotlin/org/rust/lang/core/completion/RsCompletionTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/completion/RsCompletionTest.kt
@@ -769,4 +769,18 @@ class RsCompletionTest : RsCompletionTestBase() {
             use self::/*caret*/
         }
     """)
+
+    fun `test completion after tuple field expr`() = doSingleCompletion("""
+        struct S { field: i32 }
+        fn main() {
+            let x = (0, S { field: 0 });
+            x.1./*caret*/
+        }
+    """, """
+        struct S { field: i32 }
+        fn main() {
+            let x = (0, S { field: 0 });
+            x.1.field/*caret*/
+        }
+    """)
 }