Skip to content

Commit 6448ec9

Browse files
Florian3ktgodzik
authored andcommitted
fix java record varargs field accessor
[Cherry-picked 93f0e65]
1 parent 38be421 commit 6448ec9

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,14 @@ object JavaParsers {
873873
fieldsByName -= name
874874
end for
875875

876+
// accessor for record's vararg field (T...) returns array type (T[])
877+
def adaptVarargsType(tpt: Tree) = tpt match
878+
case PostfixOp(tpt2, Ident(tpnme.raw.STAR)) => arrayOf(tpt2)
879+
case _ => tpt
880+
876881
val accessors =
877882
(for (name, (tpt, annots)) <- fieldsByName yield
878-
DefDef(name, List(Nil), tpt, unimplementedExpr)
883+
DefDef(name, List(Nil), adaptVarargsType(tpt), unimplementedExpr)
879884
.withMods(Modifiers(Flags.JavaDefined | Flags.Method | Flags.Synthetic))
880885
).toList
881886

tests/pos-java16+/java-records/FromScala.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ object C:
4949
def useR4: Unit =
5050
val r4 = R4(1)
5151
val i: Int = r4.t
52+
53+
def useR5: Unit =
54+
val r5 = R5("hi", 1, 2, 3)
55+
val xs: Array[Int] = r5.values
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public record R5(String s, int... values) {}

0 commit comments

Comments
 (0)