Repository: intellij-rust/intellij-rust
Issue: include! macro causes exception

## Environment

* **Intellij-Rust plugin version:** git - d87603e6130b6b6e194a2195d97efec099c94708
* **Rust toolchain version:** current nightly
* **IDE name and version:**  IntelliJ IDEA 2019.1.3 (Community Edition)
Build #IC-191.7479.19, built on May 28, 2019
* **Operating system:** Windows 10

## Problem description
IDE error:
```
java.lang.AssertionError: file=FILE; tree=Element(FILE)
 each of class class org.rust.lang.core.psi.impl.RsIncludeMacroArgumentImpl; valid=true; ref=com.intellij.psi.impl.source.SubstrateRef$StubRef@3a30442b
 each of class class org.rust.lang.core.psi.impl.RsMacroCallImpl; valid=true; ref=com.intellij.psi.impl.source.SubstrateRef$StubRef@44f86ca3
 each of class class org.rust.lang.core.psi.impl.RsModItemImpl; valid=true; ref=com.intellij.psi.impl.source.SubstrateRef$StubRef@467dbf7e
 each of class class org.rust.lang.core.psi.impl.RsBlockImpl; valid=true; ref=com.intellij.psi.impl.source.SubstrateRef$StubRef@64f9f3eb
 each of class class org.rust.lang.core.psi.impl.RsFunctionImpl; valid=true; ref=com.intellij.psi.impl.source.SubstrateRef$StubRef@5c50f243
 each of class class org.rust.lang.core.psi.RsFile; valid=true; same file=true; current tree= Element(FILE); stubTree=null; physical=false
 each stub RsPlaceholderStub
 each stub RsMacroCallStub
 each stub RsModItemStub
 each stub RsPlaceholderStub
 each stub RsFunctionStub
 each stub RsFileStub{myFile=FILE, myInvalidationReason=null, myStubRoots=[RsFileStub], stubTree=null}
	at com.intellij.extapi.psi.StubBasedPsiElementBase.notBoundInExistingAst(StubBasedPsiElementBase.java:226)
	at com.intellij.extapi.psi.StubBasedPsiElementBase.getNode(StubBasedPsiElementBase.java:139)
	at com.intellij.extapi.psi.ASTDelegatePsiElement.getText(ASTDelegatePsiElement.java:146)
	at com.intellij.psi.impl.PsiElementBase.notNullChild(PsiElementBase.java:285)
	at org.rust.lang.core.psi.impl.RsIncludeMacroArgumentImpl.getExpr(RsIncludeMacroArgumentImpl.java:38)
	at org.rust.lang.core.psi.ext.RsMacroCallKt.getIncludingFilePath(RsMacroCall.kt:61)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion.key(RsIncludeMacroIndex.kt:69)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion.index(RsIncludeMacroIndex.kt:34)
	at org.rust.lang.core.stubs.StubIndexingKt.indexMacroCall(StubIndexing.kt:87)
	at org.rust.lang.core.stubs.RsMacroCallStub$Type.indexStub(StubImplementations.kt:1072)
	at org.rust.lang.core.stubs.RsMacroCallStub$Type.indexStub(StubImplementations.kt:1050)
	at com.intellij.psi.stubs.ObjectStubTree.indexStubTree(ObjectStubTree.java:69)
	at com.intellij.psi.stubs.StubUpdatingIndex.calcStubIndicesValueMap(StubUpdatingIndex.java:408)
	at com.intellij.psi.stubs.StubUpdatingIndex.access$300(StubUpdatingIndex.java:54)
	at com.intellij.psi.stubs.StubUpdatingIndex$3.lambda$map$0(StubUpdatingIndex.java:277)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:940)
	at com.intellij.psi.stubs.StubUpdatingIndex$3.map(StubUpdatingIndex.java:220)
	at com.intellij.psi.stubs.StubUpdatingIndex$3.map(StubUpdatingIndex.java:202)
	at com.intellij.util.indexing.impl.MapReduceIndex.mapInput(MapReduceIndex.java:264)
	at com.intellij.util.indexing.VfsAwareMapReduceIndex.calculateUpdateData(VfsAwareMapReduceIndex.java:83)
	at com.intellij.util.indexing.impl.MapReduceIndex.update(MapReduceIndex.java:212)
	at com.intellij.util.indexing.FileBasedIndexImpl.updateSingleIndex(FileBasedIndexImpl.java:1673)
	at com.intellij.util.indexing.FileBasedIndexImpl.lambda$doIndexFileContent$27(FileBasedIndexImpl.java:1601)
	at com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl.freezeFileTypeTemporarilyIn(FileTypeManagerImpl.java:522)
	at com.intellij.util.indexing.FileBasedIndexImpl.doIndexFileContent(FileBasedIndexImpl.java:1560)
	at com.intellij.util.indexing.FileBasedIndexImpl.indexFileContent(FileBasedIndexImpl.java:1547)
	at com.intellij.util.indexing.UnindexedFilesUpdater.lambda$indexFiles$0(UnindexedFilesUpdater.java:109)
	at com.intellij.openapi.project.CacheUpdateRunner$MyRunnable.lambda$run$0(CacheUpdateRunner.java:216)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1166)
	at com.intellij.openapi.project.CacheUpdateRunner$MyRunnable.lambda$run$1(CacheUpdateRunner.java:232)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:164)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:586)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:532)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:86)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:151)
	at com.intellij.openapi.project.CacheUpdateRunner$MyRunnable.run(CacheUpdateRunner.java:229)
	at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:311)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
```

