Skip to content

Commit

Permalink
Refactored to try mitigate JBR/Kotlin static initializer deadlock.
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
Benjamin-Dobell committed Jan 27, 2021
1 parent 1e0c80f commit 5018fdb
Show file tree
Hide file tree
Showing 38 changed files with 281 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class LuaParameterInfoHandler : ParameterInfoHandler<LuaArgs, ParameterInfoType>
o.sig.processParameters(null, o.isColonStyle) { idx, pi ->
if (idx > 0) append(", ")
if (idx == index) start = length
val ty = pi.ty ?: Ty.UNKNOWN
val ty = pi.ty ?: Primitives.UNKNOWN
append(pi.name)
append(": ")
append(ty.displayName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class AssignTypeInspection : StrictInspection() {

if (assigneeMemberType != null) {
// table<K, V> will always accept nil value assignment i.e. entry removal
val targetTy = if (assigneeCandidateOwnerTy is ITyGeneric && assigneeCandidateOwnerTy.base == Ty.TABLE) {
assigneeMemberType.union(Ty.NIL, context)
val targetTy = if (assigneeCandidateOwnerTy is ITyGeneric && assigneeCandidateOwnerTy.base == Primitives.TABLE) {
assigneeMemberType.union(Primitives.NIL, context)
} else assigneeMemberType

val processor = ProblemUtil.unionAwareProblemProcessor(assigneeOwnerType, assigneeCandidateOwnerTy, context, processProblem)
Expand Down Expand Up @@ -130,7 +130,7 @@ class AssignTypeInspection : StrictInspection() {
} else value

if (LuaSettings.instance.isNilStrict) {
variadicTy = Ty.NIL.union(variadicTy, context)
variadicTy = Primitives.NIL.union(variadicTy, context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import com.tang.intellij.lua.comment.psi.LuaDocVisitor
import com.tang.intellij.lua.comment.psi.api.LuaComment
import com.tang.intellij.lua.psi.*
import com.tang.intellij.lua.search.SearchContext
import com.tang.intellij.lua.ty.ITy
import com.tang.intellij.lua.ty.ProblemUtil
import com.tang.intellij.lua.ty.Ty
import com.tang.intellij.lua.ty.TyVarianceFlags
import com.tang.intellij.lua.ty.*

class IllegalOverrideInspection : LocalInspectionTool() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
Expand Down Expand Up @@ -74,7 +71,7 @@ class IllegalOverrideInspection : LocalInspectionTool() {
inspectMember(context, superTy, tableField, docType.getType(), docType)
}
} else {
inspectMember(context, superTy, tableField, valueExpr.guessType(context) ?: Ty.UNKNOWN, valueExpr)
inspectMember(context, superTy, tableField, valueExpr.guessType(context) ?: Primitives.UNKNOWN, valueExpr)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MatchFunctionSignatureInspection : StrictInspection() {
// Guess parent types
val context = SearchContext.get(o.project)
o.expressionList.forEach { expr ->
if (expr.guessType(context) == Ty.NIL) {
if (expr.guessType(context) == Primitives.NIL) {
// If parent type is nil add error
myHolder.registerProblem(expr, "Trying to index a nil type.")
}
Expand All @@ -58,14 +58,14 @@ class MatchFunctionSignatureInspection : StrictInspection() {

val searchContext = PsiSearchContext(o)
val prefixExpr = o.expression
var resolvedTy = prefixExpr.guessType(searchContext)?.let { Ty.resolve(it, searchContext) } ?: Ty.UNKNOWN
var resolvedTy = prefixExpr.guessType(searchContext)?.let { Ty.resolve(it, searchContext) } ?: Primitives.UNKNOWN

if (resolvedTy is TyUnion && resolvedTy.size == 2 && resolvedTy.getChildTypes().last().isAnonymous) {
resolvedTy = resolvedTy.getChildTypes().first()
}

TyUnion.each(resolvedTy) {
if (it == Ty.FUNCTION || (it.isUnknown && LuaSettings.instance.isUnknownCallable)) {
if (it == Primitives.FUNCTION || (it.isUnknown && LuaSettings.instance.isUnknownCallable)) {
return@each
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class ReturnTypeInspection : StrictInspection() {
guessSuperReturnTypes(bodyOwner, context)
} else {
bodyOwner.tagReturn?.type
} ?: TyMultipleResults(listOf(Ty.UNKNOWN), true)
} ?: TyMultipleResults(listOf(Primitives.UNKNOWN), true)

val guessedReturnTy = context.withMultipleResults {
o.exprList?.guessType(context)
} ?: Ty.VOID
} ?: Primitives.VOID

val guessedReturnTyLists = if (guessedReturnTy is TyUnion && guessedReturnTy.getChildTypes().any { it is TyMultipleResults }) {
guessedReturnTy.getChildTypes().map { toList(it) }
Expand All @@ -72,7 +72,7 @@ class ReturnTypeInspection : StrictInspection() {

for (i in 0 until guessedReturnTyList.size) {
val element = o.exprList?.getExpressionAt(i) ?: o
val targetType = abstractTys.getOrNull(i) ?: variadicAbstractType ?: Ty.VOID
val targetType = abstractTys.getOrNull(i) ?: variadicAbstractType ?: Primitives.VOID
val varianceFlags = if (element is LuaTableExpr) TyVarianceFlags.WIDEN_TABLES else 0

ProblemUtil.contravariantOf(targetType, guessedReturnTyList[i], context, varianceFlags, null, element) { problem ->
Expand Down Expand Up @@ -115,7 +115,7 @@ class ReturnTypeInspection : StrictInspection() {
} else null

for (i in 0 until abstractTys.size) {
val targetType = expectedReturnTys.getOrNull(i) ?: expectedVariadicReturnTy ?: Ty.VOID
val targetType = expectedReturnTys.getOrNull(i) ?: expectedVariadicReturnTy ?: Primitives.VOID

if (!targetType.contravariantOf(abstractTys[i], context, 0)) {
val element = statementDocTagType.typeList?.tyList?.let { it.getOrNull(i) ?: it.last() } ?: statementDocTagType
Expand Down Expand Up @@ -175,7 +175,7 @@ class ReturnTypeInspection : StrictInspection() {

private fun toList(type: ITy): List<ITy> {
return when (type) {
Ty.VOID -> emptyList()
Primitives.VOID -> emptyList()
is TyMultipleResults -> type.list
else -> listOf(type)
}
Expand Down Expand Up @@ -216,7 +216,7 @@ class ReturnTypeInspection : StrictInspection() {
returnDef?.type
}

if (type != null && type != Ty.VOID && o.textLength != 0) {
if (type != null && type != Primitives.VOID && o.textLength != 0) {
myHolder.registerProblem(o, "Return type '%s' specified but no return values found.".format(type.displayName))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import com.tang.intellij.lua.psi.LuaIndexExpr
import com.tang.intellij.lua.psi.LuaVisitor
import com.tang.intellij.lua.psi.prefixExpression
import com.tang.intellij.lua.search.PsiSearchContext
import com.tang.intellij.lua.ty.Ty
import com.tang.intellij.lua.ty.TySnippet
import com.tang.intellij.lua.ty.isGlobal
import com.tang.intellij.lua.ty.isUnknown
import com.tang.intellij.lua.ty.*

class UndeclaredMemberInspection : StrictInspection() {
override fun buildVisitor(myHolder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor =
object : LuaVisitor() {
override fun visitIndexExpr(o: LuaIndexExpr) {
val context = PsiSearchContext(o)
val prefix = o.prefixExpression.guessType(context) ?: Ty.UNKNOWN
val prefix = o.prefixExpression.guessType(context) ?: Primitives.UNKNOWN
val memberName = o.name

Ty.eachResolved(prefix, context) { prefixTy ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ fun getName(docTypeRef: LuaDocTypeRef): String {

fun resolveType(docTypeRef: LuaDocTypeRef, context: SearchContext): ITy {
if (docTypeRef.name == Constants.WORD_TABLE) {
return Ty.TABLE
return Primitives.TABLE
} else if (docTypeRef.name == Constants.WORD_SELF) {
val contextClass = LuaPsiTreeUtil.findContextClass(docTypeRef, context) as? ITyClass
return if (contextClass != null) TyClass.createSelfType(contextClass) else Ty.UNKNOWN
return if (contextClass != null) TyClass.createSelfType(contextClass) else Primitives.UNKNOWN
}

return LuaScopedTypeTree.get(docTypeRef.containingFile).findName(context, docTypeRef, docTypeRef.text)?.type ?: Ty.create(docTypeRef.text)
Expand Down Expand Up @@ -106,13 +106,13 @@ fun guessType(tagField: LuaDocTagField, context: SearchContext): ITy {
val stub = tagField.stub
if (stub != null)
return stub.valueTy
return tagField.valueType?.getType() ?: Ty.UNKNOWN
return tagField.valueType?.getType() ?: Primitives.UNKNOWN
}

fun guessParentType(tagField: LuaDocTagField, context: SearchContext): ITy {
val parent = tagField.parent
val classDef = PsiTreeUtil.findChildOfType(parent, LuaDocTagClass::class.java)
return classDef?.type ?: Ty.UNKNOWN
return classDef?.type ?: Primitives.UNKNOWN
}

fun getVisibility(tagField: LuaDocTagField): Visibility {
Expand All @@ -139,15 +139,15 @@ fun getType(tagParamDec: LuaDocTagParam): ITy {
if (substitutor != null) {
ty.substitute(substitutor)
} else ty
} ?: Ty.UNKNOWN
} ?: Primitives.UNKNOWN
}

fun getType(vararg: LuaDocTagVararg): ITy {
return vararg.ty?.getType() ?: Ty.UNKNOWN
return vararg.ty?.getType() ?: Primitives.UNKNOWN
}

fun getType(vararg: LuaDocVarargParam): ITy {
return vararg.ty?.getType() ?: Ty.UNKNOWN
return vararg.ty?.getType() ?: Primitives.UNKNOWN
}

fun getType(returnList: LuaDocReturnList): ITy {
Expand All @@ -170,7 +170,7 @@ private fun getReturnType(functionReturnType: LuaDocFunctionReturnType): ITy? {
}

fun getType(tagReturn: LuaDocTagReturn): ITy {
return tagReturn.functionReturnType?.let { getReturnType(it) } ?: Ty.VOID
return tagReturn.functionReturnType?.let { getReturnType(it) } ?: Primitives.VOID
}

/**
Expand Down Expand Up @@ -238,20 +238,20 @@ fun getType(tagType: LuaDocTagType, index: Int): ITy {
val tyList = tagType.typeList?.tyList

if (tyList == null) {
return Ty.UNKNOWN
return Primitives.UNKNOWN
}

return tyList.getOrNull(index)?.getType() ?: if (tagType.variadic != null) {
tyList.last().getType()
} else {
Ty.UNKNOWN
Primitives.UNKNOWN
}
}

fun getType(tagType: LuaDocTagType): ITy {
val list = tagType.typeList?.tyList?.map { it.getType() }
return if (list == null) {
Ty.UNKNOWN
Primitives.UNKNOWN
} else if (list.size == 1 && tagType.variadic == null) {
list.first()
} else {
Expand All @@ -263,13 +263,13 @@ fun getType(tagNot: LuaDocTagNot, index: Int): ITy {
val tyList = tagNot.typeList?.tyList

if (tyList == null) {
return Ty.VOID
return Primitives.VOID
}

return tyList.getOrNull(index)?.getType() ?: if (tagNot.variadic != null) {
tyList.last().getType()
} else {
Ty.VOID
Primitives.VOID
}
}

Expand All @@ -282,7 +282,7 @@ fun getType(tagNot: LuaDocTagNot): ITy {
list.first()
}
} else {
Ty.VOID
Primitives.VOID
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ fun getType(luaDocGeneralTy: LuaDocGeneralTy): ITy {
val typeRef = luaDocGeneralTy.typeRef

if (typeRef.id == null) {
return Ty.TABLE
return Primitives.TABLE
}

return withRecursionGuard("getType", luaDocGeneralTy) {
Expand Down Expand Up @@ -375,7 +375,7 @@ fun getType(numberLiteral: LuaDocNumberLiteralTy): ITy {
val valueString = if (numberLiteral.negative != null) "-${n}" else n.toString()
return if (n != null) {
TyPrimitiveLiteral.getTy(TyPrimitiveKind.Number, valueString)
} else Ty.UNKNOWN
} else Primitives.UNKNOWN
}

fun getType(stringLiteral: LuaDocStringLiteralTy): ITy {
Expand All @@ -391,7 +391,7 @@ fun getType(unionTy: LuaDocUnionTy): ITy {
SearchContext.withDumb(unionTy.project, null) { context ->
TyUnion.union(ty, docTy.getType(), context)
}
} ?: Ty.UNKNOWN
} ?: Primitives.UNKNOWN
}

fun getReference(see: LuaDocTagSee): PsiReference? {
Expand Down Expand Up @@ -451,7 +451,7 @@ fun guessType(f: LuaDocTableField, context: SearchContext): ITy {
stub.valueTy
} else {
f.valueType?.getType()
} ?: Ty.UNKNOWN
} ?: Primitives.UNKNOWN
}

fun isExplicitlyTyped(f: LuaDocTableField): Boolean {
Expand All @@ -475,7 +475,7 @@ fun getType(alias: LuaDocTagAlias): TyAlias {
return if (stub != null) {
return stub.type
} else {
TyAlias(alias.name, alias.genericDefList.map { TyGenericParameter(it) }.toTypedArray(), alias.ty?.getType() ?: Ty.UNKNOWN)
TyAlias(alias.name, alias.genericDefList.map { TyGenericParameter(it) }.toTypedArray(), alias.ty?.getType() ?: Primitives.UNKNOWN)
}
}

Expand All @@ -493,7 +493,7 @@ fun getVisibility(luaDocPrimitiveTableTy: LuaDocPrimitiveTableTy): Visibility {

// WARNING: LuaClassMember requires us to implement guessType() returning the *member* value type.
fun guessType(luaDocPrimitiveTableTy: LuaDocPrimitiveTableTy, context: SearchContext): ITy {
return Ty.UNKNOWN
return Primitives.UNKNOWN
}

fun guessParentType(luaDocPrimitiveTableTy: LuaDocPrimitiveTableTy, context: SearchContext): ITy {
Expand All @@ -518,7 +518,7 @@ fun getVisibility(luaDocGenericTableTy: LuaDocGenericTableTy): Visibility {

// WARNING: LuaClassMember requires us to implement guessType() returning the *member* value type.
fun guessType(luaDocGenericTableTy: LuaDocGenericTableTy, context: SearchContext): ITy {
return luaDocGenericTableTy.valueType?.getType() ?: Ty.UNKNOWN
return luaDocGenericTableTy.valueType?.getType() ?: Primitives.UNKNOWN
}

fun guessParentType(luaDocGenericTableTy: LuaDocGenericTableTy, context: SearchContext): ITy {
Expand All @@ -542,7 +542,7 @@ fun getVisibility(luaDocArrTy: LuaDocArrTy): Visibility {
}

fun guessIndexType(luaDocArrTy: LuaDocArrTy, context: SearchContext): ITy {
return Ty.NUMBER
return Primitives.NUMBER
}

// WARNING: LuaClassMember requires us to implement guessType() returning the *member* value type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class LuaCommentImpl(node: ASTNode) : ASTWrapperPsiElement(node), LuaComment {
val typeDef = tagType
return if (typeDef != null) {
if (context.supportsMultipleResults) typeDef.getType() else typeDef.getType(context.index)
} else Ty.UNKNOWN
} else Primitives.UNKNOWN
}

override fun isOverride(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun renderSignature(sb: StringBuilder, signature: IFunSignature, tyRenderer: ITy

if (params != null || varargTy != null) {
params?.forEach {
sig.add("${it.name}: ${tyRenderer.render(it.ty ?: Ty.UNKNOWN)}")
sig.add("${it.name}: ${tyRenderer.render(it.ty ?: Primitives.UNKNOWN)}")
}
varargTy?.let {
sig.add("...: ${tyRenderer.render(it)}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class LuaDocumentationProvider : AbstractDocumentationProvider(), DocumentationP

renderDefinition(sb) {
sb.append("local <b>${element.name}</b>: ")
val ty = element.guessType(SearchContext.get(element.project)) ?: Ty.UNKNOWN
val ty = element.guessType(SearchContext.get(element.project)) ?: Primitives.UNKNOWN
renderTy(sb, ty, tyRenderer)
}

Expand Down Expand Up @@ -118,7 +118,7 @@ class LuaDocumentationProvider : AbstractDocumentationProvider(), DocumentationP
return false
}

val ty = effectiveMember.guessType(context) ?: Ty.UNKNOWN
val ty = effectiveMember.guessType(context) ?: Primitives.UNKNOWN
val tyRenderer = renderer

renderDefinition(sb) {
Expand Down Expand Up @@ -238,7 +238,7 @@ class LuaDocumentationProvider : AbstractDocumentationProvider(), DocumentationP
if (docParamDef != null) {
renderDocParam(sb, docParamDef, tyRenderer, true)
} else {
val ty = infer(paramDef, SearchContext.get(paramDef.project)) ?: Ty.UNKNOWN
val ty = infer(paramDef, SearchContext.get(paramDef.project)) ?: Primitives.UNKNOWN
sb.append("<b>param</b> <code>${paramDef.name}</code> : ")
renderTy(sb, ty, tyRenderer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
if (curClass != null) {
member.name?.let { memberName ->
if (prefixMatcher.prefixMatches(memberName) && curClass.isVisibleInScope(context.project, contextTy, member.visibility)) {
var memberTy = member.guessType(context) ?: Ty.UNKNOWN
var memberTy = member.guessType(context) ?: Primitives.UNKNOWN

subsequentChildTys.forEach { childTy ->
if (!childTy.isGlobal) {
Expand Down Expand Up @@ -184,7 +184,7 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
member,
memberClass.getMemberSubstitutor(context),
memberClass,
member.guessType(context) ?: Ty.UNKNOWN,
member.guessType(context) ?: Primitives.UNKNOWN,
completionMode,
handlerProcessor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class LuaFieldLookupElement(val fieldName: String, val field: LuaClassField, val
}

val type: ITy by lazy {
ty ?: field.guessType(SearchContext.get(field.project)) ?: Ty.UNKNOWN
ty ?: field.guessType(SearchContext.get(field.project)) ?: Primitives.UNKNOWN
}

private fun lazyInit() {
Expand Down
Loading

0 comments on commit 5018fdb

Please sign in to comment.