Skip to content

Commit

Permalink
Merge #4346
Browse files Browse the repository at this point in the history
4346: Fix switch to AST when resolving nested macro with the new engine r=vlad20012 a=vlad20012

The bug was introduced in #3750 (not released yet)

Co-authored-by: vlad20012 <beskvlad@gmail.com>
  • Loading branch information
bors[bot] and vlad20012 committed Sep 2, 2019
2 parents 07a0cab + 386ab71 commit 65f713d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/main/kotlin/org/rust/lang/core/macros/RsExpandedElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package org.rust.lang.core.macros

import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.rust.lang.core.psi.RsFile
import org.rust.lang.core.psi.RsMacroArgument
import org.rust.lang.core.psi.RsMacroCall
import org.rust.lang.core.psi.RsPath
import org.rust.lang.core.psi.ext.*
import org.rust.lang.core.stubs.index.RsIncludeMacroIndex

Expand Down Expand Up @@ -144,10 +144,20 @@ private fun PsiElement.findElementExpandedFromNonRecursive(): PsiElement? {
}

private fun mapOffsetFromExpansionToCallBody(call: RsMacroCall, offset: Int): Int? {
return mapOffsetFromExpansionToCallBodyRelative(call, offset)
?.fromBodyRelativeOffset(call)
}

private fun mapOffsetFromExpansionToCallBodyRelative(call: RsMacroCall, offset: Int): Int? {
val expansion = call.expansion ?: return null
val fileOffset = call.expansionContext.expansionFileStartOffset
return expansion.ranges.mapOffsetFromExpansionToCallBody(offset - fileOffset)
?.fromBodyRelativeOffset(call)
}

fun PsiElement.cameFromMacroCall(): Boolean {
val call = findMacroCallExpandedFromNonRecursive() ?: return false
val startOffset = (this as? RsPath)?.greenStub?.startOffset ?: startOffset
return mapOffsetFromExpansionToCallBodyRelative(call, startOffset) != null
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ fun processMacroCallPathResolveVariants(path: RsPath, isCompletion: Boolean, pro
if (isCompletion) {
processMacroCallVariantsInScope(path, processor)
} else {
if (path.findElementExpandedFrom(strict = false) == null) {
if (!path.cameFromMacroCall()) {
// Handles `#[macro_export(local_inner_macros)]`
// this "recursive" macro resolve should not be a problem because
// 1. we resolve the macro from which [path] is expanded, so it can't run into infinite recursion
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/org/rust/lang/core/stubs/StubImplementations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RsFileStub : PsiFileStubImpl<RsFile> {

object Type : IStubFileElementType<RsFileStub>(RsLanguage) {
// Bump this number if Stub structure changes
override fun getStubVersion(): Int = 179
override fun getStubVersion(): Int = 180

override fun getBuilder(): StubBuilder = object : DefaultStubBuilder() {
override fun createStubForFile(file: PsiFile): StubElement<*> = RsFileStub(file as RsFile)
Expand Down Expand Up @@ -719,7 +719,8 @@ class RsPathStub(
parent: StubElement<*>?, elementType: IStubElementType<*, *>,
val referenceName: String,
val hasColonColon: Boolean,
val kind: PathKind
val kind: PathKind,
val startOffset: Int
) : StubBase<RsPath>(parent, elementType) {

object Type : RsStubElementType<RsPathStub, RsPath>("PATH") {
Expand All @@ -729,20 +730,22 @@ class RsPathStub(
RsPathImpl(stub, this)

override fun createStub(psi: RsPath, parentStub: StubElement<*>?) =
RsPathStub(parentStub, this, psi.referenceName, psi.hasColonColon, psi.kind)
RsPathStub(parentStub, this, psi.referenceName, psi.hasColonColon, psi.kind, psi.startOffset)

override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?) =
RsPathStub(parentStub, this,
dataStream.readName()!!.string,
dataStream.readBoolean(),
dataStream.readEnum()
dataStream.readEnum(),
dataStream.readVarInt()
)

override fun serialize(stub: RsPathStub, dataStream: StubOutputStream) =
with(dataStream) {
writeName(stub.referenceName)
writeBoolean(stub.hasColonColon)
writeEnum(stub.kind)
writeVarInt(stub.startOffset)
}
}
}
Expand Down

0 comments on commit 65f713d

Please sign in to comment.