Don't create NamespaceType for enum entries

Enum entry expressions have the type of their enum, so it shouldn't be possible
to access static nested classes of enum entries from outside.

This fixes BlackBoxCodegenTestGenerated$CallableReference.testEnumNameMethod
This commit is contained in:
Alexander Udalov
2013-11-21 16:13:10 +04:00
parent e48c91bba4
commit 2e4807856f
3 changed files with 41 additions and 16 deletions
@@ -151,11 +151,19 @@ public class CallExpressionResolver {
@NotNull ClassifierDescriptor classifier, @NotNull ClassifierDescriptor classifier,
@NotNull ResolutionContext context @NotNull ResolutionContext context
) { ) {
if (isLHSOfDot(expression) && classifier instanceof ClassDescriptor) { if (!isLHSOfDot(expression) || !(classifier instanceof ClassDescriptor)) {
return classObjectType;
}
ClassDescriptor classDescriptor = (ClassDescriptor) classifier;
if (classDescriptor.getKind() == ClassKind.ENUM_ENTRY) {
return classObjectType;
}
List<JetScope> scopes = new ArrayList<JetScope>(3); List<JetScope> scopes = new ArrayList<JetScope>(3);
scopes.add(classObjectType.getMemberScope()); scopes.add(classObjectType.getMemberScope());
scopes.add(getStaticNestedClassesScope((ClassDescriptor) classifier)); scopes.add(getStaticNestedClassesScope(classDescriptor));
Name referencedName = expression.getReferencedNameAsName(); Name referencedName = expression.getReferencedNameAsName();
NamespaceDescriptor namespace = context.scope.getNamespace(referencedName); NamespaceDescriptor namespace = context.scope.getNamespace(referencedName);
@@ -167,8 +175,6 @@ public class CallExpressionResolver {
JetScope scope = new ChainedScope(classifier, scopes.toArray(new JetScope[scopes.size()])); JetScope scope = new ChainedScope(classifier, scopes.toArray(new JetScope[scopes.size()]));
return new NamespaceType(referencedName, scope, new ExpressionReceiver(expression, classObjectType)); return new NamespaceType(referencedName, scope, new ExpressionReceiver(expression, classObjectType));
} }
return classObjectType;
}
private boolean furtherNameLookup( private boolean furtherNameLookup(
@NotNull JetSimpleNameExpression expression, @NotNull JetSimpleNameExpression expression,
@@ -0,0 +1,14 @@
enum class E {
FIRST
SECOND {
class A
}
}
val foo: Any.() -> Unit = {}
fun f1() = E.FIRST.foo()
fun f2() = E.FIRST.(foo)()
fun f3() = E.SECOND.foo()
fun f4() = E.SECOND.(foo)()
fun f5() = E.SECOND.<!UNRESOLVED_REFERENCE!>A<!>()
@@ -2532,6 +2532,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/enum"), Pattern.compile("^(.+)\\.kt$"), true); JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/enum"), Pattern.compile("^(.+)\\.kt$"), true);
} }
@TestMetadata("dontCreateNamespaceTypeForEnumEntry.kt")
public void testDontCreateNamespaceTypeForEnumEntry() throws Exception {
doTest("compiler/testData/diagnostics/tests/enum/dontCreateNamespaceTypeForEnumEntry.kt");
}
@TestMetadata("entryShouldBeOfEnumType.kt") @TestMetadata("entryShouldBeOfEnumType.kt")
public void testEntryShouldBeOfEnumType() throws Exception { public void testEntryShouldBeOfEnumType() throws Exception {
doTest("compiler/testData/diagnostics/tests/enum/entryShouldBeOfEnumType.kt"); doTest("compiler/testData/diagnostics/tests/enum/entryShouldBeOfEnumType.kt");