Skip to content

Commit d1b17f0

Browse files
committed
Prohibit objects inside inner classes
#KT-16232 Fixed
1 parent 5b9b003 commit d1b17f0

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ enum BadNamedArgumentsTarget {
892892

893893
DiagnosticFactory1<PsiElement, ClassDescriptor> INACCESSIBLE_OUTER_CLASS_EXPRESSION = DiagnosticFactory1.create(ERROR);
894894
DiagnosticFactory0<KtClass> NESTED_CLASS_NOT_ALLOWED = DiagnosticFactory0.create(ERROR, DECLARATION_NAME);
895+
DiagnosticFactory0<KtObjectDeclaration> NESTED_OBJECT_NOT_ALLOWED = DiagnosticFactory0.create(ERROR, DECLARATION_NAME);
895896

896897
//Inline and inlinable parameters
897898
DiagnosticFactory2<KtElement, DeclarationDescriptor, DeclarationDescriptor> NON_PUBLIC_CALL_FROM_PUBLIC_INLINE = DiagnosticFactory2.create(ERROR, CALL_ELEMENT);

compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ public String render(@NotNull IncompatibleVersionErrorData<?> incompatibility, @
437437

438438
MAP.put(INACCESSIBLE_OUTER_CLASS_EXPRESSION, "Expression is inaccessible from a nested class ''{0}'', use ''inner'' keyword to make the class inner", NAME);
439439
MAP.put(NESTED_CLASS_NOT_ALLOWED, "Nested class is not allowed here, use 'inner' keyword to make the class inner");
440+
MAP.put(NESTED_OBJECT_NOT_ALLOWED, "Objects inside inner classes are prohibited");
440441

441442
MAP.put(HAS_NEXT_MISSING, "hasNext() cannot be called on iterator() of type ''{0}''", RENDER_TYPE);
442443
MAP.put(HAS_NEXT_FUNCTION_AMBIGUITY, "hasNext() is ambiguous for iterator() of type ''{0}''", RENDER_TYPE);

compiler/frontend/src/org/jetbrains/kotlin/resolve/ModifiersChecker.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.*;
3737

3838
import static org.jetbrains.kotlin.diagnostics.Errors.NESTED_CLASS_NOT_ALLOWED;
39+
import static org.jetbrains.kotlin.diagnostics.Errors.NESTED_OBJECT_NOT_ALLOWED;
3940
import static org.jetbrains.kotlin.lexer.KtTokens.*;
4041
import static org.jetbrains.kotlin.psi.KtStubbedPsiUtil.getContainingDeclaration;
4142

@@ -219,10 +220,20 @@ public void checkParameterHasNoValOrVar(
219220

220221
public void checkModifiersForDeclaration(@NotNull KtDeclaration modifierListOwner, @NotNull MemberDescriptor descriptor) {
221222
checkNestedClassAllowed(modifierListOwner, descriptor);
223+
checkObjectInsideInnerClass(modifierListOwner, descriptor);
222224
checkTypeParametersModifiers(modifierListOwner);
223225
checkModifierListCommon(modifierListOwner, descriptor);
224226
}
225227

228+
private void checkObjectInsideInnerClass(@NotNull KtDeclaration modifierListOwner, @NotNull MemberDescriptor descriptor) {
229+
if (modifierListOwner instanceof KtObjectDeclaration) {
230+
KtObjectDeclaration ktObject = (KtObjectDeclaration) modifierListOwner;
231+
if (!ktObject.isLocal() && !ktObject.isCompanion() && isIllegalNestedClass(descriptor)) {
232+
trace.report(NESTED_OBJECT_NOT_ALLOWED.on(ktObject));
233+
}
234+
}
235+
}
236+
226237
private void checkModifierListCommon(@NotNull KtDeclaration modifierListOwner, @NotNull DeclarationDescriptor descriptor) {
227238
AnnotationUseSiteTargetChecker.INSTANCE.check(modifierListOwner, descriptor, trace);
228239
runDeclarationCheckers(modifierListOwner, descriptor);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SKIP_TXT
2+
class Outer {
3+
inner class Inner1 {
4+
<!NESTED_OBJECT_NOT_ALLOWED!>object Obj1<!>
5+
6+
<!WRONG_MODIFIER_CONTAINING_DECLARATION!>companion<!> object Obj2
7+
8+
inner class Inner2 {
9+
<!NESTED_OBJECT_NOT_ALLOWED!>object Obj3<!>
10+
}
11+
}
12+
}

compiler/testData/diagnostics/tests/modifiers/const/applicability.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ enum class MyEnum {
6161

6262
class Outer {
6363
inner class Inner {
64-
object C {
64+
<!NESTED_OBJECT_NOT_ALLOWED!>object C<!> {
6565
const val a = 18
6666
}
6767
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11425,6 +11425,12 @@ public void testNestedClassNotAllowed() throws Exception {
1142511425
doTest(fileName);
1142611426
}
1142711427

11428+
@TestMetadata("nestedObject.kt")
11429+
public void testNestedObject() throws Exception {
11430+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inner/nestedObject.kt");
11431+
doTest(fileName);
11432+
}
11433+
1142811434
@TestMetadata("nestedVsInnerAccessOuterMember.kt")
1142911435
public void testNestedVsInnerAccessOuterMember() throws Exception {
1143011436
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inner/nestedVsInnerAccessOuterMember.kt");

0 commit comments

Comments
 (0)