Skip to content

Commit b0e8ac0

Browse files
committed
resolve end name later
1 parent 4ae44d6 commit b0e8ac0

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ object desugar {
182182
tpt = TypeTree(defn.UnitType),
183183
rhs = setterRhs
184184
).withMods((mods | Accessor) &~ (CaseAccessor | GivenOrImplicit | Lazy))
185-
.dropEndSpan
185+
.dropEndIndex
186186
Thicket(vdef1, setter)
187187
}
188188
else vdef1
@@ -875,7 +875,7 @@ object desugar {
875875
val modul = ValDef(moduleName, clsRef, New(clsRef, Nil))
876876
.withMods(mods.toTermFlags & RetainedModuleValFlags | ModuleValCreationFlags)
877877
.withSpan(mdef.span.startPos)
878-
.withEndSpan(copyFrom=mdef)
878+
.withEndIndex(copyFrom=mdef)
879879
val ValDef(selfName, selfTpt, _) = impl.self
880880
val selfMods = impl.self.mods
881881
if (!selfTpt.isEmpty) report.error(ObjectMayNotHaveSelfType(mdef), impl.self.srcPos)

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,35 +333,44 @@ object Trees {
333333

334334
import WithEndMarker.*
335335

336-
final def endToken: SimpleName = endMarker.stripModuleClassSuffix.lastPart
336+
final def endSpan(using Context): Span =
337+
self.getAttachment(EndIndex) match
338+
case Some(end) =>
339+
val realName = endName.stripModuleClassSuffix.lastPart
340+
Span(end - realName.length, end)
341+
case none => NoSpan
337342

338-
protected def endMarker: Name
343+
protected def endName(using Context): Name
339344

340-
final def withEndSpan(span: Span): self.type =
341-
self.withAttachment(EndMarker, span)
345+
final def withEndIndex(index: Int): self.type =
346+
self.withAttachment(EndIndex, index)
342347

343-
final def withEndSpan(copyFrom: WithEndMarker): self.type =
344-
copyFrom.endSpan.foreach(span => withEndSpan(span=span))
348+
final def withEndIndex(copyFrom: WithEndMarker): self.type =
349+
copyFrom.endIndex.foreach(index => withEndIndex(index))
345350
this
346351

347-
final def dropEndSpan: self.type =
348-
self.removeAttachment(EndMarker)
352+
final def dropEndIndex: self.type =
353+
self.removeAttachment(EndIndex)
349354
this
350355

351-
final def endSpan: Option[Span] = self.getAttachment(EndMarker)
356+
protected def endIndex: Option[Int] = self.getAttachment(EndIndex)
352357

353358
object WithEndMarker:
354359
/** Property key for trees with an `end` marker */
355-
private val EndMarker: Property.StickyKey[Span] = Property.StickyKey()
360+
private val EndIndex: Property.StickyKey[Int] = Property.StickyKey()
356361

357362
end WithEndMarker
358363

359364
abstract class NamedDefTree[-T >: Untyped](implicit @constructorOnly src: SourceFile)
360365
extends NameTree[T] with DefTree[T] with WithEndMarker {
361366
type ThisTree[-T >: Untyped] <: NamedDefTree[T]
362367

363-
protected def endMarker =
368+
protected def endName(using Context) =
364369
if name == nme.CONSTRUCTOR then nme.this_
370+
else srcName
371+
372+
protected def srcName(using Context): Name =
373+
if symbol.isPackageObject then symbol.owner.name
365374
else name
366375

367376
/** The position of the name defined by this definition.
@@ -376,7 +385,6 @@ object Trees {
376385
val point = span.point
377386
if (rawMods.is(Synthetic) || span.isSynthetic || name.toTermName == nme.ERROR) Span(point)
378387
else {
379-
val srcName = if symbol.isPackageObject then symbol.owner.name else name
380388
val realName = srcName.stripModuleClassSuffix.lastPart
381389
Span(point, point + realName.length, point)
382390
}
@@ -895,7 +903,7 @@ object Trees {
895903
extends ProxyTree[T] with WithEndMarker {
896904
type ThisTree[-T >: Untyped] = PackageDef[T]
897905
def forwardTo: RefTree[T] = pid
898-
protected def endMarker: Name = pid.name
906+
protected def endName(using Context): Name = pid.name
899907
}
900908

901909
/** arg @annot */

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,8 +1300,7 @@ object Parsers {
13001300
if didMatch then
13011301
stat match
13021302
case stat: WithEndMarker =>
1303-
val end = in.lastCharOffset
1304-
stat.withEndSpan(span=Span(end - stat.endToken.length, end))
1303+
stat.withEndIndex(index=in.lastCharOffset)
13051304
case _ =>
13061305
()
13071306
didMatch

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class ExtractSemanticDB extends Phase:
241241

242242
tree match
243243
case tree: WithEndMarker =>
244-
for endSpan <- tree.endSpan do
244+
val endSpan = tree.endSpan
245+
if endSpan.exists then
245246
registerUseGuarded(None, tree.symbol, endSpan, tree.source)
246247
case _ =>
247248

0 commit comments

Comments
 (0)