FIR: fix incorrect reporting of underscores, simplify code, fix test data file

This commit is contained in:
Ivan Kochurkin
2021-04-29 19:36:03 +03:00
committed by TeamCityServer
parent 9b39a8abc2
commit 7ec709cf46
2 changed files with 22 additions and 42 deletions
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.stubs.elements.KtDotQualifiedExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtDotQualifiedExpressionElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtNameReferenceExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtNameReferenceExpressionElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtParameterElementType
object FirReservedUnderscoreExpressionChecker : FirBasicExpressionChecker() { object FirReservedUnderscoreExpressionChecker : FirBasicExpressionChecker() {
override fun check(expression: FirStatement, context: CheckerContext, reporter: DiagnosticReporter) { override fun check(expression: FirStatement, context: CheckerContext, reporter: DiagnosticReporter) {
@@ -156,49 +155,30 @@ private fun reportIfUnderscore(
reportIfUnderscore(rawIdentifier, source, context, reporter, isSingleUnderscoreAllowed) reportIfUnderscore(rawIdentifier, source, context, reporter, isSingleUnderscoreAllowed)
fun reportIfAnyDescendantIsUnderscore(typeRefSource: FirSourceElement?) { val returnOrReceiverTypeRef = when (declaration) {
if (typeRefSource == null) return is FirValueParameter -> declaration.returnTypeRef.source
is FirFunction<*> -> declaration.receiverTypeRef?.source
else -> null
}
val isReport = when (typeRefSource) { val isReportUnderscoreInReturnOrReceiverTypeRef = when (returnOrReceiverTypeRef) {
is FirPsiSourceElement<*> -> { is FirPsiSourceElement<*> -> {
val psi = typeRefSource.psi val psi = returnOrReceiverTypeRef.psi
psi !is KtFunctionLiteral && psi.anyDescendantOfType<LeafPsiElement> { isUnderscore(it.text) } psi is KtTypeReference && psi.anyDescendantOfType<LeafPsiElement> { isUnderscore(it.text) }
}
is FirLightSourceElement ->
source?.treeStructure?.findFirstDescendant(typeRefSource.lighterASTNode) { node -> isUnderscore(node.toString()) } != null
else ->
false
} }
is FirLightSourceElement -> {
if (isReport) { val lighterASTNode = returnOrReceiverTypeRef.lighterASTNode
reporter.reportOn( lighterASTNode.tokenType == KtNodeTypes.TYPE_REFERENCE &&
typeRefSource, source?.treeStructure?.findFirstDescendant(lighterASTNode)
FirErrors.UNDERSCORE_USAGE_WITHOUT_BACKTICKS, { it.tokenType == KtNodeTypes.REFERENCE_EXPRESSION && isUnderscore(it.toString()) } != null
context }
) else -> {
false
} }
} }
if (declaration is FirValueParameter) { if (isReportUnderscoreInReturnOrReceiverTypeRef) {
val isReport = when (val returnTypeRefSource = declaration.returnTypeRef.source) { reporter.reportOn(returnOrReceiverTypeRef, FirErrors.UNDERSCORE_USAGE_WITHOUT_BACKTICKS, context)
is FirPsiSourceElement<*> -> {
val psi = returnTypeRefSource.psi
psi !is KtFunctionLiteral && psi !is KtParameter
}
is FirLightSourceElement -> {
val tokenType = returnTypeRefSource.lighterASTNode.tokenType
tokenType !is KtParameterElementType && tokenType != KtNodeTypes.CLASS
}
else -> {
false
}
}
if (isReport) {
reportIfAnyDescendantIsUnderscore(declaration.returnTypeRef.source)
}
} else if (declaration is FirFunction<*>) {
reportIfAnyDescendantIsUnderscore(declaration.receiverTypeRef?.source)
} }
} }
@@ -7,11 +7,11 @@ FILE: class.kt
public final [BODY_RESOLVE] fun q(): <ERROR TYPE REF: Symbol not found for C> { public final [BODY_RESOLVE] fun q(): <ERROR TYPE REF: Symbol not found for C> {
} }
private final [BODY_RESOLVE] val y: R|ERROR CLASS: Symbol not found for C| = this@R|/B|.R|/B.q|() private final [BODY_RESOLVE] val y: <ERROR TYPE REF: Symbol not found for C> = this@R|/B|.R|/B.q|()
[BODY_RESOLVE] private get(): R|ERROR CLASS: Symbol not found for C| [BODY_RESOLVE] private get(): <ERROR TYPE REF: Symbol not found for C>
public final [BODY_RESOLVE] fun foo([BODY_RESOLVE] a: <ERROR TYPE REF: Symbol not found for A>): <ERROR TYPE REF: Unresolved name: with> { public final [BODY_RESOLVE] fun foo([BODY_RESOLVE] a: <ERROR TYPE REF: Symbol not found for A>): <ERROR TYPE REF: Unresolved name: with> {
^foo <Unresolved name: with>#(R|<local>/a|, <L> = [BODY_RESOLVE] with@fun <anonymous>(): R|ERROR CLASS: Unresolved name: bar| <inline=Unknown> { ^foo <Unresolved name: with>#(R|<local>/a|, <L> = [BODY_RESOLVE] with@fun <anonymous>(): <ERROR TYPE REF: Unresolved name: bar> <inline=Unknown> {
^ <Unresolved name: bar>#(String(a), this@R|/B|.R|/B.y|) ^ <Unresolved name: bar>#(String(a), this@R|/B|.R|/B.y|)
} }
) )