Skip to content

Commit dc60bcc

Browse files
oderskyKordyjan
authored andcommitted
Address review comments
1 parent 59c5391 commit dc60bcc

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import SourceVersion.*
66
import Feature.*
77
import core.Contexts.Context
88

9-
class MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion):
10-
assert(warnFrom.ordinal <= errorFrom.ordinal)
9+
class MigrationVersion(
10+
val warnFrom: SourceVersion,
11+
val errorFrom: SourceVersion):
12+
require(warnFrom.ordinal <= errorFrom.ordinal)
13+
1114
def needsPatch(using Context): Boolean =
12-
sourceVersion.isMigrating && sourceVersion.isAtLeast(errorFrom)
15+
sourceVersion.isMigrating && sourceVersion.isAtLeast(warnFrom)
16+
17+
def patchFrom: SourceVersion =
18+
warnFrom.prevMigrating
1319

1420
object MigrationVersion:
1521

@@ -27,6 +33,8 @@ object MigrationVersion:
2733

2834
val AscriptionAfterPattern = MigrationVersion(`3.3`, future)
2935

36+
val ExplicitContextBoundArgument = MigrationVersion(`3.4`, `3.5`)
37+
3038
val AlphanumericInfix = MigrationVersion(`3.4`, future)
3139
val RemoveThisQualifier = MigrationVersion(`3.4`, future)
3240
val UninitializedVars = MigrationVersion(`3.4`, future)

compiler/src/dotty/tools/dotc/config/SourceVersion.scala

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ enum SourceVersion:
1818
def stable: SourceVersion =
1919
if isMigrating then SourceVersion.values(ordinal + 1) else this
2020

21+
def prevMigrating: SourceVersion =
22+
if isMigrating then this else SourceVersion.values(ordinal - 1).prevMigrating
23+
2124
def isAtLeast(v: SourceVersion) = stable.ordinal >= v.ordinal
2225

2326
def isAtMost(v: SourceVersion) = stable.ordinal <= v.ordinal

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Symbols.*
1313
import Trees.*
1414
import ProtoTypes.*
1515
import Decorators.*
16-
import config.MigrationVersion
16+
import config.MigrationVersion as mv
1717
import config.Feature.{sourceVersion, migrateTo3}
1818
import config.SourceVersion.*
1919
import reporting.*
@@ -30,6 +30,15 @@ trait Migrations:
3030

3131
import tpd.*
3232

33+
/** Run `migration`, asserting we are in the proper Typer (not a ReTyper) */
34+
inline def migrate[T](inline migration: T): T =
35+
assert(!this.isInstanceOf[ReTyper])
36+
migration
37+
38+
/** Run `migration`, provided we are in the proper Typer (not a ReTyper) */
39+
inline def migrate(inline migration: Unit): Unit =
40+
if !this.isInstanceOf[ReTyper] then migration
41+
3342
/** Flag & migrate `?` used as a higher-kinded type parameter
3443
* Warning in 3.0-migration, error from 3.0
3544
*/
@@ -40,7 +49,7 @@ trait Migrations:
4049
else ""
4150
val namePos = tree.sourcePos.withSpan(tree.nameSpan)
4251
report.errorOrMigrationWarning(
43-
em"`?` is not a valid type name$addendum", namePos, MigrationVersion.Scala2to3)
52+
em"`?` is not a valid type name$addendum", namePos, mv.Scala2to3)
4453

