Skip to content

Commit 6c86910

Browse files
committed
Add a -3.6-migration warning for opaque select changes
1 parent 4fc8564 commit 6c86910

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
767767
val qual1 = qual.cast(liftedTp)
768768
val tree1 = cpy.Select(tree0)(qual1, selName)
769769
val rawType1 = selectionType(tree1, qual1)
770-
tryType(tree1, qual1, rawType1)
770+
val adapted = tryType(tree1, qual1, rawType1)
771+
if !adapted.isEmpty && sourceVersion == `3.6-migration` then
772+
val adaptedOld = tryExt(tree, qual)
773+
if !adaptedOld.isEmpty then
774+
val symOld = adaptedOld.symbol
775+
val underlying = liftedTp match
776+
case tp: TypeProxy => i" ${tp.translucentSuperType}"
777+
case _ => ""
778+
report.migrationWarning(
779+
em"""Previously this selected the extension ${symOld}${symOld.showExtendedLocation}
780+
|Now it selects $selName on the opaque type's underlying type$underlying
781+
|
782+
|You can change this back by selecting $adaptedOld
783+
|Or by defining the extension method outside of the opaque type's scope.
784+
|""", tree0)
785+
adapted
771786
else EmptyTree
772787

773788
// Otherwise, try to expand a named tuple selection

tests/warn/i21239.Frac.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Migration Warning: tests/warn/i21239.Frac.scala:14:8 ----------------------------------------------------------------
2+
14 | f + Frac.wrap(((-g.numerator).toLong << 32) | (g.unwrap & 0xFFFFFFFFL)) // warn
3+
| ^^^
4+
| Previously this selected the extension method + in object Frac
5+
| Now it selects + on the opaque type's underlying type Long
6+
|
7+
| You can change this back by selecting kse.maths.Frac.+(f)
8+
| Or by defining the extension method outside of the opaque type's scope.

tests/warn/i21239.Frac.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package kse.maths
2+
3+
import scala.language.`3.6-migration`
4+
5+
opaque type Frac = Long
6+
object Frac {
7+
inline def wrap(f: Long): kse.maths.Frac = f
8+
extension (f: Frac)
9+
inline def unwrap: Long = f
10+
inline def numerator: Int = ((f: Long) >>> 32).toInt
11+
extension (f: kse.maths.Frac)
12+
def +(g: Frac): kse.maths.Frac = f // eliding domain-specific addition logic
13+
def -(g: Frac): kse.maths.Frac =
14+
f + Frac.wrap(((-g.numerator).toLong << 32) | (g.unwrap & 0xFFFFFFFFL)) // warn
15+
}

0 commit comments

Comments
 (0)