Skip to content

Commit

Permalink
Merge pull request #66 from pontem-network/spec-fun-scoping
Browse files Browse the repository at this point in the history
fix spec fun scoping
  • Loading branch information
mkurnikov authored Aug 24, 2022
2 parents 4df4b13 + 0436a60 commit ede3fa6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/move/cli/BuildDirectoryWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class BuildDirectoryWatcher(
}

override fun after(events: MutableList<out VFileEvent>) {
val builds = moveProjects.map { FileUtil.join(it.contentRoot.path, "build") }
val buildDirectories = moveProjects.map { FileUtil.join(it.contentRoot.path, "build") }
for (event in events) {
if (builds.any { event.pathStartsWith(it) }) {
if (buildDirectories.any { event.pathStartsWith(it) }) {
onChange()
return
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/org/move/cli/MoveTomlWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class MoveTomlWatcher(
}
}

fun VFileEvent.pathStartsWith(prefix: String): Boolean = path.startsWith(prefix) ||
this is VFilePropertyChangeEvent && oldPath.startsWith(prefix)
fun VFileEvent.pathStartsWith(prefix: String): Boolean =
path.startsWith(prefix) ||
this is VFilePropertyChangeEvent && oldPath.startsWith(prefix)

fun VFileEvent.pathEndsWith(suffix: String): Boolean = path.endsWith(suffix) ||
this is VFilePropertyChangeEvent && oldPath.endsWith(suffix)
fun VFileEvent.pathEndsWith(suffix: String): Boolean =
path.endsWith(suffix) ||
this is VFilePropertyChangeEvent && oldPath.endsWith(suffix)
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ object TypesCompletionProvider : MvPathCompletionProvider() {
return ItemVis(
setOf(Namespace.TYPE),
Visibility.none(),
mslScope = MslScope.NONE,
mslScope = pathElement.mslScope,
itemScope = pathElement.itemScope,
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/org/move/lang/core/psi/ext/PsiElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ fun PsiElement.equalsTo(another: PsiElement): Boolean =
fun PsiElement.isMsl(): Boolean {
return getProjectPsiDependentCache(this) {
if (it !is MvElement) return@getProjectPsiDependentCache false

// use items always non-msl, otherwise import resolution doesn't work correctly
if (it is MvUseItem) return@getProjectPsiDependentCache false

val specElement = PsiTreeUtil.findFirstParent(it, false) { parent ->
parent is MvSpecFunction
|| parent is MvItemSpecBlockExpr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.move.lang.core.resolve

import org.move.lang.core.psi.MvNamedElement
import org.move.lang.core.psi.ext.isMsl

data class SimpleScopeEntry<T : MvNamedElement>(
val name: String,
Expand All @@ -11,7 +12,9 @@ fun interface MatchingProcessor<T : MvNamedElement> {
fun match(entry: SimpleScopeEntry<T>): Boolean

fun match(itemVis: ItemVis, element: T): Boolean {
if (element.isMsl() && !itemVis.isMsl) return false
if (!element.isVisibleInScope(itemVis.itemScope)) return false

val name = element.name ?: return false
val entry = SimpleScopeEntry(name, element)
return match(entry)
Expand Down
42 changes: 42 additions & 0 deletions src/test/kotlin/org/move/lang/resolve/ResolveSpecsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,46 @@ class ResolveSpecsTest: ResolveTestCase() {
}
}
""")

fun `test spec functions not available in main code`() = checkByCode("""
module 0x1::main {
fun call() {
spec_add();
//^ unresolved
}
}
spec 0x1::main {
spec fun spec_add(): u8 { 1 }
}
""")

fun `test spec functions are available in spec code`() = checkByCode("""
module 0x1::main {
fun call() {
}
spec call {
spec_add();
//^
}
}
spec 0x1::main {
spec fun spec_add(): u8 { 1 }
//X
}
""")

fun `test spec functions are available in spec module code`() = checkByCode("""
module 0x1::main {
fun call() {
}
}
spec 0x1::main {
spec fun spec_add(): u8 { 1 }
//X
spec call {
spec_add();
//^
}
}
""")
}

0 comments on commit ede3fa6

Please sign in to comment.