KT-8263: Conditional operators are not parsed correctly

This commit is contained in:
Iven Krall
2022-06-12 20:05:43 +02:00
committed by teamcity
parent 2053363def
commit ec8da2033c
37 changed files with 4847 additions and 218 deletions
@@ -235,6 +235,7 @@ public interface Errors {
DiagnosticFactoryForDeprecation1<PsiElement, CallableDescriptor> PROGRESSIONS_CHANGING_RESOLVE = DiagnosticFactoryForDeprecation1.create(LanguageFeature.ProgressionsChangingResolve);
DiagnosticFactory0<PsiElement> EXPRESSION_AFTER_TYPE_REFERENCE_WITHOUT_SPACING_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> TYPE_ARGUMENT_LIST_LIKE_EXPRESSION = DiagnosticFactory0.create(ERROR);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -629,6 +629,7 @@ public class DefaultErrorMessages {
"See https://youtrack.jetbrains.com/issue/KT-49276 for more details. " +
"Please specify a progression type of argument explicitly through explicit cast to resolve to a proper declaration", COMPACT);
MAP.put(EXPRESSION_AFTER_TYPE_REFERENCE_WITHOUT_SPACING_NOT_ALLOWED, "Expression body directly after type without a whitespace in-between the '>=' is only allowed since version 1.8");
MAP.put(TYPE_ARGUMENT_LIST_LIKE_EXPRESSION, "Type argument list like expressions are only allowed since version 1.8");
MAP.put(TOO_MANY_ARGUMENTS, "Too many arguments for {0}", FQ_NAMES_IN_TYPES);
@@ -54,6 +54,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
UnsupportedUntilRangeDeclarationChecker,
DataObjectContentChecker,
ExpressionAfterTypeParameterWithoutSpacingChecker,
TypeArgumentListLikeExpressionsChecker,
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2022 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.resolve.checkers
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.children
object TypeArgumentListLikeExpressionsChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (context.languageVersionSettings.supportsFeature(LanguageFeature.AllowTypeArgumentListLikeExpressions)) return
if (declaration.parent !is KtFile) return
val visitor = object : KtTreeVisitorVoid() {
override fun visitBinaryExpression(expression: KtBinaryExpression) {
super.visitBinaryExpression(expression)
if (wasTypeArgumentListLikeExpression(expression)) {
context.trace.report(Errors.TYPE_ARGUMENT_LIST_LIKE_EXPRESSION.on(expression.operationReference))
}
}
override fun visitUserType(type: KtUserType) {
super.visitUserType(type)
if (wasTypeArgumentListLikeExpression(type)) {
var parent = type.parent ?: return
while (parent !is KtBinaryExpression) {
parent = parent.parent ?: return
}
context.trace.report(Errors.TYPE_ARGUMENT_LIST_LIKE_EXPRESSION.on(parent.operationReference))
}
}
private fun wasTypeArgumentListLikeExpression(element: KtElement): Boolean {
return element.node.children().any { it.elementType == KtNodeTypes.TYPE_ARGUMENT_LIST_LIKE_EXPRESSION }
}
}
declaration.accept(visitor)
}
}