Skip to content

Commit

Permalink
Add a -3.6-migration warning for opaque select changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Aug 7, 2024
1 parent 4fc8564 commit 6c86910
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val qual1 = qual.cast(liftedTp)
val tree1 = cpy.Select(tree0)(qual1, selName)
val rawType1 = selectionType(tree1, qual1)
tryType(tree1, qual1, rawType1)
val adapted = tryType(tree1, qual1, rawType1)
if !adapted.isEmpty && sourceVersion == `3.6-migration` then
val adaptedOld = tryExt(tree, qual)
if !adaptedOld.isEmpty then
val symOld = adaptedOld.symbol
val underlying = liftedTp match
case tp: TypeProxy => i" ${tp.translucentSuperType}"
case _ => ""
report.migrationWarning(
em"""Previously this selected the extension ${symOld}${symOld.showExtendedLocation}
|Now it selects $selName on the opaque type's underlying type$underlying
|
|You can change this back by selecting $adaptedOld
|Or by defining the extension method outside of the opaque type's scope.
|""", tree0)
adapted
else EmptyTree

// Otherwise, try to expand a named tuple selection
Expand Down
8 changes: 8 additions & 0 deletions tests/warn/i21239.Frac.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Migration Warning: tests/warn/i21239.Frac.scala:14:8 ----------------------------------------------------------------
14 | f + Frac.wrap(((-g.numerator).toLong << 32) | (g.unwrap & 0xFFFFFFFFL)) // warn
| ^^^
| Previously this selected the extension method + in object Frac
| Now it selects + on the opaque type's underlying type Long
|
| You can change this back by selecting kse.maths.Frac.+(f)
| Or by defining the extension method outside of the opaque type's scope.
15 changes: 15 additions & 0 deletions tests/warn/i21239.Frac.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kse.maths

import scala.language.`3.6-migration`

opaque type Frac = Long
object Frac {
inline def wrap(f: Long): kse.maths.Frac = f
extension (f: Frac)
inline def unwrap: Long = f
inline def numerator: Int = ((f: Long) >>> 32).toInt
extension (f: kse.maths.Frac)
def +(g: Frac): kse.maths.Frac = f // eliding domain-specific addition logic
def -(g: Frac): kse.maths.Frac =
f + Frac.wrap(((-g.numerator).toLong << 32) | (g.unwrap & 0xFFFFFFFFL)) // warn
}

0 comments on commit 6c86910

Please sign in to comment.