Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 65f62db

Browse files
committed
Add modifier to symbol that doesn't have a return type
The compiler reports for def meth and def meth: Unit the same tree, especially the `TypeTree` are exactly the same. The only way to find out if the document contains the return type or not is to do a lookup to the file. Fixes #1002221
1 parent 7c7e481 commit 65f62db

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

org.scala-refactoring.library/src/main/scala/scala/tools/refactoring/sourcegen/ReusingPrinter.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,19 @@ trait ReusingPrinter extends TreePrintingTraversals with AbstractPrinter {
977977
}
978978

979979
val body = p(rhs)
980-
val resultType = p(tpt, before = Requisite.allowSurroundingWhitespace(":", ": "))
980+
981+
def existsTptInFile = tpt match {
982+
case tpt: TypeTree =>
983+
val textInFile = betweenStartAndEnd(tpt).asText
984+
textInFile == tpt.toString() || textInFile == tpt.original.toString()
985+
case _ => false
986+
}
987+
988+
val resultType =
989+
if (body == EmptyFragment && !existsTptInFile)
990+
EmptyFragment
991+
else
992+
p(tpt, before = Requisite.allowSurroundingWhitespace(":", ": "))
981993

982994
def hasEqualInSource = {
983995
val originalDefDef = orig(tree)

org.scala-refactoring.library/src/test/scala/scala/tools/refactoring/tests/sourcegen/ReusingPrinterTest.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,57 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
146146
d.copy(mods = d.mods.withFlag(Flag.FINAL).withFlag(Flag.CASE).withFlag(Flag.PRIVATE)) replaces d
147147
}
148148
}}
149+
150+
@Test
151+
def add_modifier_to_def_without_return_type() = """
152+
trait T {
153+
def meth: Int
154+
}
155+
trait TT extends T {
156+
def meth
157+
}
158+
""" becomes """
159+
trait T {
160+
def meth: Int
161+
}
162+
trait TT extends T {
163+
override def meth
164+
}
165+
""" after topdown { matchingChildren {
166+
filter {
167+
case d: DefDef =>
168+
d.symbol.isOverridingSymbol && !d.symbol.isOverride
169+
} &>
170+
transform {
171+
case d: DefDef =>
172+
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE)) replaces d
173+
}
174+
}}
175+
176+
@Test
177+
def add_modifier_to_val_without_return_type() = """
178+
trait T {
179+
def meth: Int
180+
}
181+
trait TT extends T {
182+
val meth
183+
}
184+
""" becomes """
185+
trait T {
186+
def meth: Int
187+
}
188+
trait TT extends T {
189+
override val meth
190+
}
191+
""" after topdown { matchingChildren {
192+
filter {
193+
case d: ValDef =>
194+
val getter = d.symbol.getter(d.symbol.owner)
195+
getter.isOverridingSymbol && !getter.isOverride
196+
} &>
197+
transform {
198+
case d: ValDef =>
199+
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE)) replaces d
200+
}
201+
}}
149202
}

0 commit comments

Comments
 (0)