Fix access to top-level declarations inside anonymous initializer

#KT-16583 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-03-02 13:52:19 +03:00
parent 0e5603f644
commit e2dcec62d3
5 changed files with 55 additions and 2 deletions
@@ -0,0 +1,20 @@
private const val A = 0L
private val B = 0L
private fun sample() = 0L
private class PrivateClass
class Foo {
var bar: Long = 0
private var other: PrivateClass? = null
init {
bar = A
bar = B
bar = sample()
other = PrivateClass()
}
constructor()
}
@@ -0,0 +1,21 @@
package
private const val A: kotlin.Long = 0.toLong()
private val B: kotlin.Long = 0.toLong()
private fun sample(): kotlin.Long
public final class Foo {
public constructor Foo()
public final var bar: kotlin.Long
private final var other: PrivateClass?
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
}
private final class PrivateClass {
public constructor PrivateClass()
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
}
@@ -3406,6 +3406,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("initializerWithSecondaryConstructor.kt")
public void testInitializerWithSecondaryConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/constructorConsistency/initializerWithSecondaryConstructor.kt");
doTest(fileName);
}
@TestMetadata("initwithgetter.kt")
public void testInitwithgetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/constructorConsistency/initwithgetter.kt");
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -39,6 +39,8 @@ public class Visibilities {
return true;
}
// Note that this method returns false if `from` declaration is `init` initializer
// because initializer does not have source element
private boolean inSameFile(@NotNull DeclarationDescriptor what, @NotNull DeclarationDescriptor from) {
SourceFile fromContainingFile = DescriptorUtils.getContainingSourceFile(from);
if (fromContainingFile != SourceFile.NO_SOURCE_FILE) {
@@ -47,9 +49,13 @@ public class Visibilities {
return false;
}
private boolean hasContainingSourceFile(@NotNull DeclarationDescriptor descriptor) {
return DescriptorUtils.getContainingSourceFile(descriptor) != SourceFile.NO_SOURCE_FILE;
}
@Override
public boolean isVisible(@Nullable ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
if (DescriptorUtils.isTopLevelDeclaration(what)) {
if (DescriptorUtils.isTopLevelDeclaration(what) && hasContainingSourceFile(from)) {
return inSameFile(what, from);
}