## Steps to reproduce

Paste this code somewhere in the project:

```Rust
fn f() {
    mod m {
        include!("a");
    }
}
```

Some time ago Rust plugin just stopped working on my project. When I open my project it initially works but shortly after just stops - no autocompletion, no semantic highlighting, etc. So I decided to troubleshoot it myself, cloned this repo, compiled it and ran - and that's when I saw the error above. The question is - why this error was not shown when I was running plugin as usual? This would allow me to report it much sooner.


Changed files:
src/test/kotlin/org/rust/lang/core/resolve/RsIncludeMacroResolveTest.kt, src/test/kotlin/org/rust/lang/core/stubs/RsStubTest.kt, src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt

diff --git a/src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt b/src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt
index 2c4002ea3..153c62d00 100644
--- a/src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt
+++ b/src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt
@@ -36,7 +36,7 @@ class RsFileStub : PsiFileStubImpl<RsFile> {
 
     object Type : IStubFileElementType<RsFileStub>(RsLanguage) {
         // Bump this number if Stub structure changes
-        override fun getStubVersion(): Int = 172
+        override fun getStubVersion(): Int = 173
 
         override fun getBuilder(): StubBuilder = object : DefaultStubBuilder() {
             override fun createStubForFile(file: PsiFile): StubElement<*> = RsFileStub(file as RsFile)
@@ -1149,8 +1149,7 @@ class RsExprStubType<PsiT : RsElement>(
     debugName: String,
     psiCtor: (RsPlaceholderStub, IStubElementType<*, *>) -> PsiT
 ) : RsPlaceholderStub.Type<PsiT>(debugName, psiCtor) {
-    override fun shouldCreateStub(node: ASTNode): Boolean =
-        shouldCreateExprStub(node)
+    override fun shouldCreateStub(node: ASTNode): Boolean = shouldCreateExprStub(node)
 }
 
 class RsLitExprStub(
@@ -1159,8 +1158,7 @@ class RsLitExprStub(
 ) : RsPlaceholderStub(parent, elementType) {
     object Type : RsStubElementType<RsLitExprStub, RsLitExpr>("LIT_EXPR") {
 
-        override fun shouldCreateStub(node: ASTNode): Boolean =
-            shouldCreateExprStub(node)
+        override fun shouldCreateStub(node: ASTNode): Boolean = shouldCreateExprStub(node)
 
         override fun serialize(stub: RsLitExprStub, dataStream: StubOutputStream) {
             stub.kind.serialize(dataStream)
@@ -1176,8 +1174,14 @@ class RsLitExprStub(
     }
 }
 
-private fun shouldCreateExprStub(node: ASTNode): Boolean =
-    createStubIfParentIsStub(node) && node.psi.ancestors.none { it is RsBlock && it.parent is RsFunction }
+private fun shouldCreateExprStub(node: ASTNode): Boolean {
+    if (!createStubIfParentIsStub(node)) return false
+    val element = node.psi.ancestors.firstOrNull {
+        val parent = it.parent
+        parent is RsItemElement || parent is RsMod
+    }
+    return element != null && !(element is RsBlock && element.parent is RsFunction)
+}
 
 class RsUnaryExprStub(
     parent: StubElement<*>?, elementType: IStubElementType<*, *>,
@@ -1185,8 +1189,7 @@ class RsUnaryExprStub(
 ) : RsPlaceholderStub(parent, elementType) {
     object Type : RsStubElementType<RsUnaryExprStub, RsUnaryExpr>("UNARY_EXPR") {
 
-        override fun shouldCreateStub(node: ASTNode): Boolean =
-            createStubIfParentIsStub(node) && node.psi.parent?.parent !is RsFunction
+        override fun shouldCreateStub(node: ASTNode): Boolean = shouldCreateExprStub(node)
 
         override fun serialize(stub: RsUnaryExprStub, dataStream: StubOutputStream) {
             dataStream.writeEnum(stub.operatorType)
diff --git a/src/test/kotlin/org/rust/lang/core/resolve/RsIncludeMacroResolveTest.kt b/src/test/kotlin/org/rust/lang/core/resolve/RsIncludeMacroResolveTest.kt
index af5124e48..4ae26f90f 100644
--- a/src/test/kotlin/org/rust/lang/core/resolve/RsIncludeMacroResolveTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/resolve/RsIncludeMacroResolveTest.kt
@@ -99,6 +99,19 @@ class RsIncludeMacroResolveTest : RsResolveTestBase() {
                   //^ lib.rs
     """)
 
+    fun `test include in function local module`() = checkResolve("""
+    //- lib.rs
+        fn foo() {
+            mod foo {
+                include!("bar.rs");
+            }
+            foo::Foo;
+                //^ bar.rs
+        }
+    //- bar.rs
+        struct Foo;
+    """)
+
     fun `test include file in included file 1`() = checkResolve("""
     //- lib.rs
         include!("foo.rs");
diff --git a/src/test/kotlin/org/rust/lang/core/stubs/RsStubTest.kt b/src/test/kotlin/org/rust/lang/core/stubs/RsStubTest.kt
index 63abb7724..d12882394 100644
--- a/src/test/kotlin/org/rust/lang/core/stubs/RsStubTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/stubs/RsStubTest.kt
@@ -194,9 +194,12 @@ class RsStubTest : RsTestBase() {
         fn foo() {
             if true {
                 struct S;
-                2 + 2
+                1 + 2
+            } else if false {
+                3 + 4
             } else {
-                2 + 2
+                struct T;
+                -5
             };
         }
     """, """
@@ -206,6 +209,36 @@ class RsStubTest : RsTestBase() {
             BLOCK:RsPlaceholderStub
               BLOCK:RsPlaceholderStub
                 STRUCT_ITEM:RsStructItemStub
+              BLOCK:RsPlaceholderStub
+                STRUCT_ITEM:RsStructItemStub
+    """)
+
+    fun `test expressions are stubbed inside function local module`() = doTest("""
+        fn foo() {
+            mod bar {
+                include!("a.rs");
+            }
+        }     
+    """, """
+        RsFileStub
+          FUNCTION:RsFunctionStub
+            VALUE_PARAMETER_LIST:RsPlaceholderStub
+            BLOCK:RsPlaceholderStub
+              MOD_ITEM:RsModItemStub
+                MACRO_CALL:RsMacroCallStub
+                  PATH:RsPathStub
+                  INCLUDE_MACRO_ARGUMENT:RsPlaceholderStub
+                    LIT_EXPR:RsLitExprStub
+    """)
+
+    fun `test expressions are stubs inside file`() = doTest("""
+        include!("foo.rs");    
+    """, """
+        RsFileStub
+          MACRO_CALL:RsMacroCallStub
+            PATH:RsPathStub
+            INCLUDE_MACRO_ARGUMENT:RsPlaceholderStub
+              LIT_EXPR:RsLitExprStub
     """)
 
     private fun doTest(@Language("Rust") code: String, expectedStubText: String) {