diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt index b9c1df1c649..967ce863a31 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -230,6 +230,13 @@ class CandidateResolver( } private fun checkOuterClassMemberIsAccessible(context: CallCandidateResolutionContext<*>): Boolean { + + fun KtElement.insideScript() = (containingFile as? KtFile)?.isScript ?: false + + // context.scope doesn't contains outer class implicit receiver if we inside nested class + // Outer scope for some class in script file is scopeForInitializerResolution see: DeclarationScopeProviderImpl.getResolutionScopeForDeclaration + if (!context.call.callElement.insideScript()) return true + // In "this@Outer.foo()" the error will be reported on "this@Outer" instead if (context.call.getExplicitReceiver() != null || context.call.getDispatchReceiver() != null) return true diff --git a/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.kt b/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.kt new file mode 100644 index 00000000000..3212cae4cd1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.kt @@ -0,0 +1,33 @@ +fun foo(init: A.() -> Unit) { } + +class A { + var x: Int = 0 + companion object { + val f = foo() { + x = 1 + } + } +} + +class B(val a: String) { + fun f() = 0 + companion object { + fun B.bar() = a + f() + } +} + +open class C { + fun bar() {} +} + +class E: C() { + + class D { + init { + with(C()) { + bar() + this.bar() + } + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.txt b/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.txt new file mode 100644 index 00000000000..2fa39c23458 --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.txt @@ -0,0 +1,59 @@ +package + +public fun foo(/*0*/ init: A.() -> kotlin.Unit): kotlin.Unit + +public final class A { + public constructor A() + public final var x: kotlin.Int + 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 companion object Companion { + private constructor Companion() + public final val f: kotlin.Unit + 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 B { + public constructor B(/*0*/ a: kotlin.String) + public final val a: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun f(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + public companion object Companion { + private constructor Companion() + 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 fun B.bar(): kotlin.String + } +} + +public open class C { + public constructor C() + public final fun bar(): kotlin.Unit + 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 E : C { + public constructor E() + public final override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit + 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 D { + public constructor D() + 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 + } +} diff --git a/compiler/testData/diagnostics/tests/scopes/inheritance/statics/companionObject/inheritFromContainingClass.kt b/compiler/testData/diagnostics/tests/scopes/inheritance/statics/companionObject/inheritFromContainingClass.kt index 2c7ea84747a..e3d50f4fc08 100644 --- a/compiler/testData/diagnostics/tests/scopes/inheritance/statics/companionObject/inheritFromContainingClass.kt +++ b/compiler/testData/diagnostics/tests/scopes/inheritance/statics/companionObject/inheritFromContainingClass.kt @@ -27,8 +27,8 @@ open class A : J() { fun test() { foo() bar() - val a: Int = baz() - val b: T = baz() + val a: Int = baz() + val b: T = baz() } } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 4e244a24716..faae6800644 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -15101,6 +15101,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("kt3856.kt") + public void testKt3856() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/inheritance/kt3856.kt"); + doTest(fileName); + } + @TestMetadata("methodsPriority.kt") public void testMethodsPriority() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/inheritance/methodsPriority.kt");