Skip to content

Commit 8b1e36a

Browse files
authored
Merge pull request #6748 from dotty-staging/fix-enum-spans
Fix span of simple enum cases
2 parents 42c422b + 1a1f424 commit 8b1e36a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ object desugar {
895895
pats map {
896896
case id: Ident =>
897897
expandSimpleEnumCase(id.name.asTermName, mods,
898-
Span(pdef.span.start, id.span.end, id.span.start))
898+
Span(id.span.start, id.span.end, id.span.start))
899899
}
900900
else {
901901
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,23 @@ object Trees {
354354
if (span.exists) {
355355
val point = span.point
356356
if (rawMods.is(Synthetic) || name.toTermName == nme.ERROR) Span(point)
357-
else Span(point, point + name.stripModuleClassSuffix.lastPart.length, point)
357+
else {
358+
val realName = name.stripModuleClassSuffix.lastPart.toString
359+
val nameStart =
360+
if (point != span.start) point
361+
else {
362+
// Point might be too far away from start to be recorded. In this case we fall back to scanning
363+
// forwards from the start offset for the name.
364+
// Note: This might be inaccurate since scanning might hit accidentally the same
365+
// name (e.g. in a comment) before finding the real definition.
366+
// To make this behavior more robust we'd have to change the trees for definitions to contain
367+
// a fully positioned Ident in place of a name.
368+
val idx = source.content().indexOfSlice(realName, point)
369+
if (idx >= 0) idx
370+
else point // use `point` anyway. This is important if no source exists so scanning fails
371+
}
372+
Span(point, point + realName.length, point)
373+
}
358374
}
359375
else span
360376
}

0 commit comments

Comments
 (0)