From a59cedcd39bbb01d68116ebb0e3b9c8e8c81e570 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 4 Aug 2020 11:58:56 +0300 Subject: [PATCH] [FIR] Fix non-fake sources for constructor delegates --- .../FirCommonConstructorDelegationIssuesChecker.kt | 11 ++++++++--- .../FirDelegationSuperCallInEnumConstructorChecker.kt | 6 ++---- .../fir/lightTree/converter/DeclarationsConverter.kt | 6 +++++- .../org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt | 6 +++++- .../rawBuilder/declarations/constructorInObject.txt | 2 +- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirCommonConstructorDelegationIssuesChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirCommonConstructorDelegationIssuesChecker.kt index a2db96ab9f9..509af4907ef 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirCommonConstructorDelegationIssuesChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirCommonConstructorDelegationIssuesChecker.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration +import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter @@ -48,8 +49,12 @@ object FirCommonConstructorDelegationIssuesChecker : FirMemberDeclarationChecker if (hasPrimaryConstructor) { for (it in otherConstructors) { - if (it.delegatedConstructor?.isThis == false) { - reporter.reportPrimaryConstructorDelegationCallExpected(it.delegatedConstructor?.source) + if (it.delegatedConstructor?.isThis != true) { + if (it.delegatedConstructor?.source != null) { + reporter.reportPrimaryConstructorDelegationCallExpected(it.delegatedConstructor?.source) + } else { + reporter.reportPrimaryConstructorDelegationCallExpected(it.source) + } } } } else { @@ -59,7 +64,7 @@ object FirCommonConstructorDelegationIssuesChecker : FirMemberDeclarationChecker // couldn't find proper super() constructor implicitly if ( callee is FirErrorNamedReference && callee.diagnostic is ConeAmbiguityError && - it.delegatedConstructor?.source?.startOffset == it.delegatedConstructor?.source?.endOffset + it.delegatedConstructor?.source?.kind is FirFakeSourceElementKind ) { reporter.reportExplicitDelegationCallRequired(it.source) } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegationSuperCallInEnumConstructorChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegationSuperCallInEnumConstructorChecker.kt index 323c23f2176..285bfb8c23f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegationSuperCallInEnumConstructorChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegationSuperCallInEnumConstructorChecker.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter @@ -21,13 +22,10 @@ object FirDelegationSuperCallInEnumConstructorChecker : FirMemberDeclarationChec } for (it in declaration.declarations) { - // checking offsets is needed because we - // still have a real source element ("") here, - // even if the user hasn't written anything if ( it is FirConstructor && !it.isPrimary && it.delegatedConstructor?.isThis == false && - it.delegatedConstructor?.source?.startOffset != it.delegatedConstructor?.source?.endOffset + it.delegatedConstructor?.source?.kind !is FirFakeSourceElementKind ) { reporter.report(it.delegatedConstructor?.source) } diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index 31311893c36..7550e355f53 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -842,7 +842,11 @@ class DeclarationsConverter( } return buildDelegatedConstructorCall { - source = constructorDelegationCall.toFirSourceElement() + source = if (isImplicit) { + constructorDelegationCall.toFirSourceElement().fakeElement(FirFakeSourceElementKind.ImplicitConstructor) + } else { + constructorDelegationCall.toFirSourceElement() + } constructedTypeRef = delegatedType this.isThis = isThis extractArgumentsFrom(firValueArguments, stubMode) diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 8d207320465..e8ff2ad7955 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -1096,7 +1096,11 @@ class RawFirBuilder( hasPrimaryConstructor: Boolean, ): FirDelegatedConstructorCall { val isThis = isCallToThis //|| (isImplicit && hasPrimaryConstructor) - val source = this.toFirSourceElement() + val source = if (isImplicit) { + this.toFirSourceElement(FirFakeSourceElementKind.ImplicitConstructor) + } else { + this.toFirSourceElement() + } val delegatedType = when { isThis -> delegatedSelfTypeRef else -> delegatedSuperTypeRef diff --git a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/constructorInObject.txt b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/constructorInObject.txt index 0fb314d5dd2..cc7128cace8 100644 --- a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/constructorInObject.txt +++ b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/constructorInObject.txt @@ -19,7 +19,7 @@ FILE: constructorInObject.kt } public? constructor(): R|| { - this|>() + super() } }