Skip to content

Commit c954ef0

Browse files
committed
Fix scala#5720: don't output X <: Long in Java generic signatures
In generic signature generator `jsig`, flag `primitiveOK` remembers if `jsig` is allowed to output primitive types or not; for instance, upper bounds can't contain primitive types. When transforming higher-kinded upper bounds `[X] => T`, `jsig` correctly recurs on `T`, but must remember flag `primitiveOK`. The status of `toplevel` should not change either (since it tracks if are still at the toplevel of the output signature, or instead we have started producing output), so passing it seems safest. Ideally we should test it at least on higher types whose body is a suitable `ClassInfo`; with polymorphic function types, we should also test `PolyType` bodies. Revised with tests requested in review.
1 parent 05cdff8 commit c954ef0

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ object GenericSignatures {
283283
jsig(atp, toplevel, primitiveOK)
284284

285285
case hktl: HKTypeLambda =>
286-
jsig(hktl.finalResultType)
286+
jsig(hktl.finalResultType, toplevel, primitiveOK)
287287

288288
case _ =>
289289
val etp = erasure(tp)

tests/pos/i5720.scala

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Ensure we omit valid JVM generic signatures when upper bounds are primitive
2+
// types. Java generic signatures cannot use primitive types in that position.
3+
4+
class AUnit[B[_] <: Unit]
5+
class ABoolean[B[_] <: Boolean]
6+
7+
class AByte[B[_] <: Byte]
8+
class AChar[B[_] <: Char]
9+
10+
class AShort[B[_] <: Short]
11+
class AInt[B[_] <: Int]
12+
class ALong[B[_] <: Long]
13+
14+
class AFloat[B[_] <: Float]
15+
class ADouble[B[_] <: Double]
16+
17+
class ValClU(val i: Unit) extends AnyVal
18+
class AValClU[B[_] <: ValClU]
19+
class ValClI(val i: Int) extends AnyVal
20+
class AValClI[B[_] <: ValClI]
21+
22+
package object opaquetypes {
23+
opaque type Logarithm = Double
24+
object Logarithm {
25+
class ALogarithm[B[_] <: Logarithm]
26+
type ALog = ALogarithm[[x] => Logarithm]
27+
type ALogD = ALogarithm[[x] => Double]
28+
type ALogN = ALogarithm[[x] => Nothing]
29+
30+
type A1Log = opaquetypes.ALogarithm[[x] => Logarithm]
31+
// type A1LogD = opaquetypes.ALogarithm[[x] => Double]
32+
type A1LogN = opaquetypes.ALogarithm[[x] => Nothing]
33+
34+
class ALogNested[B[_] <: Logarithm]
35+
class ValClLogNested(val i: Logarithm) extends AnyVal
36+
class AValClLogNested[B[_] <: ValClLogNested]
37+
38+
type A = AValClLogNested[[x] => ValClLogNested]
39+
type AN = AValClLogNested[[x] => Nothing]
40+
}
41+
class ValClLog(val i: Logarithm) extends AnyVal
42+
class AValClLog[B[_] <: ValClLog]
43+
type AClLog = AValClLog[[x] => ValClLog]
44+
type AClLogN = AValClLog[[x] => Nothing]
45+
46+
class ADouble[B[_] <: Double]
47+
type AD = ADouble[[x] => Double]
48+
type ADN = ADouble[[x] => Nothing]
49+
50+
class ALogarithm[B[_] <: Logarithm]
51+
type ALog = ALogarithm[[x] => Logarithm]
52+
type ALogN = ALogarithm[[x] => Nothing]
53+
54+
type A1Log = Logarithm.ALogarithm[[x] => Logarithm]
55+
type A1LogN = Logarithm.ALogarithm[[x] => Nothing]
56+
}
57+
58+
object Main {
59+
type AU = AUnit[[x] => Unit]
60+
type AUN = AUnit[[x] => Nothing]
61+
62+
type AB = ABoolean[[x] => Boolean]
63+
type ABN = ABoolean[[x] => Nothing]
64+
65+
66+
type ABy = AByte[[x] => Byte]
67+
type AByN = AByte[[x] => Nothing]
68+
69+
type AC = AChar[[x] => Char]
70+
type ACN = AChar[[x] => Nothing]
71+
72+
73+
type AS = AShort[[x] => Short]
74+
type ASN = AShort[[x] => Nothing]
75+
76+
type AI = AInt[[x] => Int]
77+
type AIN = AInt[[x] => Nothing]
78+
79+
type AL = ALong[[x] => Long]
80+
type ALN = ALong[[x] => Nothing]
81+
82+
83+
type AF = AFloat[[x] => Float]
84+
type AFN = AFloat[[x] => Nothing]
85+
86+
type AD = ADouble[[x] => Double]
87+
type ADN = ADouble[[x] => Nothing]
88+
89+
90+
type ACU = AValClU[[x] => ValClU]
91+
type ACUN = AValClU[[x] => Nothing]
92+
93+
type ACI = AValClI[[x] => ValClI]
94+
type ACIN = AValClI[[x] => Nothing]
95+
}

0 commit comments

Comments
 (0)