Skip to content

Commit 0b4f3ba

Browse files
committed
fix: map name position to desugared version of named context bound
1 parent a5a9fc8 commit 0b4f3ba

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,20 @@ object desugar {
272272
case ContextBounds(tbounds, ctxbounds) =>
273273
val isMember = evidenceFlags.isAllOf(DeferredGivenFlags)
274274
for bound <- ctxbounds do
275-
val evidenceName = bound match
275+
val (evidenceName, spanPoint) = bound match
276276
case ContextBoundTypeTree(_, _, ownName) if !ownName.isEmpty =>
277-
ownName // if there is an explicitly given name, use it.
277+
val realName = ownName.stripModuleClassSuffix.lastPart
278+
(ownName, bound.span.end - realName.length) // if there is an explicitly given name, use it.
278279
case _ =>
279280
if Config.nameSingleContextBounds
280281
&& !isMember
281282
&& ctxbounds.tail.isEmpty
282283
&& Feature.enabled(Feature.modularity)
283-
then tdef.name.toTermName
284-
else freshName(bound)
284+
then (tdef.name.toTermName, bound.span.point)
285+
else (freshName(bound), bound.span.point)
285286
evidenceNames += evidenceName
286-
val evidenceParam = ValDef(evidenceName, bound, EmptyTree).withFlags(evidenceFlags)
287+
val evidenceParam =
288+
ValDef(evidenceName, bound, EmptyTree).withFlags(evidenceFlags).withSpan(bound.span.withPoint(spanPoint))
287289
evidenceParam.pushAttachment(ContextBoundParam, ())
288290
evidenceBuf += evidenceParam
289291
tbounds

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,8 @@ object Parsers {
22652265
in.nextToken()
22662266
ident()
22672267
else EmptyTermName
2268-
ContextBoundTypeTree(t, pname, ownName)
2268+
val newSpan = t.span.withPoint(t.span.end).withEnd(in.lastOffset)
2269+
ContextBoundTypeTree(t, pname, ownName).withSpan(newSpan)
22692270

22702271
/** ContextBounds ::= ContextBound [`:` ContextBounds]
22712272
* | `{` ContextBound {`,` ContextBound} `}`

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24662466
def typedContextBoundTypeTree(tree: untpd.ContextBoundTypeTree)(using Context): Tree =
24672467
val tycon = typedType(tree.tycon)
24682468
def spliced(tree: Tree) = untpd.TypedSplice(tree)
2469-
val tparam = untpd.Ident(tree.paramName).withSpan(tree.span)
2469+
val tparam = untpd.Ident(tree.paramName).withSpan(tree.span.withEnd(tree.span.point))
24702470
if tycon.tpe.typeParams.nonEmpty then
24712471
val tycon0 = tycon.withType(tycon.tpe.etaCollapse)
24722472
typed(untpd.AppliedTypeTree(spliced(tycon0), tparam :: Nil))

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverDefnSuite.scala

+24
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,27 @@ class HoverDefnSuite extends BaseHoverSuite:
238238
|""".stripMargin,
239239
"val foo: Int".hover
240240
)
241+
242+
@Test def `i22335` =
243+
check(
244+
"""|def fromInt[T: Numeric as n@@um](t: Int): T = num.fromInt(t)
245+
|""".stripMargin,
246+
"""|num: Numeric[T]
247+
|""".stripMargin.hover
248+
)
249+
250+
@Test def `i22335-2` =
251+
check(
252+
"""|def showMax[X : {Numeric as nu@@m, Ordered as ord}](x: X, y: X): String = ???
253+
|""".stripMargin,
254+
"""|num: Numeric[X]
255+
|""".stripMargin.hover
256+
)
257+
258+
@Test def `i22335-3` =
259+
check(
260+
"""|def showMax[X : {Nu@@meric as num, Ordered as ord}](x: X, y: X): String = ???
261+
|""".stripMargin,
262+
"""|type Numeric: Numeric
263+
|""".stripMargin.hover
264+
)

0 commit comments

Comments
 (0)