Skip to content

Commit

Permalink
Use more specific error for secondary constructor bodies in inline cl…
Browse files Browse the repository at this point in the history
…asses

 - Also, this error allows IDE to provide quick-fix that changes language
 version in a project that simplifies the adoption
 - There is no point in
 changing K2 part as there this feature will be enabled by default

 ^KT-56224 Fixed
  • Loading branch information
zarechenskiy committed Feb 6, 2023
1 parent e09cd0b commit 9d488c6
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 6 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ public interface Errors {
DiagnosticFactory0<KtTypeReference> VALUE_CLASS_CANNOT_EXTEND_CLASSES = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> VALUE_CLASS_CANNOT_BE_RECURSIVE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> MULTI_FIELD_VALUE_CLASS_PRIMARY_CONSTRUCTOR_DEFAULT_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> RESERVED_MEMBER_INSIDE_VALUE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<KtTypeReference> TYPE_ARGUMENT_ON_TYPED_VALUE_CLASS_EQUALS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INNER_CLASS_INSIDE_VALUE_CLASS = DiagnosticFactory0.create(ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ public static DiagnosticRenderer getRendererForDiagnostic(@NotNull UnboundDiagno
MAP.put(VALUE_CLASS_CANNOT_EXTEND_CLASSES, "Value class cannot extend classes");
MAP.put(VALUE_CLASS_CANNOT_BE_RECURSIVE, "Value class cannot be recursive");
MAP.put(MULTI_FIELD_VALUE_CLASS_PRIMARY_CONSTRUCTOR_DEFAULT_PARAMETER, "Default parameters are not supported in the primary constructor of a multi-field value class");
MAP.put(SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS, "Secondary constructors with bodies are reserved for for future releases");
MAP.put(RESERVED_MEMBER_INSIDE_VALUE_CLASS, "Member with the name ''{0}'' is reserved for future releases", STRING);
MAP.put(TYPE_ARGUMENT_ON_TYPED_VALUE_CLASS_EQUALS, "Type arguments for typed value class equals must be only star projections");
MAP.put(INNER_CLASS_INSIDE_VALUE_CLASS, "Value class cannot have inner classes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ class ReservedMembersAndConstructsForValueClass : DeclarationChecker {
val bodyExpression = secondaryConstructor.bodyExpression
if (secondaryConstructor.hasBlockBody() && bodyExpression is KtBlockExpression) {
val lBrace = bodyExpression.lBrace ?: return
context.trace.report(Errors.SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS.on(lBrace))
context.trace.report(
Errors.UNSUPPORTED_FEATURE.on(
lBrace,
LanguageFeature.ValueClassesSecondaryConstructorWithBody to context.languageVersionSettings
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// !LANGUAGE: -ValueClassesSecondaryConstructorWithBody
// WITH_STDLIB

@JvmInline
value class Foo(val x: String) {
constructor(i: Int) : this(i.toString()) <!SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS!>{
println(i)
}<!>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// !LANGUAGE: -ValueClassesSecondaryConstructorWithBody
// WITH_STDLIB

@JvmInline
value class Foo(val x: String) {
constructor(i: Int) : this(i.toString()) <!UNSUPPORTED_FEATURE!>{<!>
println(i)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package

@kotlin.jvm.JvmInline public final value class Foo {
public constructor Foo(/*0*/ i: kotlin.Int)
public constructor Foo(/*0*/ x: kotlin.String)
public final val x: kotlin.String
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ value class IC4(val s: String) : WithBox {

@JvmInline
value class IC5(val a: String) {
constructor(i: Int) : this(i.toString()) <!SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS!>{<!>
constructor(i: Int) : this(i.toString()) <!UNSUPPORTED_FEATURE!>{<!>
TODO("something")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ value class IC4(val s: String) : WithBox {

@JvmInline
value class IC5(val a: String) {
constructor(i: Int) : this(i.toString()) <!SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS!>{<!>
constructor(i: Int) : this(i.toString()) <!UNSUPPORTED_FEATURE!>{<!>
TODO("something")
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ value class MFVC4(val s: String, val t: String) : WithBox {

@JvmInline
value class MFVC5(val a: String, val b: String) {
constructor(i: Int) : this(i.toString(), "6") <!SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_VALUE_CLASS!>{<!>
constructor(i: Int) : this(i.toString(), "6") <!UNSUPPORTED_FEATURE!>{<!>
TODO("something")
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d488c6

Please sign in to comment.