From 8d7c5b375e27579880ff36fcc8e8e3e1894e32e3 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Fri, 4 Aug 2023 10:43:25 +0200 Subject: [PATCH] [FIR] Replace usages of FirExpression.typeRef with coneTypeOrNull #KT-59855 Fixed --- .../components/KtFirExpressionTypeProvider.kt | 6 +- .../FirCompileTimeConstantEvaluator.kt | 6 +- .../references/FirReferenceResolveHelper.kt | 4 +- .../fir/file/structure/FirElementsRecorder.kt | 2 +- .../StubBasedAnnotationDeserializer.kt | 18 +- .../fir/transformers/LLFirBodyLazyResolver.kt | 5 +- .../api/fir/util/firCheckResolvedUtils.kt | 31 ++- .../invalidCode/javaClassLiteral.txt | 2 +- ...qualifiedPartOfQualifiedCallUnresolved.txt | 2 +- ...iedPartOfQualifiedCallUnresolvedScript.txt | 2 +- .../resolve/problems/invalidCode.fir.txt | 2 +- .../FirJsModuleGetClassCallChecker.kt | 2 +- .../declaration/FirAnnotationChecker.kt | 16 +- .../checkers/declaration/declarationUtils.kt | 11 +- .../expression/FirNamedVarargChecker.kt | 7 +- ...irTypeParameterInQualifiedAccessChecker.kt | 16 +- .../FirUselessTypeOperationCallChecker.kt | 10 +- .../type/FirOptionalExpectationTypeChecker.kt | 2 +- .../ErrorNodeDiagnosticCollectorComponent.kt | 19 +- .../jetbrains/kotlin/fir/dump/HtmlFirDump.kt | 4 +- .../AbstractAnnotationDeserializer.kt | 16 +- .../FirEnumEntryDeserializerAccessUtil.kt | 9 +- .../deserialization/FirMemberDeserializer.kt | 5 +- .../deserialization/FirTypeDeserializer.kt | 4 +- .../kotlin/fir/backend/Fir2IrVisitor.kt | 4 +- .../backend/generators/AdapterGenerator.kt | 2 +- .../jetbrains/kotlin/fir/java/JavaUtils.kt | 4 +- .../java/deserialization/AnnotationsLoader.kt | 14 +- .../java/enhancement/SignatureEnhancement.kt | 7 +- .../kotlin/fir/java/javaAnnotationsMapping.kt | 25 +-- .../fir/plugin/FunctionBuildingContext.kt | 2 +- .../kotlin/fir/resolve/calls/FirReceivers.kt | 17 +- .../fir/resolve/transformers/PhaseUtils.kt | 4 +- .../jetbrains/kotlin/fir/types/TypeUtils.kt | 1 - .../fir/builder/AbstractRawFirBuilder.kt | 9 +- .../kotlin/fir/builder/ConversionUtils.kt | 31 +-- .../jetbrains/kotlin/fir/FirCallResolver.kt | 8 +- .../kotlin/fir/QualifiedNameResolution.kt | 15 +- .../kotlin/fir/resolve/ResolveUtils.kt | 76 ++++---- .../calls/CallableReferenceResolution.kt | 12 +- .../fir/resolve/calls/VisibilityUtils.kt | 7 +- .../tower/FirInvokeResolveTowerExtension.kt | 2 +- .../calls/tower/FirTowerResolveTask.kt | 18 +- .../fir/resolve/calls/tower/TowerLevels.kt | 3 +- .../fir/resolve/dfa/FirDataFlowAnalyzer.kt | 11 +- .../dfa/cfg/ControlFlowGraphBuilder.kt | 15 +- .../inference/FirBuilderInferenceSession.kt | 23 ++- .../fir/resolve/inference/FirCallCompleter.kt | 5 +- .../inference/PostponedArgumentsAnalyzer.kt | 20 +- ...rCallCompletionResultsWriterTransformer.kt | 98 ++++------ .../FirSpecificTypeResolverTransformer.kt | 9 +- .../transformers/FirSyntheticCallGenerator.kt | 4 +- .../FirWhenExhaustivenessTransformer.kt | 13 +- ...eralAndOperatorApproximationTransformer.kt | 13 +- .../body/resolve/BodyResolveUtils.kt | 26 +-- .../body/resolve/FirArrayOfCallTransformer.kt | 2 +- ...ControlFlowStatementsResolveTransformer.kt | 21 ++- .../FirDeclarationsResolveTransformer.kt | 64 +++---- .../FirExpressionsResolveTransformer.kt | 176 ++++++++---------- ...actContractResolveTransformerDispatcher.kt | 2 +- ...FirSpecificAnnotationResolveTransformer.kt | 5 +- .../kotlin/fir/FirLookupTrackerComponent.kt | 30 +-- .../kotlin/fir/resolve/dfa/cfg/CFGNode.kt | 12 +- .../jetbrains/kotlin/fir/EnumClassUtils.kt | 4 +- .../builder/FirConstExpressionBuilder.kt | 28 +-- .../org/jetbrains/kotlin/KtSourceElement.kt | 5 - .../diagnostics/tests/EnumEntryAsType.fir.kt | 2 +- .../testData/diagnostics/tests/kt49438.fir.kt | 2 +- .../tests/resolve/HiddenDeclarations.fir.kt | 2 +- .../variables/reassignedDependency.fir.kt | 9 + .../variables/reassignedDependency.kt | 9 + .../typeVariableShouldNotBeFixed.fir.kt | 2 +- .../compiler/visualizer/FirVisualizer.kt | 16 +- ...rAssignmentPluginAssignAltererExtension.kt | 4 +- .../MembersOfSerializerGenerator.kt | 2 +- .../FirScriptConfigurationExtensionImpl.kt | 7 +- 76 files changed, 538 insertions(+), 565 deletions(-) diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirExpressionTypeProvider.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirExpressionTypeProvider.kt index be30277451b..50cec24e56d 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirExpressionTypeProvider.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirExpressionTypeProvider.kt @@ -72,7 +72,7 @@ internal class KtFirExpressionTypeProvider( // For unresolved `super`, we manually create an intersection type so that IDE features like completion can work correctly. val containingClass = (fir.dispatchReceiver as? FirThisReceiverExpression)?.calleeReference?.boundSymbol as? FirClassSymbol<*> - if (fir.calleeReference is FirSuperReference && fir.typeRef is FirErrorTypeRef && containingClass != null) { + if (fir.calleeReference is FirSuperReference && fir.coneTypeOrNull is ConeErrorType && containingClass != null) { val superTypes = containingClass.resolvedSuperTypes when (superTypes.size) { 0 -> analysisSession.builtinTypes.ANY @@ -214,9 +214,7 @@ internal class KtFirExpressionTypeProvider( private fun getExpectedTypeByTypeCast(expression: PsiElement): KtType? { val typeCastExpression = expression.unwrapQualified { castExpr, expr -> castExpr.left == expr } ?: return null - with(analysisSession) { - return typeCastExpression.right?.getKtType() - } + return getKtExpressionType(typeCastExpression) } private fun getExpectedTypeOfFunctionParameter(expression: PsiElement): KtType? { diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/evaluate/FirCompileTimeConstantEvaluator.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/evaluate/FirCompileTimeConstantEvaluator.kt index 2d14d735777..03674125ade 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/evaluate/FirCompileTimeConstantEvaluator.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/evaluate/FirCompileTimeConstantEvaluator.kt @@ -178,13 +178,13 @@ internal object FirCompileTimeConstantEvaluator { val opr1 = evaluate(functionCall.explicitReceiver, mode) ?: return null opr1.evaluate(function)?.let { - return it.adjustType(functionCall.type) + return it.adjustType(functionCall.coneTypeOrNull) } val argument = functionCall.arguments.firstOrNull() ?: return null val opr2 = evaluate(argument, mode) ?: return null opr1.evaluate(function, opr2)?.let { - return it.adjustType(functionCall.type) + return it.adjustType(functionCall.coneTypeOrNull) } return null } @@ -207,7 +207,7 @@ internal object FirCompileTimeConstantEvaluator { } // Lastly, we should preserve the resolved type of the original function call. return expression.apply { - replaceType(expectedType) + replaceConeTypeOrNull(expectedType) } } diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/FirReferenceResolveHelper.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/FirReferenceResolveHelper.kt index f3cd8da1713..868c013da8a 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/FirReferenceResolveHelper.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/FirReferenceResolveHelper.kt @@ -422,7 +422,7 @@ internal object FirReferenceResolveHelper { // accessing the `super` property on `this`, hence this weird looking if condition. In addition, the current class type is available // from the dispatch receiver `this`. if (expression is KtLabelReferenceExpression && fir is FirPropertyAccessExpression && fir.calleeReference is FirSuperReference) { - return listOfNotNull(fir.dispatchReceiver.type?.toTargetSymbol(session, symbolBuilder)) + return listOfNotNull(fir.dispatchReceiver.coneTypeOrNull?.toTargetSymbol(session, symbolBuilder)) } val receiverOrImplicitInvoke = if (fir is FirImplicitInvokeCall) { fir.explicitReceiver?.unwrapSmartcastExpression() @@ -733,7 +733,7 @@ internal object FirReferenceResolveHelper { session: FirSession, symbolBuilder: KtSymbolByFirBuilder ): Collection { - val type = fir.type ?: return emptyList() + val type = fir.coneTypeOrNull ?: return emptyList() return listOfNotNull(type.toTargetSymbol(session, symbolBuilder)) } diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/structure/FirElementsRecorder.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/structure/FirElementsRecorder.kt index 9acd0799baf..7ffbed2e83a 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/structure/FirElementsRecorder.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/file/structure/FirElementsRecorder.kt @@ -218,7 +218,7 @@ internal open class FirElementsRecorder : FirVisitor { buildArrayLiteral { source = KtRealPsiSourceElement(sourceElement) - typeRef = buildResolvedTypeRef { - // Not quite precise, yet doesn't require annotation resolution - type = (inferArrayValueType(value.value) ?: session.builtinTypes.anyType.type).createArrayType() - } + // Not quite precise, yet doesn't require annotation resolution + coneTypeOrNull = (inferArrayValueType(value.value) ?: session.builtinTypes.anyType.type).createArrayType() argumentList = buildArgumentList { value.value.mapTo(arguments) { resolveValue(sourceElement, it) } @@ -208,7 +204,7 @@ class StubBasedAnnotationDeserializer( kind, value, setType = true - ).apply { this.replaceTypeRef(typeRef) } + ).apply { this.replaceConeTypeOrNull(typeRef.type) } } private fun PsiElement.toEnumEntryReferenceExpression(classId: ClassId, entryName: Name): FirExpression { @@ -228,7 +224,7 @@ class StubBasedAnnotationDeserializer( name = entryName } if (enumEntrySymbol != null) { - typeRef = enumEntrySymbol.returnTypeRef + coneTypeOrNull = enumEntrySymbol.returnTypeRef.coneTypeOrNull } } } diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirBodyLazyResolver.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirBodyLazyResolver.kt index 361ff212d37..e289d34b7e1 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirBodyLazyResolver.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirBodyLazyResolver.kt @@ -43,7 +43,6 @@ import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirBodyResolve import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector import org.jetbrains.kotlin.fir.resolve.transformers.contracts.FirContractsDslNames import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase -import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForClass import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType @@ -399,7 +398,7 @@ private val FirFunction.isCertainlyResolved: Boolean } val body = this.body ?: return false // Not completely sure - return body !is FirLazyBlock && body.typeRef is FirResolvedTypeRef + return body !is FirLazyBlock && body.coneTypeOrNull != null } private val FirVariable.initializerIfUnresolved: FirExpression? @@ -472,4 +471,4 @@ private fun requireSameSize(old: List, new: List) { private class LLFirCodeFragmentContext( override val towerDataContext: FirTowerDataContext, override val variables: Map, Set> -) : FirCodeFragmentContext \ No newline at end of file +) : FirCodeFragmentContext diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/util/firCheckResolvedUtils.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/util/firCheckResolvedUtils.kt index d870a9b7640..299cbd0dbe4 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/util/firCheckResolvedUtils.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/util/firCheckResolvedUtils.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirResolvable import org.jetbrains.kotlin.fir.expressions.impl.FirResolvedArgumentList import org.jetbrains.kotlin.fir.references.* +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef @@ -49,6 +50,26 @@ internal inline fun checkTypeRefIsResolved( } } +internal inline fun checkExpressionTypeIsResolved( + type: ConeKotlinType?, + typeName: String, + owner: FirElementWithResolveState, + extraAttachment: ExceptionAttachmentBuilder.() -> Unit = {}, +) { + checkWithAttachment( + condition = type != null, + message = { + buildString { + append("Expected resolved expression type") + append(" for $typeName of ${owner::class.simpleName}(${(owner as? FirDeclaration)?.origin})") + } + } + ) { + withFirEntry("firDeclaration", owner) + extraAttachment() + } +} + internal fun checkAnnotationTypeIsResolved(annotationContainer: T) where T : FirAnnotationContainer, T : FirElementWithResolveState { annotationContainer.annotations.forEach { annotation -> checkTypeRefIsResolved(annotation.annotationTypeRef, "annotation type", owner = annotationContainer) { @@ -59,7 +80,7 @@ internal fun checkAnnotationTypeIsResolved(annotationContainer: T) where T : internal fun checkBodyIsResolved(function: FirFunction) { val block = function.body ?: return - checkTypeRefIsResolved(block.typeRef, "block type", function) { + checkExpressionTypeIsResolved(block.coneTypeOrNull, "block type", function) { withFirEntry("block", block) } } @@ -67,7 +88,7 @@ internal fun checkBodyIsResolved(function: FirFunction) { internal fun checkStatementsAreResolved(script: FirScript) { for (statement in script.statements) { if (statement.isScriptStatement && statement is FirExpression) { - checkTypeRefIsResolved(statement.typeRef, "script statement", script) { + checkExpressionTypeIsResolved(statement.coneTypeOrNull, "script statement", script) { withFirEntry("expression", statement) } } @@ -114,14 +135,14 @@ internal fun checkReferenceIsResolved( internal fun checkInitializerIsResolved(variable: FirVariable) { val initializer = variable.initializer ?: return - checkTypeRefIsResolved(initializer.typeRef, "initializer type", variable) { + checkExpressionTypeIsResolved(initializer.coneTypeOrNull, "initializer type", variable) { withFirEntry("initializer", initializer) } } internal fun checkDefaultValueIsResolved(parameter: FirValueParameter) { val defaultValue = parameter.defaultValue ?: return - checkTypeRefIsResolved(defaultValue.typeRef, "default value type", parameter) { + checkExpressionTypeIsResolved(defaultValue.coneTypeOrNull, "default value type", parameter) { withFirEntry("defaultValue", defaultValue) } } @@ -194,7 +215,7 @@ internal fun checkAnnotationArgumentsMappingIsResolved( } for (argument in annotation.argumentMapping.mapping.values) { - checkTypeRefIsResolved(argument.typeRef, "annotation argument", annotationContainer) { + checkExpressionTypeIsResolved(argument.coneTypeOrNull, "annotation argument", annotationContainer) { withFirEntry("firAnnotation", annotation) withFirEntry("firArgument", argument) } diff --git a/analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/javaClassLiteral.txt b/analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/javaClassLiteral.txt index 912381cf3eb..f035a422299 100644 --- a/analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/javaClassLiteral.txt +++ b/analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/javaClassLiteral.txt @@ -9,7 +9,7 @@ FIR FILE: FILE: [ResolvedTo(IMPORTS)] javaClassLiteral.kt public final [ResolvedTo(BODY_RESOLVE)] fun main([ResolvedTo(BODY_RESOLVE)] args: R|kotlin/Array|): R|kotlin/Unit| { [ResolvedTo(BODY_RESOLVE)] lval anyClass: R|kotlin/Any| = R|kotlin/Any.Any|() - R|/funOne|(ERROR_EXPR(Incorrect selector expression)R|/anyClass|) + R|/funOne|(ERROR_EXPR(Incorrect selector expression)R|/anyClass|) } public final [ResolvedTo(CONTRACTS)] fun funOne([ResolvedTo(CONTRACTS)] x: R|kotlin/Any|): R|kotlin/Unit| { } diff --git a/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolved.txt b/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolved.txt index 7bc48189864..cbad6bd8455 100644 --- a/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolved.txt +++ b/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolved.txt @@ -14,7 +14,7 @@ FILE: [ResolvedTo(IMPORTS)] qualifiedPartOfQualifiedCallUnresolved.kt public final [ResolvedTo(BODY_RESOLVE)] fun handleLeftBracketInFragment(): R|kotlin/Unit| { { - [ResolvedTo(BODY_RESOLVE)] lval : = #() + [ResolvedTo(BODY_RESOLVE)] lval : R|ERROR CLASS: Unresolved name: peek| = #() [ResolvedTo(BODY_RESOLVE)] lval : = R|/|.# R|/|.# = R|/|.#() R|/| diff --git a/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolvedScript.txt b/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolvedScript.txt index 85f10d0ade0..c4fb0c338a5 100644 --- a/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolvedScript.txt +++ b/analysis/low-level-api-fir/testdata/getOrBuildFir/qualifiedExpressions/qualifiedPartOfQualifiedCallUnresolvedScript.txt @@ -18,7 +18,7 @@ FILE: [ResolvedTo(IMPORTS)] qualifiedPartOfQualifiedCallUnresolvedScript.kts public final [ResolvedTo(BODY_RESOLVE)] fun handleLeftBracketInFragment(): R|kotlin/Unit| { { - [ResolvedTo(BODY_RESOLVE)] lval : = #() + [ResolvedTo(BODY_RESOLVE)] lval : R|ERROR CLASS: Unresolved name: peek| = #() [ResolvedTo(BODY_RESOLVE)] lval : = R|/|.# R|/|.# = R|/|.#() R|/| diff --git a/compiler/fir/analysis-tests/testData/resolve/problems/invalidCode.fir.txt b/compiler/fir/analysis-tests/testData/resolve/problems/invalidCode.fir.txt index 4e384c652a6..724016e8a07 100644 --- a/compiler/fir/analysis-tests/testData/resolve/problems/invalidCode.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/problems/invalidCode.fir.txt @@ -6,7 +6,7 @@ FILE: a.kt public abstract interface I : R|kotlin/Any| { public open fun f(): { - ^f ERROR_EXPR(The expression cannot be a selector (occur after a dot))String() + ^f ERROR_EXPR(The expression cannot be a selector (occur after a dot))String() } public final class C : R|b/I| { diff --git a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/expression/FirJsModuleGetClassCallChecker.kt b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/expression/FirJsModuleGetClassCallChecker.kt index 2fd7aa1d160..c6f7ef25462 100644 --- a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/expression/FirJsModuleGetClassCallChecker.kt +++ b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/expression/FirJsModuleGetClassCallChecker.kt @@ -17,6 +17,6 @@ import org.jetbrains.kotlin.fir.types.toSymbol object FirJsModuleGetClassCallChecker : FirGetClassCallChecker() { override fun check(expression: FirGetClassCall, context: CheckerContext, reporter: DiagnosticReporter) { val callee = expression.argument.coneTypeOrNull?.toSymbol(context.session) ?: return - checkJsModuleUsage(callee, context, reporter, expression.argument.typeRef.source ?: expression.source) + checkJsModuleUsage(callee, context, reporter, expression.source) } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationChecker.kt index ab520320446..2b5e95ce832 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationChecker.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.fromPrimaryConstructor import org.jetbrains.kotlin.fir.declarations.utils.hasBackingField -import org.jetbrains.kotlin.fir.delegatedWrapperData import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.languageVersionSettings import org.jetbrains.kotlin.fir.packageFqName @@ -79,7 +78,7 @@ object FirAnnotationChecker : FirBasicDeclarationChecker() { val receiverParameter = declaration.receiverParameter if (receiverParameter != null) { for (receiverAnnotation in receiverParameter.annotations) { - reportIfMfvc(context, reporter, receiverAnnotation, "receivers", receiverParameter.typeRef) + reportIfMfvc(context, reporter, receiverAnnotation, "receivers", receiverParameter.typeRef.coneType) } } } @@ -103,7 +102,13 @@ object FirAnnotationChecker : FirBasicDeclarationChecker() { } } - private fun reportIfMfvc(context: CheckerContext, reporter: DiagnosticReporter, annotation: FirAnnotation, hint: String, type: FirTypeRef) { + private fun reportIfMfvc( + context: CheckerContext, + reporter: DiagnosticReporter, + annotation: FirAnnotation, + hint: String, + type: ConeKotlinType, + ) { if (type.needsMultiFieldValueClassFlattening(context.session)) { reporter.reportOn(annotation.source, FirErrors.ANNOTATION_ON_ILLEGAL_MULTI_FIELD_VALUE_CLASS_TYPED_TARGET, hint, context) } @@ -120,7 +125,8 @@ object FirAnnotationChecker : FirBasicDeclarationChecker() { val (hint, type) = when (annotation.useSiteTarget) { FIELD -> "fields" to ((declaration as? FirBackingField)?.returnTypeRef ?: return) - PROPERTY_DELEGATE_FIELD -> "delegate fields" to ((declaration as? FirBackingField)?.propertySymbol?.delegate?.typeRef ?: return) + PROPERTY_DELEGATE_FIELD -> "delegate fields" to ((declaration as? FirBackingField)?.propertySymbol?.delegate?.coneTypeOrNull + ?: return) RECEIVER -> "receivers" to ((declaration as? FirCallableDeclaration)?.receiverParameter?.typeRef ?: return) FILE, PROPERTY, PROPERTY_GETTER, PROPERTY_SETTER, CONSTRUCTOR_PARAMETER, SETTER_PARAMETER, null -> when { declaration is FirProperty && !declaration.isLocal -> { @@ -141,7 +147,7 @@ object FirAnnotationChecker : FirBasicDeclarationChecker() { else -> return } } - reportIfMfvc(context, reporter, annotation, hint, type) + reportIfMfvc(context, reporter, annotation, hint, type as? ConeKotlinType ?: (type as FirTypeRef).coneType) } private fun checkAnnotationTarget( diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/declarationUtils.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/declarationUtils.kt index 3dc67b99e51..b99f16a4b42 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/declarationUtils.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/declarationUtils.kt @@ -15,11 +15,8 @@ import org.jetbrains.kotlin.fir.declarations.utils.* import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.SymbolInternals import org.jetbrains.kotlin.fir.symbols.impl.* -import org.jetbrains.kotlin.fir.types.FirTypeRef -import org.jetbrains.kotlin.fir.types.coneType +import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.FirImplicitUnitTypeRef -import org.jetbrains.kotlin.fir.types.isNullable -import org.jetbrains.kotlin.fir.types.typeContext internal fun isInsideExpectClass(containingClass: FirClass, context: CheckerContext): Boolean { return isInsideSpecificClass(containingClass, context) { klass -> klass is FirRegularClass && klass.isExpect } @@ -102,8 +99,10 @@ fun FirClassSymbol<*>.primaryConstructorSymbol(session: FirSession): FirConstruc return fir.primaryConstructorIfAny(session) } -fun FirTypeRef.needsMultiFieldValueClassFlattening(session: FirSession) = with(session.typeContext) { - coneType.typeConstructor().isMultiFieldValueClass() && !coneType.isNullable +fun FirTypeRef.needsMultiFieldValueClassFlattening(session: FirSession): Boolean = coneType.needsMultiFieldValueClassFlattening(session) + +fun ConeKotlinType.needsMultiFieldValueClassFlattening(session: FirSession) = with(session.typeContext) { + typeConstructor().isMultiFieldValueClass() && !isNullable } val FirCallableSymbol<*>.hasExplicitReturnType: Boolean diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirNamedVarargChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirNamedVarargChecker.kt index 386f1aeead5..8c64c18e9fe 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirNamedVarargChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirNamedVarargChecker.kt @@ -44,12 +44,11 @@ object FirNamedVarargChecker : FirCallChecker() { } return } - val typeRef = argument.expression.typeRef - if (typeRef is FirErrorTypeRef) return + val type = argument.expression.coneTypeOrNull + if (type is ConeErrorType) return if (argument.expression is FirArrayLiteral) return - @OptIn(UnexpandedTypeCheck::class) - if (allowAssignArray && typeRef.isArrayType) return + if (allowAssignArray && type?.isArrayType == true) return if (isAnnotation) { reporter.reportOn( diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirTypeParameterInQualifiedAccessChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirTypeParameterInQualifiedAccessChecker.kt index 921aeacdd5f..ba4bbcaf445 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirTypeParameterInQualifiedAccessChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirTypeParameterInQualifiedAccessChecker.kt @@ -5,16 +5,16 @@ package org.jetbrains.kotlin.fir.analysis.checkers.expression -import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.diagnostics.DiagnosticReporter -import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.diagnostics.reportOn +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext +import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression import org.jetbrains.kotlin.fir.expressions.FirResolvedReifiedParameterReference import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeTypeParameterInQualifiedAccess -import org.jetbrains.kotlin.fir.types.FirErrorTypeRef -import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.types.ConeErrorType +import org.jetbrains.kotlin.fir.types.ConeKotlinType object FirTypeParameterInQualifiedAccessChecker : FirQualifiedAccessExpressionChecker() { override fun check(expression: FirQualifiedAccessExpression, context: CheckerContext, reporter: DiagnosticReporter) { @@ -36,7 +36,7 @@ object FirTypeParameterInQualifiedAccessChecker : FirQualifiedAccessExpressionCh val secondLast = context.callsOrAssignments.elementAtOrNull(context.callsOrAssignments.size - 2) if (secondLast is FirQualifiedAccessExpression && secondLast.explicitReceiver == expression) return - val diagnostic = expression.typeRef.coneTypeParameterInQualifiedAccess ?: return + val diagnostic = expression.coneTypeOrNull?.coneTypeParameterInQualifiedAccess ?: return val source = expression.source ?: return reporter.reportOn(source, FirErrors.TYPE_PARAMETER_IS_NOT_AN_EXPRESSION, diagnostic.symbol, context) } @@ -49,7 +49,7 @@ object FirTypeParameterInQualifiedAccessChecker : FirQualifiedAccessExpressionCh val explicitReceiver = expression.explicitReceiver val typeParameterSymbol = (explicitReceiver as? FirResolvedReifiedParameterReference)?.symbol - ?: explicitReceiver?.typeRef?.coneTypeParameterInQualifiedAccess?.symbol + ?: explicitReceiver?.coneTypeOrNull?.coneTypeParameterInQualifiedAccess?.symbol ?: return if (expression is FirCallableReferenceAccess) { reporter.reportOn(expression.source, FirErrors.CALLABLE_REFERENCE_LHS_NOT_A_CLASS, context) @@ -58,6 +58,6 @@ object FirTypeParameterInQualifiedAccessChecker : FirQualifiedAccessExpressionCh } } - private val FirTypeRef.coneTypeParameterInQualifiedAccess: ConeTypeParameterInQualifiedAccess? - get() = (this as? FirErrorTypeRef)?.diagnostic as? ConeTypeParameterInQualifiedAccess + private val ConeKotlinType.coneTypeParameterInQualifiedAccess: ConeTypeParameterInQualifiedAccess? + get() = (this as? ConeErrorType)?.diagnostic as? ConeTypeParameterInQualifiedAccess } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUselessTypeOperationCallChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUselessTypeOperationCallChecker.kt index 7797325dcf7..eb3c68ace2c 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUselessTypeOperationCallChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUselessTypeOperationCallChecker.kt @@ -5,13 +5,15 @@ package org.jetbrains.kotlin.fir.analysis.checkers.expression -import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.diagnostics.DiagnosticReporter -import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.diagnostics.reportOn +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.isRefinementUseless import org.jetbrains.kotlin.fir.analysis.checkers.shouldCheckForExactType -import org.jetbrains.kotlin.fir.expressions.* +import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors +import org.jetbrains.kotlin.fir.expressions.FirOperation +import org.jetbrains.kotlin.fir.expressions.FirTypeOperatorCall +import org.jetbrains.kotlin.fir.expressions.argument import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.types.* @@ -39,7 +41,7 @@ object FirUselessTypeOperationCallChecker : FirTypeOperatorCallChecker() { FirOperation.IS -> reporter.reportOn(expression.source, FirErrors.USELESS_IS_CHECK, true, context) FirOperation.NOT_IS -> reporter.reportOn(expression.source, FirErrors.USELESS_IS_CHECK, false, context) FirOperation.AS, FirOperation.SAFE_AS -> { - if ((arg.typeRef as? FirResolvedTypeRef)?.isFromStubType != true) { + if (!expression.argFromStubType) { reporter.reportOn(expression.source, FirErrors.USELESS_CAST, context) } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/type/FirOptionalExpectationTypeChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/type/FirOptionalExpectationTypeChecker.kt index 754435bc4fd..36a53eff0c4 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/type/FirOptionalExpectationTypeChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/type/FirOptionalExpectationTypeChecker.kt @@ -29,7 +29,7 @@ object FirOptionalExpectationTypeChecker : FirTypeRefChecker() { } val annotationContainer = context.annotationContainers.lastOrNull() - if (annotationContainer?.annotations?.any { it.typeRef == typeRef } == true) return + if (annotationContainer?.annotations?.any { it.annotationTypeRef == typeRef } == true) return reporter.reportOn(source, FirErrors.OPTIONAL_DECLARATION_OUTSIDE_OF_ANNOTATION_ENTRY, context) } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt index b3df5d784bf..ab747e2f181 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt @@ -6,12 +6,12 @@ package org.jetbrains.kotlin.fir.analysis.collectors.components import org.jetbrains.kotlin.KtFakeSourceElementKind -import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.KtNodeTypes -import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext +import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.fakeElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.diagnostics.toFirDiagnostics import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.diagnostics.* @@ -19,8 +19,12 @@ import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.references.FirErrorNamedReference import org.jetbrains.kotlin.fir.references.FirNamedReference import org.jetbrains.kotlin.fir.references.FirResolvedErrorReference +import org.jetbrains.kotlin.fir.references.FirThisReference import org.jetbrains.kotlin.fir.resolve.diagnostics.* -import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.ConeErrorType +import org.jetbrains.kotlin.fir.types.FirErrorTypeRef +import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef +import org.jetbrains.kotlin.fir.types.renderForDebugging class ErrorNodeDiagnosticCollectorComponent( session: FirSession, @@ -85,7 +89,7 @@ class ErrorNodeDiagnosticCollectorComponent( } private fun FirExpression?.cannotBeResolved(): Boolean { - return when (val diagnostic = (this?.typeRef as? FirErrorTypeRef)?.diagnostic) { + return when (val diagnostic = (this?.coneTypeOrNull as? ConeErrorType)?.diagnostic) { is ConeUnresolvedNameError, is ConeInstanceAccessBeforeSuperCall, is ConeAmbiguousSuper -> true is ConeSimpleDiagnostic -> diagnostic.kind == DiagnosticKind.NotASupertype || diagnostic.kind == DiagnosticKind.SuperNotAvailable || @@ -130,6 +134,11 @@ class ErrorNodeDiagnosticCollectorComponent( reportFirDiagnostic(errorPrimaryConstructor.diagnostic, errorPrimaryConstructor.source, data) } + override fun visitThisReference(thisReference: FirThisReference, data: CheckerContext) { + val diagnostic = thisReference.diagnostic ?: return + reportFirDiagnostic(diagnostic, thisReference.source, data) + } + private fun reportFirDiagnostic( diagnostic: ConeDiagnostic, source: KtSourceElement?, diff --git a/compiler/fir/dump/src/org/jetbrains/kotlin/fir/dump/HtmlFirDump.kt b/compiler/fir/dump/src/org/jetbrains/kotlin/fir/dump/HtmlFirDump.kt index ba92b1a5f16..463d9e681e1 100644 --- a/compiler/fir/dump/src/org/jetbrains/kotlin/fir/dump/HtmlFirDump.kt +++ b/compiler/fir/dump/src/org/jetbrains/kotlin/fir/dump/HtmlFirDump.kt @@ -1469,7 +1469,7 @@ class HtmlFirDump internal constructor(private var linkResolver: FirLinkResolver } private fun FlowContent.generate(unitExpression: FirUnitExpression) { - generate(unitExpression.typeRef) + generate(unitExpression.coneType) } private fun FlowContent.generate(breakExpression: FirBreakExpression) { @@ -1530,7 +1530,7 @@ class HtmlFirDump internal constructor(private var linkResolver: FirLinkResolver } private fun FlowContent.generate(expression: FirExpression) { - exprType(expression.typeRef) { + exprType(expression.coneType.toFirResolvedTypeRef()) { when (expression) { is FirBlock -> generateBlockIfAny(expression) is FirGetClassCall -> generate(expression) diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractAnnotationDeserializer.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractAnnotationDeserializer.kt index f306a96d004..4f1910d2315 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractAnnotationDeserializer.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractAnnotationDeserializer.kt @@ -271,16 +271,14 @@ abstract class AbstractAnnotationDeserializer( val classId = nameResolver.getClassId(value.classId) val lookupTag = classId.toLookupTag() val referencedType = lookupTag.constructType(emptyArray(), isNullable = false) - val resolvedTypeRef = buildResolvedTypeRef { - type = StandardClassIds.KClass.constructClassLikeType(arrayOf(referencedType), false) - } + val resolvedType = StandardClassIds.KClass.constructClassLikeType(arrayOf(referencedType), false) argumentList = buildUnaryArgumentList( buildClassReferenceExpression { classTypeRef = buildResolvedTypeRef { type = referencedType } - typeRef = resolvedTypeRef + coneTypeOrNull = resolvedType } ) - typeRef = resolvedTypeRef + coneTypeOrNull = resolvedType } ENUM -> buildPropertyAccessExpression { val classId = nameResolver.getClassId(value.classId) @@ -300,7 +298,7 @@ abstract class AbstractAnnotationDeserializer( name = entryName } if (enumEntrySymbol != null) { - typeRef = enumEntrySymbol.returnTypeRef + coneTypeOrNull = enumEntrySymbol.returnTypeRef.coneTypeOrNull } } ARRAY -> { @@ -309,9 +307,7 @@ abstract class AbstractAnnotationDeserializer( argumentList = buildArgumentList { value.arrayElementList.mapTo(arguments) { resolveValue(it, nameResolver) { expectedArrayElementType } } } - typeRef = buildResolvedTypeRef { - type = expectedArrayElementType.createArrayType() - } + coneTypeOrNull = expectedArrayElementType.createArrayType() } } @@ -320,6 +316,6 @@ abstract class AbstractAnnotationDeserializer( } private fun const(kind: ConstantValueKind, value: T, typeRef: FirResolvedTypeRef): FirConstExpression { - return buildConstExpression(null, kind, value, setType = true).apply { this.replaceTypeRef(typeRef) } + return buildConstExpression(null, kind, value, setType = true).apply { this.replaceConeTypeOrNull(typeRef.coneType) } } } diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirEnumEntryDeserializerAccessUtil.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirEnumEntryDeserializerAccessUtil.kt index 6a18dfdef1d..6b5e5dfb40e 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirEnumEntryDeserializerAccessUtil.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirEnumEntryDeserializerAccessUtil.kt @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference import org.jetbrains.kotlin.fir.resolve.providers.getClassDeclaredPropertySymbols import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider import org.jetbrains.kotlin.fir.types.ConeTypeProjection -import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.toLookupTag @@ -44,9 +43,7 @@ fun FirEnumEntryDeserializedAccessExpression.toQualifiedPropertyAccessExpression } } - typeRef = buildResolvedTypeRef { - type = ConeClassLikeTypeImpl( - enumClassId.toLookupTag(), ConeTypeProjection.EMPTY_ARRAY, isNullable = false - ) - } + coneTypeOrNull = ConeClassLikeTypeImpl( + enumClassId.toLookupTag(), ConeTypeProjection.EMPTY_ARRAY, isNullable = false + ) } diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt index 867cd9999a9..e329df10cfc 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.toEffectiveVisibility import org.jetbrains.kotlin.fir.types.ConeLookupTagBasedType +import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.coneType @@ -469,7 +470,7 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { proto.contextReceiverTypes(c.typeTable).mapTo(contextReceivers, ::loadContextReceiver) }.apply { - initializer?.replaceTypeRef(returnTypeRef) + initializer?.replaceConeTypeOrNull(returnTypeRef.type) this.versionRequirements = versionRequirements replaceDeprecationsProvider(getDeprecationsProvider(c.session)) setLazyPublishedVisibility(c.session) @@ -696,6 +697,6 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { }.toList() } - private fun ProtoBuf.Type.toTypeRef(context: FirDeserializationContext): FirTypeRef = + private fun ProtoBuf.Type.toTypeRef(context: FirDeserializationContext): FirResolvedTypeRef = context.typeDeserializer.typeRef(this) } diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt index e1fcbc05eee..ec422e2c541 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt @@ -108,7 +108,7 @@ class FirTypeDeserializer( } } - fun typeRef(proto: ProtoBuf.Type): FirTypeRef { + fun typeRef(proto: ProtoBuf.Type): FirResolvedTypeRef { return buildResolvedTypeRef { annotations += annotationDeserializer.loadTypeAnnotations(proto, nameResolver) type = type(proto, annotations.computeTypeAttributes(moduleData.session, shouldExpandTypeAliases = false)) @@ -288,4 +288,4 @@ class FirTypeDeserializer( val coneType = type(type) return coneType.toTypeProjection(variance) } -} \ No newline at end of file +} diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 827b7522203..1cb659e254b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -1098,7 +1098,7 @@ class Fir2IrVisitor( source = elvisExpression.source moduleData = session.moduleData origin = FirDeclarationOrigin.Source - returnTypeRef = elvisExpression.lhs.typeRef + returnTypeRef = elvisExpression.lhs.coneType.toFirResolvedTypeRef() name = Name.special("") initializer = elvisExpression.lhs symbol = FirPropertySymbol(name) @@ -1511,7 +1511,7 @@ class Fir2IrVisitor( private fun convertToArrayLiteral(arrayLiteral: FirArrayLiteral): IrVararg { return arrayLiteral.convertWithOffsets { startOffset, endOffset -> val arrayType = arrayLiteral.coneType.toIrType() - val elementType = if (arrayLiteral.typeRef is FirResolvedTypeRef) { + val elementType = if (arrayLiteral.coneTypeOrNull != null) { arrayType.getArrayElementType(irBuiltIns) } else { createErrorType() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt index 8d187f64285..33f94d93b9a 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt @@ -551,7 +551,7 @@ internal class AdapterGenerator( private fun needSamConversion(argument: FirExpression, parameter: FirValueParameter): Boolean { // If the type of the argument is already an explicitly subtype of the type of the parameter, we don't need SAM conversion. - if (argument.typeRef !is FirResolvedTypeRef || + if (argument.coneTypeOrNull == null || AbstractTypeChecker.isSubtypeOf( session.typeContext.newTypeCheckerState( errorTypesEqualToAnything = false, stubTypesEqualToAnything = true diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt index 067b4b85b9b..fd2c76b8962 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt @@ -123,9 +123,7 @@ private fun List.createArrayLiteral(session: FirSession, kind: ConstantVa arguments += element.createConstantOrError(session) } } - typeRef = buildResolvedTypeRef { - type = kind.expectedConeType(session).createArrayType() - } + coneTypeOrNull = kind.expectedConeType(session).createArrayType() } } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt index c19919018e6..90b17dceae1 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt @@ -42,9 +42,7 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko val resolvedClassTypeRef = classId.toLookupTag().toDefaultResolvedTypeRef() return buildClassReferenceExpression { classTypeRef = resolvedClassTypeRef - typeRef = buildResolvedTypeRef { - type = StandardClassIds.KClass.constructClassLikeType(arrayOf(resolvedClassTypeRef.type), false) - } + coneTypeOrNull = StandardClassIds.KClass.constructClassLikeType(arrayOf(resolvedClassTypeRef.type), false) } } @@ -52,7 +50,7 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko visitExpression(name, buildGetClassCall { val argument = value.toFirClassReferenceExpression() argumentList = buildUnaryArgumentList(argument) - typeRef = argument.typeRef + coneTypeOrNull = argument.coneTypeOrNull }) } @@ -78,7 +76,7 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko elements.add(buildGetClassCall { val argument = value.toFirClassReferenceExpression() argumentList = buildUnaryArgumentList(argument) - typeRef = argument.typeRef + coneTypeOrNull = argument.coneTypeOrNull }) } @@ -96,11 +94,9 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko override fun visitEnd() { visitExpression(name, buildArrayLiteral { guessArrayTypeIfNeeded(name, elements)?.let { - typeRef = it + coneTypeOrNull = it.coneTypeOrNull } ?: elements.firstOrNull()?.coneType?.createOutArrayType()?.let { - typeRef = buildResolvedTypeRef { - type = it - } + coneTypeOrNull = it } argumentList = buildArgumentList { arguments += elements diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt index 81d876f7300..e301d37b6f5 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt @@ -24,8 +24,8 @@ import org.jetbrains.kotlin.fir.declarations.utils.* import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.expressions.FirConstExpression import org.jetbrains.kotlin.fir.expressions.unexpandedClassId -import org.jetbrains.kotlin.fir.java.* import org.jetbrains.kotlin.fir.java.FirJavaTypeConversionMode +import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack import org.jetbrains.kotlin.fir.java.declarations.* import org.jetbrains.kotlin.fir.java.resolveIfJavaType import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol @@ -41,7 +41,6 @@ import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef -import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry import org.jetbrains.kotlin.load.java.AnnotationQualifierApplicabilityType import org.jetbrains.kotlin.load.java.FakePureImplementationsProvider @@ -54,8 +53,8 @@ import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeParameterMarker import org.jetbrains.kotlin.types.model.TypeSystemContext -import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment import org.jetbrains.kotlin.util.PrivateForInline +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment class FirSignatureEnhancement( private val owner: FirRegularClass, @@ -363,7 +362,7 @@ class FirSignatureEnhancement( } }.apply { val newValueParameters = firMethod.valueParameters.zip(enhancedValueParameterTypes) { valueParameter, enhancedReturnType -> - valueParameter.defaultValue?.replaceTypeRef(enhancedReturnType) + valueParameter.defaultValue?.replaceConeTypeOrNull(enhancedReturnType.coneType) buildValueParameter { source = valueParameter.source diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt index 9b7d9b45306..28f7e141511 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt @@ -90,9 +90,8 @@ internal fun JavaAnnotationArgument.toFirExpression( ) is JavaArrayAnnotationArgument -> buildArrayLiteral { val argumentTypeRef = expectedTypeRef?.let { - typeRef = if (it is FirJavaTypeRef) buildResolvedTypeRef { - type = it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) - } else it + coneTypeOrNull = + if (it is FirJavaTypeRef) it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) else it.coneType buildResolvedTypeRef { type = it.coneTypeSafe()?.lowerBoundIfFlexible()?.arrayElementType() ?: ConeErrorType(ConeSimpleDiagnostic("expected type is not array type")) @@ -111,10 +110,10 @@ internal fun JavaAnnotationArgument.toFirExpression( argumentList = buildUnaryArgumentList( buildClassReferenceExpression { classTypeRef = resolvedClassTypeRef - typeRef = resolvedTypeRef + coneTypeOrNull = resolvedTypeRef.coneType } ) - typeRef = resolvedTypeRef + coneTypeOrNull = resolvedTypeRef.coneType } is JavaAnnotationAsAnnotationArgument -> getAnnotation().toFirAnnotationCall(session) else -> buildErrorExpression { @@ -168,13 +167,11 @@ private fun buildEnumCall(session: FirSession, classId: ClassId?, entryName: Nam } if (classId != null) { - this.typeRef = buildResolvedTypeRef { - type = ConeClassLikeTypeImpl( - classId.toLookupTag(), - emptyArray(), - isNullable = false - ) - } + this.coneTypeOrNull = ConeClassLikeTypeImpl( + classId.toLookupTag(), + emptyArray(), + isNullable = false + ) } } } @@ -194,9 +191,7 @@ private fun List.mapJavaTargetArguments(session: FirSess isNullable = false, ConeAttributes.Empty ) - typeRef = buildResolvedTypeRef { - type = elementConeType - } + coneTypeOrNull = elementConeType varargElementType = buildResolvedTypeRef { type = elementConeType.createOutArrayType() } diff --git a/compiler/fir/plugin-utils/src/org/jetbrains/kotlin/fir/plugin/FunctionBuildingContext.kt b/compiler/fir/plugin-utils/src/org/jetbrains/kotlin/fir/plugin/FunctionBuildingContext.kt index c97398d8216..4b08fd8d0c6 100644 --- a/compiler/fir/plugin-utils/src/org/jetbrains/kotlin/fir/plugin/FunctionBuildingContext.kt +++ b/compiler/fir/plugin-utils/src/org/jetbrains/kotlin/fir/plugin/FunctionBuildingContext.kt @@ -88,7 +88,7 @@ public sealed class FunctionBuildingContext( symbol = FirValueParameterSymbol(name) if (valueParameter.hasDefaultValue) { // TODO: check how it will actually work in fir2ir - defaultValue = buildExpressionStub { typeRef = session.builtinTypes.nothingType } + defaultValue = buildExpressionStub { coneTypeOrNull = session.builtinTypes.nothingType.type } } this.containingFunctionSymbol = containingFunctionSymbol isCrossinline = valueParameter.isCrossinline diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt index abf49f3a557..f25c351a94f 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.fakeElement import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.copyWithNewSourceKind import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.diagnostics.ConeIntermediateDiagnostic import org.jetbrains.kotlin.fir.expressions.FirCheckNotNullCall @@ -29,8 +28,11 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirScriptSymbol -import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.ConeErrorType +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef +import org.jetbrains.kotlin.fir.types.coneTypeSafe +import org.jetbrains.kotlin.fir.types.constructType import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.SmartcastStability @@ -116,12 +118,12 @@ sealed class ImplicitReceiverValue>( buildSmartCastExpression { originalExpression = originalReceiverExpression smartcastType = buildResolvedTypeRef { - source = originalReceiverExpression.typeRef.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) + source = originalReceiverExpression.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) type = this@ImplicitReceiverValue.type } - typesFromSmartCast = listOf(type) + typesFromSmartCast = listOf(this@ImplicitReceiverValue.type) smartcastStability = SmartcastStability.STABLE_VALUE - typeRef = smartcastType.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) + coneTypeOrNull = this@ImplicitReceiverValue.type } } else { originalReceiverExpression @@ -195,16 +197,15 @@ private fun receiverExpression( boundSymbol = symbol this.contextReceiverNumber = contextReceiverNumber } - val typeRef = type.toFirResolvedTypeRef() return when (inaccessibleReceiver) { false -> buildThisReceiverExpression { this.calleeReference = calleeReference - this.typeRef = typeRef + this.coneTypeOrNull = type isImplicit = true } true -> buildInaccessibleReceiverExpression { this.calleeReference = calleeReference - this.typeRef = typeRef + this.coneTypeOrNull = type } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/transformers/PhaseUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/transformers/PhaseUtils.kt index 71915802112..6623437b280 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/transformers/PhaseUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/transformers/PhaseUtils.kt @@ -15,7 +15,7 @@ import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.coneTypeSafe -fun ConeKotlinType.ensureResolvedTypeDeclaration( +fun ConeKotlinType?.ensureResolvedTypeDeclaration( useSiteSession: FirSession, requiredPhase: FirResolvePhase = FirResolvePhase.DECLARATIONS, ) { @@ -29,5 +29,5 @@ fun FirTypeRef.ensureResolvedTypeDeclaration( useSiteSession: FirSession, requiredPhase: FirResolvePhase = FirResolvePhase.DECLARATIONS, ) { - coneTypeSafe()?.ensureResolvedTypeDeclaration(useSiteSession, requiredPhase) + coneTypeSafe().ensureResolvedTypeDeclaration(useSiteSession, requiredPhase) } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index 59542a8c39b..18706a5a9ba 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -415,7 +415,6 @@ fun FirTypeRef.withReplacedConeType( type = newType annotations += this@withReplacedConeType.annotations delegatedTypeRef = this@withReplacedConeType.delegatedTypeRef - isFromStubType = this@withReplacedConeType.type is ConeStubType } } } diff --git a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt index 94b59394914..0fe46965b29 100644 --- a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt +++ b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt @@ -25,10 +25,7 @@ import org.jetbrains.kotlin.fir.references.builder.buildImplicitThisReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference import org.jetbrains.kotlin.fir.symbols.impl.* -import org.jetbrains.kotlin.fir.types.ConeClassLikeType -import org.jetbrains.kotlin.fir.types.FirErrorTypeRef -import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef -import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl @@ -1116,13 +1113,13 @@ fun FirRegularClassBuilder.createDa ) = buildPropertyAccessExpression { this.source = parameterSource - typeRef = firPropertyReturnTypeRefWithCorrectSourceKind + coneTypeOrNull = firPropertyReturnTypeRefWithCorrectSourceKind.coneTypeOrNull this.dispatchReceiver = buildThisReceiverExpression { this.source = parameterSource calleeReference = buildImplicitThisReference { boundSymbol = this@createDataClassCopyFunction.symbol } - typeRef = classTypeRefWithCorrectSourceKind + coneTypeOrNull = classTypeRefWithCorrectSourceKind.coneTypeOrNull } calleeReference = buildResolvedNamedReference { this.source = parameterSource diff --git a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/ConversionUtils.kt b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/ConversionUtils.kt index 1940bf1d222..925068573d1 100644 --- a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/ConversionUtils.kt +++ b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/ConversionUtils.kt @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.fir.contracts.FirLegacyRawContractDescription import org.jetbrains.kotlin.fir.contracts.builder.buildLegacyRawContractDescription import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin -import org.jetbrains.kotlin.fir.declarations.FirFunction import org.jetbrains.kotlin.fir.declarations.FirReceiverParameter import org.jetbrains.kotlin.fir.declarations.FirVariable import org.jetbrains.kotlin.fir.declarations.builder.* @@ -42,14 +41,16 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirPropertyAccessorSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirValueParameterSymbol import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.ConeStarProjection -import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance -import org.jetbrains.kotlin.fir.types.impl.* +import org.jetbrains.kotlin.fir.types.constructClassLikeType +import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl +import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImplWithoutSource import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.SpecialNames +import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.expressions.OperatorConventions @@ -351,9 +352,7 @@ fun FirPropertyBuilder.generateAccessorsByDelegate( calleeReference = buildImplicitThisReference { boundSymbol = ownerRegularOrAnonymousObjectSymbol } - typeRef = buildResolvedTypeRef { - type = context.dispatchReceiverTypesStack.last() - } + coneTypeOrNull = context.dispatchReceiverTypesStack.last() } else -> buildConstExpression(null, ConstantValueKind.Null, null, setType = false) } @@ -376,21 +375,27 @@ fun FirPropertyBuilder.generateAccessorsByDelegate( name = this@generateAccessorsByDelegate.name resolvedSymbol = this@generateAccessorsByDelegate.symbol } - typeRef = when { + coneTypeOrNull = when { !isMember && !isExtension -> if (isVar) { - FirImplicitKMutableProperty0TypeRef(null, ConeStarProjection) + StandardClassIds.KMutableProperty0.constructClassLikeType(arrayOf(ConeStarProjection)) } else { - FirImplicitKProperty0TypeRef(null, ConeStarProjection) + StandardClassIds.KProperty0.constructClassLikeType(arrayOf(ConeStarProjection)) } isMember && isExtension -> if (isVar) { - FirImplicitKMutableProperty2TypeRef(null, ConeStarProjection, ConeStarProjection, ConeStarProjection) + StandardClassIds.KMutableProperty2.constructClassLikeType( + arrayOf( + ConeStarProjection, + ConeStarProjection, + ConeStarProjection + ) + ) } else { - FirImplicitKProperty2TypeRef(null, ConeStarProjection, ConeStarProjection, ConeStarProjection) + StandardClassIds.KProperty2.constructClassLikeType(arrayOf(ConeStarProjection, ConeStarProjection, ConeStarProjection)) } else -> if (isVar) { - FirImplicitKMutableProperty1TypeRef(null, ConeStarProjection, ConeStarProjection) + StandardClassIds.KMutableProperty1.constructClassLikeType(arrayOf(ConeStarProjection, ConeStarProjection)) } else { - FirImplicitKProperty1TypeRef(null, ConeStarProjection, ConeStarProjection) + StandardClassIds.KProperty1.constructClassLikeType(arrayOf(ConeStarProjection, ConeStarProjection)) } } this@generateAccessorsByDelegate.typeParameters.mapTo(typeArguments) { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt index f09974f95ad..8517267fd8d 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirCallResolver.kt @@ -118,9 +118,9 @@ class FirCallResolver( candidate?.updateSourcesOfReceivers() functionCall } - val typeRef = components.typeFromCallee(resultFunctionCall) - if (typeRef.type is ConeErrorType) { - resultFunctionCall.resultType = typeRef + val type = components.typeFromCallee(resultFunctionCall).type + if (type is ConeErrorType) { + resultFunctionCall.resultType = type } return resultFunctionCall @@ -344,7 +344,7 @@ class FirCallResolver( return buildResolvedReifiedParameterReference { source = nameReference.source symbol = referencedSymbol - typeRef = typeForReifiedParameterReference(this) + coneTypeOrNull = typeForReifiedParameterReference(this) } } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/QualifiedNameResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/QualifiedNameResolution.kt index 8281abf28d3..aad08313c6e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/QualifiedNameResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/QualifiedNameResolution.kt @@ -6,6 +6,8 @@ package org.jetbrains.kotlin.fir import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.config.ApiVersion +import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.declarations.fullyExpandedClass import org.jetbrains.kotlin.fir.declarations.getDeprecationForCallSite @@ -19,8 +21,7 @@ import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.calls.getSingleVisibleClassifier import org.jetbrains.kotlin.fir.resolve.createCurrentScopeList import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeDeprecated -import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType -import org.jetbrains.kotlin.fir.resolve.typeForQualifier +import org.jetbrains.kotlin.fir.resolve.setTypeOfQualifier import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName @@ -41,7 +42,7 @@ fun BodyResolveComponents.resolveRootPartOfQualifier( this.nonFatalDiagnostics.addAll(nonFatalDiagnosticsFromExpression.orEmpty()) annotations += qualifiedAccess.annotations }.apply { - resultType = typeForQualifier(this) + setTypeOfQualifier(session) } } @@ -77,7 +78,7 @@ fun BodyResolveComponents.resolveRootPartOfQualifier( ) annotations += qualifiedAccess.annotations }.apply { - resultType = typeForQualifier(this) + setTypeOfQualifier(session) } } } @@ -126,7 +127,7 @@ fun FirResolvedQualifier.continueQualifier( ) ) }.apply { - resultType = components.typeForQualifier(this) + setTypeOfQualifier(components.session) } } } @@ -154,7 +155,7 @@ private fun FqName.continueQualifierInPackage( this.nonFatalDiagnostics.addAll(nonFatalDiagnosticsFromExpression.orEmpty()) annotations += qualifiedAccess.annotations }.apply { - resultType = components.typeForQualifier(this) + setTypeOfQualifier(components.session) } } @@ -179,7 +180,7 @@ private fun FqName.continueQualifierInPackage( isFullyQualified = true annotations += qualifiedAccess.annotations }.apply { - resultType = components.typeForQualifier(this) + setTypeOfQualifier(components.session) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt index 68def670ae8..81568d68acf 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt @@ -65,8 +65,7 @@ fun FirAnonymousFunction.addReturnToLastStatementIfNeeded(session: FirSession) { val lastStatement = body.statements.lastOrNull() as? FirExpression ?: return if (lastStatement is FirReturnExpression) return - val returnType = (body.typeRef as? FirResolvedTypeRef) ?: return - @OptIn(UnexpandedTypeCheck::class) + val returnType = body.coneTypeOrNull ?: return if (returnType.isNothing) return val returnTarget = FirFunctionTarget(null, isLambda = isLambda).also { it.bind(this) } @@ -212,50 +211,51 @@ fun BodyResolveComponents.buildResolvedQualifierForClass( this.nonFatalDiagnostics.addAll(nonFatalDiagnostics) this.annotations.addAll(annotations) }.build().apply { - resultType = if (classId.isLocal) { - typeForQualifierByDeclaration(regularClass.fir, resultType, session) - ?: session.builtinTypes.unitType + if (classId.isLocal) { + resultType = typeForQualifierByDeclaration(regularClass.fir, resultType, session) + ?.also { replaceCanBeValue(true) } + ?: session.builtinTypes.unitType.type } else { - typeForQualifier(this) + setTypeOfQualifier(session) } } } -fun BodyResolveComponents.typeForQualifier(resolvedQualifier: FirResolvedQualifier): FirTypeRef { - val classSymbol = resolvedQualifier.symbol - val resultType = resolvedQualifier.resultType +fun FirResolvedQualifier.setTypeOfQualifier(session: FirSession) { + val classSymbol = symbol + val resultType = resultType if (classSymbol != null) { classSymbol.lazyResolveToPhase(FirResolvePhase.TYPES) val declaration = classSymbol.fir - if (declaration !is FirTypeAlias || resolvedQualifier.typeArguments.isEmpty()) { - typeForQualifierByDeclaration(declaration, resultType, session)?.let { return it } + if (declaration !is FirTypeAlias || typeArguments.isEmpty()) { + val typeByDeclaration = typeForQualifierByDeclaration(declaration, resultType, session) + if (typeByDeclaration != null) { + this.resultType = typeByDeclaration + replaceCanBeValue(true) + return + } } } - return session.builtinTypes.unitType + this.resultType = session.builtinTypes.unitType.type } -internal fun typeForReifiedParameterReference(parameterReferenceBuilder: FirResolvedReifiedParameterReferenceBuilder): FirTypeRef { - val resultType = parameterReferenceBuilder.typeRef +internal fun typeForReifiedParameterReference(parameterReferenceBuilder: FirResolvedReifiedParameterReferenceBuilder): ConeLookupTagBasedType { val typeParameterSymbol = parameterReferenceBuilder.symbol - return resultType.resolvedTypeFromPrototype(typeParameterSymbol.constructType(emptyArray(), false)) + return typeParameterSymbol.constructType(emptyArray(), false) } -internal fun typeForQualifierByDeclaration(declaration: FirDeclaration, resultType: FirTypeRef, session: FirSession): FirTypeRef? { +internal fun typeForQualifierByDeclaration(declaration: FirDeclaration, resultType: ConeKotlinType?, session: FirSession): ConeKotlinType? { if (declaration is FirTypeAlias) { val expandedDeclaration = declaration.expandedConeType?.lookupTag?.toSymbol(session)?.fir ?: return null return typeForQualifierByDeclaration(expandedDeclaration, resultType, session) } if (declaration is FirRegularClass) { if (declaration.classKind == ClassKind.OBJECT) { - return resultType.resolvedTypeFromPrototype( - declaration.symbol.constructType(emptyArray(), false), - ) + return declaration.symbol.constructType(emptyArray(), false) } else { val companionObjectSymbol = declaration.companionObjectSymbol if (companionObjectSymbol != null) { - return resultType.resolvedTypeFromPrototype( - companionObjectSymbol.constructType(emptyArray(), false), - ) + return companionObjectSymbol.constructType(emptyArray(), false) } } } @@ -395,11 +395,11 @@ private val ConeKotlinType.isKindOfNothing get() = lowerBoundIfFlexible().let { it.isNothing || it.isNullableNothing } private fun FirSmartCastExpressionBuilder.applyResultTypeRef() { - typeRef = + coneTypeOrNull = if (smartcastStability == SmartcastStability.STABLE_VALUE) - smartcastType.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) + smartcastType.coneTypeOrNull else - originalExpression.typeRef.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) + originalExpression.coneTypeOrNull } private fun BodyResolveComponents.transformExpressionUsingSmartcastInfo( @@ -424,10 +424,8 @@ private fun BodyResolveComponents.transformExpressionUsingSm val intersectedType = ConeTypeIntersector.intersectTypes(session.typeContext, allTypes) if (intersectedType == originalType && intersectedType !is ConeDynamicType) return null val intersectedTypeRef = buildResolvedTypeRef { - source = expression.resultType.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) + source = expression.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) type = intersectedType - annotations += expression.resultType.annotations - delegatedTypeRef = expression.resultType } // Example (1): if (x is String) { ... }, where x: dynamic @@ -443,10 +441,8 @@ private fun BodyResolveComponents.transformExpressionUsingSm val reducedTypes = typesFromSmartCast.filterTo(mutableListOf()) { !it.isKindOfNothing } val reducedIntersectedType = ConeTypeIntersector.intersectTypes(session.typeContext, reducedTypes) val reducedIntersectedTypeRef = buildResolvedTypeRef { - source = expression.resultType.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) + source = expression.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) type = reducedIntersectedType - annotations += expression.resultType.annotations - delegatedTypeRef = expression.resultType } return buildSmartCastExpression { originalExpression = expression @@ -474,17 +470,17 @@ fun FirCheckedSafeCallSubject.propagateTypeFromOriginalReceiver( ) { // If the receiver expression is smartcast to `null`, it would have `Nothing?` as its type, which may not have members called by user // code. Hence, we fallback to the type before intersecting with `Nothing?`. - val receiverType = ((nullableReceiverExpression as? FirSmartCastExpression) + val receiverType = (nullableReceiverExpression as? FirSmartCastExpression) ?.takeIf { it.isStable } ?.smartcastTypeWithoutNullableNothing - ?: nullableReceiverExpression.typeRef) - .coneTypeSafe() ?: return + ?.coneTypeSafe() + ?: nullableReceiverExpression.coneTypeOrNull + ?: return val expandedReceiverType = receiverType.fullyExpandedType(session) val updatedReceiverType = expandedReceiverType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext).independentInstance() - val resolvedTypeRef = typeRef.resolvedTypeFromPrototype(updatedReceiverType) - replaceTypeRef(resolvedTypeRef) - session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, source, file.source) + replaceConeTypeOrNull(updatedReceiverType) + session.lookupTracker?.recordTypeResolveAsLookup(updatedReceiverType, source, file.source) } fun FirSafeCallExpression.propagateTypeFromQualifiedAccessAfterNullCheck( @@ -505,9 +501,9 @@ fun FirSafeCallExpression.propagateTypeFromQualifiedAccessAfterNullCheck( } } - val resolvedTypeRef = typeRef.resolvedTypeFromPrototype(resultingType.independentInstance()) - replaceTypeRef(resolvedTypeRef) - session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, source, file.source) + val independentInstance = resultingType.independentInstance() + replaceConeTypeOrNull(independentInstance) + session.lookupTracker?.recordTypeResolveAsLookup(independentInstance, source, file.source) } private val FirExpression.isCallToStatementLikeFunction: Boolean diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt index f5ec8eb7ecf..d836d5a7564 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt @@ -17,14 +17,10 @@ import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirNamedArgumentExpression import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier import org.jetbrains.kotlin.fir.expressions.builder.buildNamedArgumentExpression -import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents -import org.jetbrains.kotlin.fir.resolve.DoubleColonLHS -import org.jetbrains.kotlin.fir.resolve.createFunctionType +import org.jetbrains.kotlin.fir.resolve.* import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnsupportedCallableReferenceTarget -import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.inference.extractInputOutputTypesFromCallableReferenceExpectedType import org.jetbrains.kotlin.fir.resolve.inference.model.ConeArgumentConstraintPosition -import org.jetbrains.kotlin.fir.resolve.scope import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.types.* @@ -408,18 +404,18 @@ private fun createFakeArgumentsForReference( } class FirFakeArgumentForCallableReference( - val index: Int + val index: Int, ) : FirExpression() { override val source: KtSourceElement? get() = null - override val typeRef: FirTypeRef + override val coneTypeOrNull: ConeKotlinType get() = shouldNotBeCalled() override val annotations: List get() = shouldNotBeCalled() - override fun replaceTypeRef(newTypeRef: FirTypeRef) { + override fun replaceConeTypeOrNull(newConeTypeOrNull: ConeKotlinType?) { shouldNotBeCalled() } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/VisibilityUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/VisibilityUtils.kt index 53617ea4756..f173750a657 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/VisibilityUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/VisibilityUtils.kt @@ -8,7 +8,8 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.fakeElement -import org.jetbrains.kotlin.fir.* +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.FirVisibilityChecker import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.getExplicitBackingField import org.jetbrains.kotlin.fir.declarations.utils.isStatic @@ -103,12 +104,12 @@ private fun removeSmartCastTypeForAttemptToFitVisibility(dispatchReceiver: FirEx buildSmartCastExpression { this.originalExpression = originalExpression smartcastType = buildResolvedTypeRef { - source = originalExpression.typeRef.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) + source = originalExpression.source?.fakeElement(KtFakeSourceElementKind.SmartCastedTypeRef) type = originalTypeNotNullable } typesFromSmartCast = listOf(originalTypeNotNullable) smartcastStability = expressionWithSmartcastIfStable.smartcastStability - typeRef = smartcastType.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) + coneTypeOrNull = originalTypeNotNullable } else -> originalExpression } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt index 10eae0d1b66..0a3c4efac5e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirInvokeResolveTowerExtension.kt @@ -310,7 +310,7 @@ private fun BodyResolveComponents.createExplicitReceiverForInvokeByCallable( candidate ) dispatchReceiver = candidate.dispatchReceiverExpression() - this.typeRef = returnTypeCalculator.tryCalculateReturnType(symbol.fir) + coneTypeOrNull = returnTypeCalculator.tryCalculateReturnType(symbol.fir).type if (!invokeBuiltinExtensionMode) { extensionReceiver = extensionReceiverExpression diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolveTask.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolveTask.kt index 509f7856c48..498730361fa 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolveTask.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolveTask.kt @@ -15,14 +15,13 @@ import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionStub import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedQualifier -import org.jetbrains.kotlin.fir.resolve.* +import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents +import org.jetbrains.kotlin.fir.resolve.DoubleColonLHS import org.jetbrains.kotlin.fir.resolve.calls.* -import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType +import org.jetbrains.kotlin.fir.resolve.setTypeOfQualifier import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.impl.FirWhenSubjectImportingScope import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol -import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef -import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST @@ -113,7 +112,7 @@ internal abstract class FirBaseTowerResolveTask( this.symbol = it this.source = source?.fakeElement(KtFakeSourceElementKind.ImplicitReceiver) }.apply { - resultType = components.typeForQualifier(this) + setTypeOfQualifier(components.session) } ExpressionReceiverValue(resolvedQualifier) } @@ -231,13 +230,10 @@ internal open class FirTowerResolveTask( processClassifierScope(info, qualifierReceiver) if (resolvedQualifier.symbol != null) { - val typeRef = resolvedQualifier.typeRef if (info.callKind == CallKind.CallableReference && info.lhs is DoubleColonLHS.Type) { val stubReceiver = buildExpressionStub { source = info.explicitReceiver?.source - this.typeRef = buildResolvedTypeRef { - type = info.lhs.type - } + this.coneTypeOrNull = info.lhs.type } val stubReceiverInfo = info.replaceExplicitReceiver(stubReceiver) @@ -245,8 +241,8 @@ internal open class FirTowerResolveTask( runResolverForExpressionReceiver(stubReceiverInfo, stubReceiver, parentGroup = TowerGroup.QualifierValue) } - // NB: yet built-in Unit is used for "no-value" type - if (typeRef !is FirImplicitBuiltinTypeRef) { + // NB: canBeValue means it's resolved to an object or companion object + if (resolvedQualifier.canBeValue) { runResolverForExpressionReceiver(info, resolvedQualifier, parentGroup = TowerGroup.QualifierValue) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt index 48858e93dce..ed36d5ecb64 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt @@ -21,7 +21,6 @@ import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedQualifier import org.jetbrains.kotlin.fir.resolve.* import org.jetbrains.kotlin.fir.resolve.calls.* import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider -import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType import org.jetbrains.kotlin.fir.scopes.* import org.jetbrains.kotlin.fir.scopes.impl.FirDefaultStarImportingScope import org.jetbrains.kotlin.fir.scopes.impl.importedFromObjectOrStaticData @@ -337,7 +336,7 @@ class ScopeTowerLevel( this.symbol = this@toResolvedQualifierExpressionReceiver this.source = source?.fakeElement(KtFakeSourceElementKind.ImplicitReceiver) }.apply { - resultType = bodyResolveComponents.typeForQualifier(this) + setTypeOfQualifier(bodyResolveComponents.session) } return ExpressionReceiverValue(resolvedQualifier) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt index 940b68b7da2..80c28f4b80b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt @@ -31,7 +31,10 @@ import org.jetbrains.kotlin.fir.scopes.getFunctions import org.jetbrains.kotlin.fir.scopes.impl.toConeType import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol -import org.jetbrains.kotlin.fir.symbols.impl.* +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.ConstantValueKind @@ -906,7 +909,7 @@ abstract class FirDataFlowAnalyzer( } fun exitConstExpression(constExpression: FirConstExpression<*>) { - if (constExpression.resultType is FirResolvedTypeRef) return + if (constExpression.coneTypeOrNull != null) return graphBuilder.exitConstExpression(constExpression).mergeIncomingFlow() } @@ -1129,7 +1132,7 @@ abstract class FirDataFlowAnalyzer( val elvisVariable by lazy { variableStorage.createSynthetic(elvisExpression) } // If (x ?: null) != null then x != null - if (elvisExpression.rhs.resultType.isNullableNothing) { + if (elvisExpression.rhs.resultType?.isNullableNothing == true) { val lhsVariable = variableStorage.getOrCreateIfReal(flow, elvisExpression.lhs) if (lhsVariable != null) { flow.addImplication((elvisVariable notEq null) implies (lhsVariable notEq null)) @@ -1137,7 +1140,7 @@ abstract class FirDataFlowAnalyzer( } // If (null ?: x) != null then x != null - if (elvisExpression.lhs.resultType.isNullableNothing) { + if (elvisExpression.lhs.resultType?.isNullableNothing == true) { val rhsVariable = variableStorage.getOrCreateIfReal(flow, elvisExpression.rhs) if (rhsVariable != null) { flow.addImplication((elvisVariable notEq null) implies (rhsVariable notEq null)) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt index a2379374863..18ed3b81f38 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt @@ -1083,9 +1083,8 @@ class ControlFlowGraphBuilder { // it would be much easier if we could build calls after full completion only, at least for Nothing calls // KT-59726 // @returns `true` if node actually returned Nothing - @OptIn(UnexpandedTypeCheck::class) private fun completeFunctionCall(node: FunctionCallNode): Boolean { - if (!node.fir.resultType.isNothing) return false + if (node.fir.resultType?.isNothing != true) return false val stub = StubNode(node.owner, node.level) val edges = node.followingNodes.map { it to node.edgeTo(it) } CFGNode.removeAllOutgoingEdges(node) @@ -1101,9 +1100,8 @@ class ControlFlowGraphBuilder { // ----------------------------------- Resolvable call ----------------------------------- - @OptIn(UnexpandedTypeCheck::class) fun exitQualifiedAccessExpression(qualifiedAccessExpression: FirQualifiedAccessExpression): QualifiedAccessNode { - val returnsNothing = qualifiedAccessExpression.resultType.isNothing + val returnsNothing = qualifiedAccessExpression.resultType?.isNothing == true val node = createQualifiedAccessNode(qualifiedAccessExpression) if (returnsNothing) { addNonSuccessfullyTerminatingNode(node) @@ -1113,9 +1111,8 @@ class ControlFlowGraphBuilder { return node } - @OptIn(UnexpandedTypeCheck::class) fun exitSmartCastExpression(smartCastExpression: FirSmartCastExpression): SmartCastExpressionExitNode { - val returnsNothing = smartCastExpression.resultType.isNothing + val returnsNothing = smartCastExpression.resultType?.isNothing == true val node = createSmartCastExitNode(smartCastExpression) if (returnsNothing) { addNonSuccessfullyTerminatingNode(node) @@ -1147,9 +1144,8 @@ class ControlFlowGraphBuilder { return argumentListSplitNodes.pop()?.also { addNewSimpleNode(it) } } - @OptIn(UnexpandedTypeCheck::class) fun exitFunctionCall(functionCall: FirFunctionCall, callCompleted: Boolean): FunctionCallNode { - val returnsNothing = functionCall.resultType.isNothing + val returnsNothing = functionCall.resultType?.isNothing == true val node = createFunctionCallNode(functionCall) unifyDataFlowFromPostponedLambdas(node, callCompleted) if (returnsNothing) { @@ -1193,11 +1189,10 @@ class ControlFlowGraphBuilder { return createThrowExceptionNode(throwExpression).also { addNonSuccessfullyTerminatingNode(it) } } - @OptIn(UnexpandedTypeCheck::class) fun exitCheckNotNullCall(checkNotNullCall: FirCheckNotNullCall, callCompleted: Boolean): CheckNotNullCallNode { val node = createCheckNotNullCallNode(checkNotNullCall) unifyDataFlowFromPostponedLambdas(node, callCompleted) - if (checkNotNullCall.resultType.isNothing) { + if (checkNotNullCall.resultType?.isNothing == true) { addNonSuccessfullyTerminatingNode(node) } else { addNewSimpleNode(node) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt index 5885be54fb8..8394651ff7c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt @@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction import org.jetbrains.kotlin.fir.declarations.FirDeclaration import org.jetbrains.kotlin.fir.declarations.hasAnnotation import org.jetbrains.kotlin.fir.expressions.* -import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.resolve.calls.ImplicitExtensionReceiverValue import org.jetbrains.kotlin.fir.resolve.calls.ResolutionContext @@ -19,7 +18,6 @@ import org.jetbrains.kotlin.fir.resolve.calls.candidate import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.replaceStubsAndTypeVariablesToErrors -import org.jetbrains.kotlin.fir.resolve.transformers.FirCallCompletionResultsWriterTransformer import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.visitors.FirDefaultTransformer import org.jetbrains.kotlin.fir.visitors.transformSingle @@ -27,7 +25,9 @@ import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode -import org.jetbrains.kotlin.resolve.calls.inference.model.* +import org.jetbrains.kotlin.resolve.calls.inference.model.BuilderInferencePosition +import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage +import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.registerTypeVariableIfNotPresent import org.jetbrains.kotlin.resolve.descriptorUtil.BUILDER_INFERENCE_ANNOTATION_FQ_NAME import org.jetbrains.kotlin.types.model.TypeConstructorMarker @@ -268,10 +268,27 @@ class FirStubTypeTransformer(private val substitutor: ConeSubstitutor) : FirDefa if (element is FirResolvable) { element.candidate()?.let { processCandidate(it) } } + + // Since FirExpressions don't have typeRefs, they need to be updated separately. + // FirAnonymousFunctionExpression doesn't support replacing the type + // since it delegates the getter to the underlying FirAnonymousFunction. + if (element is FirExpression && element !is FirAnonymousFunctionExpression) { + element.coneTypeOrNull + ?.let(substitutor::substituteOrNull) + ?.let { element.replaceConeTypeOrNull(it) } + } + @Suppress("UNCHECKED_CAST") return element.transformChildren(this, data = null) as E } + override fun transformTypeOperatorCall(typeOperatorCall: FirTypeOperatorCall, data: Nothing?): FirStatement { + if (typeOperatorCall.argument.coneTypeOrNull is ConeStubType) { + typeOperatorCall.replaceArgFromStubType(true) + } + return super.transformTypeOperatorCall(typeOperatorCall, data) + } + override fun transformResolvedTypeRef(resolvedTypeRef: FirResolvedTypeRef, data: Nothing?): FirTypeRef = substitutor.substituteOrNull(resolvedTypeRef.type)?.let { resolvedTypeRef.withReplacedConeType(it) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index 85f4ef3e1d9..23a501640be 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -64,9 +64,8 @@ class FirCallCompleter( val initialType = typeRef.initialTypeOfCandidate(candidate) if (call is FirExpression) { - val resolvedTypeRef = typeRef.resolvedTypeFromPrototype(initialType) - call.resultType = resolvedTypeRef - session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, call.source, components.context.file.source) + call.resultType = initialType + session.lookupTracker?.recordTypeResolveAsLookup(initialType, call.source, components.context.file.source) } addConstraintFromExpectedType( diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt index df6b3a7c173..7d55da93d83 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt @@ -17,8 +17,6 @@ import org.jetbrains.kotlin.fir.resolve.shouldReturnUnit import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype import org.jetbrains.kotlin.fir.types.* -import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef -import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzerContext import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode @@ -84,20 +82,14 @@ class PostponedArgumentsAnalyzer( callableReferenceAccess.apply { replaceCalleeReference(namedReference) val typeForCallableReference = atom.resultingTypeForCallableReference - val resolvedTypeRef = when { - typeForCallableReference != null -> buildResolvedTypeRef { - type = typeForCallableReference - } - namedReference is FirErrorReferenceWithCandidate -> buildErrorTypeRef { - diagnostic = namedReference.diagnostic - } - else -> buildErrorTypeRef { - diagnostic = ConeUnresolvedReferenceError(callableReferenceAccess.calleeReference.name) - } + val resolvedType = when { + typeForCallableReference != null -> typeForCallableReference + namedReference is FirErrorReferenceWithCandidate -> ConeErrorType(namedReference.diagnostic) + else -> ConeErrorType(ConeUnresolvedReferenceError(callableReferenceAccess.calleeReference.name)) } - replaceTypeRef(resolvedTypeRef) + replaceConeTypeOrNull(resolvedType) resolutionContext.session.lookupTracker?.recordTypeResolveAsLookup( - resolvedTypeRef, source, resolutionContext.bodyResolveComponents.file.source + resolvedType, source, resolutionContext.bodyResolveComponents.file.source ) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index d87de9ff9bf..9b69efa0587 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -5,9 +5,7 @@ package org.jetbrains.kotlin.fir.resolve.transformers -import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind -import org.jetbrains.kotlin.fakeElement import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.isInline @@ -40,7 +38,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef -import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.builder.buildStarProjection import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl @@ -102,20 +99,12 @@ class FirCallCompletionResultsWriterTransformer( val subCandidate = calleeReference.candidate val declaration = subCandidate.symbol.fir val typeArguments = computeTypeArguments(qualifiedAccessExpression, subCandidate) - val typeRef = if (declaration is FirCallableDeclaration) { + val type = if (declaration is FirCallableDeclaration) { val calculated = typeCalculator.tryCalculateReturnType(declaration) if (calculated !is FirErrorTypeRef) { - buildResolvedTypeRef { - source = calculated.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef) - annotations += calculated.annotations - type = calculated.type - } + calculated.type } else { - buildErrorTypeRef { - source = calculated.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef) - type = calculated.type - diagnostic = calculated.diagnostic - } + ConeErrorType(calculated.diagnostic) } } else { // this branch is for cases when we have @@ -123,14 +112,12 @@ class FirCallCompletionResultsWriterTransformer( // e.g. `T::toString` where T is a generic type. // in these cases we should report an error on // the calleeReference.source which is not a fake source. - buildErrorTypeRef { - source = calleeReference.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef) - diagnostic = - when (declaration) { - is FirTypeParameter -> ConeTypeParameterInQualifiedAccess(declaration.symbol) - else -> ConeSimpleDiagnostic("Callee reference to candidate without return type: ${declaration.render()}") - } - } + ConeErrorType( + when (declaration) { + is FirTypeParameter -> ConeTypeParameterInQualifiedAccess(declaration.symbol) + else -> ConeSimpleDiagnostic("Callee reference to candidate without return type: ${declaration.render()}") + } + ) } var dispatchReceiver = subCandidate.dispatchReceiverExpression() @@ -159,12 +146,12 @@ class FirCallCompletionResultsWriterTransformer( qualifiedAccessExpression.replaceNonFatalDiagnostics(nonFatalDiagnostics) } - qualifiedAccessExpression.replaceTypeRef(typeRef) + qualifiedAccessExpression.replaceConeTypeOrNull(type) if (declaration !is FirErrorFunction) { qualifiedAccessExpression.replaceTypeArguments(typeArguments) } - session.lookupTracker?.recordTypeResolveAsLookup(typeRef, qualifiedAccessExpression.source, context.file.source) + session.lookupTracker?.recordTypeResolveAsLookup(type, qualifiedAccessExpression.source, context.file.source) return qualifiedAccessExpression } @@ -181,12 +168,11 @@ class FirCallCompletionResultsWriterTransformer( qualifiedAccessExpression } val result = prepareQualifiedTransform(qualifiedAccessExpression, calleeReference) - val typeRef = result.typeRef as FirResolvedTypeRef val subCandidate = calleeReference.candidate - val resultType = typeRef.substituteTypeRef(subCandidate) + val resultType = result.coneTypeOrNull?.substituteType(subCandidate) resultType.ensureResolvedTypeDeclaration(session) - result.replaceTypeRef(resultType) + result.replaceConeTypeOrNull(resultType) session.lookupTracker?.recordTypeResolveAsLookup(resultType, qualifiedAccessExpression.source, context.file.source) if (mode == Mode.DelegatedPropertyCompletion) { @@ -208,10 +194,8 @@ class FirCallCompletionResultsWriterTransformer( val calleeReference = functionCall.calleeReference as? FirNamedReferenceWithCandidate ?: return functionCall val result = prepareQualifiedTransform(functionCall, calleeReference) - val typeRef = result.typeRef as FirResolvedTypeRef val subCandidate = calleeReference.candidate - val resultType: FirTypeRef - resultType = typeRef.substituteTypeRef(subCandidate) + val resultType = result.coneType.substituteType(subCandidate) if (calleeReference.isError) { subCandidate.argumentMapping?.let { result.replaceArgumentList(buildArgumentListForErrorCall(result.argumentList, it)) @@ -238,7 +222,7 @@ class FirCallCompletionResultsWriterTransformer( } val expectedArgumentsTypeMapping = runIf(!calleeReference.isError) { subCandidate.createArgumentsMapping() } result.argumentList.transformArguments(this, expectedArgumentsTypeMapping) - result.replaceTypeRef(resultType) + result.replaceConeTypeOrNull(resultType) session.lookupTracker?.recordTypeResolveAsLookup(resultType, functionCall.source, context.file.source) if (mode == Mode.DelegatedPropertyCompletion) { @@ -308,19 +292,19 @@ class FirCallCompletionResultsWriterTransformer( } } - private fun D.replaceTypeRefWithSubstituted( + private fun D.replaceTypeWithSubstituted( calleeReference: FirNamedReferenceWithCandidate, typeRef: FirResolvedTypeRef, ): D { - val resultTypeRef = typeRef.substituteTypeRef(calleeReference.candidate) - replaceTypeRef(resultTypeRef) - session.lookupTracker?.recordTypeResolveAsLookup(resultTypeRef, source, context.file.source) + val resultType = typeRef.type.substituteType(calleeReference.candidate) + replaceConeTypeOrNull(resultType) + session.lookupTracker?.recordTypeResolveAsLookup(resultType, source, context.file.source) return this } - private fun FirResolvedTypeRef.substituteTypeRef( + private fun ConeKotlinType.substituteType( candidate: Candidate, - ): FirResolvedTypeRef { + ): ConeKotlinType { val initialType = candidate.substitutor.substituteOrSelf(type) val substitutedType = finallySubstituteOrNull(initialType) val finalType = typeApproximator.approximateToSuperType( @@ -332,9 +316,7 @@ class FirCallCompletionResultsWriterTransformer( // // In FE1.0, it's not necessary since the annotation for elvis have some strange form (see org.jetbrains.kotlin.resolve.descriptorUtil.AnnotationsWithOnly) // that is not propagated further. - val withRemovedExactAttribute = finalType?.removeExactAttribute() - - return withReplacedConeType(withRemovedExactAttribute) + return finalType?.removeExactAttribute() ?: this } private fun ConeKotlinType.removeExactAttribute(): ConeKotlinType { @@ -370,15 +352,16 @@ class FirCallCompletionResultsWriterTransformer( val subCandidate = calleeReference.candidate val typeArguments = computeTypeArguments(callableReferenceAccess, subCandidate) - val typeRef = callableReferenceAccess.typeRef as FirResolvedTypeRef - - val initialType = calleeReference.candidate.substitutor.substituteOrSelf(typeRef.type) + val initialType = calleeReference.candidate.substitutor.substituteOrSelf(callableReferenceAccess.coneType) val finalType = finallySubstituteOrSelf(initialType) - val resultType = typeRef.withReplacedConeType(finalType) - callableReferenceAccess.replaceTypeRef(resultType) + callableReferenceAccess.replaceConeTypeOrNull(finalType) callableReferenceAccess.replaceTypeArguments(typeArguments) - session.lookupTracker?.recordTypeResolveAsLookup(resultType, typeRef.source ?: callableReferenceAccess.source, context.file.source) + session.lookupTracker?.recordTypeResolveAsLookup( + finalType, + callableReferenceAccess.source ?: callableReferenceAccess.source, + context.file.source + ) val resolvedReference = when (calleeReference) { is FirErrorReferenceWithCandidate -> calleeReference.toErrorReference(calleeReference.diagnostic) @@ -413,9 +396,8 @@ class FirCallCompletionResultsWriterTransformer( ): FirStatement { val originalType = qualifiedAccessExpression.coneType val substitutedReceiverType = finallySubstituteOrNull(originalType) ?: return qualifiedAccessExpression - val resolvedTypeRef = qualifiedAccessExpression.typeRef.resolvedTypeFromPrototype(substitutedReceiverType) - qualifiedAccessExpression.replaceTypeRef(resolvedTypeRef) - session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, qualifiedAccessExpression.source, context.file.source) + qualifiedAccessExpression.replaceConeTypeOrNull(substitutedReceiverType) + session.lookupTracker?.recordTypeResolveAsLookup(substitutedReceiverType, qualifiedAccessExpression.source, context.file.source) return qualifiedAccessExpression } @@ -651,16 +633,15 @@ class FirCallCompletionResultsWriterTransformer( override fun transformBlock(block: FirBlock, data: ExpectedArgumentType?): FirStatement { val initialType = block.coneTypeSafe() if (initialType != null) { - val finalType = finallySubstituteOrNull(initialType) - var resultType = block.resultType.withReplacedConeType(finalType) - resultType.coneTypeSafe()?.let { - resultType = resultType.resolvedTypeFromPrototype(it.getApproximatedType(data?.getExpectedType(block)?.fullyExpandedType(session))) + var resultType = finallySubstituteOrNull(initialType) ?: block.resultType + (resultType as? ConeIntegerLiteralType)?.let { + resultType = it.getApproximatedType(data?.getExpectedType(block)?.fullyExpandedType(session)) } - block.replaceTypeRef(resultType) + block.replaceConeTypeOrNull(resultType) session.lookupTracker?.recordTypeResolveAsLookup(resultType, block.source, context.file.source) } transformElement(block, data) - if (block.resultType is FirErrorTypeRef) { + if (block.resultType is ConeErrorType) { block.writeResultType(session) } return block @@ -709,7 +690,7 @@ class FirCallCompletionResultsWriterTransformer( } val typeRef = typeCalculator.tryCalculateReturnType(declaration) - syntheticCall.replaceTypeRefWithSubstituted(calleeReference, typeRef) + syntheticCall.replaceTypeWithSubstituted(calleeReference, typeRef) transformSyntheticCallChildren(syntheticCall, data) return syntheticCall.apply { @@ -761,7 +742,7 @@ class FirCallCompletionResultsWriterTransformer( } override fun transformArrayLiteral(arrayLiteral: FirArrayLiteral, data: ExpectedArgumentType?): FirStatement { - if (arrayLiteral.typeRef !is FirImplicitTypeRef) return arrayLiteral + if (arrayLiteral.coneTypeOrNull != null) return arrayLiteral val expectedArrayType = data?.getExpectedType(arrayLiteral) val expectedArrayElementType = expectedArrayType?.arrayElementType() arrayLiteral.transformChildren(this, expectedArrayElementType?.toExpectedType()) @@ -770,9 +751,8 @@ class FirCallCompletionResultsWriterTransformer( typeApproximator.approximateToSuperType(it, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference) ?: it } ?: expectedArrayElementType ?: session.builtinTypes.nullableAnyType.type - arrayLiteral.resultType = arrayLiteral.typeRef.resolvedTypeFromPrototype( + arrayLiteral.resultType = arrayElementType.createArrayType(createPrimitiveArrayTypeIfPossible = expectedArrayType?.isPrimitiveArray == true) - ) return arrayLiteral } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt index e7740c1084e..5638c409771 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSpecificTypeResolverTransformer.kt @@ -7,7 +7,7 @@ package org.jetbrains.kotlin.fir.resolve.transformers import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.fakeElement -import org.jetbrains.kotlin.fir.* +import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirFile import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirValueParameter @@ -15,6 +15,9 @@ import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeUnexpectedTypeArgumentsError import org.jetbrains.kotlin.fir.expressions.FirStatement +import org.jetbrains.kotlin.fir.lookupTracker +import org.jetbrains.kotlin.fir.recordTypeLookup +import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.resolve.FirTypeResolutionResult import org.jetbrains.kotlin.fir.resolve.SupertypeSupplier import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnsupportedDefaultValueInFunctionType @@ -238,9 +241,7 @@ class FirSpecificTypeResolverTransformer( override fun transformValueParameter(valueParameter: FirValueParameter, data: ScopeClassDeclaration): FirStatement { val result = transformElement(valueParameter, data) result.defaultValue?.let { - it.resultType = buildErrorTypeRef { - diagnostic = ConeUnsupportedDefaultValueInFunctionType(it.source) - } + it.resultType = ConeErrorType(ConeUnsupportedDefaultValueInFunctionType(it.source)) } return result } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt index e1d971ca5fd..c386d55bc31 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt @@ -192,7 +192,7 @@ class FirSyntheticCallGenerator( source = arrayLiteral.source }.also { if (arrayOfSymbol == null) { - it.resultType = components.typeFromCallee(it) + it.resultType = components.typeFromCallee(it).type } } } @@ -242,7 +242,7 @@ class FirSyntheticCallGenerator( // If the callable reference cannot be resolved with the expected type, let's try to resolve it with any type and report // something like INITIALIZER_TYPE_MISMATCH or NONE_APPLICABLE instead of UNRESOLVED_REFERENCE. - check(callableReferenceAccess.calleeReference is FirSimpleNamedReference && callableReferenceAccess.typeRef is FirImplicitTypeRef) { + check(callableReferenceAccess.calleeReference is FirSimpleNamedReference && callableReferenceAccess.coneTypeOrNull == null) { "Expected FirCallableReferenceAccess to be unresolved." } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt index de5989adda4..f56514002a7 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt @@ -9,12 +9,18 @@ import org.jetbrains.kotlin.contracts.description.LogicOperationKind import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.diagnostics.WhenMissingCase -import org.jetbrains.kotlin.fir.* -import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.FirElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.FirEnumEntry +import org.jetbrains.kotlin.fir.declarations.FirRegularClass +import org.jetbrains.kotlin.fir.declarations.collectEnumEntries +import org.jetbrains.kotlin.fir.declarations.getSealedClassInheritors import org.jetbrains.kotlin.fir.declarations.utils.isExpect import org.jetbrains.kotlin.fir.declarations.utils.modality +import org.jetbrains.kotlin.fir.enumWhenTracker import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.impl.FirElseIfTrueCondition +import org.jetbrains.kotlin.fir.reportEnumUsageInWhen import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider @@ -217,8 +223,7 @@ private object WhenOnNullableExhaustivenessChecker : WhenExhaustivenessChecker() private object ConditionChecker : AbstractConditionChecker() { override fun visitEqualityOperatorCall(equalityOperatorCall: FirEqualityOperatorCall, data: Flags) { val argument = equalityOperatorCall.arguments[1] - @OptIn(UnexpandedTypeCheck::class) - if (argument.typeRef.isNullableNothing) { + if (argument.coneTypeOrNull?.isNullableNothing == true) { data.containsNull = true } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt index b176ffa33f4..1ca4b69c3ad 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.scope import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType -import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator import org.jetbrains.kotlin.fir.scopes.getFunctions import org.jetbrains.kotlin.fir.scopes.impl.originalForWrappedIntegerOperator @@ -64,7 +63,7 @@ class IntegerLiteralAndOperatorApproximationTransformer( ): FirStatement { val type = constExpression.coneTypeSafe() ?: return constExpression val approximatedType = type.getApproximatedType(data?.fullyExpandedType(session)) - constExpression.resultType = constExpression.resultType.resolvedTypeFromPrototype(approximatedType) + constExpression.resultType = approximatedType @Suppress("UNCHECKED_CAST") val kind = approximatedType.toConstKind() as ConstantValueKind constExpression.replaceKind(kind) @@ -83,7 +82,7 @@ class IntegerLiteralAndOperatorApproximationTransformer( call.transformExtensionReceiver(this, null) call.argumentList.transformArguments(this, null) - call.resultType = call.resultType.resolvedTypeFromPrototype(approximatedType) + call.resultType = approximatedType val calleeReference = call.calleeReference // callee reference may also be an error reference and it's ok if wrapped operator function leaks throw it @@ -110,15 +109,15 @@ class IntegerLiteralAndOperatorApproximationTransformer( if (approximatedType.isInt || approximatedType.isUInt) return call val typeBeforeConversion = if (operatorType.isUnsigned) { - session.builtinTypes.uIntType + session.builtinTypes.uIntType.type } else { - session.builtinTypes.intType + session.builtinTypes.intType.type } - call.replaceTypeRef(typeBeforeConversion) + call.replaceConeTypeOrNull(typeBeforeConversion) return buildFunctionCall { source = call.source?.fakeElement(KtFakeSourceElementKind.IntToLongConversion) - typeRef = session.builtinTypes.longType + coneTypeOrNull = session.builtinTypes.longType.type explicitReceiver = call dispatchReceiver = call this.calleeReference = buildResolvedNamedReference { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt index f1b95e96f29..89e29101dd4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt @@ -5,8 +5,10 @@ package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve -import org.jetbrains.kotlin.* -import org.jetbrains.kotlin.fir.* +import org.jetbrains.kotlin.KtFakeSourceElementKind +import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fakeElement +import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind @@ -16,15 +18,14 @@ import org.jetbrains.kotlin.fir.expressions.FirNamedArgumentExpression import org.jetbrains.kotlin.fir.expressions.builder.buildVarargArgumentsExpression import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider import org.jetbrains.kotlin.fir.types.* -import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.ConstantValueKind -internal inline var FirExpression.resultType: FirTypeRef - get() = typeRef +internal inline var FirExpression.resultType: ConeKotlinType? + get() = coneTypeOrNull set(type) { - replaceTypeRef(type) + replaceConeTypeOrNull(type) } internal fun remapArgumentsWithVararg( @@ -43,7 +44,7 @@ internal fun remapArgumentsWithVararg( val varargArgument = buildVarargArgumentsExpression { // TODO: ideally we should use here a source from the use-site and not from the declaration-site, KT-59682 this.varargElementType = varargParameterTypeRef.withReplacedConeType(varargElementType, KtFakeSourceElementKind.VarargArgument) - this.typeRef = varargParameterTypeRef.withReplacedConeType(varargArrayType, KtFakeSourceElementKind.VarargArgument) + this.coneTypeOrNull = varargArrayType var startOffset = Int.MAX_VALUE var endOffset = 0 var firstVarargElementSource: KtSourceElement? = null @@ -87,16 +88,9 @@ fun FirBlock.writeResultType(session: FirSession) { else -> null } resultType = if (resultExpression == null) { - resultType.resolvedTypeFromPrototype(session.builtinTypes.unitType.type) + session.builtinTypes.unitType.type } else { - val theType = resultExpression.resultType - if (theType is FirResolvedTypeRef) { - theType.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) - } else { - buildErrorTypeRef { - diagnostic = ConeSimpleDiagnostic("No type for block", DiagnosticKind.InferenceError) - } - } + resultExpression.resultType ?: ConeErrorType(ConeSimpleDiagnostic("No type for block", DiagnosticKind.InferenceError)) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirArrayOfCallTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirArrayOfCallTransformer.kt index 752a519bb69..69135c3a38d 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirArrayOfCallTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirArrayOfCallTransformer.kt @@ -45,7 +45,7 @@ class FirArrayOfCallTransformer : FirDefaultTransformer() { } } } - typeRef = functionCall.typeRef + coneTypeOrNull = functionCall.coneTypeOrNull } val calleeReference = functionCall.calleeReference diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt index 88aa99940b3..d0edacea035 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt @@ -10,11 +10,13 @@ import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.impl.FirElseIfTrueCondition import org.jetbrains.kotlin.fir.expressions.impl.FirEmptyExpressionBlock import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference -import org.jetbrains.kotlin.fir.resolve.* +import org.jetbrains.kotlin.fir.resolve.ResolutionMode import org.jetbrains.kotlin.fir.resolve.calls.isUnitOrFlexibleUnit +import org.jetbrains.kotlin.fir.resolve.fullyExpandedType +import org.jetbrains.kotlin.fir.resolve.transformWhenSubjectExpressionUsingSmartcastInfo import org.jetbrains.kotlin.fir.resolve.transformers.FirSyntheticCallGenerator import org.jetbrains.kotlin.fir.resolve.transformers.FirWhenExhaustivenessTransformer -import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype +import org.jetbrains.kotlin.fir.resolve.withExpectedType import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.visitors.transformSingle @@ -54,7 +56,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes // ------------------------------- When expressions ------------------------------- override fun transformWhenExpression(whenExpression: FirWhenExpression, data: ResolutionMode): FirStatement { - if (whenExpression.calleeReference is FirResolvedNamedReference && whenExpression.resultType !is FirImplicitTypeRef) { + if (whenExpression.calleeReference is FirResolvedNamedReference && whenExpression.resultType != null) { return whenExpression } whenExpression.annotations.forEach { it.accept(this, data) } @@ -114,7 +116,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes private fun FirWhenExpression.replaceReturnTypeIfNotExhaustive(): FirWhenExpression { if (!isProperlyExhaustive) { - resultType = resultType.resolvedTypeFromPrototype(session.builtinTypes.unitType.type) + resultType = session.builtinTypes.unitType.type } return this } @@ -141,7 +143,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes data: ResolutionMode ): FirStatement { val parentWhen = whenSubjectExpression.whenRef.value - val subjectType = parentWhen.subject?.resultType ?: parentWhen.subjectVariable?.returnTypeRef + val subjectType = parentWhen.subject?.resultType ?: parentWhen.subjectVariable?.returnTypeRef?.coneTypeOrNull if (subjectType != null) { whenSubjectExpression.resultType = subjectType } @@ -152,7 +154,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes // ------------------------------- Try/catch expressions ------------------------------- override fun transformTryExpression(tryExpression: FirTryExpression, data: ResolutionMode): FirStatement { - if (tryExpression.calleeReference is FirResolvedNamedReference && tryExpression.resultType !is FirImplicitTypeRef) { + if (tryExpression.calleeReference is FirResolvedNamedReference && tryExpression.resultType != null) { return tryExpression } @@ -215,7 +217,6 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes data: ResolutionMode, ): FirStatement { return throwExpression.apply { - replaceTypeRef(throwExpression.typeRef.transform(transformer, data)) transformAnnotations(transformer, data) transformException(transformer, withExpectedType(session.builtinTypes.throwableType)) dataFlowAnalyzer.exitThrowExceptionNode(this) @@ -268,7 +269,7 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes val newReturnType = lhsType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext) .convertToNonRawVersion() - result.replaceTypeRef(result.typeRef.resolvedTypeFromPrototype(newReturnType)) + result.replaceConeTypeOrNull(newReturnType) isLhsNotNull = true } } @@ -279,8 +280,8 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirAbstractBodyRes ) { // Sometimes return type for special call for elvis operator might be nullable, // but result is not nullable if the right type is not nullable - result.replaceTypeRef( - result.typeRef.withReplacedConeType(result.coneType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext)) + result.replaceConeTypeOrNull( + result.coneType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext) ) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index 445e5712dfb..9ed99f18f0c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -228,29 +228,29 @@ open class FirDeclarationsResolveTransformer( // get() = delegate.getValue(thisRef, kProperty: KProperty0/1/2<..., SomeType>) // set() = delegate.getValue(thisRef, kProperty: KProperty0/1/2<..., SomeType>, value) val propertyReferenceAccess = resolvedArgumentMapping?.keys?.toList()?.getOrNull(1) as? FirCallableReferenceAccess ?: return - val typeRef = propertyReferenceAccess.typeRef - if (typeRef is FirResolvedTypeRef && property.returnTypeRef is FirResolvedTypeRef) { - val typeArguments = (typeRef.type as ConeClassLikeType).typeArguments + val type = propertyReferenceAccess.coneTypeOrNull + if (type != null && property.returnTypeRef is FirResolvedTypeRef) { + val typeArguments = (type.type as ConeClassLikeType).typeArguments val extensionType = property.receiverParameter?.typeRef?.coneType val dispatchType = context.containingClass?.let { containingClass -> containingClass.symbol.constructStarProjectedType(containingClass.typeParameters.size) } - propertyReferenceAccess.replaceTypeRef( - buildResolvedTypeRef { - source = typeRef.source - annotations.addAll(typeRef.annotations) - type = (typeRef.type as ConeClassLikeType).lookupTag.constructClassType( - typeArguments.mapIndexed { index, argument -> - when (index) { - typeArguments.lastIndex -> property.returnTypeRef.coneType - 0 -> extensionType ?: dispatchType - else -> dispatchType - } ?: argument - }.toTypedArray(), - isNullable = false + propertyReferenceAccess.replaceConeTypeOrNull( + (type.type as ConeClassLikeType).lookupTag.constructClassType( + typeArguments.mapIndexed { index, argument -> + when (index) { + typeArguments.lastIndex -> property.returnTypeRef.coneType + 0 -> extensionType ?: dispatchType + else -> dispatchType + } ?: argument + }.toTypedArray(), + isNullable = false + ).also { + session.lookupTracker?.recordTypeResolveAsLookup( + it, + propertyReferenceAccess.source ?: source, + components.file.source ) - }.also { - session.lookupTracker?.recordTypeResolveAsLookup(it, propertyReferenceAccess.source ?: source, components.file.source) } ) } @@ -343,9 +343,9 @@ open class FirDeclarationsResolveTransformer( val substitutor = createTypeSubstitutorByTypeConstructor( typeVariableTypeToStubType, session.typeContext, approximateIntegerLiterals = true ) - val delegateExpressionTypeRef = delegateExpression.typeRef - val stubTypeSubstituted = substitutor.substituteOrNull(delegateExpressionTypeRef.coneType) - delegateExpression.replaceTypeRef(delegateExpressionTypeRef.withReplacedConeType(stubTypeSubstituted)) + val delegateExpressionType = delegateExpression.coneType + val stubTypeSubstituted = substitutor.substituteOrNull(delegateExpressionType) + delegateExpression.replaceConeTypeOrNull(stubTypeSubstituted) } } @@ -377,7 +377,7 @@ open class FirDeclarationsResolveTransformer( ) ) - provideDelegateCall.replaceTypeRef(provideDelegateCall.typeRef.resolvedTypeFromPrototype(stubTypeSubstituted)) + provideDelegateCall.replaceConeTypeOrNull(stubTypeSubstituted) return provideDelegateCall } @@ -643,19 +643,19 @@ open class FirDeclarationsResolveTransformer( if (result.returnTypeRef is FirImplicitTypeRef) { val simpleFunction = function as? FirSimpleFunction val returnExpression = (body?.statements?.singleOrNull() as? FirReturnExpression)?.result - val returnTypeRef = if (returnExpression?.typeRef is FirResolvedTypeRef) { - returnExpression.resultType.approximateDeclarationType( + val expressionType = returnExpression?.coneTypeOrNull + val returnTypeRef = expressionType + ?.toFirResolvedTypeRef(result.returnTypeRef.source) + ?.approximateDeclarationType( session, simpleFunction?.visibilityForApproximation(), isLocal = simpleFunction?.isLocal == true, isInlineFunction = simpleFunction?.isInline == true - ).copyWithNewSource(result.returnTypeRef.source) - } else { - buildErrorTypeRef { + ) + ?: buildErrorTypeRef { source = result.returnTypeRef.source diagnostic = ConeSimpleDiagnostic("empty body", DiagnosticKind.Other) } - } result.transformReturnTypeRef(transformer, withExpectedType(returnTypeRef)) } @@ -982,7 +982,7 @@ open class FirDeclarationsResolveTransformer( val inferredType = if (backingField is FirDefaultPropertyBackingField) { propertyType } else { - backingField.initializer?.unwrapSmartcastExpression()?.typeRef + backingField.initializer?.unwrapSmartcastExpression()?.coneTypeOrNull?.toFirResolvedTypeRef() } val resultType = inferredType ?: return backingField.transformReturnTypeRef( @@ -1011,7 +1011,7 @@ open class FirDeclarationsResolveTransformer( val resultType = when { initializer != null -> { val unwrappedInitializer = initializer.unwrapSmartcastExpression() - unwrappedInitializer.resultType + unwrappedInitializer.resultType?.toFirResolvedTypeRef() } variable.getter != null && variable.getter !is FirDefaultPropertyAccessor -> variable.getter?.returnTypeRef else -> null @@ -1094,10 +1094,10 @@ open class FirDeclarationsResolveTransformer( private val FirVariable.initializerResolved: Boolean get() { val initializer = initializer ?: return false - return initializer.typeRef is FirResolvedTypeRef && initializer !is FirErrorExpression + return initializer.coneTypeOrNull != null && initializer !is FirErrorExpression } protected val FirFunction.bodyResolved: Boolean - get() = body !is FirLazyBlock && body?.typeRef is FirResolvedTypeRef + get() = body !is FirLazyBlock && body?.coneTypeOrNull != null } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt index 783a1cdfaa3..0662079bc47 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt @@ -37,7 +37,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.isWrappedIntegerOperatorForUnsignedT import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef -import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef +import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImplWithoutSource import org.jetbrains.kotlin.fir.visitors.FirDefaultTransformer import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.TransformData @@ -68,15 +68,13 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT } override fun transformExpression(expression: FirExpression, data: ResolutionMode): FirStatement { - if (expression.resultType is FirImplicitTypeRef && expression !is FirWrappedExpression) { - val type = buildErrorTypeRef { - source = expression.source - diagnostic = ConeSimpleDiagnostic( + if (expression.resultType == null && expression !is FirWrappedExpression) { + expression.resultType = ConeErrorType( + ConeSimpleDiagnostic( "Type calculating for ${expression::class} is not supported", DiagnosticKind.InferenceError ) - } - expression.resultType = type + ) } return (expression.transformChildren(transformer, data) as FirStatement) } @@ -97,7 +95,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT data: ResolutionMode, isUsedAsReceiver: Boolean, ): FirStatement { - if (qualifiedAccessExpression.typeRef is FirResolvedTypeRef && qualifiedAccessExpression.calleeReference !is FirSimpleNamedReference) { + if (qualifiedAccessExpression.coneTypeOrNull != null && qualifiedAccessExpression.calleeReference !is FirSimpleNamedReference) { return qualifiedAccessExpression } @@ -115,21 +113,16 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT } } val implicitType = implicitReceiver?.originalType - qualifiedAccessExpression.resultType = when { - implicitReceiver is InaccessibleImplicitReceiverValue -> buildErrorTypeRef { - source = qualifiedAccessExpression.source - diagnostic = ConeInstanceAccessBeforeSuperCall("") - } - implicitType != null -> implicitType.toFirResolvedTypeRef(callee.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef)) - labelName != null -> buildErrorTypeRef { - source = qualifiedAccessExpression.source - diagnostic = ConeSimpleDiagnostic("Unresolved this@$labelName", DiagnosticKind.UnresolvedLabel) - } - else -> buildErrorTypeRef { - source = qualifiedAccessExpression.source - diagnostic = ConeSimpleDiagnostic("'this' is not defined in this context", DiagnosticKind.NoThis) - } + val resultType: ConeKotlinType = when { + implicitReceiver is InaccessibleImplicitReceiverValue -> ConeErrorType(ConeInstanceAccessBeforeSuperCall("")) + implicitType != null -> implicitType + labelName != null -> ConeErrorType(ConeSimpleDiagnostic("Unresolved this@$labelName", DiagnosticKind.UnresolvedLabel)) + else -> ConeErrorType(ConeSimpleDiagnostic("'this' is not defined in this context", DiagnosticKind.NoThis)) } + (resultType as? ConeErrorType)?.diagnostic?.let { + callee.replaceDiagnostic(it) + } + qualifiedAccessExpression.resultType = resultType qualifiedAccessExpression } is FirSuperReference -> { @@ -141,12 +134,12 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT } is FirDelegateFieldReference -> { val delegateFieldSymbol = callee.resolvedSymbol - qualifiedAccessExpression.resultType = delegateFieldSymbol.fir.delegate!!.typeRef + qualifiedAccessExpression.resultType = delegateFieldSymbol.fir.delegate!!.coneTypeOrNull qualifiedAccessExpression } is FirResolvedNamedReference, is FirErrorNamedReference -> { - if (qualifiedAccessExpression.typeRef !is FirResolvedTypeRef) { + if (qualifiedAccessExpression.coneTypeOrNull == null) { storeTypeFromCallee(qualifiedAccessExpression, isLhsOfAssignment = false) } qualifiedAccessExpression @@ -271,7 +264,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT return markSuperReferenceError(diagnostic, superReferenceContainer, superReference) } superTypeRef is FirResolvedTypeRef -> { - superReferenceContainer.resultType = superTypeRef.copyWithNewSourceKind(KtFakeSourceElementKind.SuperCallExplicitType) + superReferenceContainer.resultType = superTypeRef.type } superTypeRef !is FirImplicitTypeRef -> { components.typeResolverTransformer.withBareTypes { @@ -302,8 +295,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT source = superTypeRef.source diagnostic = ConeSimpleDiagnostic("Not a super type", DiagnosticKind.NotASupertype) } - superReferenceContainer.resultType = - actualSuperTypeRef.copyWithNewSourceKind(KtFakeSourceElementKind.SuperCallExplicitType) + superReferenceContainer.resultType = actualSuperTypeRef.type superReference.replaceSuperTypeRef(actualSuperTypeRef) } else -> { @@ -320,8 +312,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT diagnostic = ConeAmbiguousSuper(types) } } - superReferenceContainer.resultType = - resultType.copyWithNewSourceKind(KtFakeSourceElementKind.SuperCallExplicitType) + superReferenceContainer.resultType = resultType.type superReference.replaceSuperTypeRef(resultType) } } @@ -336,7 +327,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT val resultType = buildErrorTypeRef { diagnostic = superNotAvailableDiagnostic } - superReferenceContainer.resultType = resultType + superReferenceContainer.resultType = resultType.type superReference.replaceSuperTypeRef(resultType) superReferenceContainer.replaceCalleeReference(buildErrorNamedReference { source = superReferenceContainer.source?.fakeElement(KtFakeSourceElementKind.ReferenceInAtomicQualifiedAccess) @@ -404,7 +395,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT val calleeReference = functionCall.calleeReference if ( (calleeReference is FirResolvedNamedReference || calleeReference is FirErrorNamedReference) && - functionCall.resultType is FirImplicitTypeRef + functionCall.resultType == null ) { storeTypeFromCallee(functionCall, isLhsOfAssignment = false) } @@ -496,7 +487,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT val integerOperatorCall = buildIntegerLiteralOperatorCall { source = originalCall.source - typeRef = originalCall.typeRef.resolvedTypeFromPrototype(integerOperatorType) + coneTypeOrNull = integerOperatorType annotations.addAll(originalCall.annotations) typeArguments.addAll(originalCall.typeArguments) calleeReference = originalCall.calleeReference @@ -543,7 +534,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT block.transformOtherChildren(transformer, data) if (data is ResolutionMode.WithExpectedType && data.expectedTypeRef.coneTypeSafe()?.isUnitOrFlexibleUnit == true) { // Unit-coercion - block.resultType = data.expectedTypeRef + block.resultType = data.expectedTypeRef.type } else { // Bottom-up propagation: from the return type of the last expression in the block to the block type block.writeResultType(session) @@ -568,7 +559,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT data: ResolutionMode ): FirStatement = whileAnalysing(session, comparisonExpression) { return (comparisonExpression.transformChildren(transformer, ResolutionMode.ContextIndependent) as FirComparisonExpression).also { - it.resultType = comparisonExpression.typeRef.resolvedTypeFromPrototype(builtinTypes.booleanType.type) + it.resultType = builtinTypes.booleanType.type dataFlowAnalyzer.exitComparisonExpressionCall(it) } } @@ -729,7 +720,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT source = desugaredSource, name = name, initializer = initializer, - typeRef = initializer.typeRef.copyWithNewSource(desugaredSource), + typeRef = initializer.coneTypeOrNull?.toFirResolvedTypeRef(desugaredSource), ) fun buildAndResolveOperatorCall(receiver: FirExpression): FirFunctionCall = buildFunctionCall { @@ -786,7 +777,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT statements += unaryVariable.toQualifiedAccess() } }.apply { - replaceTypeRef((statements.last() as FirExpression).typeRef.copyWithNewSource(null)) + replaceConeTypeOrNull((statements.last() as FirExpression).coneTypeOrNull) } return if (originalExpression is FirSafeCallExpression) { @@ -813,7 +804,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT equalityOperatorCall .transformAnnotations(transformer, ResolutionMode.ContextIndependent) .replaceArgumentList(buildBinaryArgumentList(leftArgumentTransformed, rightArgumentTransformed)) - equalityOperatorCall.resultType = equalityOperatorCall.typeRef.resolvedTypeFromPrototype(builtinTypes.booleanType.type) + equalityOperatorCall.resultType = builtinTypes.booleanType.type dataFlowAnalyzer.exitEqualityOperatorCall(equalityOperatorCall) return equalityOperatorCall @@ -887,22 +878,15 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT when (resolved.operation) { FirOperation.IS, FirOperation.NOT_IS -> { - resolved.resultType = session.builtinTypes.booleanType + resolved.resultType = session.builtinTypes.booleanType.type } FirOperation.AS -> { - resolved.resultType = buildResolvedTypeRef { - source = conversionTypeRef.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef) - type = conversionTypeRef.coneType - annotations += conversionTypeRef.annotations - } + resolved.resultType = conversionTypeRef.coneType } FirOperation.SAFE_AS -> { - resolved.resultType = - conversionTypeRef.withReplacedConeType( - conversionTypeRef.coneType.withNullability( - ConeNullability.NULLABLE, session.typeContext, - ), - ) + resolved.resultType = conversionTypeRef.coneType.withNullability( + ConeNullability.NULLABLE, session.typeContext, + ) } else -> error("Unknown type operator: ${resolved.operation}") } @@ -949,7 +933,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT // fun checkNotNull(arg: K?): K // ...in order to get the not-nullable type of the argument. - if (checkNotNullCall.calleeReference is FirResolvedNamedReference && checkNotNullCall.resultType !is FirImplicitTypeRef) { + if (checkNotNullCall.calleeReference is FirResolvedNamedReference && checkNotNullCall.resultType != null) { return checkNotNullCall } @@ -970,7 +954,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT binaryLogicExpression: FirBinaryLogicExpression, data: ResolutionMode, ): FirStatement = whileAnalysing(session, binaryLogicExpression) { - val booleanType = binaryLogicExpression.typeRef.resolvedTypeFromPrototype(builtinTypes.booleanType.type) + val booleanType = builtinTypes.booleanType.type.toFirResolvedTypeRef() return binaryLogicExpression.also(dataFlowAnalyzer::enterBinaryLogicExpression) .transformLeftOperand(this, ResolutionMode.WithExpectedType(booleanType)) .also(dataFlowAnalyzer::exitLeftBinaryLogicExpressionArgument) @@ -978,25 +962,22 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT .transformRightOperand(this, ResolutionMode.WithExpectedType(booleanType)) .also(dataFlowAnalyzer::exitBinaryLogicExpression) .transformOtherChildren(transformer, ResolutionMode.WithExpectedType(booleanType)) - .also { it.resultType = booleanType } + .also { it.resultType = booleanType.type } } override fun transformDesugaredAssignmentValueReferenceExpression( desugaredAssignmentValueReferenceExpression: FirDesugaredAssignmentValueReferenceExpression, - data: ResolutionMode + data: ResolutionMode, ): FirStatement { val referencedExpression = desugaredAssignmentValueReferenceExpression.expressionRef.value if (referencedExpression is FirQualifiedAccessExpression) { val typeFromCallee = components.typeFromCallee(referencedExpression) - desugaredAssignmentValueReferenceExpression.resultType = typeFromCallee.withReplacedConeType( - session.typeApproximator.approximateToSubType( - typeFromCallee.type, - TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference - ) - ) + desugaredAssignmentValueReferenceExpression.resultType = session.typeApproximator.approximateToSubType( + typeFromCallee.type, + TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference + ) ?: typeFromCallee.type } else { - desugaredAssignmentValueReferenceExpression.resultType = - referencedExpression.resultType.copyWithNewSourceKind(KtFakeSourceElementKind.ImplicitTypeRef) + desugaredAssignmentValueReferenceExpression.resultType = referencedExpression.resultType } return desugaredAssignmentValueReferenceExpression } @@ -1036,7 +1017,10 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT val result = variableAssignment.transformRValue( transformer, - withExpectedType(variableAssignment.lValue.typeRef, expectedTypeMismatchIsReportedInChecker = true), + withExpectedType( + variableAssignment.lValue.coneTypeOrNull?.toFirResolvedTypeRef() ?: FirImplicitTypeRefImplWithoutSource, + expectedTypeMismatchIsReportedInChecker = true + ), ) (result as? FirVariableAssignment)?.let { dataFlowAnalyzer.exitVariableAssignment(it) } @@ -1089,7 +1073,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT getClassCall.transformAnnotations(transformer, ResolutionMode.ContextIndependent) val arg = getClassCall.argument val dataForLhs = if (arg is FirConstExpression<*>) { - withExpectedType(arg.typeRef.resolvedTypeFromPrototype(arg.kind.expectedConeType(session))) + withExpectedType(arg.kind.expectedConeType(session).toFirResolvedTypeRef()) } else { ResolutionMode.ContextIndependent } @@ -1124,14 +1108,14 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT ConeStarProjection } } - val coneType = symbol?.constructType(typeArguments, isNullable = false) - if (coneType != null) { - lhs.replaceTypeRef( - buildResolvedTypeRef { type = coneType }.also { + val type = symbol?.constructType(typeArguments, isNullable = false) + if (type != null) { + lhs.replaceConeTypeOrNull( + type.also { session.lookupTracker?.recordTypeResolveAsLookup(it, getClassCall.source, components.file.source) } ) - coneType + type } else { lhs.coneType } @@ -1143,18 +1127,15 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT else -> { if (!shouldComputeTypeOfGetClassCallWithNotQualifierInLhs(getClassCall)) return transformedGetClassCall val resultType = lhs.resultType - if (resultType is FirErrorTypeRef) { - resultType.coneType + if (resultType is ConeErrorType) { + resultType } else { - ConeKotlinTypeProjectionOut(resultType.coneType) + ConeKotlinTypeProjectionOut(resultType!!) } } } - transformedGetClassCall.resultType = - buildResolvedTypeRef { - type = StandardClassIds.KClass.constructClassLikeType(arrayOf(typeOfExpression), false) - } + transformedGetClassCall.resultType = StandardClassIds.KClass.constructClassLikeType(arrayOf(typeOfExpression), false) dataFlowAnalyzer.exitGetClassCall(transformedGetClassCall) return transformedGetClassCall } @@ -1206,7 +1187,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT } dataFlowAnalyzer.exitConstExpression(constExpression as FirConstExpression<*>) - constExpression.resultType = constExpression.resultType.resolvedTypeFromPrototype(type) + constExpression.resultType = type return when (val resolvedType = constExpression.coneType) { is ConeErrorType -> buildErrorExpression { @@ -1513,11 +1494,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT statements += indexVariables statements += setCall }.also { - it.replaceTypeRef( - buildResolvedTypeRef { - type = session.builtinTypes.unitType.type - } - ) + it.replaceConeTypeOrNull(session.builtinTypes.unitType.type) } } } @@ -1537,7 +1514,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT source = lhsGetCall.explicitReceiver?.source?.fakeElement(KtFakeSourceElementKind.DesugaredCompoundAssignment), name = SpecialNames.ARRAY, initializer = initializer, - typeRef = initializer.typeRef.copyWithNewSourceKind(KtFakeSourceElementKind.DesugaredCompoundAssignment), + typeRef = initializer.coneTypeOrNull?.toFirResolvedTypeRef(initializer.source?.fakeElement(KtFakeSourceElementKind.DesugaredCompoundAssignment)), ) val indexVariables = lhsGetCall.arguments.flatMap { @@ -1551,7 +1528,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT source = index.source?.fakeElement(KtFakeSourceElementKind.DesugaredCompoundAssignment), name = SpecialNames.subscribeOperatorIndex(i), initializer = index, - typeRef = index.typeRef, + typeRef = index.coneTypeOrNull?.toFirResolvedTypeRef(), ) } @@ -1581,7 +1558,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT arguments += indicesQualifiedAccess.subList(i, i + varargSize) i += varargSize source = argument.source - typeRef = argument.typeRef + coneTypeOrNull = argument.coneTypeOrNull varargElementType = argument.varargElementType } } else { @@ -1590,7 +1567,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT } } origin = FirFunctionCallOrigin.Operator - typeRef = lhsGetCall.typeRef + coneTypeOrNull = lhsGetCall.coneTypeOrNull } val generator = GeneratorOfPlusAssignCalls( @@ -1665,14 +1642,11 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT override fun transformAnonymousObjectExpression( anonymousObjectExpression: FirAnonymousObjectExpression, - data: ResolutionMode + data: ResolutionMode, ): FirStatement { anonymousObjectExpression.transformAnonymousObject(transformer, data) - if (anonymousObjectExpression.typeRef !is FirResolvedTypeRef) { - anonymousObjectExpression.resultType = buildResolvedTypeRef { - source = anonymousObjectExpression.source?.fakeElement(KtFakeSourceElementKind.ImplicitTypeRef) - this.type = anonymousObjectExpression.anonymousObject.defaultType() - } + if (anonymousObjectExpression.coneTypeOrNull == null) { + anonymousObjectExpression.resultType = anonymousObjectExpression.anonymousObject.defaultType() } dataFlowAnalyzer.exitAnonymousObjectExpression(anonymousObjectExpression) return anonymousObjectExpression @@ -1690,17 +1664,15 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT internal fun storeTypeFromCallee(access: FirQualifiedAccessExpression, isLhsOfAssignment: Boolean) { val typeFromCallee = components.typeFromCallee(access) - access.resultType = typeFromCallee.withReplacedConeType( - if (isLhsOfAssignment) { - session.typeApproximator.approximateToSubType( - typeFromCallee.type, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference - ) - } else { - session.typeApproximator.approximateToSuperType( - typeFromCallee.type, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference - ) - } - ) + access.resultType = if (isLhsOfAssignment) { + session.typeApproximator.approximateToSubType( + typeFromCallee.type, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference + ) + } else { + session.typeApproximator.approximateToSuperType( + typeFromCallee.type, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference + ) + } ?: typeFromCallee.type } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/contracts/FirAbstractContractResolveTransformerDispatcher.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/contracts/FirAbstractContractResolveTransformerDispatcher.kt index bbb87e1e639..15da36056a8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/contracts/FirAbstractContractResolveTransformerDispatcher.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/contracts/FirAbstractContractResolveTransformerDispatcher.kt @@ -156,7 +156,7 @@ abstract class FirAbstractContractResolveTransformerDispatcher( val resolvedContractCall = withContractModeDisabled { contractDescription.contractCall .transformSingle(transformer, ResolutionMode.ContextIndependent) - .apply { replaceTypeRef(session.builtinTypes.unitType) } + .apply { replaceConeTypeOrNull(session.builtinTypes.unitType.type) } } if (resolvedContractCall.toResolvedCallableSymbol()?.callableId != FirContractsDslNames.CONTRACT) { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/AbstractFirSpecificAnnotationResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/AbstractFirSpecificAnnotationResolveTransformer.kt index c48e1296b01..ac5d69e9ca6 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/AbstractFirSpecificAnnotationResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/AbstractFirSpecificAnnotationResolveTransformer.kt @@ -37,7 +37,6 @@ import org.jetbrains.kotlin.fir.types.builder.buildStarProjection import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance import org.jetbrains.kotlin.fir.types.builder.buildUserTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl -import org.jetbrains.kotlin.fir.types.impl.FirImplicitUnitTypeRef import org.jetbrains.kotlin.fir.types.impl.FirQualifierPartImpl import org.jetbrains.kotlin.fir.types.impl.FirTypeArgumentListImpl import org.jetbrains.kotlin.fir.visitors.FirDefaultTransformer @@ -144,7 +143,7 @@ abstract class AbstractFirSpecificAnnotationResolveTransformer( source = receiver.source packageFqName = symbol.classId.packageFqName relativeClassFqName = symbol.classId.relativeClassName - typeRef = FirImplicitUnitTypeRef(receiver.typeRef.source) + coneTypeOrNull = session.builtinTypes.unitType.type this.symbol = symbol isFullyQualified = segments.isNotEmpty() } @@ -190,7 +189,7 @@ abstract class AbstractFirSpecificAnnotationResolveTransformer( calleeSymbol.containingClassLookupTag() ?.let { ConeClassLikeTypeImpl(it, emptyArray(), false) } - ?.let { replaceTypeRef(typeRef.resolvedTypeFromPrototype(it)) } + ?.let { replaceConeTypeOrNull(it) } } } diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/FirLookupTrackerComponent.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/FirLookupTrackerComponent.kt index 4dedbe600f2..ddab7bf878d 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/FirLookupTrackerComponent.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/FirLookupTrackerComponent.kt @@ -37,25 +37,25 @@ fun FirLookupTrackerComponent.recordTypeLookup(typeRef: FirTypeRef, inScopes: Li fun FirLookupTrackerComponent.recordTypeResolveAsLookup(typeRef: FirTypeRef, source: KtSourceElement?, fileSource: KtSourceElement?) { if (typeRef !is FirResolvedTypeRef) return // TODO: check if this is the correct behavior - if (source == null && fileSource == null) return // TODO: investigate all cases + recordTypeResolveAsLookup(typeRef.type, source, fileSource) +} - fun recordIfValid(type: ConeKotlinType) { - if (type is ConeErrorType) return // TODO: investigate whether some cases should be recorded, e.g. unresolved - type.classId?.let { - if (!it.isLocal) { - if (it.shortClassName.asString() != "Companion") { - recordLookup(it.shortClassName, it.packageFqName.asString(), source, fileSource) - } else { - recordLookup(it.outerClassId!!.shortClassName, it.outerClassId!!.packageFqName.asString(), source, fileSource) - } +fun FirLookupTrackerComponent.recordTypeResolveAsLookup(type: ConeKotlinType?, source: KtSourceElement?, fileSource: KtSourceElement?) { + if (type == null) return + if (source == null && fileSource == null) return // TODO: investigate all cases + if (type is ConeErrorType) return // TODO: investigate whether some cases should be recorded, e.g. unresolved + type.classId?.let { + if (!it.isLocal) { + if (it.shortClassName.asString() != "Companion") { + recordLookup(it.shortClassName, it.packageFqName.asString(), source, fileSource) + } else { + recordLookup(it.outerClassId!!.shortClassName, it.outerClassId!!.packageFqName.asString(), source, fileSource) } } - type.typeArguments.forEach { - if (it is ConeKotlinType) recordIfValid(it) - } } - - recordIfValid(typeRef.type) + type.typeArguments.forEach { + if (it is ConeKotlinType) recordTypeResolveAsLookup(it, source, fileSource) + } } diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt index 43b13adc409..56b8d247535 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt @@ -16,13 +16,13 @@ import org.jetbrains.kotlin.fir.resolve.dfa.FlowPath import org.jetbrains.kotlin.fir.resolve.dfa.PersistentFlow import org.jetbrains.kotlin.fir.resolve.dfa.controlFlowGraph import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol -import org.jetbrains.kotlin.fir.types.FirTypeRef -import org.jetbrains.kotlin.fir.types.UnexpandedTypeCheck +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.coneType -import org.jetbrains.kotlin.fir.types.impl.FirImplicitNothingTypeRef +import org.jetbrains.kotlin.fir.types.constructClassLikeType import org.jetbrains.kotlin.fir.types.isNothing import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirVisitor +import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.utils.SmartList @RequiresOptIn @@ -869,16 +869,14 @@ class WhenSubjectExpressionExitNode(owner: ControlFlowGraph, override val fir: F object FirStub : FirExpression() { override val source: KtSourceElement? get() = null - override val typeRef: FirTypeRef = FirImplicitNothingTypeRef(null) + override val coneTypeOrNull: ConeKotlinType = StandardClassIds.Nothing.constructClassLikeType() override val annotations: List get() = listOf() override fun acceptChildren(visitor: FirVisitor, data: D) {} override fun transformAnnotations(transformer: FirTransformer, data: D): FirExpression = this override fun transformChildren(transformer: FirTransformer, data: D): FirElement = this override fun replaceAnnotations(newAnnotations: List) { assert(newAnnotations.isEmpty()) } - - @OptIn(UnexpandedTypeCheck::class) - override fun replaceTypeRef(newTypeRef: FirTypeRef) { assert(newTypeRef.isNothing) } + override fun replaceConeTypeOrNull(newConeTypeOrNull: ConeKotlinType?) { assert(newConeTypeOrNull?.isNothing == true) } } class FakeExpressionEnterNode(owner: ControlFlowGraph, level: Int) : CFGNode(owner, level), GraphEnterNodeMarker, GraphExitNodeMarker { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/EnumClassUtils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/EnumClassUtils.kt index 38de1a1d5d8..b6de06d4884 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/EnumClassUtils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/EnumClassUtils.kt @@ -69,7 +69,7 @@ fun FirRegularClassBuilder.generateValuesFunction( symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, ENUM_VALUES)) resolvePhase = this@generateValuesFunction.resolvePhase body = buildEmptyExpressionBlock().also { - it.replaceType(returnTypeRef.type) + it.replaceConeTypeOrNull(returnTypeRef.type) } }.apply { containingClassForStaticMemberAttr = this@generateValuesFunction.symbol.toLookupTag() @@ -126,7 +126,7 @@ fun FirRegularClassBuilder.generateValueOfFunction( } resolvePhase = this@generateValueOfFunction.resolvePhase body = buildEmptyExpressionBlock().also { - it.replaceType(returnTypeRef.type) + it.replaceConeTypeOrNull(returnTypeRef.type) } }.apply { containingClassForStaticMemberAttr = this@generateValueOfFunction.symbol.toLookupTag() diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/builder/FirConstExpressionBuilder.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/builder/FirConstExpressionBuilder.kt index a56b9f5f4eb..dce43602bd2 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/builder/FirConstExpressionBuilder.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/builder/FirConstExpressionBuilder.kt @@ -24,20 +24,20 @@ fun buildConstExpression( return FirConstExpressionImpl(source, null, annotations.toMutableOrEmpty(), kind, value).also { if (setType) { when (kind) { - ConstantValueKind.Boolean -> it.type = StandardClassIds.Boolean.constructClassLikeType() - ConstantValueKind.Byte -> it.type = StandardClassIds.Byte.constructClassLikeType() - ConstantValueKind.Char -> it.type = StandardClassIds.Char.constructClassLikeType() - ConstantValueKind.Double -> it.type = StandardClassIds.Double.constructClassLikeType() - ConstantValueKind.Float -> it.type = StandardClassIds.Float.constructClassLikeType() - ConstantValueKind.Int -> it.type = StandardClassIds.Int.constructClassLikeType() - ConstantValueKind.Long -> it.type = StandardClassIds.Long.constructClassLikeType() - ConstantValueKind.Null -> it.type = StandardClassIds.Any.constructClassLikeType(isNullable = true) - ConstantValueKind.Short -> it.type = StandardClassIds.Short.constructClassLikeType() - ConstantValueKind.String -> it.type = StandardClassIds.String.constructClassLikeType() - ConstantValueKind.UnsignedByte -> it.type = StandardClassIds.UByte.constructClassLikeType() - ConstantValueKind.UnsignedInt -> it.type = StandardClassIds.UInt.constructClassLikeType() - ConstantValueKind.UnsignedLong -> it.type = StandardClassIds.ULong.constructClassLikeType() - ConstantValueKind.UnsignedShort -> it.type = StandardClassIds.UShort.constructClassLikeType() + ConstantValueKind.Boolean -> it.coneTypeOrNull = StandardClassIds.Boolean.constructClassLikeType() + ConstantValueKind.Byte -> it.coneTypeOrNull = StandardClassIds.Byte.constructClassLikeType() + ConstantValueKind.Char -> it.coneTypeOrNull = StandardClassIds.Char.constructClassLikeType() + ConstantValueKind.Double -> it.coneTypeOrNull = StandardClassIds.Double.constructClassLikeType() + ConstantValueKind.Float -> it.coneTypeOrNull = StandardClassIds.Float.constructClassLikeType() + ConstantValueKind.Int -> it.coneTypeOrNull = StandardClassIds.Int.constructClassLikeType() + ConstantValueKind.Long -> it.coneTypeOrNull = StandardClassIds.Long.constructClassLikeType() + ConstantValueKind.Null -> it.coneTypeOrNull = StandardClassIds.Any.constructClassLikeType(isNullable = true) + ConstantValueKind.Short -> it.coneTypeOrNull = StandardClassIds.Short.constructClassLikeType() + ConstantValueKind.String -> it.coneTypeOrNull = StandardClassIds.String.constructClassLikeType() + ConstantValueKind.UnsignedByte -> it.coneTypeOrNull = StandardClassIds.UByte.constructClassLikeType() + ConstantValueKind.UnsignedInt -> it.coneTypeOrNull = StandardClassIds.UInt.constructClassLikeType() + ConstantValueKind.UnsignedLong -> it.coneTypeOrNull = StandardClassIds.ULong.constructClassLikeType() + ConstantValueKind.UnsignedShort -> it.coneTypeOrNull = StandardClassIds.UShort.constructClassLikeType() ConstantValueKind.IntegerLiteral, ConstantValueKind.UnsignedIntegerLiteral, ConstantValueKind.Error, diff --git a/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt b/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt index d4a88d45ff0..1bfb1d2a94e 100644 --- a/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt +++ b/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt @@ -209,11 +209,6 @@ sealed class KtFakeSourceElementKind(final override val shouldSkipErrorTypeRepor // where `Supertype` has a fake source object SuperCallImplicitType : KtFakeSourceElementKind() - // Consider `super.foo()`. The source PSI `Supertype` is referenced by both the qualified access expression - // `super` and the calleeExpression `super`. To avoid having two FIR elements sharing the same source, this fake - // source is assigned to the qualified access expression. - object SuperCallExplicitType : KtFakeSourceElementKind(shouldSkipErrorTypeReporting = true) - // fun foo(vararg args: Int) {} // fun bar(1, 2, 3) --> [resolved] fun bar(VarargArgument(1, 2, 3)) object VarargArgument : KtFakeSourceElementKind() diff --git a/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt b/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt index edfdff53079..194fbd11890 100644 --- a/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt +++ b/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt @@ -17,7 +17,7 @@ class MyColor(val x: Color.RED, y: Color.RED fun local(arg: Color.RED): Color.RED = arg val temp: Color.RED = Color.RED - temp as? Color.RED + temp as? Color.RED if (temp is Color.RED) { return temp as Color.RED } diff --git a/compiler/testData/diagnostics/tests/kt49438.fir.kt b/compiler/testData/diagnostics/tests/kt49438.fir.kt index 113c45a9f6b..4702376d00f 100644 --- a/compiler/testData/diagnostics/tests/kt49438.fir.kt +++ b/compiler/testData/diagnostics/tests/kt49438.fir.kt @@ -1,7 +1,7 @@ fun foo(x: K) {} val x1 = foo<(unresolved) -> Float> { it.toFloat() } val x2 = foo<(unresolved) -> Float> { it -> it.toFloat() } -val x3 = foo<unresolved.() -> Float> { this.toFloat() } +val x3 = foo<unresolved.() -> Float> { this.toFloat() } val x4 = foo<(Array<unresolved>) -> Int> { it.size } fun bar() = foo<(T) -> String> { it.toString() } diff --git a/compiler/testData/diagnostics/tests/resolve/HiddenDeclarations.fir.kt b/compiler/testData/diagnostics/tests/resolve/HiddenDeclarations.fir.kt index 4ae5957be57..09ad8d947cf 100644 --- a/compiler/testData/diagnostics/tests/resolve/HiddenDeclarations.fir.kt +++ b/compiler/testData/diagnostics/tests/resolve/HiddenDeclarations.fir.kt @@ -40,7 +40,7 @@ open class A { fun foo() { topLevelFun() topLevelFun(1) - topLevelProperty++ + topLevelProperty++ "".topLevelExtensionFun() 1.topLevelExtensionFun() "".topLevelExtensionProperty diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt index f759458e86f..01c0f2bd70c 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt @@ -75,3 +75,12 @@ fun test5() { c.x.length // bad d.x.length // ok } + +fun test6() { + var c: C? = null + var maybeC: C? = C("") + if (c == null) { + c = maybeC ?: throw Exception() + } + c.x +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt index ea659791116..11586b97694 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt @@ -75,3 +75,12 @@ fun test5() { c.x.length // bad d.x.length // ok } + +fun test6() { + var c: C? = null + var maybeC: C? = C("") + if (c == null) { + c = maybeC ?: throw Exception() + } + c.x +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/typeVariableShouldNotBeFixed.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/typeVariableShouldNotBeFixed.fir.kt index a9f6f0453fe..3556c030def 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/builderInference/typeVariableShouldNotBeFixed.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/builderInference/typeVariableShouldNotBeFixed.fir.kt @@ -25,7 +25,7 @@ fun materialize(): Processor = TODO() private fun foo(model: Model) { materialize().apply { context( - this, + this, Exec { m, p -> p.process(m) } // Note: Builder inference ) } diff --git a/compiler/visualizer/render-fir/src/org/jetbrains/kotlin/compiler/visualizer/FirVisualizer.kt b/compiler/visualizer/render-fir/src/org/jetbrains/kotlin/compiler/visualizer/FirVisualizer.kt index 0fff61452cb..162104b2414 100644 --- a/compiler/visualizer/render-fir/src/org/jetbrains/kotlin/compiler/visualizer/FirVisualizer.kt +++ b/compiler/visualizer/render-fir/src/org/jetbrains/kotlin/compiler/visualizer/FirVisualizer.kt @@ -47,7 +47,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { private fun Stack.push( levelName: String, - defaultValues: MutableList = mutableListOf() + defaultValues: MutableList = mutableListOf(), ) = this.add(levelName to defaultValues) private fun Stack.pop() = this.removeAt(this.size - 1) @@ -164,7 +164,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { if (function.isLocal) stack.addName(function.name ?: ANONYMOUS_NAME) stack.push((function.name ?: ANONYMOUS_NAME)) if (function.equalsToken != null) { - function.bodyExpression!!.firstOfTypeWithRender(function.equalsToken) { this.result.typeRef } + function.bodyExpression!!.firstOfTypeWithRender(function.equalsToken) { this.result.coneType.toFirResolvedTypeRef() } ?: function.firstOfTypeWithRender(function.equalsToken) { this.returnTypeRef } } super.visitNamedFunction(function) @@ -249,12 +249,12 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { } override fun visitIfExpression(expression: KtIfExpression) { - expression.firstOfTypeWithRender { this.typeRef } + expression.firstOfTypeWithRender { this.coneType.toFirResolvedTypeRef() } super.visitIfExpression(expression) } override fun visitWhenExpression(expression: KtWhenExpression) { - expression.firstOfTypeWithRender { this.typeRef } + expression.firstOfTypeWithRender { this.coneType.toFirResolvedTypeRef() } super.visitWhenExpression(expression) } @@ -306,7 +306,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { } override fun visitWhenEntry(ktWhenEntry: KtWhenEntry) { - ktWhenEntry.firstOfTypeWithRender(ktWhenEntry.expression) { this.result.typeRef } + ktWhenEntry.firstOfTypeWithRender(ktWhenEntry.expression) { this.result.coneType.toFirResolvedTypeRef() } super.visitWhenEntry(ktWhenEntry) } @@ -564,7 +564,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { fir.receiverParameter?.accept(this, data) data.append(".").append(callableName) } - call.dispatchReceiver.typeRef.annotations.any { it.isExtensionFunctionAnnotationCall } -> { + call.dispatchReceiver.coneType.isExtensionFunctionType -> { withExtensionFunctionType = true fir.valueParameters.first().returnTypeRef.accept(this, data) data.append(".").append(callableName) @@ -786,7 +786,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { override fun visitConstExpression(constExpression: FirConstExpression, data: StringBuilder) { when (constExpression.kind) { ConstantValueKind.String -> return - ConstantValueKind.Null -> constExpression.typeRef.accept(this, data) + ConstantValueKind.Null -> constExpression.coneType.tryToRenderConeAsFunctionType(data) else -> data.append(constExpression.kind) } } @@ -796,7 +796,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() { when { fir is FirRegularClass && fir.classKind != ClassKind.ENUM_CLASS && fir.companionObjectSymbol?.defaultType() == resolvedQualifier.coneTypeSafe() -> { data.append("companion object ") - data.append(resolvedQualifier.typeRef.render()).append(": ") + data.append(resolvedQualifier.coneType.toFirResolvedTypeRef().render()).append(": ") data.append(fir.symbol.classId.asString().removeCurrentFilePackage()) } fir is FirClass -> { diff --git a/plugins/assign-plugin/assign-plugin.k2/src/org/jetbrains/kotlin/assignment/plugin/k2/FirAssignmentPluginAssignAltererExtension.kt b/plugins/assign-plugin/assign-plugin.k2/src/org/jetbrains/kotlin/assignment/plugin/k2/FirAssignmentPluginAssignAltererExtension.kt index 55608ae23f5..7bf9bff8f60 100644 --- a/plugins/assign-plugin/assign-plugin.k2/src/org/jetbrains/kotlin/assignment/plugin/k2/FirAssignmentPluginAssignAltererExtension.kt +++ b/plugins/assign-plugin/assign-plugin.k2/src/org/jetbrains/kotlin/assignment/plugin/k2/FirAssignmentPluginAssignAltererExtension.kt @@ -54,7 +54,7 @@ class FirAssignmentPluginAssignAltererExtension( source = variableAssignment.source?.fakeElement(KtFakeSourceElementKind.DesugaredCompoundAssignment) explicitReceiver = buildPropertyAccessExpression { source = leftArgument.source - typeRef = leftResolvedType + coneTypeOrNull = leftResolvedType.type calleeReference = leftArgument (variableAssignment.lValue as? FirQualifiedAccessExpression)?.typeArguments?.let(typeArguments::addAll) annotations += variableAssignment.annotations @@ -71,4 +71,4 @@ class FirAssignmentPluginAssignAltererExtension( origin = FirFunctionCallOrigin.Regular } } -} \ No newline at end of file +} diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt index 837342dd7f5..22275fb1400 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt @@ -57,7 +57,7 @@ class MembersOfSerializerGenerator(session: FirSession) : FirDeclarationGenerati val function = createMemberFunction(owner, Key, callableId.callableName, session.builtinTypes.unitType.type) { valueParameter(X_NAME, argumentClassId.createConeType(session)) }.apply { - replaceBody(buildBlock {}.apply { replaceTypeRef(session.builtinTypes.unitType) }) + replaceBody(buildBlock {}.apply { replaceConeTypeOrNull(session.builtinTypes.unitType.type) }) } return listOf(function.symbol) } diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/services/FirScriptConfigurationExtensionImpl.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/services/FirScriptConfigurationExtensionImpl.kt index 8b9c98016e7..928b6b32e4b 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/services/FirScriptConfigurationExtensionImpl.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/services/FirScriptConfigurationExtensionImpl.kt @@ -26,8 +26,11 @@ import org.jetbrains.kotlin.fir.symbols.SymbolInternals import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol import org.jetbrains.kotlin.fir.types.builder.buildUserTypeRef +import org.jetbrains.kotlin.fir.types.coneTypeOrNull +import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImplWithoutSource import org.jetbrains.kotlin.fir.types.impl.FirQualifierPartImpl import org.jetbrains.kotlin.fir.types.impl.FirTypeArgumentListImpl +import org.jetbrains.kotlin.fir.types.toFirResolvedTypeRef import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name @@ -148,12 +151,12 @@ class FirScriptConfiguratorExtensionImpl( moduleData = session.moduleData origin = FirDeclarationOrigin.ScriptCustomization.ResultProperty initializer = lastExpression - returnTypeRef = lastExpression.typeRef + returnTypeRef = lastExpression.coneTypeOrNull?.toFirResolvedTypeRef() ?: FirImplicitTypeRefImplWithoutSource getter = FirDefaultPropertyGetter( lastExpression.source, session.moduleData, FirDeclarationOrigin.ScriptCustomization.ResultProperty, - lastExpression.typeRef, + lastExpression.coneTypeOrNull?.toFirResolvedTypeRef() ?: FirImplicitTypeRefImplWithoutSource, Visibilities.Public, this.symbol, )