Skip to content

Commit 44d022c

Browse files
committed
fix #9324: forbid no-arg java.lang.Enum ctor for migration mode
1 parent 8c56525 commit 44d022c

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,10 @@ trait Applications extends Compatibility {
895895
case funRef: TermRef =>
896896
val app = ApplyTo(tree, fun1, funRef, proto, pt)
897897
convertNewGenericArray(
898-
widenEnumCase(
899-
postProcessByNameArgs(funRef, app).computeNullable(),
900-
pt))
898+
guardJavaEnumCtor(
899+
widenEnumCase(
900+
postProcessByNameArgs(funRef, app).computeNullable(),
901+
pt)))
901902
case _ =>
902903
handleUnexpectedFunType(tree, fun1)
903904
}
@@ -1113,6 +1114,12 @@ trait Applications extends Compatibility {
11131114
tree
11141115
}
11151116

1117+
def guardJavaEnumCtor(tree: Tree)(using Context): Tree =
1118+
if config.Feature.migrateTo3 && tree.symbol == defn.JavaEnumClass.primaryConstructor then
1119+
report.error(
1120+
em"""not enough arguments for constructor Enum: (name: String, ordinal: Int): ${tree.tpe}""", tree.srcPos)
1121+
tree
1122+
11161123
/** If `tree` is a complete application of a compiler-generated `apply`
11171124
* or `copy` method of an enum case, widen its type to the underlying
11181125
* type by means of a type ascription, as long as the widened type is
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg/extend-java-enum-migration.scala:9:22 --------------------------------------------------------------
2+
9 |final class C extends jl.Enum[C] // error
3+
| ^^^^^^^^^^
4+
| not enough arguments for constructor Enum: (name: String, ordinal: Int): Enum[C]
5+
-- Error: tests/neg/extend-java-enum-migration.scala:11:17 -------------------------------------------------------------
6+
11 |object O extends jl.Enum[O.type] // error
7+
| ^^^^^^^^^^^^^^^
8+
| not enough arguments for constructor Enum: (name: String, ordinal: Int): Enum[O.type]
9+
-- Error: tests/neg/extend-java-enum-migration.scala:14:6 --------------------------------------------------------------
10+
14 |class Sub extends T // error
11+
| ^
12+
| not enough arguments for constructor Enum: (name: String, ordinal: Int): Enum[T]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.{lang => jl}
2+
3+
import language.`3.0-migration`
4+
5+
// This file is different from `tests/neg/extend-java-enum.scala` as we
6+
// are testing that it is illegal to *not* pass arguments to jl.Enum
7+
// in 3.0-migration
8+
9+
final class C extends jl.Enum[C] // error
10+
11+
object O extends jl.Enum[O.type] // error
12+
13+
trait T extends jl.Enum[T] // ok
14+
class Sub extends T // error

0 commit comments

Comments
 (0)