Skip to content

Commit 111ca83

Browse files
committed
[JEP-409] Add support for non-sealed classes and interfaces in Java
1 parent de004ca commit 111ca83

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

+9
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,15 @@ object JavaParsers {
492492
case SEALED =>
493493
flags |= Flags.Sealed
494494
in.nextToken()
495+
// JEP-409: Special trick for the 'non-sealed' java keyword
496+
case IDENTIFIER if in.name.toString == "non" =>
497+
val lookahead = in.LookaheadScanner()
498+
({lookahead.nextToken(); lookahead.token}, {lookahead.nextToken(); lookahead.name.toString}) match
499+
case (MINUS, "sealed") =>
500+
in.nextToken(); in.nextToken() // skip '-' and 'sealed'. Nothing more to do
501+
case _ =>
502+
syntaxError(em"Identifier '${in.name}' is not allowed here")
503+
in.nextToken()
495504
case _ =>
496505
val privateWithin: TypeName =
497506
if (isPackageAccess && !inInterface) thisPackageName

Diff for: tests/neg/i18533.check

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
3 |class Pet permits Cat { // error
33
| ^^^^^^^
44
| A type declaration that has a permits clause should have a sealed modifier
5+
-- Error: tests/neg/i18533/non-SCALA_ONLY.java:4:7 ---------------------------------------------------------------------
6+
4 |public non class Test { // error
7+
| ^^^
8+
| Identifier 'non' is not allowed here

Diff for: tests/neg/i18533/Cat_SCALA_ONLY.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package i18533;
22

3-
public class Cat extends Pet {
3+
public final class Cat extends Pet {
44

55
}

Diff for: tests/neg/i18533/non-SCALA_ONLY.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package i18533;
2+
3+
// Special test for the non-sealed trick (See JavaParsers.scala::modifiers)
4+
public non class Test { // error
5+
6+
}

Diff for: tests/pos/i18533/Dog.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package i18533;
2+
3+
public non-sealed class Dog extends Pet {
4+
5+
}

Diff for: tests/pos/i18533/Pet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package i18533;
22

3-
public sealed class Pet permits Cat {
3+
public sealed class Pet permits Cat, Dog {
44

55
}

0 commit comments

Comments
 (0)