Repository: intellij-rust/intellij-rust
Issue: smart features are broken on 0.2.0.2099-181

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **Intellij-Rust plugin version:**
0.2.0.2099-181
* **IDE name and version:**
CLion 2018.1.5
Build #CL-181.5281.33, built on June 14, 2018
* **Operating system:**
Linux
## Problem description
Rust plugin smart features (navigation/completion/etc) are broken on 0.2.0.2099-181, rollback to 0.2.0.2098-181 works for me. Noticed lots of the following exceptions in a log:
<details><summary>Stack trace</summary>

```
java.lang.IllegalStateException: Check failed.
	at org.rust.lang.core.resolve.NameResolutionKt.exportedMacros(NameResolution.kt:483)
	at org.rust.lang.core.resolve.NameResolutionKt.visibleMacrosInternal(NameResolution.kt:448)
	at org.rust.lang.core.resolve.NameResolutionKt.access$visibleMacrosInternal(NameResolution.kt:1)
	at org.rust.lang.core.resolve.NameResolutionKt$visibleMacros$1.compute(NameResolution.kt:420)
	at com.intellij.psi.util.CachedValuesManager.lambda$getCachedValue$0(CachedValuesManager.java:140)
	at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:49)
	at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:222)
	at com.intellij.psi.impl.PsiCachedValue.getValueWithLock(PsiCachedValue.java:60)
	at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:38)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:136)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:126)
	at org.rust.lang.core.resolve.NameResolutionKt.visibleMacros(NameResolution.kt:419)
	at org.rust.lang.core.resolve.NameResolutionKt.access$visibleMacros(NameResolution.kt:1)
	at org.rust.lang.core.resolve.NameResolutionKt$processMacroCallVariants$result$2.invoke(NameResolution.kt:386)
	at org.rust.lang.core.resolve.NameResolutionKt$processMacroCallVariants$result$2.invoke(NameResolution.kt)
	at org.rust.lang.core.resolve.NameResolutionKt.walkUp(NameResolution.kt:841)
	at org.rust.lang.core.resolve.NameResolutionKt.processMacroCallVariants(NameResolution.kt:384)
	at org.rust.lang.core.resolve.ref.RsMacroCallReferenceImpl$resolveInner$1.invoke(RsMacroCallReferenceImpl.kt:20)
	at org.rust.lang.core.resolve.ref.RsMacroCallReferenceImpl$resolveInner$1.invoke(RsMacroCallReferenceImpl.kt:14)
	at org.rust.lang.core.resolve.ProcessorsKt.collectResolveVariants(Processors.kt:72)
	at org.rust.lang.core.resolve.ref.RsMacroCallReferenceImpl.resolveInner(RsMacroCallReferenceImpl.kt:20)
	at org.rust.lang.core.resolve.ref.RsReferenceCached$Resolver.resolve(RsReferenceCached.kt:37)
	at org.rust.lang.core.resolve.ref.RsReferenceCached$Resolver.resolve(RsReferenceCached.kt:35)
	at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$resolve$0(ResolveCache.java:152)
	at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:112)
	at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:151)
	at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:241)
	at org.rust.lang.core.resolve.ref.RsReferenceCached.cachedMultiResolve(RsReferenceCached.kt:30)
	at org.rust.lang.core.resolve.ref.RsReferenceCached.multiResolve(RsReferenceCached.kt:23)
	at com.intellij.psi.PsiPolyVariantReferenceBase.resolve(PsiPolyVariantReferenceBase.java:46)
	at org.rust.lang.core.resolve.ref.RsReferenceBase.resolve(RsReferenceBase.kt:26)
	at org.rust.lang.core.macros.MacroExpansionKt.expandMacro(MacroExpansion.kt:29)
	at org.rust.lang.core.psi.ext.RsMacroCallKt$expansion$1.compute(RsMacroCall.kt:53)
	at com.intellij.psi.util.CachedValuesManager.lambda$getCachedValue$0(CachedValuesManager.java:140)
	at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:49)
	at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:222)
	at com.intellij.psi.impl.PsiCachedValue.getValueWithLock(PsiCachedValue.java:60)
	at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:38)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:136)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:126)
	at org.rust.lang.core.psi.ext.RsMacroCallKt.getExpansion(RsMacroCall.kt:52)
	at org.rust.lang.core.psi.ext.RsItemsOwnerKt.processMacroCall(RsItemsOwner.kt:60)
	at org.rust.lang.core.psi.ext.RsItemsOwnerKt.processExpandedItems(RsItemsOwner.kt:48)
	at org.rust.lang.core.resolve.ItemResolutionKt.processItemDeclarations(ItemResolution.kt:85)
	at org.rust.lang.core.resolve.NameResolutionKt.processLexicalDeclarations(NameResolution.kt:707)
	at org.rust.lang.core.resolve.NameResolutionKt.access$processLexicalDeclarations(NameResolution.kt:1)
	at org.rust.lang.core.resolve.NameResolutionKt$processNestedScopesUpwards$2.invoke(NameResolution.kt:817)
	at org.rust.lang.core.resolve.NameResolutionKt$processNestedScopesUpwards$2.invoke(NameResolution.kt)
	at org.rust.lang.core.resolve.NameResolutionKt.walkUp(NameResolution.kt:841)
	at org.rust.lang.core.resolve.NameResolutionKt.walkUp$default(NameResolution.kt:835)
	at org.rust.lang.core.resolve.NameResolutionKt.processNestedScopesUpwards(NameResolution.kt:809)
	at org.rust.lang.core.resolve.NameResolutionKt.processPatBindingResolveVariants(NameResolution.kt:296)
	at org.rust.lang.core.resolve.ref.RsPatBindingReferenceImpl$resolveInner$1.invoke(RsPatBindingReferenceImpl.kt:24)
	at org.rust.lang.core.resolve.ref.RsPatBindingReferenceImpl$resolveInner$1.invoke(RsPatBindingReferenceImpl.kt:16)
	at org.rust.lang.core.resolve.ProcessorsKt.collectResolveVariants(Processors.kt:72)
	at org.rust.lang.core.resolve.ref.RsPatBindingReferenceImpl.resolveInner(RsPatBindingReferenceImpl.kt:24)
	at org.rust.lang.core.resolve.ref.RsReferenceCached$Resolver.resolve(RsReferenceCached.kt:37)
	at org.rust.lang.core.resolve.ref.RsReferenceCached$Resolver.resolve(RsReferenceCached.kt:35)
	at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$resolve$0(ResolveCache.java:152)
	at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:112)
	at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:151)
	at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:241)
	at org.rust.lang.core.resolve.ref.RsReferenceCached.cachedMultiResolve(RsReferenceCached.kt:30)
	at org.rust.lang.core.resolve.ref.RsReferenceCached.multiResolve(RsReferenceCached.kt:23)
	at com.intellij.psi.PsiPolyVariantReferenceBase.resolve(PsiPolyVariantReferenceBase.java:46)
	at org.rust.lang.core.resolve.ref.RsReferenceBase.resolve(RsReferenceBase.kt:26)
	at org.rust.lang.core.psi.ext.RsPatBindingKt.isReferenceToConstant(RsPatBinding.kt:30)
	at org.rust.ide.annotator.RsHighlightingAnnotator.annotate(RsHighlightingAnnotator.kt:23)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.a(DefaultHighlightVisitor.java:139)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.visit(DefaultHighlightVisitor.java:102)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:371)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:303)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:330)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:333)
	at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.analyze(DefaultHighlightVisitor.java:86)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:333)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.a(GeneralHighlightingPass.java:300)
	at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectInformationWithProgress(GeneralHighlightingPass.java:239)
	at com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.doCollectInformation(ProgressableTextEditorHighlightingPass.java:83)
	at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:70)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.b(PassExecutorService.java:437)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1134)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.a(PassExecutorService.java:430)
	at com.intellij.openapi.progress.impl.CoreProgressManager.a(CoreProgressManager.java:580)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:525)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:85)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.c(PassExecutorService.java:429)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.d(PassExecutorService.java:405)
	at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:143)
	at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:218)
	at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:403)
	at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask$1.exec(JobLauncherImpl.java:170)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
```

