Skip to content

Commit 2b8aa2f

Browse files
committed
Improve handling of nameSpan
Even with the fix to enum spans, we could still get a case where a point to a name is missing (maybe because there is a long comment between the start of the definition and the name). The current fix makes the computation succeed more often, but it is not perfect.
1 parent c8265f1 commit 2b8aa2f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,20 @@ object Trees {
351351
if (span.exists) {
352352
val point = span.point
353353
if (rawMods.is(Synthetic) || name.toTermName == nme.ERROR) Span(point)
354-
else Span(point, point + name.stripModuleClassSuffix.lastPart.length, point)
354+
else {
355+
// Note: This might be inaccurate in the case where the span does not have a separate point
356+
// since the name position is too far away from the start position. In this case we scan
357+
// from the start position to find the name. But scanning might hit accidentally on the same
358+
// name (e.g. in a comment).
359+
// To make this behavior more robust we'd have to change the trees for definitions to contain
360+
// a fully positioned Ident in place of a name. Or else change the positioning scheme for enums
361+
// so that a case valdef does not include its synthesized RHS (which would mean we have to weaken
362+
// our invariants on position nesting).
363+
val realName = name.stripModuleClassSuffix.lastPart.toString
364+
def nameStart = source.content().indexOfSlice(realName, point)
365+
if (nameStart >= 0) Span(point, point + realName.length, point)
366+
else Span(point)
367+
}
355368
}
356369
else span
357370
}

0 commit comments

Comments
 (0)