Repository: intellij-rust/intellij-rust
Issue: Assert failed in `ImplLookup.selectWithoutConfirm` on `rayon` crate

## Environment

* **IntelliJ Rust plugin version:** 0.4.182.SNAPSHOT-222-dev
* **Rust toolchain version:** 1.64.0 (a55dd71d5 2022-09-19) x86_64-unknown-linux-gnu
* **IDE name and version:** IntelliJ IDEA 2022.2.2 Ultimate Edition (IU-222.4167.29)
* **Operating system:** Linux 6.0.1-zen2-1-zen
* **Macro expansion:** enabled
* **Additional experimental features:** org.rust.cargo.emulate.terminal, org.rust.macros.proc

## Problem description
`RsRealProjectAnalysisTest` currently fails on some crates with exception:

<details>

```
java.lang.IllegalStateException: Check failed.
	at org.rust.lang.core.resolve.ImplLookup.selectWithoutConfirm(ImplLookup.kt:462)
	at org.rust.lang.core.resolve.ImplLookup.selectStrictWithoutConfirm(ImplLookup.kt:434)
	at org.rust.lang.core.resolve.ImplLookup.canSelect(ImplLookup.kt:427)
	at org.rust.lang.core.resolve.ImplLookup.canSelect$default(ImplLookup.kt:426)
	at org.rust.lang.core.resolve.ImplLookup.isTraitImplemented(ImplLookup.kt:1285)
	at org.rust.lang.core.resolve.ImplLookup.isSized(ImplLookup.kt:1260)
	at org.rust.ide.annotator.RsErrorAnnotatorKt.checkTypesAreSized$isError(RsErrorAnnotator.kt:1830)
	at org.rust.ide.annotator.RsErrorAnnotatorKt.checkTypesAreSized(RsErrorAnnotator.kt:1844)
	at org.rust.ide.annotator.RsErrorAnnotatorKt.access$checkTypesAreSized(RsErrorAnnotator.kt:1)
	at org.rust.ide.annotator.RsErrorAnnotator.checkFunction(RsErrorAnnotator.kt:1223)
	at org.rust.ide.annotator.RsErrorAnnotator.access$checkFunction(RsErrorAnnotator.kt:86)
	at org.rust.ide.annotator.RsErrorAnnotator$annotateInternal$visitor$1.visitFunction(RsErrorAnnotator.kt:102)
	at org.rust.lang.core.psi.impl.RsFunctionImpl.accept(RsFunctionImpl.java:27)
	at org.rust.lang.core.psi.impl.RsFunctionImpl.accept(RsFunctionImpl.java:32)
	at org.rust.ide.annotator.RsErrorAnnotator.annotateInternal(RsErrorAnnotator.kt:158)
	at org.rust.ide.annotator.AnnotatorBase.annotate(AnnotatorBase.kt:21)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.runAnnotators(DefaultHighlightVisitor.java:131)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.visit(DefaultHighlightVisitor.java:108)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.runVisitors(GeneralHighlightingPass.java:347)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$collectHighlights$7(GeneralHighlightingPass.java:285)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:306)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$analyzeByVisitors$8(GeneralHighlightingPass.java:309)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.analyze(DefaultHighlightVisitor.java:93)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:309)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectHighlights(GeneralHighlightingPass.java:276)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectInformationWithProgress(GeneralHighlightingPass.java:221)
	at com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.doCollectInformation(ProgressableTextEditorHighlightingPass.java:97)
	at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:57)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$1(PassExecutorService.java:400)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1154)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$2(PassExecutorService.java:393)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:608)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:683)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:639)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:607)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.doRun(PassExecutorService.java:392)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$run$0(PassExecutorService.java:368)
	at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:174)
	at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:215)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:366)
	at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask$1.exec(JobLauncherImpl.java:184)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
```

</details>

## Steps to reproduce

* Replace [this `testAssert`](https://github.com/intellij-rust/intellij-rust/blob/de5862fbace9fd5b7e2e6d9c3763883d997e1104/src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt#L462) with usual `check`
*
  ```
  git clone https://github.com/clap-rs/clap
  git checkout cbb05d54f850bcb16090799187747f62540ba703
  ```

* Open file `rayon-1.5.3/src/iter/map_with.rs`


Changed files:
src/main/kotlin/org/rust/lang/core/types/infer/TypeInference.kt, src/test/kotlin/org/rust/lang/core/type/RsTypeResolvingTest.kt

diff --git a/src/main/kotlin/org/rust/lang/core/types/infer/TypeInference.kt b/src/main/kotlin/org/rust/lang/core/types/infer/TypeInference.kt
index 65616ee03..6f4431563 100644
--- a/src/main/kotlin/org/rust/lang/core/types/infer/TypeInference.kt
+++ b/src/main/kotlin/org/rust/lang/core/types/infer/TypeInference.kt
@@ -888,9 +888,10 @@ class RsInferenceContext(
     fun constVarForParam(const: CtConstParameter): Const = CtInferVar(const)
 
     fun <T : TypeFoldable<T>> fullyNormalizeAssociatedTypesIn(ty: T): T {
-        return ty.foldTyProjectionWith {
-            optNormalizeProjectionType(it, 0)?.value ?: TyUnknown
-        }
+        val (normalizedTy, obligations) = normalizeAssociatedTypesIn(ty)
+        obligations.forEach(fulfill::registerPredicateObligation)
+        fulfill.selectWherePossible()
+        return fullyResolve(normalizedTy)
     }
 
     /** Deeply normalize projection types. See [normalizeProjectionType] */
diff --git a/src/test/kotlin/org/rust/lang/core/type/RsTypeResolvingTest.kt b/src/test/kotlin/org/rust/lang/core/type/RsTypeResolvingTest.kt
index 1c9ce77c3..b94d00a8b 100644
--- a/src/test/kotlin/org/rust/lang/core/type/RsTypeResolvingTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/type/RsTypeResolvingTest.kt
@@ -580,6 +580,32 @@ class RsTypeResolvingTest : RsTypificationTestBase() {
                //^ Foo<A1, 1, C1>
     """)
 
+    fun `test normalizable Self-related associated type path`() = testType("""
+        struct S<T>(T);
+        trait Foo<T> {
+            type Item;
+            fn foo(&self) -> Self::Item;
+        }
+        impl<T, C, R> Foo<T> for S<C> where C: Foo<R>
+        {
+            type Item = C::Item;
+            fn foo(&self) -> Self::Item { todo!() }
+        }                        //^ <C as Foo<R>>::Item
+    """, normalize = true)
+
+    fun `test normalizable associated type path with nested obligations`() = testType("""
+        struct W<T>(T);
+        trait Foo { type Item; }
+        impl<A, B> Foo for W<A> where A: Foo<Item=B> { type Item = B; }
+        struct S;
+        struct X;
+        impl Foo for S {
+            type Item = X;
+        }
+        type T = <W<S> as Foo>::Item;
+                              //^ X
+    """, normalize = true)
+
     /**
      * Checks the type of the element in [code] pointed to by `//^` marker.
      */