FIR: Support DEBUG_INFO_EXPRESSION_TYPE

This commit is contained in:
Denis Zharkov
2020-04-14 11:44:37 +03:00
parent 6cd1ae9c82
commit 27b860682b
6 changed files with 133 additions and 20 deletions
@@ -0,0 +1,9 @@
fun foo(
x1: String,
x2: Collection<CharSequence>,
x3: MutableMap<out CharSequence, in MutableList<*>>
) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>x1<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.Collection<kotlin.CharSequence>")!>x2<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.MutableMap<out kotlin.CharSequence, in kotlin.collections.MutableList<*>>")!>x3<!>
}
@@ -0,0 +1,6 @@
FILE: debugExpressionType.kt
public final fun foo(x1: R|kotlin/String|, x2: R|kotlin/collections/Collection<kotlin/CharSequence>|, x3: R|kotlin/collections/MutableMap<out kotlin/CharSequence, in kotlin/collections/MutableList<*>>|): R|kotlin/Unit| {
R|<local>/x1|
R|<local>/x2|
R|<local>/x3|
}
@@ -584,6 +584,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/companionInvoke.kt");
}
@TestMetadata("debugExpressionType.kt")
public void testDebugExpressionType() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/debugExpressionType.kt");
}
@TestMetadata("debugInfoCall.kt")
public void testDebugInfoCall() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/debugInfoCall.kt");
@@ -584,6 +584,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/companionInvoke.kt");
}
@TestMetadata("debugExpressionType.kt")
public void testDebugExpressionType() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/debugExpressionType.kt");
}
@TestMetadata("debugInfoCall.kt")
public void testDebugInfoCall() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/debugInfoCall.kt");
@@ -17,6 +17,8 @@ import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration
import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirExpressionWithSmartcast
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference
import org.jetbrains.kotlin.fir.references.FirNamedReference
@@ -31,6 +33,9 @@ import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitorVoid
import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
import org.jetbrains.kotlin.name.FqNameUnsafe
@@ -124,6 +129,14 @@ abstract class AbstractFirDiagnosticsTest : AbstractFirBaseDiagnosticsTest() {
object : FirDefaultVisitorVoid() {
override fun visitElement(element: FirElement) {
if (element is FirExpression) {
result.addIfNotNull(
createExpressionTypeDiagnosticIfExpected(
element, diagnosedRangesToDiagnosticNames
)
)
}
element.acceptChildren(this)
}
@@ -139,46 +152,67 @@ abstract class AbstractFirDiagnosticsTest : AbstractFirBaseDiagnosticsTest() {
return result
}
fun createCallDiagnosticIfExpected(
fun createExpressionTypeDiagnosticIfExpected(
element: FirExpression,
diagnosedRangesToDiagnosticNames: MutableMap<IntRange, MutableSet<String>>
): FirDiagnosticWithParameters1<FirSourceElement, String>? =
DebugInfoDiagnosticFactory1.EXPRESSION_TYPE.createDebugInfoDiagnostic(element, diagnosedRangesToDiagnosticNames) {
element.typeRef.renderAsString((element as? FirExpressionWithSmartcast)?.originalType)
}
private fun FirTypeRef.renderAsString(originalTypeRef: FirTypeRef?): String {
val type = coneTypeSafe<ConeKotlinType>() ?: return "Type is unknown"
val rendered = type.renderForDebugInfo()
val originalTypeRendered = originalTypeRef?.coneTypeSafe<ConeKotlinType>()?.renderForDebugInfo() ?: return rendered
return "$rendered & $originalTypeRendered"
}
private fun createCallDiagnosticIfExpected(
element: FirElement,
reference: FirNamedReference,
diagnosedRangesToDiagnosticNames: MutableMap<IntRange, MutableSet<String>>
): FirDiagnosticWithParameters1<FirSourceElement, String>? =
DebugInfoDiagnosticFactory1.CALL.createDebugInfoDiagnostic(element, diagnosedRangesToDiagnosticNames) {
val resolvedSymbol = (reference as? FirResolvedNamedReference)?.resolvedSymbol
val fqName = resolvedSymbol?.fqNameUnsafe()
Renderers.renderCallInfo(fqName, getTypeOfCall(reference, resolvedSymbol))
}
private fun DebugInfoDiagnosticFactory1.createDebugInfoDiagnostic(
element: FirElement,
diagnosedRangesToDiagnosticNames: MutableMap<IntRange, MutableSet<String>>,
argument: () -> String,
): FirDiagnosticWithParameters1<FirSourceElement, String>? {
val resolvedSymbol = (reference as? FirResolvedNamedReference)?.resolvedSymbol
val sourceElement = element.source ?: return null
if (diagnosedRangesToDiagnosticNames[sourceElement.startOffset..sourceElement.endOffset]?.contains(this.name) != true) return null
val factory = DebugInfoDiagnosticFactory1.CALL
if (diagnosedRangesToDiagnosticNames[sourceElement.startOffset..sourceElement.endOffset]?.contains(factory.name) != true) return null
val fqName = resolvedSymbol?.fqNameUnsafe()
val argument = Renderers.renderCallInfo(fqName, getTypeOfCall(reference, resolvedSymbol))
val argumentText = argument()
return when (sourceElement) {
is FirPsiSourceElement<*> -> FirPsiDiagnosticWithParameters1(
sourceElement,
argument,
factory.severity,
argumentText,
severity,
FirDiagnosticFactory1(
factory.name,
factory.severity,
factory
name,
severity,
this
)
)
is FirLightSourceElement -> FirLightDiagnosticWithParameters1(
sourceElement,
argument,
factory.severity,
argumentText,
severity,
FirDiagnosticFactory1<FirSourceElement, PsiElement, String>(
factory.name,
factory.severity,
factory
name,
severity,
this
)
)
}
}
private fun AbstractFirBasedSymbol<*>.fqNameUnsafe(): FqNameUnsafe? = when (this) {
is FirClassLikeSymbol<*> -> classId.asSingleFqName().toUnsafe()
is FirCallableSymbol<*> -> callableId.asFqName().toUnsafe()
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir
import org.jetbrains.kotlin.fir.types.*
fun ConeKotlinType.renderForDebugInfo(): String {
val nullabilitySuffix = if (this !is ConeKotlinErrorType && this !is ConeClassErrorType) nullability.suffix else ""
return when (this) {
is ConeTypeVariableType -> "TypeVariable(${this.lookupTag.name})"
is ConeDefinitelyNotNullType -> "${original.renderForDebugInfo()}!!"
is ConeClassErrorType -> "ERROR CLASS: $reason"
is ConeCapturedType -> "CapturedType(${constructor.projection.renderForDebugInfo()})"
is ConeClassLikeType -> {
buildString {
append(lookupTag.classId.asSingleFqName().asString())
if (typeArguments.isNotEmpty()) {
append(typeArguments.joinToString(prefix = "<", postfix = ">") {
it.renderForDebugInfo()
})
}
}
}
is ConeLookupTagBasedType -> {
lookupTag.name.asString()
}
is ConeFlexibleType -> {
buildString {
append(lowerBound.renderForDebugInfo())
append("..")
append(upperBound.renderForDebugInfo())
}
}
is ConeIntersectionType -> {
intersectedTypes.joinToString(
separator = " & ",
) { it.renderForDebugInfo() }
}
is ConeStubType -> "Stub: $variable"
is ConeIntegerLiteralType -> "ILT: $value"
} + nullabilitySuffix
}
private fun ConeTypeProjection.renderForDebugInfo(): String {
return when (this) {
ConeStarProjection -> "*"
is ConeKotlinTypeProjectionIn -> "in ${type.renderForDebugInfo()}"
is ConeKotlinTypeProjectionOut -> "out ${type.renderForDebugInfo()}"
is ConeKotlinType -> renderForDebugInfo()
}
}