[FIR] Implement ILLEGAL_SELECTOR

This commit is contained in:
Ivan Kochurkin
2021-06-25 16:03:25 +03:00
parent a7276b25ae
commit 9f7a8c3948
16 changed files with 98 additions and 57 deletions
@@ -106,6 +106,7 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
parameter<String>("functionName")
parameter<Boolean>("hasValueParameters")
}
val ILLEGAL_SELECTOR by error<PsiElement>()
}
val SUPER by object : DiagnosticGroup("Super") {
@@ -120,6 +120,7 @@ object FirErrors {
// Call resolution
val CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS by error0<KtExpression>()
val FUNCTION_CALL_EXPECTED by error2<PsiElement, String, Boolean>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
val ILLEGAL_SELECTOR by error0<PsiElement>()
// Super
val SUPER_IS_NOT_AN_EXPRESSION by error0<PsiElement>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
@@ -579,8 +579,9 @@ fun checkTypeMismatch(
}
internal fun checkCondition(condition: FirExpression, context: CheckerContext, reporter: DiagnosticReporter) {
val coneType = condition.typeRef.coneType.lowerBoundIfFlexible()
if (coneType !is ConeKotlinErrorType &&
val coneType = condition.typeRef.coneTypeSafe<ConeKotlinType>()?.lowerBoundIfFlexible()
if (coneType != null &&
coneType !is ConeKotlinErrorType &&
!coneType.isSubtypeOf(context.session.typeContext, context.session.builtinTypes.booleanType.type)
) {
reporter.reportOn(condition.source, FirErrors.CONDITION_TYPE_MISMATCH, coneType, context)
@@ -163,6 +163,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.GETTER_VISIBILITY
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.HAS_NEXT_FUNCTION_AMBIGUITY
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ILLEGAL_CONST_EXPRESSION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ILLEGAL_KOTLIN_VERSION_STRING_VALUE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ILLEGAL_SELECTOR
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ILLEGAL_UNDERSCORE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INAPPLICABLE_CANDIDATE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INAPPLICABLE_FILE_TARGET
@@ -446,6 +447,7 @@ class FirDefaultErrorMessages {
)
map.put(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS, "Cannot create an instance of an abstract class")
map.put(FUNCTION_CALL_EXPECTED, "Function invocation ''{0}({1})'' expected", TO_STRING, FUNCTION_PARAMETERS)
map.put(ILLEGAL_SELECTOR, "The expression cannot be a selector (occur after a dot)")
// Supertypes
map.put(ENUM_AS_SUPERTYPE, "Enum as supertype")
@@ -360,6 +360,7 @@ private fun ConeSimpleDiagnostic.getFactory(source: FirSourceElement): FirDiagno
DiagnosticKind.IllegalEscape -> FirErrors.ILLEGAL_ESCAPE
DiagnosticKind.RecursiveTypealiasExpansion -> FirErrors.RECURSIVE_TYPEALIAS_EXPANSION
DiagnosticKind.LoopInSupertype -> FirErrors.CYCLIC_INHERITANCE_HIERARCHY
DiagnosticKind.IllegalSelector -> FirErrors.ILLEGAL_SELECTOR
DiagnosticKind.UnresolvedSupertype,
DiagnosticKind.UnresolvedExpandedType,
DiagnosticKind.Other -> FirErrors.OTHER_ERROR
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.lightTree.converter
import com.intellij.lang.LighterASTNode
import com.intellij.psi.TokenType
import com.intellij.util.diff.FlyweightCapableTreeStructure
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
@@ -41,6 +42,7 @@ import org.jetbrains.kotlin.fir.types.FirTypeProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtNameReferenceExpressionElementType
import org.jetbrains.kotlin.types.ConstantValueKind
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -196,7 +198,7 @@ class ExpressionsConverter(
buildSingleExpressionBlock(buildErrorExpression(null, ConeSimpleDiagnostic("Lambda has no body", DiagnosticKind.Syntax)))
}
context.firFunctionTargets.removeLast()
}.also {
}.also {
target.bind(it)
}
return buildAnonymousFunctionExpression {
@@ -306,7 +308,7 @@ class ExpressionsConverter(
else -> if (it.isExpression()) leftArgAsFir = getAsFirExpression(it, "No left operand")
}
}
return buildTypeOperatorCall {
source = binaryExpression.toFirSourceElement()
operation = operationTokenName.toFirOperation()
@@ -478,7 +480,7 @@ class ExpressionsConverter(
private fun convertQualifiedExpression(dotQualifiedExpression: LighterASTNode): FirExpression {
var isSelector = false
var isSafe = false
var firSelector: FirExpression = buildErrorExpression(null, ConeSimpleDiagnostic("Qualified expression without selector", DiagnosticKind.Syntax)) //after dot
var firSelector: FirExpression? = null
var firReceiver: FirExpression? = null //before dot
dotQualifiedExpression.forEachChildren {
when (it.tokenType) {
@@ -488,10 +490,26 @@ class ExpressionsConverter(
isSelector = true
}
else -> {
if (isSelector && it.tokenType != TokenType.ERROR_ELEMENT)
firSelector = getAsFirExpression(it, "Incorrect selector expression")
else
firReceiver = getAsFirExpression(it, "Incorrect receiver expression")
val isEffectiveSelector = isSelector && it.tokenType != TokenType.ERROR_ELEMENT
val firExpression =
getAsFirExpression<FirExpression>(it, "Incorrect ${if (isEffectiveSelector) "selector" else "receiver"} expression")
if (isEffectiveSelector) {
firSelector =
if (it.tokenType is KtNameReferenceExpressionElementType || it.tokenType == KtNodeTypes.CALL_EXPRESSION) {
firExpression
} else {
buildErrorExpression {
source = it.toFirSourceElement()
diagnostic = ConeSimpleDiagnostic(
"The expression cannot be a selector (occur after a dot)",
DiagnosticKind.IllegalSelector
)
expression = firExpression
}
}
} else {
firReceiver = firExpression
}
}
}
}
@@ -511,7 +529,11 @@ class ExpressionsConverter(
@OptIn(FirImplementationDetail::class)
it.replaceSource(dotQualifiedExpression.toFirSourceElement())
}
return firSelector
return firSelector ?: buildErrorExpression(
null,
ConeSimpleDiagnostic("Qualified expression without selector", DiagnosticKind.Syntax)
)
}
/**
@@ -687,7 +709,7 @@ class ExpressionsConverter(
buildWhenBranch {
source = entrySource
condition = firCondition
result = branch
result = branch
}
} else {
val firCondition = entry.toFirWhenConditionWithoutSubject()
@@ -33,10 +33,7 @@ import org.jetbrains.kotlin.fir.scopes.FirScopeProvider
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.*
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.fir.types.impl.FirQualifierPartImpl
import org.jetbrains.kotlin.fir.types.impl.FirTypeArgumentListImpl
import org.jetbrains.kotlin.fir.types.impl.FirTypePlaceholderProjection
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.Name
@@ -201,11 +198,37 @@ open class RawFirBuilder(
private fun KtExpression?.toFirExpression(
errorReason: String,
kind: DiagnosticKind = DiagnosticKind.ExpressionExpected,
): FirExpression =
if (stubMode) buildExpressionStub()
else convertSafe() ?: buildErrorExpression(
this?.toFirSourceElement(), ConeSimpleDiagnostic(errorReason, kind),
)
): FirExpression {
if (stubMode) {
return buildExpressionStub()
} else {
val result = this.convertSafe<FirExpression>()
if (result != null) {
if (this != null &&
this !is KtNameReferenceExpression &&
this !is KtCallExpression &&
this !is KtConstantExpression &&
getQualifiedExpressionForSelector() != null
) {
return buildErrorExpression {
source = toFirSourceElement()
diagnostic =
ConeSimpleDiagnostic(
"The expression cannot be a selector (occur after a dot)",
DiagnosticKind.IllegalSelector
)
expression = result
}
}
return result
}
return buildErrorExpression(
this?.toFirSourceElement(), ConeSimpleDiagnostic(errorReason, kind),
)
}
}
private inline fun KtExpression.toFirStatement(errorReasonLazy: () -> String): FirStatement =
convertSafe() ?: buildErrorExpression(this.toFirSourceElement(), ConeSimpleDiagnostic(errorReasonLazy(), DiagnosticKind.Syntax))
@@ -22,6 +22,7 @@ enum class DiagnosticKind {
UnresolvedLabel,
NoThis,
IllegalConstExpression,
IllegalSelector,
IllegalUnderscore,
DeserializationError,
InferenceError,
@@ -70,9 +70,9 @@ fun main1() {
1.<!UNRESOLVED_REFERENCE!>"sdf"<!>()
1."sdf"
1.{}
1.<!INVALID_IF_AS_EXPRESSION!>if<!> (true) {}
1.<!ILLEGAL_SELECTOR!>"sdf"<!>
1.<!ILLEGAL_SELECTOR!>{}<!>
1.<!ILLEGAL_SELECTOR!><!INVALID_IF_AS_EXPRESSION!>if<!> (true) {}<!>
}
fun test() {
@@ -1,5 +1,5 @@
val receiver = { Int.(<!SYNTAX!><!>) <!SYNTAX!>-><!> }
val receiverWithParameter = { Int.(<!UNRESOLVED_REFERENCE!>a<!>) <!SYNTAX!>-><!> }
val receiverWithParameter = { Int.<!ILLEGAL_SELECTOR!>(<!UNRESOLVED_REFERENCE!>a<!>)<!> <!SYNTAX!>-><!> }
val receiverAndReturnType = { Int.(<!SYNTAX!><!>)<!SYNTAX!>: Int -> 5<!> }
val receiverAndReturnTypeWithParameter = { Int.(<!UNRESOLVED_REFERENCE!>a<!><!SYNTAX!><!SYNTAX!><!>: Int): Int -> 5<!> }
@@ -1,29 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun foo(x: Any) {
x.<!SYNTAX!><!>
val foo = 1
x.<!SYNTAX!><!>
fun bar() = 2
x.
fun String.() = 3
var a = 24.<!SYNTAX!><!>
var b = 42.0
}
class A {
val z = "a".<!SYNTAX!><!>
val x = 4
val y = "b".<!SYNTAX!><!>
fun baz() = 5
val q = "c".
fun String.() = 6
var a = 24.<!SYNTAX!><!>
var b = 42.0
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun foo(x: Any) {
@@ -1,8 +1,8 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION
fun test() {
"a"."b"::<!UNRESOLVED_REFERENCE!>foo<!>
"a"."b"::class
"a"."b"."c"::<!UNRESOLVED_REFERENCE!>foo<!>
"a"."b"."c"::class
"a".<!ILLEGAL_SELECTOR!>"b"<!>::<!UNRESOLVED_REFERENCE!>foo<!>
"a".<!ILLEGAL_SELECTOR!>"b"<!>::class
"a"."b".<!ILLEGAL_SELECTOR!>"c"<!>::<!UNRESOLVED_REFERENCE!>foo<!>
"a"."b".<!ILLEGAL_SELECTOR!>"c"<!>::class
}
@@ -291,6 +291,12 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.ILLEGAL_SELECTOR) { firDiagnostic ->
IllegalSelectorImpl(
firDiagnostic as FirPsiDiagnostic,
token,
)
}
add(FirErrors.SUPER_IS_NOT_AN_EXPRESSION) { firDiagnostic ->
SuperIsNotAnExpressionImpl(
firDiagnostic as FirPsiDiagnostic,
@@ -226,6 +226,10 @@ sealed class KtFirDiagnostic<PSI: PsiElement> : KtDiagnosticWithPsi<PSI> {
abstract val hasValueParameters: Boolean
}
abstract class IllegalSelector : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = IllegalSelector::class
}
abstract class SuperIsNotAnExpression : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = SuperIsNotAnExpression::class
}
@@ -338,6 +338,13 @@ internal class FunctionCallExpectedImpl(
override val firDiagnostic: FirPsiDiagnostic by weakRef(firDiagnostic)
}
internal class IllegalSelectorImpl(
firDiagnostic: FirPsiDiagnostic,
override val token: ValidityToken,
) : KtFirDiagnostic.IllegalSelector(), KtAbstractFirDiagnostic<PsiElement> {
override val firDiagnostic: FirPsiDiagnostic by weakRef(firDiagnostic)
}
internal class SuperIsNotAnExpressionImpl(
firDiagnostic: FirPsiDiagnostic,
override val token: ValidityToken,