Skip to content

Commit e93460b

Browse files
committed
Retain positions of operators after desugaring
1 parent b2408db commit e93460b

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ object desugar {
3939
*/
4040
val MultiLineInfix: Property.Key[Unit] = Property.StickyKey()
4141

42+
/** An attachment for retaining information about the original span of the operator after desugaring it to Select. */
43+
val OperatorSpan: Property.Key[Span] = Property.StickyKey()
44+
4245
/** What static check should be applied to a Match? */
4346
enum MatchCheck {
4447
case None, Exhaustive, IrrefutablePatDef, IrrefutableGenFrom
@@ -1215,7 +1218,7 @@ object desugar {
12151218
case _ => arg
12161219
}
12171220
def makeOp(fn: Tree, arg: Tree, selectPos: Span) =
1218-
val sel = Select(fn, op.name).withSpan(selectPos)
1221+
val sel = Select(fn, op.name).withSpan(selectPos).withAttachment(OperatorSpan, op.span)
12191222
if (left.sourcePos.endLine < op.sourcePos.startLine)
12201223
sel.pushAttachment(MultiLineInfix, ())
12211224
arg match

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,10 @@ object Trees {
419419
def nameSpan(using Context): Span =
420420
if span.exists then
421421
val point = span.point
422-
if span.isSynthetic || name.toTermName == nme.ERROR then Span(point)
423-
else Span(point, span.end, point)
422+
getAttachment(desugar.OperatorSpan) match
423+
case Some(s) => s
424+
case None if span.isSynthetic || name.toTermName == nme.ERROR => Span(point)
425+
case None => Span(point, span.end, point)
424426
else span
425427
}
426428

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ext
2+
3+
/*<-ext::RightAssociativeExtension$package.*/extension (s/*<-ext::RightAssociativeExtension$package.`:*:`().(s)*/: String/*->scala::Predef.String#*/)
4+
def :*:/*<-ext::RightAssociativeExtension$package.`:*:`().*/ (i/*<-ext::RightAssociativeExtension$package.`:*:`().(i)*/: Int/*->scala::Int#*/): (String/*->scala::Predef.String#*/, Int/*->scala::Int#*/) = (/*->scala::Tuple2.apply().*/s/*->ext::RightAssociativeExtension$package.`:*:`().(s)*/, i/*->ext::RightAssociativeExtension$package.`:*:`().(i)*/)
5+
6+
val b/*<-ext::RightAssociativeExtension$package.b.*/ = "foo" :*:/*->ext::RightAssociativeExtension$package.`:*:`().*/ 23
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ext
2+
3+
extension (s: String)
4+
def :*: (i: Int): (String, Int) = (s, i)
5+
6+
val b = "foo" :*: 23

tests/semanticdb/metac.expect

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,40 @@ Occurrences:
24152415
[31:10..31:11): T -> prefixes/M.T#
24162416
[31:14..31:17): ??? -> scala/Predef.`???`().
24172417

2418+
expect/RightAssociativeExtension.scala
2419+
--------------------------------------
2420+
2421+
Summary:
2422+
Schema => SemanticDB v4
2423+
Uri => RightAssociativeExtension.scala
2424+
Text => empty
2425+
Language => Scala
2426+
Symbols => 5 entries
2427+
Occurrences => 14 entries
2428+
2429+
Symbols:
2430+
ext/RightAssociativeExtension$package. => final package object ext
2431+
ext/RightAssociativeExtension$package.`:*:`(). => method :*:
2432+
ext/RightAssociativeExtension$package.`:*:`().(i) => param i
2433+
ext/RightAssociativeExtension$package.`:*:`().(s) => param s
2434+
ext/RightAssociativeExtension$package.b. => val method b
2435+
2436+
Occurrences:
2437+
[0:8..0:11): ext <- ext/
2438+
[2:0..2:0): <- ext/RightAssociativeExtension$package.
2439+
[2:11..2:12): s <- ext/RightAssociativeExtension$package.`:*:`().(s)
2440+
[2:14..2:20): String -> scala/Predef.String#
2441+
[3:6..3:9): :*: <- ext/RightAssociativeExtension$package.`:*:`().
2442+
[3:11..3:12): i <- ext/RightAssociativeExtension$package.`:*:`().(i)
2443+
[3:14..3:17): Int -> scala/Int#
2444+
[3:21..3:27): String -> scala/Predef.String#
2445+
[3:29..3:32): Int -> scala/Int#
2446+
[3:37..3:37): -> scala/Tuple2.apply().
2447+
[3:37..3:38): s -> ext/RightAssociativeExtension$package.`:*:`().(s)
2448+
[3:40..3:41): i -> ext/RightAssociativeExtension$package.`:*:`().(i)
2449+
[5:4..5:5): b <- ext/RightAssociativeExtension$package.b.
2450+
[5:14..5:17): :*: -> ext/RightAssociativeExtension$package.`:*:`().
2451+
24182452
expect/Selfs.scala
24192453
------------------
24202454

0 commit comments

Comments
 (0)