From b0e301ae9fc372bdd32f50be7db3baa2eab457d2 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 29 Mar 2012 20:40:24 +0400 Subject: [PATCH] Invisible call candidates removed from consideration --- .../lang/resolve/calls/TaskPrioritizer.java | 19 +++- .../diagnostics/tests/scopes/visibility.jet | 90 +++++++++++++++++++ 2 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/scopes/visibility.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java index 1051f85891c..9784cc77681 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/TaskPrioritizer.java @@ -16,6 +16,8 @@ package org.jetbrains.jet.lang.resolve.calls; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,6 +32,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.NamespaceType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; @@ -212,9 +215,19 @@ import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor return false; } - private void addTask(@NotNull List> result, @NotNull Collection> candidates, @NotNull BasicResolutionContext context, @NotNull JetReferenceExpression functionReference) { - if (candidates.isEmpty()) return; - result.add(new ResolutionTask(candidates, functionReference, context)); + private void addTask(@NotNull List> result, @NotNull Collection> candidates, @NotNull final BasicResolutionContext context, @NotNull JetReferenceExpression functionReference) { + Collection> visibleCandidates = Collections2.filter(candidates, new Predicate>() { + @Override + public boolean apply(@Nullable ResolvedCallImpl call) { + if (call == null) return false; + D candidateDescriptor = call.getCandidateDescriptor(); + if (ErrorUtils.isError(candidateDescriptor)) return true; + Visibility visibility = candidateDescriptor.getVisibility(); + return visibility.isVisible(candidateDescriptor, context.scope.getContainingDeclaration()); + } + }); + if (visibleCandidates.isEmpty()) return; + result.add(new ResolutionTask(visibleCandidates, functionReference, context)); } @NotNull diff --git a/compiler/testData/diagnostics/tests/scopes/visibility.jet b/compiler/testData/diagnostics/tests/scopes/visibility.jet new file mode 100644 index 00000000000..00782813b5f --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/visibility.jet @@ -0,0 +1,90 @@ +//FILE:a.kt +package test_visibility + +protected val protected_val : Int = 4 +protected fun protected_fun() {} + +private val private_val : Int = 4 +private fun private_fun() {} + +val internal_val : Int = 34 +fun internal_fun() {} + +fun test1() { + private_fun(); +} + +class Y { + fun test2() { + private_fun(); + } +} + +class A { + private val i = 23 + private val v: B = B() + private fun f(i: Int): B = B() + + fun test() { + doSmth(i) + } +} + +class B { + fun bMethod() {} +} + +fun test3(a: A) { + a.v //todo .bMethod() + a.f(0, 1) //todo .bMethod() +} + +trait T + +open class C : T { + protected var i : Int = 34 + fun test5() { + doSmth(i) + } +} + +fun test4(c: C) { + c.i++ +} + +class D : C() { + val j = i + fun test6() { + doSmth(i) + } +} + +class E : C() { + fun test7() { + doSmth(i) + } +} + +class F : C() { + fun test8(c: C) { + doSmth(c.i) + } +} + +class G : T { + fun test8(c: C) { + doSmth(c.i) + } +} + +fun doSmth(i: Int) = i + +//FILE:b.kt +package test_visibility2 + +import test_visibility.* + +fun test() { + internal_fun() + private_fun() +}