Skip to content

Commit 5c74900

Browse files
committed
Fix name clashes because of @usecase
1 parent 5a6927e commit 5c74900

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import dotty.tools.dotc.core.Symbols.Symbol
1313
class UsecasePhase extends DocMiniPhase {
1414
private def defdefToDef(d: tpd.DefDef, sym: Symbol)(implicit ctx: Context) = DefImpl(
1515
sym,
16-
d.name.decode.toString,
16+
d.name.show.split("\\$").head, // UseCase defs get $pos appended to their names
1717
flags(d), path(d.symbol),
1818
returnType(d.tpt.tpe),
1919
typeParams(d.symbol),

dottydoc/test/UsecaseTest.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class UsecaseTest extends DottyTest {
2929
checkSources(source :: Nil) { packages =>
3030
packages("scala") match {
3131
case PackageImpl(_, _, List(trt: Trait), _, _) =>
32-
val List(map: Def) = trt.members
32+
val List(foo: Def) = trt.members
3333

34-
val returnValue = map.returnValue match {
34+
val returnValue = foo.returnValue match {
3535
case ref: TypeReference => ref.title
3636
case _ =>
3737
assert(
@@ -42,11 +42,22 @@ class UsecaseTest extends DottyTest {
4242
}
4343

4444
assert(
45-
map.typeParams.isEmpty,
45+
foo.typeParams.isEmpty,
4646
"Type parameters were not stripped by usecase"
4747
)
4848
assert(returnValue == "A", "Incorrect return type after usecase")
49+
50+
assert(foo.name == "foo", s"Incorrect name after transform: ${foo.name}")
4951
}
5052
}
5153
}
54+
55+
@Test def checkIterator = {
56+
val sources =
57+
"./scala-scala/src/library/scala/collection/Iterator.scala" :: Nil
58+
59+
checkFiles(sources) { packages =>
60+
// success if typer throws no errors! :)
61+
}
62+
}
5263
}

src/dotty/tools/dotc/core/Comments.scala

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Symbols._
88
import Contexts.Context
99
import Flags.EmptyFlags
1010
import dotc.util.SourceFile
11-
import dotc.util.Positions.Position
11+
import dotc.util.Positions._
1212
import dotc.parsing.Parsers.Parser
1313
import dotty.tools.dottydoc.model.comment.CommentUtils._
1414

@@ -38,14 +38,22 @@ object Comments {
3838
private def decomposeUseCase(start: Int, end: Int): UseCase = {
3939
val codeStart = skipWhitespace(raw, start + "@usecase".length)
4040
val codeEnd = skipToEol(raw, codeStart)
41-
val code = raw.substring(codeStart, codeEnd)
42-
val codePos = Position(codeStart, codeEnd)
41+
val code = raw.substring(codeStart, codeEnd) + " = ???"
42+
val codePos = subPos(codeStart, codeEnd)
4343
val commentStart = skipLineLead(raw, codeEnd + 1) min end
4444
val comment = "/** " + raw.substring(commentStart, end) + "*/"
45-
val commentPos = Position(commentStart, end)
45+
val commentPos = subPos(commentStart, end)
4646

47-
UseCase(Comment(commentPos, comment), code + " = ???", codePos)
47+
UseCase(Comment(commentPos, comment), code, codePos)
4848
}
49+
50+
private def subPos(start: Int, end: Int) =
51+
if (pos == NoPosition) NoPosition
52+
else {
53+
val start1 = pos.start + start
54+
val end1 = pos.end + end
55+
pos withStart start1 withPoint start1 withEnd end1
56+
}
4957
}
5058

5159
case class UseCase(comment: Comment, code: String, codePos: Position)(implicit ctx: Context) {
@@ -56,9 +64,11 @@ object Comments {
5664
val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start, EmptyFlags)
5765

5866
tree match {
59-
case tree: untpd.DefDef => tree
67+
case tree: untpd.DefDef =>
68+
val newName = (tree.name.show + "$" + codePos.start).toTermName
69+
untpd.DefDef(newName, tree.tparams, tree.vparamss, tree.tpt, tree.rhs)
6070
case _ =>
61-
ctx.error("proper def was not found in `@usecase`", codePos)
71+
ctx.error("proper definition was not found in `@usecase`", codePos)
6272
tree
6373
}
6474
}
@@ -67,11 +77,14 @@ object Comments {
6777
var tpdCode: tpd.DefDef = _
6878

6979
def typeTree()(implicit ctx: Context): Unit = untpdCode match {
70-
case df: untpd.DefDef => ctx.typer.typedDefDef(df, symbol) match {
71-
case tree: tpd.DefDef => tpdCode = tree
72-
case _ => ctx.error("proper def was not found in `@usecase`", codePos)
73-
}
74-
case _ => ctx.error("proper def was not found in `@usecase`", codePos)
80+
case df: untpd.DefDef =>
81+
ctx.typer.typedDefDef(df, symbol) match {
82+
case tree: tpd.DefDef => tpdCode = tree
83+
case _ =>
84+
ctx.error("proper def could not be typed from `@usecase`", codePos)
85+
}
86+
case _ =>
87+
ctx.error("proper def was not found in `@usecase`", codePos)
7588
}
7689
}
7790
}

0 commit comments

Comments
 (0)