4554
def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(using Context): Tree = {
4655
val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree: @unchecked
@@ -52,8 +61,8 @@ trait Migrations:
5261
case _ =>
5362
val recovered = typed(qual)(using ctx.fresh.setExploreTyperState())
5463
val msg = OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen, tree)
55-
report.errorOrMigrationWarning(msg, tree.srcPos, MigrationVersion.Scala2to3)
56-
if MigrationVersion.Scala2to3.needsPatch then
64+
report.errorOrMigrationWarning(msg, tree.srcPos, mv.Scala2to3)
65+
if mv.Scala2to3.needsPatch then
5766
// Under -rewrite, patch `x _` to `(() => x)`
5867
msg.actions
5968
.headOption
@@ -69,16 +78,16 @@ trait Migrations:
6978
case _ =>
7079
("(() => ", ")")
7180
}
81+
val mversion = mv.FunctionUnderscore
7282
def remedy =
7383
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
7484
else s"use `$prefix<function>$suffix` instead"
75-
def rewrite = Message.rewriteNotice("This construct", `3.4-migration`)
85+
def rewrite = Message.rewriteNotice("This construct", mversion.patchFrom)
7686
report.errorOrMigrationWarning(
7787
em"""The syntax `<function> _` is no longer supported;
7888
|you can $remedy$rewrite""",
79-
tree.srcPos,
80-
MigrationVersion.FunctionUnderscore)
81-
if MigrationVersion.FunctionUnderscore.needsPatch then
89+
tree.srcPos, mversion)
90+
if mversion.needsPatch then
8291
patch(Span(tree.span.start), prefix)
8392
patch(Span(qual.span.end, tree.span.end), suffix)
8493

@@ -89,19 +98,20 @@ trait Migrations:
8998
* Warning in 3.4, error in 3.5, rewrite in 3.5-migration.
9099
*/
91100
def contextBoundParams(tree: Tree, tp: Type, pt: FunProto)(using Context): Unit =
101+
val mversion = mv.ExplicitContextBoundArgument
92102
def isContextBoundParams = tp.stripPoly match
93103
case MethodType(ContextBoundParamName(_) :: _) => true
94104
case _ => false
95105
if sourceVersion.isAtLeast(`3.4`)
96106
&& isContextBoundParams
97107
&& pt.applyKind != ApplyKind.Using
98108
then
99-
def rewriteMsg = Message.rewriteNotice("This code", `3.5-migration`)
109+
def rewriteMsg = Message.rewriteNotice("This code", mversion.patchFrom)
100110
report.errorOrMigrationWarning(
101111
em"""Context bounds will map to context parameters.
102112
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""",
103-
tree.srcPos, MigrationVersion(`3.4`, `3.5`))
104-
if sourceVersion.isAtLeast(`3.5-migration`) then
113+
tree.srcPos, mversion)
114+
if mversion.needsPatch then
105115
patch(Span(pt.args.head.span.start), "using ")
106116
end contextBoundParams
107117

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

-1
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,4 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
189189
override protected def checkEqualityEvidence(tree: tpd.Tree, pt: Type)(using Context): Unit = ()
190190
override protected def matchingApply(methType: MethodOrPoly, pt: FunProto)(using Context): Boolean = true
191191
override protected def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree = promote(call)
192-
override protected def migrate[T](migration: => T, disabled: => T = ()): T = disabled
193192
}

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
158158
// Overridden in derived typers
159159
def newLikeThis(nestingLevel: Int): Typer = new Typer(nestingLevel)
160160

161-
// Overridden to do nothing in derived typers
162-
protected def migrate[T](migration: => T, disabled: => T = ()): T = migration
163-
164161
/** Find the type of an identifier with given `name` in given context `ctx`.
165162
* @param name the name of the identifier
166163
* @param pt the expected type
@@ -2982,7 +2979,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
29822979
}
29832980

29842981
override def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(using Context): Tree =
2985-
migrate(super.typedAsFunction(tree, pt), throw new AssertionError("can't retype a PostfixOp"))
2982+
migrate(super.typedAsFunction(tree, pt))
29862983

29872984
/** Translate infix operation expression `l op r` to
29882985
*
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//> using options -Xfatal-warnings
1+
//> using options -source 3.4
22

33
class C[T]
44
def foo[X: C] = ()
55

66
given [T]: C[T] = C[T]()
77

88
def Test =
9-
foo(C[Int]()) // error
9+
foo(C[Int]()) // warn
1010
foo(using C[Int]()) // ok

0 commit comments

Comments
 (0)