Skip to content

Commit 36d9c9e

Browse files
Remove RHS of definitions in inline traits
1 parent cc83b2a commit 36d9c9e

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class Compiler {
9191
new ExplicitSelf, // Make references to non-trivial self types explicit as casts
9292
new StringInterpolatorOpt, // Optimizes raw and s and f string interpolators by rewriting them to string concatenations or formats
9393
new DropBreaks) :: // Optimize local Break throws by rewriting them
94-
List(new PruneErasedDefs, // Drop erased definitions from scopes and simplify erased expressions
94+
List(new PruneInlineTraits,
95+
new PruneErasedDefs, // Drop erased definitions from scopes and simplify erased expressions
9596
new UninitializedDefs, // Replaces `compiletime.uninitialized` by `_`
9697
new InlinePatterns, // Remove placeholders of inlined patterns
9798
new VCInlineMethods, // Inlines calls to value class methods
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import core._
5+
import Contexts._
6+
import DenotTransformers.SymTransformer
7+
import Flags._
8+
import SymDenotations._
9+
import Symbols._
10+
import MegaPhase.MiniPhase
11+
import ast.tpd
12+
13+
class PruneInlineTraits extends MiniPhase with SymTransformer { thisTransform =>
14+
import tpd._
15+
import PruneInlineTraits._
16+
17+
override def phaseName: String = PruneInlineTraits.name
18+
19+
override def description: String = PruneInlineTraits.description
20+
21+
override def transformSym(sym: SymDenotation)(using Context): SymDenotation =
22+
if isEraseable(sym) then sym.copySymDenotation(initFlags = sym.flags | Deferred)
23+
else sym
24+
25+
override def transformValDef(tree: ValDef)(using Context): ValDef =
26+
if isEraseable(tree.symbol) then cpy.ValDef(tree)(rhs = EmptyTree)
27+
else tree
28+
29+
override def transformDefDef(tree: DefDef)(using Context): DefDef =
30+
if isEraseable(tree.symbol) then cpy.DefDef(tree)(rhs = EmptyTree)
31+
else tree
32+
33+
private def isEraseable(sym: SymDenotation)(using Context): Boolean =
34+
sym.owner.isInlineTrait
35+
&& !sym.isConstructor
36+
&& !sym.is(Param)
37+
&& !sym.is(ParamAccessor)
38+
&& !sym.is(Private)
39+
&& !sym.isLocalDummy
40+
&& !sym.isType
41+
}
42+
43+
object PruneInlineTraits {
44+
import tpd._
45+
46+
val name: String = "pruneInlineTraits"
47+
val description: String = "drop rhs definitions in inline traits"
48+
}

0 commit comments

Comments
 (0)