Skip to content

Commit e01b461

Browse files
committed
[nomaster] SI-8764 fix return type of case class productElement under Xexperimental
Under Xexperimental, productElement now returns the lub instead of the weak lub of case class parameter types (numeric widening shouldn't magically happen *inside* productElement). This was removed from 2.12.x in 6317ae2.
1 parent 7a69474 commit e01b461

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ trait SyntheticMethods extends ast.TreeDSL {
9595
// which they shouldn't.
9696
val accessorLub = (
9797
if (settings.Xexperimental) {
98-
global.weakLub(accessors map (_.tpe.finalResultType)) match {
98+
global.lub(accessors map (_.tpe.finalResultType)) match {
9999
case RefinedType(parents, decls) if !decls.isEmpty => intersectionType(parents)
100100
case tp => tp
101101
}

test/files/neg/t8764.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
t8764.scala:8: error: type mismatch;
2+
found : AnyVal
3+
required: Double
4+
val d: Double = a.productElement(0)
5+
^
6+
one error found

test/files/neg/t8764.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xexperimental

test/files/neg/t8764.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Main {
2+
3+
case class IntAndDouble(i: Int, d: Double)
4+
5+
// a.productElement used to be Int => Double
6+
// now: Int => AnyVal
7+
val a = IntAndDouble(1, 5.0)
8+
val d: Double = a.productElement(0)
9+
}

test/files/run/t8764.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
IntOnly: should return an unboxed int
2+
Int: int
3+
IntAndDouble: should just box and return Anyval
4+
Double: class java.lang.Double
5+
Int: class java.lang.Integer

test/files/run/t8764.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xexperimental

test/files/run/t8764.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test extends App {
2+
case class IntOnly(i: Int, j: Int)
3+
4+
println("IntOnly: should return an unboxed int")
5+
val a = IntOnly(1, 2)
6+
val i: Int = a.productElement(0)
7+
println(s"Int: ${a.productElement(0).getClass}")
8+
9+
case class IntAndDouble(i: Int, d: Double)
10+
11+
println("IntAndDouble: should just box and return Anyval")
12+
val b = IntAndDouble(1, 2.0)
13+
val j: AnyVal = b.productElement(0)
14+
println(s"Double: ${b.productElement(1).getClass}")
15+
println(s"Int: ${b.productElement(0).getClass}")
16+
}

0 commit comments

Comments
 (0)