Skip to content

Commit b185316

Browse files
committed
Create new HidesMembers annotation
1 parent 0c995d0 commit b185316

File tree

9 files changed

+198
-3
lines changed

9 files changed

+198
-3
lines changed

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import org.jetbrains.kotlin.descriptors.*
2121
import org.jetbrains.kotlin.incremental.components.LookupLocation
2222
import org.jetbrains.kotlin.name.Name
2323
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject
24+
import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST
2425
import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassValueDescriptor
26+
import org.jetbrains.kotlin.resolve.descriptorUtil.hasHidesMembersAnnotation
2527
import org.jetbrains.kotlin.resolve.descriptorUtil.hasLowPriorityInOverloadResolution
2628
import org.jetbrains.kotlin.resolve.scopes.*
2729
import org.jetbrains.kotlin.resolve.scopes.receivers.CastImplicitClassReceiver
2830
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
2931
import org.jetbrains.kotlin.resolve.scopes.receivers.QualifierReceiver
3032
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
33+
import org.jetbrains.kotlin.resolve.scopes.utils.collectFunctions
34+
import org.jetbrains.kotlin.resolve.scopes.utils.collectVariables
3135
import org.jetbrains.kotlin.resolve.selectMostSpecificInEachOverridableGroup
3236
import org.jetbrains.kotlin.types.ErrorUtils
3337
import org.jetbrains.kotlin.types.KotlinType
@@ -184,6 +188,28 @@ internal class SyntheticScopeBasedTowerLevel(
184188
}
185189
}
186190

