Skip to content

Commit

Permalink
fix false positives for vector type arguments check (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov authored Oct 19, 2024
1 parent 6d56c00 commit 18c4eeb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/main/kotlin/org/move/ide/annotator/MvErrorAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import org.move.ide.utils.getSignature
import org.move.lang.MvElementTypes.R_PAREN
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
import org.move.lang.core.resolve2.PathKind
import org.move.lang.core.resolve2.pathKind
import org.move.lang.core.types.address
import org.move.lang.core.types.fullname
import org.move.lang.core.types.infer.descendantHasTypeError
import org.move.lang.core.types.infer.inference
import org.move.lang.core.types.infer.loweredType
import org.move.lang.core.types.ty.*
import org.move.lang.core.types.ty.TyCallable
import org.move.lang.core.types.ty.TyFunction
import org.move.lang.core.types.ty.TyTypeParameter
import org.move.lang.core.types.ty.TyUnknown
import org.move.lang.moveProject
import org.move.lang.utils.Diagnostic
import org.move.lang.utils.addToHolder
Expand Down Expand Up @@ -160,23 +165,29 @@ class MvErrorAnnotator: MvAnnotatorBase() {
}

private fun checkMethodOrPath(methodOrPath: MvMethodOrPath, holder: MvAnnotationHolder) {
val item = methodOrPath.reference?.resolveFollowingAliases()
val msl = methodOrPath.isMslScope
val realCount = methodOrPath.typeArguments.size

val parent = methodOrPath.parent
if (item == null && methodOrPath is MvPath
&& methodOrPath.qualifier == null && methodOrPath.identifierName == "vector"
) {
val expectedCount = 1
if (realCount != expectedCount) {
Diagnostic
.TypeArgumentsNumberMismatch(methodOrPath, "vector", expectedCount, realCount)
.addToHolder(holder)
if (methodOrPath is MvPath && methodOrPath.identifierName == "vector") {
// try to check whether it's a `vector<>` type instantiation
// and it has a single type argument for the element type
run {
val rootPath = methodOrPath.rootPath()
// only if `vector` is 1-element path
if (rootPath.pathKind() !is PathKind.UnqualifiedPath) return@run
// relevant only in type position
if (rootPath.parent !is MvPathType) return@run
if (realCount != 1) {
Diagnostic
.TypeArgumentsNumberMismatch(methodOrPath, "vector", 1, realCount)
.addToHolder(holder)
}
return
}
return
}
val qualItem = item as? MvQualNamedElement ?: return

val msl = methodOrPath.isMslScope
val parent = methodOrPath.parent
val qualItem = methodOrPath.reference?.resolveFollowingAliases() as? MvQualNamedElement ?: return

val qualName = qualItem.qualName ?: return
when {
qualItem is MvStruct && parent is MvPathType -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,43 @@ class TypeParametersNumberErrorTest: AnnotatorTestCase(MvErrorAnnotator::class)
}
}
""")


fun `test no error for vector in fq expr if unresolved`() = checkErrors("""
module 0x1::m {
fun main() {
vector::push_back();
}
}
""")

fun `test no error for vector in local path expr`() = checkErrors("""
module 0x1::m {
fun main() {
vector;
}
}
""")

fun `test no error for vector in type position`() = checkErrors("""
module 0x1::m {
fun main(s: vector::Vector) {
}
}
""")

fun `test no error for vector in type position with qualifier`() = checkErrors("""
module 0x1::m {
fun main(s: std::vector) {
}
}
""")

fun `test vector type position arguments error in presence of vector module`() = checkErrors("""
module 0x1::vector {}
module 0x1::m {
use 0x1::vector;
fun main(s: <error descr="Invalid instantiation of 'vector'. Expected 1 type argument(s) but got 0">vector</error>) {}
}
""")
}

0 comments on commit 18c4eeb

Please sign in to comment.