</details>

## Steps to reproduce
Sorry, no public codebase to reproduce the issue. I use cargo workspace with several projects. The same plugin version (0.2.0.2099-181) seems to work for a simple `cargo new --bin hello` project.

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->


Changed files:
src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt

diff --git a/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt b/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
index f2d20b3a6..bb7f7e168 100644
--- a/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
+++ b/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
@@ -7,6 +7,7 @@
 
 package org.rust.lang.core.resolve
 
+import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.progress.ProgressManager
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.util.io.FileUtil
@@ -76,6 +77,8 @@ import org.rust.stdext.buildList
 //     a code fragment in a temporary file and attaching it to some existing file. See the usages of
 //     [RsCodeFragmentFactory]
 
+private val LOG = Logger.getInstance("org.rust.lang.core.resolve.NameResolution")
+
 fun processDotExprResolveVariants(
     lookup: ImplLookup,
     receiverType: Ty,
@@ -496,7 +499,10 @@ private fun visibleMacrosInternal(scope: RsItemsOwner): List<RsMacro> {
 }
 
 private fun exportedMacros(scope: RsFile): List<RsMacro> {
-    check(scope.isCrateRoot)
+    if (!scope.isCrateRoot) {
+        LOG.warn("`${scope.virtualFile}` should be crate root")
+        return emptyList()
+    }
     return CachedValuesManager.getCachedValue(scope) {
         val macros = exportedMacrosInternal(scope)
         CachedValueProvider.Result.create(macros, scope.project.rustStructureModificationTracker)
@@ -504,8 +510,6 @@ private fun exportedMacros(scope: RsFile): List<RsMacro> {
 }
 
 private fun exportedMacrosInternal(scope: RsFile): List<RsMacro> {
-    check(scope.isCrateRoot)
-
     val allExportedMacros = RsMacroIndex.allExportedMacros(scope.project)
     return buildList {
         addAll(allExportedMacros[scope].orEmpty())