-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for JEP-409 (sealed classes) + Add javacOpt directive #19080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a1c39ef
3154c59
b734eb0
b0d00da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1546,6 +1546,7 @@ class Namer { typer: Typer => | |
* (2) If may not derive from itself | ||
* (3) The class is not final | ||
* (4) If the class is sealed, it is defined in the same compilation unit as the current class | ||
* (unless defined in Java. See JEP-409) | ||
* | ||
* @param isJava If true, the parent type is in Java mode, and we do not require a stable prefix | ||
*/ | ||
|
@@ -1569,7 +1570,7 @@ class Namer { typer: Typer => | |
if pclazz.is(Final) then | ||
report.error(ExtendFinalClass(cls, pclazz), cls.srcPos) | ||
else if pclazz.isEffectivelySealed && pclazz.associatedFile != cls.associatedFile then | ||
if pclazz.is(Sealed) then | ||
if pclazz.is(Sealed) && !pclazz.is(JavaDefined) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would seem to allow extending a sealed Java class in a Scala file, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it is allowed, If I compile with scalac There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it could be interesting to add this as a test case in a follow up |
||
report.error(UnableToExtendSealedClass(pclazz), cls.srcPos) | ||
else if sourceVersion.isAtLeast(future) then | ||
checkFeature(nme.adhocExtensions, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,7 +81,14 @@ def toolArgsFor(tool: ToolName, filename: Option[String])(lines: List[String]): | |
// groups are (name, args) | ||
// note: ideally we would replace everything that requires this to use directive syntax, however scalajs: --skip has no directive equivalent yet. | ||
private val toolArg = raw"(?://|/\*| \*) ?(?i:(${ToolName.values.mkString("|")})):((?:[^*]|\*(?!/))*)".r.unanchored | ||
|
||
// ================================================================================================ | ||
// =================================== VULPIX DIRECTIVES ========================================== | ||
// ================================================================================================ | ||
|
||
/** Directive to specify to vulpix the options to pass to Dotty */ | ||
private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored | ||
private val directiveJavacOptions = raw"//> using javacOpt (.*)".r.unanchored | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inspired from |
||
|
||
// Inspect the lines for compiler options of the form | ||
// `//> using options args`, `// scalajs: args`, `/* scalajs: args`, ` * scalajs: args` etc. | ||
|
@@ -90,10 +97,15 @@ private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored | |
def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,String)] = | ||
lines.flatMap { | ||
case toolArg("scalac", _) => sys.error(s"`// scalac: args` not supported. Please use `//> using options args`${filename.fold("")(f => s" in file $f")}") | ||
case toolArg("javac", _) => sys.error(s"`// javac: args` not supported. Please use `//> using javacOpt args`${filename.fold("")(f => s" in file $f")}") | ||
case toolArg(name, args) => List((name, args)) | ||
case _ => Nil | ||
} ++ | ||
lines.flatMap { case directiveOptionsArg(args) => List(("scalac", args)) case _ => Nil } | ||
lines.flatMap { | ||
case directiveOptionsArg(args) => List(("scalac", args)) | ||
case directiveJavacOptions(args) => List(("javac", args)) | ||
case _ => Nil | ||
} | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
@@ -104,6 +116,6 @@ class ToolArgsTest: | |
@Test def `tool is present`: Unit = assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test, None)("// test: -hey" :: Nil)) | ||
@Test def `missing tool is absent`: Unit = assertEquals(Nil, toolArgsFor(ToolName.Javac, None)("// test: -hey" :: Nil)) | ||
@Test def `multitool is present`: Unit = | ||
assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test, None)("// test: -hey" :: "// javac: -d /tmp" :: Nil)) | ||
assertEquals("-d" :: "/tmp" :: Nil, toolArgsFor(ToolName.Javac, None)("// test: -hey" :: "// javac: -d /tmp" :: Nil)) | ||
assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test, None)("// test: -hey" :: "// java: -d /tmp" :: Nil)) | ||
assertEquals("-d" :: "/tmp" :: Nil, toolArgsFor(ToolName.Java, None)("// test: -hey" :: "// java: -d /tmp" :: Nil)) | ||
end ToolArgsTest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Error: tests/neg/i18533/Pet_SCALA_ONLY.java:3:10 -------------------------------------------------------------------- | ||
3 |class Pet permits Cat { // error | ||
| ^^^^^^^ | ||
| A type declaration that has a permits clause should have a sealed modifier | ||
-- Error: tests/neg/i18533/non-SCALA_ONLY.java:4:7 --------------------------------------------------------------------- | ||
4 |public non class Test { // error | ||
| ^^^ | ||
| Identifier 'non' is not allowed here |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package i18533; | ||
|
||
public final class Cat extends Pet { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package i18533; | ||
|
||
class Pet permits Cat { // error | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package i18533; | ||
|
||
// Special test for the non-sealed trick (See JavaParsers.scala::modifiers) | ||
public non class Test { // error | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//> using javacOpt --enable-preview --source 17 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These options should be removed when CI is bumped to Java 21 (See #19701) |
||
//> test: -jvm 17+ | ||
|
||
package i18533; | ||
|
||
public final class Cat extends Pet { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//> using javacOpt --enable-preview --source 17 | ||
//> test: -jvm 17+ | ||
|
||
package i18533; | ||
|
||
public non-sealed class Dog extends Pet { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//> using javacOpt --enable-preview --source 17 | ||
//> test: -jvm 17+ | ||
|
||
package i18533; | ||
|
||
public sealed class Pet permits Cat, Dog { | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.