Skip to content

Commit f68bd83

Browse files
committed
Drop PruneErasedDefs
The motivation for the phase was always unclear to me. No tests broke after the change. Piggybacked on the phase was the unrelated "anonymous class in inline method" warning. I moved it in changed form to FirstTransform. It is more efficient now since it does not need an auxiliary tree traversal.
1 parent 2d11d4c commit f68bd83

File tree

9 files changed

+60
-148
lines changed

9 files changed

+60
-148
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ class Compiler {
9292
new ExplicitSelf, // Make references to non-trivial self types explicit as casts
9393
new StringInterpolatorOpt, // Optimizes raw and s and f string interpolators by rewriting them to string concatenations or formats
9494
new DropBreaks) :: // Optimize local Break throws by rewriting them
95-
List(new PruneErasedDefs, // Drop erased definitions from scopes and simplify erased expressions
96-
new UninitializedDefs, // Replaces `compiletime.uninitialized` by `_`
95+
List(new UninitializedDefs, // Replaces `compiletime.uninitialized` by `_`
9796
new InlinePatterns, // Remove placeholders of inlined patterns
9897
new VCInlineMethods, // Inlines calls to value class methods
9998
new SeqLiterals, // Express vararg arguments as arrays

compiler/src/dotty/tools/dotc/cc/ccConfig.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ object ccConfig:
5656

5757
/** Not used currently. Handy for trying out new features */
5858
def newScheme(using ctx: Context): Boolean =
59-
ctx.settings.XdropComments.value
59+
Feature.sourceVersion.stable.isAtLeast(SourceVersion.`3.7`)
6060

6161
end ccConfig

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import StdNames.*
2020
import config.Feature
2121
import inlines.Inlines.inInlineMethod
2222
import util.Property
23+
import inlines.Inlines
24+
import reporting.InlinedAnonClassWarning
2325

2426
object FirstTransform {
2527
val name: String = "firstTransform"
@@ -207,6 +209,11 @@ class FirstTransform extends MiniPhase with SymTransformer { thisPhase =>
207209
case _ => tree
208210
}
209211

212+
override def transformTypeDef(tree: TypeDef)(using Context): Tree =
213+
if tree.symbol.isAnonymousClass && Inlines.inInlineMethod then
214+
report.warning(InlinedAnonClassWarning(), tree.symbol.sourcePos)
215+
tree
216+
210217
/** Perform one of the following simplification if applicable:
211218
*
212219
* true && y ==> y

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

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,6 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
335335
}
336336
}
337337

338-
private object dropInlines extends TreeMap {
339-
override def transform(tree: Tree)(using Context): Tree = tree match {
340-
case tree @ Inlined(call, _, expansion) =>
341-
val newExpansion = PruneErasedDefs.trivialErasedTree(tree)
342-
cpy.Inlined(tree)(call, Nil, newExpansion)
343-
case _ => super.transform(tree)
344-
}
345-
}
346-
347338
def checkUsableAsValue(tree: Tree)(using Context): Tree =
348339
def unusable(msg: Symbol => Message) =
349340
errorTree(tree, msg(tree.symbol))
@@ -414,26 +405,13 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
414405
checkUsableAsValue(tree) match
415406
case tree1: Select => transformSelect(tree1, Nil)
416407
case tree1 => tree1
417-
case tree: Apply =>
418-
val methType = tree.fun.tpe.widen.asInstanceOf[MethodType]
419-
val app =
420-
if (methType.hasErasedParams)
421-
tpd.cpy.Apply(tree)(
422-
tree.fun,
423-
tree.args.zip(methType.erasedParams).map((arg, isErased) =>
424-
if !isErased then arg
425-
else
426-
if methType.isResultDependent then
427-
Checking.checkRealizable(arg.tpe, arg.srcPos, "erased argument")
428-
if (methType.isImplicitMethod && arg.span.isSynthetic)
429-
arg match
430-
case _: RefTree | _: Apply | _: TypeApply if arg.symbol.is(Erased) =>
431-
dropInlines.transform(arg)
432-
case _ =>
433-
PruneErasedDefs.trivialErasedTree(arg)
434-
else dropInlines.transform(arg)))
435-
else
436-
tree
408+
case app: Apply =>
409+
val methType = app.fun.tpe.widen.asInstanceOf[MethodType]
410+
if (methType.hasErasedParams)
411+
for (arg, isErased) <- app.args.lazyZip(methType.erasedParams) do
412+
if isErased then
413+
if methType.isResultDependent then
414+
Checking.checkRealizable(arg.tpe, arg.srcPos, "erased argument")
437415
def app1 =
438416
// reverse order of transforming args and fun. This way, we get a chance to see other
439417
// well-formedness errors before reporting errors in possible inferred type args of fun.
@@ -499,7 +477,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
499477
registerIfHasMacroAnnotations(tree)
500478
checkErasedDef(tree)
501479
Checking.checkPolyFunctionType(tree.tpt)
502-
val tree1 = cpy.ValDef(tree)(tpt = makeOverrideTypeDeclared(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
480+
val tree1 = cpy.ValDef(tree)(tpt = makeOverrideTypeDeclared(tree.symbol, tree.tpt))
503481
if tree1.removeAttachment(desugar.UntupledParam).isDefined then
504482
checkStableSelection(tree.rhs)
505483
processValOrDefDef(super.transform(tree1))
@@ -508,7 +486,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
508486
checkErasedDef(tree)
509487
Checking.checkPolyFunctionType(tree.tpt)
510488
annotateContextResults(tree)
511-
val tree1 = cpy.DefDef(tree)(tpt = makeOverrideTypeDeclared(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
489+
val tree1 = cpy.DefDef(tree)(tpt = makeOverrideTypeDeclared(tree.symbol, tree.tpt))
512490
processValOrDefDef(superAcc.wrapDefDef(tree1)(super.transform(tree1).asInstanceOf[DefDef]))
513491
case tree: TypeDef =>
514492
registerIfHasMacroAnnotations(tree)
@@ -632,12 +610,6 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
632610
Checking.checkAndAdaptExperimentalImports(trees)
633611
super.transformStats(trees, exprOwner, wrapResult)
634612

635-
/** Transforms the rhs tree into a its default tree if it is in an `erased` val/def.
636-
* Performed to shrink the tree that is known to be erased later.
637-
*/
638-
private def normalizeErasedRhs(rhs: Tree, sym: Symbol)(using Context) =
639-
if (sym.isEffectivelyErased) dropInlines.transform(rhs) else rhs
640-
641613
private def registerNeedsInlining(tree: Tree)(using Context): Unit =
642614
if tree.symbol.is(Inline) && !Inlines.inInlineMethod && !ctx.mode.is(Mode.NoInline) then
643615
ctx.compilationUnit.needsInlining = true

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

Lines changed: 0 additions & 88 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class ResolveSuper extends MiniPhase with IdentityDenotTransformer { thisPhase =
3737

3838
override def description: String = ResolveSuper.description
3939

40-
override def runsAfter: Set[String] = Set(ElimByName.name, // verified empirically, need to figure out what the reason is.
41-
PruneErasedDefs.name) // Erased decls make `isCurrent` work incorrectly
40+
override def runsAfter: Set[String] = Set(ElimByName.name) // verified empirically, need to figure out what the reason is.
4241

4342
override def changesMembers: Boolean = true // the phase adds super accessors
4443

sbt-bridge/test/xsbt/CompileProgressSpecification.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CompileProgressSpecification {
6363
"staging",
6464
"splicing",
6565
"pickleQuotes",
66-
"MegaPhase{pruneErasedDefs,...,arrayConstructors}",
66+
"MegaPhase{uninitialized,...,arrayConstructors}",
6767
"erasure",
6868
"constructors",
6969
"genBCode"

tests/coverage/pos/macro-late-suspend/test.scoverage.check

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
# - description (can be multi-line)
1919
# ' ' sign
2020
# ------------------------------------------
21+
0
22+
macro-late-suspend/UsesTest.scala
23+
example
24+
UsesTest$package
25+
Object
26+
example.UsesTest$package
27+
<init>
28+
22
29+
22
30+
3
31+
<none>
32+
Literal
33+
true
34+
0
35+
false
36+
37+
2138
1
2239
macro-late-suspend/VisitorMacros.scala
2340
example
@@ -69,20 +86,3 @@ false
6986
false
7087
mkVisitorType[Test]
7188

72-
4
73-
macro-late-suspend/UsesTest.scala
74-
example
75-
UsesTest$package
76-
Object
77-
example.UsesTest$package
78-
<init>
79-
22
80-
22
81-
3
82-
<none>
83-
Literal
84-
true
85-
0
86-
false
87-
88-

tests/pos/erased-pure.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import language.experimental.erasedDefinitions
2+
3+
inline def id[T](x: T) = x
4+
5+
class C()
6+
7+
def foo[T](erased x: T): Unit = ()
8+
9+
class Pair[A, B](x: A, y: B)
10+
11+
12+
def Test =
13+
foo(C())
14+
foo(id(C()))
15+
foo(Pair(C(), C()))
16+
foo(Pair(C(), 22))
17+
foo(Pair(C(), "hello" + "world"))
18+
foo(id(Pair(id(C()), id("hello" + "world"))))
19+
20+
21+
22+
23+

0 commit comments

Comments
 (0)