From 599f705842f4f1e32908a1bfd4fbbe57dd7c0a2b Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Wed, 1 Dec 2021 16:21:41 +0300 Subject: [PATCH] [stdlib] Make `@LowPriorityInOverloadResolution` applicable to ctors --- .../kotlin/fir/resolve/calls/ResolutionStages.kt | 1 + .../annotations/resolveWithLowPriorityAnnotation.kt | 12 ++++++++++++ .../checkLowPriorityIsResolvedSuccessfully.kt | 11 +++++++++++ .../checkLowPriorityIsResolvedSuccessfully.txt | 11 +++++++++++ libraries/stdlib/src/kotlin/internal/Annotations.kt | 2 +- 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index 45d9b5f56f7..84cb7dbc9b6 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -461,6 +461,7 @@ internal object CheckLowPriorityInOverloadResolution : CheckerStage() { val annotations = when (val fir = candidate.symbol.fir) { is FirSimpleFunction -> fir.annotations is FirProperty -> fir.annotations + is FirConstructor -> fir.annotations else -> return } diff --git a/compiler/testData/codegen/box/annotations/resolveWithLowPriorityAnnotation.kt b/compiler/testData/codegen/box/annotations/resolveWithLowPriorityAnnotation.kt index 3a8e3929c0e..df53b15eeb2 100644 --- a/compiler/testData/codegen/box/annotations/resolveWithLowPriorityAnnotation.kt +++ b/compiler/testData/codegen/box/annotations/resolveWithLowPriorityAnnotation.kt @@ -12,8 +12,20 @@ fun bar(a: String?) = 3 fun bar(a: Any) = 4 +class MyString(val value: String) + +class Baz +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +@kotlin.internal.LowPriorityInOverloadResolution +constructor(val s: String) { + constructor(s: MyString): this(s.value) +} + +fun Baz(s: String) = Baz(MyString(s + "!")) + fun box(): String { if (foo(1) != 2) return "fail1" if (bar(null) != 3) return "fail2" + if (Baz("hello").s != "hello!") return "fail3" return "OK" } diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.kt b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.kt index e0693a2dceb..908302aabc9 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.kt @@ -14,3 +14,14 @@ class Scope { fun Scope.bar(e: Foo) { e.test {} } + +class Baz +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +@kotlin.internal.LowPriorityInOverloadResolution +constructor(val x: Foo) + +fun Baz(x: Foo): Baz { + throw NotImplementedError() +} + +fun testBaz(e: Foo) = Baz(e) diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.txt b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.txt index 6821e31e831..c1bd5e99920 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/checkLowPriorityIsResolvedSuccessfully.txt @@ -1,5 +1,7 @@ package +public fun Baz(/*0*/ x: Foo): Baz +public fun testBaz(/*0*/ e: Foo): Baz public fun Scope.bar(/*0*/ e: Foo): kotlin.Unit public fun Foo.test(): kotlin.Unit @@ -10,6 +12,14 @@ public final class Bar { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } +public final class Baz { + @kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.LowPriorityInOverloadResolution public constructor Baz(/*0*/ x: Foo) + public final val x: Foo + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + public final class Foo { public constructor Foo() @kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.LowPriorityInOverloadResolution public final val test: Bar @@ -25,3 +35,4 @@ public final class Scope { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String public final operator fun Bar.invoke(/*0*/ f: () -> kotlin.Unit): kotlin.Unit } + diff --git a/libraries/stdlib/src/kotlin/internal/Annotations.kt b/libraries/stdlib/src/kotlin/internal/Annotations.kt index 56cd7bf62b8..4efddc65c4f 100644 --- a/libraries/stdlib/src/kotlin/internal/Annotations.kt +++ b/libraries/stdlib/src/kotlin/internal/Annotations.kt @@ -22,7 +22,7 @@ internal annotation class Exact /** * Specifies that a corresponding member has the lowest priority in overload resolution. */ -@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CONSTRUCTOR) @Retention(AnnotationRetention.BINARY) internal annotation class LowPriorityInOverloadResolution