Skip to content

Commit

Permalink
Merge pull request #273 from pontem-network/move-2-compatibilities
Browse files Browse the repository at this point in the history
Compatibility with Move 2
  • Loading branch information
mkurnikov authored Jan 17, 2025
2 parents 84fd6f2 + 36d173c commit 3d27474
Show file tree
Hide file tree
Showing 41 changed files with 233 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MvProjectSettingsService(
var dumpStateOnTestFailure: Boolean by property(false)

@AffectsHighlighting
var enableMove2: Boolean by property(false)
var enableMove2: Boolean by property(true)

override fun copy(): MoveProjectSettings {
val state = MoveProjectSettings()
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/org/move/ide/annotator/MvSyntaxErrorAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
val visitor = object: MvVisitor() {

override fun visitLitExpr(expr: MvLitExpr) = checkLitExpr(moveHolder, expr)
override fun visitCastExpr(expr: MvCastExpr) = checkCastExpr(moveHolder, expr)
override fun visitFunction(o: MvFunction) = checkFunction(moveHolder, o)
override fun visitSpecFunction(o: MvSpecFunction) = checkSpecFunction(moveHolder, o)
override fun visitIndexExpr(o: MvIndexExpr) = checkIndexExpr(moveHolder, o)
Expand Down Expand Up @@ -74,15 +73,6 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
}
}

private fun checkCastExpr(holder: MvAnnotationHolder, castExpr: MvCastExpr) {
val parent = castExpr.parent
if (parent !is MvParensExpr) {
Diagnostic
.ParensAreRequiredForCastExpr(castExpr)
.addToHolder(holder)
}
}

private fun checkIndexExpr(holder: MvAnnotationHolder, indexExpr: MvIndexExpr) {
// always supported in specs
if (indexExpr.isMsl()) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import java.util.concurrent.CompletableFuture

object RsExternalLinterUtils {
private val LOG: Logger = logger<RsExternalLinterUtils>()
const val APTOS_TEST_MESSAGE: String = "RsExternalLint"
const val APTOS_TEST_MESSAGE: String = "MvExternalLint"

/**
* Returns (and caches if absent) lazily computed messages from external linter.
Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/kotlin/org/move/ide/hints/type/MvTypeHintsFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.psi.createSmartPointer
import org.move.ide.presentation.hintText
import org.move.lang.core.types.ty.Ty
import org.move.lang.core.types.ty.TyAdt
import org.move.lang.core.types.ty.TyReference
import org.move.lang.core.types.ty.TyTuple
import org.move.lang.core.types.ty.TyVector

Expand All @@ -23,6 +24,7 @@ object MvTypeHintsFactory {
is TyTuple -> tupleTypeHint(level, type)
is TyAdt -> adtTypeHint(level, type)
is TyVector -> vectorTypeHint(level, type)
is TyReference -> referenceTypeHint(level, type)
else -> {
text(type.hintText())
}
Expand Down Expand Up @@ -90,6 +92,11 @@ object MvTypeHintsFactory {
})
}

private fun PresentationTreeBuilder.referenceTypeHint(level: Int, tyRef: TyReference) {
text(if (tyRef.isMut) "&mut " else "&")
typeHint(level, tyRef.referenced)
}

private fun <T> PresentationTreeBuilder.join(
elements: List<T>,
op: PresentationTreeBuilder.(T) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MvUnresolvedReferenceInspection: MvLocalInspectionTool() {
if (msl && !isDebugModeEnabled()) return

// no error if receiver item is unknown (won't proc if unknown is nested)
if (methodOrField.inferReceiverTy(msl) is TyUnknown) return
if (methodOrField.inferReceiverTy(msl).derefIfNeeded() is TyUnknown) return

tryMultiResolveOrRegisterError(methodOrField, holder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ object CommonCompletionProvider: MvCompletionProvider() {

@VisibleForTesting
fun addMethodOrFieldVariants(element: MvMethodOrField, result: CompletionResultSet, ctx: MvCompletionContext) {
val receiverTy = element.inferReceiverTy(ctx.msl).knownOrNull() ?: return
val receiverTy = element.inferReceiverTy(ctx.msl)
// unknown, &unknown, &mut unknown
if (receiverTy.derefIfNeeded() is TyUnknown) return

val tyAdt = receiverTy.derefIfNeeded() as? TyAdt
if (tyAdt != null) {
collectCompletionVariants(result, ctx, subst = tyAdt.substitution) {
processFieldLookupResolveVariants(element, tyAdt, ctx.msl, it)
processFieldLookupResolveVariants(element, tyAdt.item, ctx.msl, it)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/org/move/lang/core/psi/ext/MvPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ fun MvPath.allowedNamespaces(isCompletion: Boolean = false): Set<Namespace> {
parent is MvCallExpr -> FUNCTIONS
parent is MvPathExpr
&& this.hasAncestor<MvAttrItemInitializer>() -> ALL_NAMESPACES
// TYPES for resource indexing, NAMES for vector indexing
parent is MvPathExpr
&& parent.parent is MvIndexExpr -> TYPES_N_ENUMS_N_NAMES

// can be anything in completion
parent is MvPathExpr -> if (isCompletion) ALL_NAMESPACES else NAMES
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/org/move/lang/core/resolve/ref/Namespace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ val ENUMS = setOf(Namespace.ENUM)
val LABELS = setOf(Namespace.LABEL)
val TYPES_N_ENUMS = setOf(Namespace.TYPE, Namespace.ENUM)

val TYPES_N_NAMES = setOf(Namespace.TYPE, Namespace.NAME)
val TYPES_N_ENUMS_N_NAMES = setOf(Namespace.TYPE, Namespace.NAME, Namespace.ENUM)
val ENUMS_N_MODULES = setOf(Namespace.ENUM, Namespace.MODULE)
val TYPES_N_ENUMS_N_MODULES = setOf(Namespace.TYPE, Namespace.ENUM, Namespace.MODULE)
//val TYPES_N_ENUMS_N_MODULES_N_NAMES = setOf(Namespace.TYPE, Namespace.ENUM, Namespace.MODULE, Namespace.NAME)

val ALL_NAMESPACES = Namespace.all()
val ITEM_NAMESPACES =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import org.move.lang.core.psi.ext.*
import org.move.lang.core.resolve.*
import org.move.lang.core.resolve.LetStmtScope.*
import org.move.lang.core.resolve.ref.ENUMS
import org.move.lang.core.resolve.ref.NAMES
import org.move.lang.core.resolve.ref.NONE
import org.move.lang.core.resolve.ref.Namespace
import org.move.lang.core.resolve.ref.Namespace.NAME
import org.move.lang.core.resolve.ref.TYPES
import org.move.lang.core.resolve2.ref.ResolutionContext
import org.move.lang.core.resolve2.util.forEachLeafSpeck
Expand All @@ -29,18 +29,15 @@ fun processItemsInScope(
for (namespace in ns) {
val elementNs = setOf(namespace)
val stop = when (namespace) {
NAME -> {
Namespace.NAME -> {
val found = when (scope) {
is MvModule -> {
// try enum variants first
if (processor.processAll(elementNs, scope.enumVariants())) return true
processor.processAllItems(
elementNs,
scope.structs(),
scope.consts()
)
if (processor.processAll(elementNs, scope.enumVariants())) {
return true
}
processor.processAllItems(elementNs, scope.constList)
}
is MvModuleSpecBlock -> processor.processAllItems(elementNs, scope.schemaList)
is MvScript -> processor.processAllItems(elementNs, scope.constList)
is MvFunctionLike -> processor.processAll(elementNs, scope.parametersAsBindings)
is MvLambdaExpr -> processor.processAll(elementNs, scope.parametersAsBindings)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.move.lang.core.resolve2

import org.move.lang.core.completion.getOriginalOrSelf
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
import org.move.lang.core.resolve.*
Expand All @@ -15,11 +14,10 @@ import org.move.lang.index.MvModuleIndex

fun processFieldLookupResolveVariants(
fieldLookup: MvMethodOrField,
receiverTy: TyAdt,
receiverItem: MvStructOrEnumItemElement,
msl: Boolean,
originalProcessor: RsResolveProcessorBase<FieldResolveVariant>
): Boolean {
val receiverItem = receiverTy.item
if (!isFieldsAccessible(fieldLookup, receiverItem, msl)) return false

val processor = originalProcessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,22 @@ class TypeInferenceWalker(
is MvConst -> item.type?.loweredType(msl) ?: TyUnknown
is MvGlobalVariableStmt -> item.type?.loweredType(true) ?: TyUnknown
is MvNamedFieldDecl -> item.type?.loweredType(msl) ?: TyUnknown
is MvStruct -> {
is MvStruct, is MvEnum -> {
if (project.moveSettings.enableIndexExpr && pathExpr.parent is MvIndexExpr) {
TyLowering().lowerPath(pathExpr.path, item, ctx.msl)
} else {
// invalid statements
TyUnknown
}
}
is MvEnumVariant -> TyLowering().lowerPath(pathExpr.path, item, ctx.msl)
is MvEnumVariant -> {
// MyEnum<u8>::MyVariant
// ^ we need this path to be able to handle explicit type parameters
val enumItemPath = pathExpr.path.qualifier ?: pathExpr.path
val baseTy = instantiatePath<TyAdt>(enumItemPath, item.enumItem)
?: return TyUnknown
baseTy
}
is MvModule -> TyUnknown
else -> debugErrorOrFallback(
"Referenced item ${item.elementType} " +
Expand Down Expand Up @@ -458,7 +465,7 @@ class TypeInferenceWalker(

val field =
resolveSingleResolveVariant(fieldLookup.referenceName) {
processFieldLookupResolveVariants(fieldLookup, tyAdt, msl, it)
processFieldLookupResolveVariants(fieldLookup, tyAdt.item, msl, it)
} as? MvFieldDecl
ctx.resolvedFields[fieldLookup] = field

Expand Down Expand Up @@ -552,11 +559,12 @@ class TypeInferenceWalker(
@Suppress("UNCHECKED_CAST")
val explicitPathTy = TyLowering().lowerPath(methodOrPath, genericItem, msl) as? T
?: return null
val typeParamToVarSubst = genericItem.tyVarsSubst

val tyVarsSubst = genericItem.tyVarsSubst
@Suppress("UNCHECKED_CAST")
// TyTypeParameter -> TyVar for every TypeParameter which is not explicit set
return explicitPathTy.substitute(tyVarsSubst) as T
@Suppress("UNCHECKED_CAST")
val explicitPathTyWithTyVars = explicitPathTy.substitute(typeParamToVarSubst) as T
return explicitPathTyWithTyVars
}

@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -714,8 +722,6 @@ class TypeInferenceWalker(
val genericItem = if (item is MvEnumVariant) item.enumItem else (item as MvStruct)
val (tyAdt, tyVarsSubst) =
instantiateMethodOrPath<TyAdt>(path, genericItem) ?: return TyUnknown
// val tyAdt = instantiatePath<TyAdt>(path, genericItem) ?: return TyUnknown
// val tyVarsSubst = genericItem.typeParamsToTyVarsSubst

expectedType?.let { expectedTy ->
val expectedTyTypeParams = expectedTy.typeParameterValues
Expand Down Expand Up @@ -811,7 +817,7 @@ class TypeInferenceWalker(
return TyUnknown
}

val derefTy = receiverTy.derefIfNeeded()
val derefTy = this.resolveTypeVarsIfPossible(receiverTy).derefIfNeeded()
return when {
derefTy is TyVector -> {
// argExpr can be either TyInteger or TyRange
Expand All @@ -829,7 +835,10 @@ class TypeInferenceWalker(
receiverTy
}
else -> {
ctx.reportTypeError(TypeError.IndexingIsNotAllowed(indexExpr.receiverExpr, receiverTy))
if (receiverTy !is TyUnknown) {
// unresolved item
ctx.reportTypeError(TypeError.IndexingIsNotAllowed(indexExpr.receiverExpr, receiverTy))
}
TyUnknown
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/org/move/lang/core/types/ty/TyAdt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ data class TyAdt(
val typeParameters = struct.typeParamsToTypeParamsSubst
return TyAdt(
struct,
// CompletionUtil.getOriginalOrSelf(struct),
typeParameters,
struct.tyTypeParams
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class TyTypeParameter private constructor(
companion object {
fun named(parameter: MvTypeParameter): TyTypeParameter {
// Treat the same parameters from original/copy files as equals
// val originalParameter = parameter
val originalParameter = CompletionUtil.getOriginalOrSelf(parameter)
return TyTypeParameter(originalParameter)
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/kotlin/org/move/lang/utils/Diagnostic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import com.intellij.openapi.util.text.StringUtil.pluralize
import com.intellij.psi.PsiElement
import org.move.ide.annotator.MvAnnotationHolder
import org.move.ide.annotator.fixes.ItemSpecSignatureFix
import org.move.ide.annotator.fixes.WrapWithParensExprFix
import org.move.ide.annotator.pluralise
import org.move.ide.inspections.fixes.EnableMoveV2Fix
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
Expand Down Expand Up @@ -144,26 +142,6 @@ sealed class Diagnostic(
}
}

class ParensAreRequiredForCastExpr(castExpr: MvCastExpr): Diagnostic(castExpr) {
override fun prepare(): PreparedAnnotation {
val castExpr = element as MvCastExpr
return PreparedAnnotation(
ERROR,
"Parentheses are required for the cast expr",
fixes = listOf(WrapWithParensExprFix(castExpr))
)
}
}

// class NativeStructNotSupported(struct: MvStruct, errorRange: TextRange): Diagnostic(struct, errorRange) {
// override fun prepare(): PreparedAnnotation {
// return PreparedAnnotation(
// ERROR,
// "Native structs aren't supported by the Move VM anymore"
// )
// }
// }

class SpecFunctionRequiresReturnType(specFunction: MvSpecFunction):
Diagnostic(
specFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MvExternalLinterPassTest: WithAptosCliTestBase(
"""
module 0x1::main {
fun main() {
1 + <error descr="$APTOS_TEST_MESSAGE">/*caret*/true</error>
1 + <error descr="$APTOS_TEST_MESSAGE">/*caret*/true</error>;
}
}
""", externalLinter = ExternalLinter.LINTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ class HighlightingAnnotatorTest: AnnotatorTestCase(HighlightingAnnotator::class)
"""
)

@MoveV2(false)
fun `test do not highlight methods if compiler v1`() = checkHighlighting(
"""
module 0x1::m {
Expand Down
Loading

0 comments on commit 3d27474

Please sign in to comment.