191+
internal class HidesMembersTowerLevel(scopeTower: ScopeTower): AbstractScopeTowerLevel(scopeTower) {
192+
override fun getVariables(name: Name, extensionReceiver: ReceiverValue?)
193+
= getCandidates(name, extensionReceiver, LexicalScope::collectVariables)
194+
195+
override fun getFunctions(name: Name, extensionReceiver: ReceiverValue?)
196+
= getCandidates(name, extensionReceiver, LexicalScope::collectFunctions)
197+
198+
private fun <T: CallableDescriptor> getCandidates(
199+
name: Name,
200+
extensionReceiver: ReceiverValue?,
201+
collectCandidates: LexicalScope.(Name, LookupLocation) -> Collection<T>
202+
): Collection<CandidateWithBoundDispatchReceiver<T>> {
203+
if (extensionReceiver == null || name !in HIDES_MEMBERS_NAME_LIST) return emptyList()
204+
205+
return scopeTower.lexicalScope.collectCandidates(name, location).filter {
206+
it.extensionReceiverParameter != null && it.hasHidesMembersAnnotation()
207+
}.map {
208+
createCandidateDescriptor(it, dispatchReceiver = null)
209+
}
210+
}
211+
}
212+
187213
private fun KotlinType.getClassifierFromMeAndSuperclasses(name: Name, location: LookupLocation): ClassifierDescriptor? {
188214
var superclass: KotlinType? = this
189215
while (superclass != null) {

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/TowerResolver.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ class TowerResolver {
103103
map { ScopeBasedTowerLevel(this@createTowerDataList, it) }
104104

105105
val nonLocalLevels = createNonLocalLevels()
106+
val hidesMembersLevel = HidesMembersTowerLevel(this)
106107
val syntheticLevel = SyntheticScopeBasedTowerLevel(this, syntheticScopes)
107108

109+
// hides members extensions for explicit receiver
110+
+ TowerData.TowerLevel(hidesMembersLevel)
108111
// possibly there is explicit member
109112
+ TowerData.Empty
110113
// synthetic member for explicit receiver
@@ -124,6 +127,9 @@ class TowerResolver {
124127

125128
val implicitReceiver = scope.implicitReceiver?.value
126129
if (implicitReceiver != null) {
130+
// hides members extensions
131+
+ TowerData.BothTowerLevelAndImplicitReceiver(hidesMembersLevel, implicitReceiver)
132+
127133
// members of implicit receiver or member extension for explicit receiver
128134
+ TowerData.TowerLevel(ReceiverScopeTowerLevel(this, implicitReceiver))
129135

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// !CHECK_TYPE
2+
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE
3+
4+
// FILE: 2.kt
5+
package b
6+
7+
import a.A
8+
9+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
10+
@kotlin.internal.HidesMembers
11+
fun A.forEach(i: Int) = i
12+
13+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
14+
@kotlin.internal.HidesMembers
15+
fun A.forEach(s: String) {}
16+
17+
18+
// FILE: 1.kt
19+
package a
20+
21+
import b.*
22+
23+
class A {
24+
fun forEach() = this
25+
fun forEach(i: Int) = this
26+
fun forEach(i: String) = this
27+
}
28+
29+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
30+
@kotlin.internal.HidesMembers
31+
fun A.forEach() = ""
32+
33+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
34+
@kotlin.internal.HidesMembers
35+
fun A.forEach(s: String) {}
36+
37+
fun test(a: A) {
38+
a.forEach() checkType { _<String>() }
39+
40+
a.forEach(1) checkType { _<Int>() }
41+
42+
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>forEach<!>("")
43+
44+
with(a) {
45+
forEach() checkType { _<String>() }
46+
47+
forEach(1) checkType { _<Int>() }
48+
49+
<!OVERLOAD_RESOLUTION_AMBIGUITY!>forEach<!>("")
50+
}
51+
}
52+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package
2+
3+
package a {
4+
public fun test(/*0*/ a: a.A): kotlin.Unit
5+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public fun a.A.forEach(): kotlin.String
6+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public fun a.A.forEach(/*0*/ s: kotlin.String): kotlin.Unit
7+
8+
public final class A {
9+
public constructor A()
10+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
11+
public final fun forEach(): a.A
12+
public final fun forEach(/*0*/ i: kotlin.Int): a.A
13+
public final fun forEach(/*0*/ i: kotlin.String): a.A
14+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
15+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
16+
}
17+
}
18+
19+
package b {
20+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public fun a.A.forEach(/*0*/ i: kotlin.Int): kotlin.Int
21+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public fun a.A.forEach(/*0*/ s: kotlin.String): kotlin.Unit
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// !CHECK_TYPE
2+
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE
3+
class A {
4+
fun forEach() = this
5+
fun forEach(i: Int) = this
6+
}
7+
8+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
9+
@kotlin.internal.HidesMembers
10+
fun A.forEach(i: Int) = i
11+
12+
class B {
13+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
14+
@kotlin.internal.HidesMembers
15+
fun A.forEach() = this@B
16+
17+
fun test(a: A) {
18+
a.forEach() checkType { _<A>() } // todo
19+
20+
with(a) {
21+
forEach() checkType { _<A>() } // todo
22+
}
23+
}
24+
}
25+
26+
fun test2(a: A) {
27+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
28+
@kotlin.internal.HidesMembers
29+
fun A.forEach() = ""
30+
31+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
32+
@kotlin.internal.HidesMembers
33+
fun A.forEach(i: Int) = ""
34+
35+
a.forEach() checkType { _<String>() }
36+
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>forEach<!>(1)
37+
38+
with(a) {
39+
forEach() checkType { _<String>() }
40+
<!OVERLOAD_RESOLUTION_AMBIGUITY!>forEach<!>(1)
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package
2+
3+
public fun test2(/*0*/ a: A): kotlin.Unit
4+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public fun A.forEach(/*0*/ i: kotlin.Int): kotlin.Int
5+
6+
public final class A {
7+
public constructor A()
8+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
9+
public final fun forEach(): A
10+
public final fun forEach(/*0*/ i: kotlin.Int): A
11+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
12+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
13+
}
14+
15+
public final class B {
16+
public constructor B()
17+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
18+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
19+
public final fun test(/*0*/ a: A): kotlin.Unit
20+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
21+
@kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) @kotlin.internal.HidesMembers() public final fun A.forEach(): B
22+
}

compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,18 @@ public void testAllFilesPresentInResolve() throws Exception {
10211021
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/resolve"), Pattern.compile("^(.+)\\.kt$"), true);
10221022
}
10231023

1024+
@TestMetadata("hidesMembers.kt")
1025+
public void testHidesMembers() throws Exception {
1026+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/resolve/hidesMembers.kt");
1027+
doTest(fileName);
1028+
}
1029+
1030+
@TestMetadata("hidesMembers2.kt")
1031+
public void testHidesMembers2() throws Exception {
1032+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/resolve/hidesMembers2.kt");
1033+
doTest(fileName);
1034+
}
1035+
10241036
@TestMetadata("javaPackageMembers.kt")
10251037
public void testJavaPackageMembers() throws Exception {
10261038
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/resolve/javaPackageMembers.kt");

core/descriptors/src/org/jetbrains/kotlin/resolve/annotationsForResolve.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
2222
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
2323
import org.jetbrains.kotlin.descriptors.annotations.Annotations
2424
import org.jetbrains.kotlin.name.FqName
25+
import org.jetbrains.kotlin.name.Name
2526
import org.jetbrains.kotlin.types.KotlinType
2627

2728
private val NO_INFER_ANNOTATION_FQ_NAME = FqName("kotlin.internal.NoInfer")
2829
private val EXACT_ANNOTATION_FQ_NAME = FqName("kotlin.internal.Exact")
30+
private val LOW_PRIORITY_IN_OVERLOAD_RESOLUTION_FQ_NAME = FqName("kotlin.internal.LowPriorityInOverloadResolution")
31+
private val HIDES_MEMBERS_ANNOTATION_FQ_NAME = FqName("kotlin.internal.HidesMembers")
32+
private val ONLY_INPUT_TYPES_FQ_NAME = FqName("kotlin.internal.OnlyInputTypes")
33+
34+
// @HidesMembers annotation only has effect for members with these names
35+
val HIDES_MEMBERS_NAME_LIST = setOf(Name.identifier("forEach"))
2936

3037
fun KotlinType.hasNoInferAnnotation(): Boolean = annotations.hasAnnotation(NO_INFER_ANNOTATION_FQ_NAME)
3138

@@ -36,11 +43,9 @@ fun Annotations.hasInternalAnnotationForResolve(): Boolean =
3643

3744
fun FqName.isInternalAnnotationForResolve() = this == NO_INFER_ANNOTATION_FQ_NAME || this == EXACT_ANNOTATION_FQ_NAME
3845

39-
private val LOW_PRIORITY_IN_OVERLOAD_RESOLUTION_FQ_NAME = FqName("kotlin.internal.LowPriorityInOverloadResolution")
40-
4146
fun CallableDescriptor.hasLowPriorityInOverloadResolution(): Boolean = annotations.hasAnnotation(LOW_PRIORITY_IN_OVERLOAD_RESOLUTION_FQ_NAME)
4247

43-
private val ONLY_INPUT_TYPES_FQ_NAME = FqName("kotlin.internal.OnlyInputTypes")
48+
fun CallableDescriptor.hasHidesMembersAnnotation(): Boolean = annotations.hasAnnotation(HIDES_MEMBERS_ANNOTATION_FQ_NAME)
4449

4550
fun TypeParameterDescriptor.hasOnlyInputTypesAnnotation(): Boolean = annotations.hasAnnotation(ONLY_INPUT_TYPES_FQ_NAME)
4651

libraries/stdlib/src/kotlin/internal/Annotations.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ internal annotation class Exact
3737
@Retention(AnnotationRetention.BINARY)
3838
internal annotation class LowPriorityInOverloadResolution
3939

40+
/**
41+
* Specifies that the corresponding member has the highest priority in overload resolution. Effectively this means that
42+
* an extension annotated with this annotation will win in overload resolution over a member with the same signature.
43+
*/
44+
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
45+
@Retention(AnnotationRetention.BINARY)
46+
internal annotation class HidesMembers
47+
4048
/**
4149
* The value of this type parameter should be mentioned in input types (argument types, receiver type or expected type).
4250
*/

0 commit comments

Comments
 (0)