[FIR] Only one specific diagnostic for data class without parameters left

^KT-60042 fixed
This commit is contained in:
Ilya Goncharov
2023-11-01 13:58:44 +00:00
committed by Space Team
parent 17dc60aac9
commit 3673dff959
13 changed files with 13 additions and 37 deletions
@@ -46,7 +46,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtObjectDeclaration import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtReturnExpression
@@ -824,12 +823,6 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token, token,
) )
} }
add(FirErrors.PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS) { firDiagnostic ->
PrimaryConstructorRequiredForDataClassImpl(
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.EXPLICIT_DELEGATION_CALL_REQUIRED) { firDiagnostic -> add(FirErrors.EXPLICIT_DELEGATION_CALL_REQUIRED) { firDiagnostic ->
ExplicitDelegationCallRequiredImpl( ExplicitDelegationCallRequiredImpl(
firDiagnostic as KtPsiDiagnostic, firDiagnostic as KtPsiDiagnostic,
@@ -65,7 +65,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtObjectDeclaration import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtReturnExpression
@@ -616,10 +615,6 @@ sealed interface KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
override val diagnosticClass get() = DelegationSuperCallInEnumConstructor::class override val diagnosticClass get() = DelegationSuperCallInEnumConstructor::class
} }
interface PrimaryConstructorRequiredForDataClass : KtFirDiagnostic<KtNamedDeclaration> {
override val diagnosticClass get() = PrimaryConstructorRequiredForDataClass::class
}
interface ExplicitDelegationCallRequired : KtFirDiagnostic<PsiElement> { interface ExplicitDelegationCallRequired : KtFirDiagnostic<PsiElement> {
override val diagnosticClass get() = ExplicitDelegationCallRequired::class override val diagnosticClass get() = ExplicitDelegationCallRequired::class
} }
@@ -628,7 +623,7 @@ sealed interface KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
override val diagnosticClass get() = SealedClassConstructorCall::class override val diagnosticClass get() = SealedClassConstructorCall::class
} }
interface DataClassWithoutParameters : KtFirDiagnostic<KtPrimaryConstructor> { interface DataClassWithoutParameters : KtFirDiagnostic<KtNamedDeclaration> {
override val diagnosticClass get() = DataClassWithoutParameters::class override val diagnosticClass get() = DataClassWithoutParameters::class
} }
@@ -66,7 +66,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtObjectDeclaration import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtReturnExpression
@@ -728,11 +727,6 @@ internal class DelegationSuperCallInEnumConstructorImpl(
token: KtLifetimeToken, token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<PsiElement>(firDiagnostic, token), KtFirDiagnostic.DelegationSuperCallInEnumConstructor ) : KtAbstractFirDiagnostic<PsiElement>(firDiagnostic, token), KtFirDiagnostic.DelegationSuperCallInEnumConstructor
internal class PrimaryConstructorRequiredForDataClassImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtNamedDeclaration>(firDiagnostic, token), KtFirDiagnostic.PrimaryConstructorRequiredForDataClass
internal class ExplicitDelegationCallRequiredImpl( internal class ExplicitDelegationCallRequiredImpl(
firDiagnostic: KtPsiDiagnostic, firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken, token: KtLifetimeToken,
@@ -746,7 +740,7 @@ internal class SealedClassConstructorCallImpl(
internal class DataClassWithoutParametersImpl( internal class DataClassWithoutParametersImpl(
firDiagnostic: KtPsiDiagnostic, firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken, token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtPrimaryConstructor>(firDiagnostic, token), KtFirDiagnostic.DataClassWithoutParameters ) : KtAbstractFirDiagnostic<KtNamedDeclaration>(firDiagnostic, token), KtFirDiagnostic.DataClassWithoutParameters
internal class DataClassVarargParameterImpl( internal class DataClassVarargParameterImpl(
firDiagnostic: KtPsiDiagnostic, firDiagnostic: KtPsiDiagnostic,
@@ -1,9 +1,9 @@
data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class A<!> {} data <!DATA_CLASS_WITHOUT_PARAMETERS!>class A<!> {}
data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class B<!> { data <!DATA_CLASS_WITHOUT_PARAMETERS!>class B<!> {
constructor() constructor()
} }
data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class C<!> { data <!DATA_CLASS_WITHOUT_PARAMETERS!>class C<!> {
constructor(x: Int) constructor(x: Int)
} }
@@ -4,7 +4,7 @@
private sealed class Sealed private sealed class Sealed
private data class SubClass1(val t: String) : Sealed() private data class SubClass1(val t: String) : Sealed()
private data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class SubClass2<!> : Sealed() private data <!DATA_CLASS_WITHOUT_PARAMETERS!>class SubClass2<!> : Sealed()
private fun foo(p: Sealed) { private fun foo(p: Sealed) {
when (p) { when (p) {
@@ -307,12 +307,11 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val SUPERTYPE_NOT_INITIALIZED by error<KtTypeReference>() val SUPERTYPE_NOT_INITIALIZED by error<KtTypeReference>()
val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error<PsiElement>() val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error<PsiElement>()
val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by error<PsiElement>() val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by error<PsiElement>()
val PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS by error<KtNamedDeclaration>(PositioningStrategy.DECLARATION_NAME)
val EXPLICIT_DELEGATION_CALL_REQUIRED by error<PsiElement>(PositioningStrategy.SECONDARY_CONSTRUCTOR_DELEGATION_CALL) val EXPLICIT_DELEGATION_CALL_REQUIRED by error<PsiElement>(PositioningStrategy.SECONDARY_CONSTRUCTOR_DELEGATION_CALL)
val SEALED_CLASS_CONSTRUCTOR_CALL by error<PsiElement>() val SEALED_CLASS_CONSTRUCTOR_CALL by error<PsiElement>()
// TODO: Consider creating a parameter list position strategy and report on the parameter list instead // TODO: Consider creating a parameter list position strategy and report on the parameter list instead
val DATA_CLASS_WITHOUT_PARAMETERS by error<KtPrimaryConstructor>() val DATA_CLASS_WITHOUT_PARAMETERS by error<KtNamedDeclaration>(PositioningStrategy.DECLARATION_NAME)
val DATA_CLASS_VARARG_PARAMETER by error<KtParameter>() val DATA_CLASS_VARARG_PARAMETER by error<KtParameter>()
val DATA_CLASS_NOT_PROPERTY_PARAMETER by error<KtParameter>() val DATA_CLASS_NOT_PROPERTY_PARAMETER by error<KtParameter>()
} }
@@ -91,7 +91,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtObjectDeclaration import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtReturnExpression
@@ -245,10 +244,9 @@ object FirErrors {
val SUPERTYPE_NOT_INITIALIZED by error0<KtTypeReference>() val SUPERTYPE_NOT_INITIALIZED by error0<KtTypeReference>()
val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error0<PsiElement>() val SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR by error0<PsiElement>()
val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by error0<PsiElement>() val DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR by error0<PsiElement>()
val PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS by error0<KtNamedDeclaration>(SourceElementPositioningStrategies.DECLARATION_NAME)
val EXPLICIT_DELEGATION_CALL_REQUIRED by error0<PsiElement>(SourceElementPositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL) val EXPLICIT_DELEGATION_CALL_REQUIRED by error0<PsiElement>(SourceElementPositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL)
val SEALED_CLASS_CONSTRUCTOR_CALL by error0<PsiElement>() val SEALED_CLASS_CONSTRUCTOR_CALL by error0<PsiElement>()
val DATA_CLASS_WITHOUT_PARAMETERS by error0<KtPrimaryConstructor>() val DATA_CLASS_WITHOUT_PARAMETERS by error0<KtNamedDeclaration>(SourceElementPositioningStrategies.DECLARATION_NAME)
val DATA_CLASS_VARARG_PARAMETER by error0<KtParameter>() val DATA_CLASS_VARARG_PARAMETER by error0<KtParameter>()
val DATA_CLASS_NOT_PROPERTY_PARAMETER by error0<KtParameter>() val DATA_CLASS_NOT_PROPERTY_PARAMETER by error0<KtParameter>()
@@ -112,7 +112,6 @@ val FIR_NON_SUPPRESSIBLE_ERROR_NAMES: Set<String> = setOf(
"SUPERTYPE_NOT_INITIALIZED", "SUPERTYPE_NOT_INITIALIZED",
"SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR", "SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR",
"DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR", "DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR",
"PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS",
"EXPLICIT_DELEGATION_CALL_REQUIRED", "EXPLICIT_DELEGATION_CALL_REQUIRED",
"SEALED_CLASS_CONSTRUCTOR_CALL", "SEALED_CLASS_CONSTRUCTOR_CALL",
"DATA_CLASS_WITHOUT_PARAMETERS", "DATA_CLASS_WITHOUT_PARAMETERS",
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.fir.analysis.checkers.declaration package org.jetbrains.kotlin.fir.analysis.checkers.declaration
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.hasValOrVar import org.jetbrains.kotlin.diagnostics.hasValOrVar
@@ -24,8 +23,8 @@ object FirDataClassPrimaryConstructorChecker : FirRegularClassChecker() {
val primaryConstructor = declaration.primaryConstructorIfAny(context.session) val primaryConstructor = declaration.primaryConstructorIfAny(context.session)
if (primaryConstructor == null || primaryConstructor.source.let { it == null || it.kind is KtFakeSourceElementKind }) { if (primaryConstructor?.source == null) {
reporter.reportOn(declaration.source, FirErrors.PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS, context) reporter.reportOn(declaration.source, FirErrors.DATA_CLASS_WITHOUT_PARAMETERS, context)
return return
} }
@@ -496,7 +496,6 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PLUGIN_ANNOTATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.POTENTIALLY_NON_REPORTED_ANNOTATION import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.POTENTIALLY_NON_REPORTED_ANNOTATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRE_RELEASE_CLASS import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRE_RELEASE_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_CLASS_MEMBER_FROM_INLINE import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_CLASS_MEMBER_FROM_INLINE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_FUNCTION_WITH_NO_BODY import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_FUNCTION_WITH_NO_BODY
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_PROPERTY_IN_INTERFACE import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PRIVATE_PROPERTY_IN_INTERFACE
@@ -943,7 +942,6 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
map.put(PROTECTED_CONSTRUCTOR_NOT_IN_SUPER_CALL, "Protected constructor ''{0}'' from other classes can only be used in super-call.", SYMBOL) map.put(PROTECTED_CONSTRUCTOR_NOT_IN_SUPER_CALL, "Protected constructor ''{0}'' from other classes can only be used in super-call.", SYMBOL)
map.put(SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR, "Supertype initialization is impossible without a primary constructor.") map.put(SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR, "Supertype initialization is impossible without a primary constructor.")
map.put(DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR, "Calls to super in enum constructors are prohibited.") map.put(DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR, "Calls to super in enum constructors are prohibited.")
map.put(PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS, "Primary constructor required for data class.")
map.put( map.put(
EXPLICIT_DELEGATION_CALL_REQUIRED, EXPLICIT_DELEGATION_CALL_REQUIRED,
"Explicit 'this' or 'super' call is required. There is no constructor in the superclass that can be called without arguments." "Explicit 'this' or 'super' call is required. There is no constructor in the superclass that can be called without arguments."
@@ -238,6 +238,7 @@ object LightTreePositioningStrategies {
//in FE 1.0 this is part of DeclarationHeader abstract strategy //in FE 1.0 this is part of DeclarationHeader abstract strategy
if (node.tokenType != KtNodeTypes.OBJECT_DECLARATION if (node.tokenType != KtNodeTypes.OBJECT_DECLARATION
&& node.tokenType != KtNodeTypes.FUN && node.tokenType != KtNodeTypes.FUN
&& node.tokenType != KtNodeTypes.PRIMARY_CONSTRUCTOR
&& node.tokenType != KtNodeTypes.SECONDARY_CONSTRUCTOR && node.tokenType != KtNodeTypes.SECONDARY_CONSTRUCTOR
&& node.tokenType != KtNodeTypes.OBJECT_LITERAL && node.tokenType != KtNodeTypes.OBJECT_LITERAL
) { ) {
@@ -1,4 +1,4 @@
data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class A<!> data <!DATA_CLASS_WITHOUT_PARAMETERS!>class A<!>
fun foo(a: A) { fun foo(a: A) {
a.<!UNRESOLVED_REFERENCE!>component1<!>() a.<!UNRESOLVED_REFERENCE!>component1<!>()
@@ -7,7 +7,7 @@ data class A2(val y: String, val z: Int) {
constructor(x: String): this(x, 0) constructor(x: String): this(x, 0)
} }
data <!PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS!>class A3<!> { data <!DATA_CLASS_WITHOUT_PARAMETERS!>class A3<!> {
constructor() constructor()
} }