Skip to content

Commit 62e3af5

Browse files
rochalatgodzik
authored andcommitted
Add enum type param support in sourceSymbol
1 parent a25fe5e commit 62e3af5

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

compiler/src/dotty/tools/dotc/core/Symbols.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,25 @@ object Symbols extends SymUtils {
357357
targets.match
358358
case (tp: NamedType) :: _ => tp.symbol.sourceSymbol
359359
case _ => this
360-
else if (denot.is(Synthetic)) {
360+
else if denot.is(Synthetic) then
361361
val linked = denot.linkedClass
362362
if (linked.exists && !linked.is(Synthetic))
363363
linked
364364
else
365365
denot.owner.sourceSymbol
366-
}
366+
else if (
367+
denot.is(TypeParam) &&
368+
denot.maybeOwner.maybeOwner.isAllOf(EnumCase) &&
369+
denot.maybeOwner.isPrimaryConstructor
370+
) then
371+
val enclosingEnumCase = denot.maybeOwner.maybeOwner
372+
val caseTypeParam = enclosingEnumCase.typeParams.find(_.name == denot.name)
373+
if caseTypeParam.exists(_.is(Synthetic)) then
374+
val enumClass = enclosingEnumCase.info.firstParent.typeSymbol
375+
val sourceTypeParam = enumClass.typeParams.find(_.name == denot.name)
376+
sourceTypeParam.getOrElse(this)
377+
else
378+
caseTypeParam.getOrElse(this)
367379
else if (denot.isPrimaryConstructor)
368380
denot.owner.sourceSymbol
369381
else this

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ object Interactive {
144144

145145
( sym == tree.symbol
146146
|| sym.exists && sym == tree.symbol.sourceSymbol
147+
|| sym.exists && sym.sourceSymbol == tree.symbol
147148
|| !include.isEmpty && sym.name == tree.symbol.name && sym.maybeOwner != tree.symbol.maybeOwner
148149
&& ( include.isOverridden && overrides(sym, tree.symbol)
149150
|| include.isOverriding && overrides(tree.symbol, sym)

language-server/test/dotty/tools/languageserver/DefinitionTest.scala

+34
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,38 @@ class DefinitionTest {
378378
.definition(m3 to m4, Nil)
379379
.definition(m5 to m6, Nil)
380380
.definition(m7 to m8, Nil)
381+
382+
@Test def typeParam: Unit = {
383+
code"""|class Foo[${m1}T${m2}]:
384+
| def test: ${m3}T${m4}"""
385+
.definition(m3 to m4, List(m1 to m2))
386+
}
387+
388+
@Test def enumTypeParam: Unit = {
389+
code"""|enum Test[${m1}T${m2}]:
390+
| case EnumCase(value: ${m3}T${m4})"""
391+
.definition(m3 to m4, List(m1 to m2))
392+
}
393+
394+
@Test def extMethodTypeParam: Unit = {
395+
code"""extension [${m1}T${m2}](string: String) def xxxx(y: ${m3}T${m4}) = ???"""
396+
.definition(m3 to m4, List(m1 to m2))
397+
}
398+
399+
@Test def typeParamCovariant: Unit = {
400+
code"""|class Foo[+${m1}T${m2}]:
401+
| def test: ${m3}T${m4}"""
402+
.definition(m3 to m4, List(m1 to m2))
403+
}
404+
405+
@Test def enumTypeParamCovariant: Unit = {
406+
code"""|enum Test[+${m1}T${m2}]:
407+
| case EnumCase(value: ${m3}T${m4})"""
408+
.definition(m3 to m4, List(m1 to m2))
409+
}
410+
411+
@Test def extMethodTypeParamCovariant: Unit = {
412+
code"""extension [+${m1}T${m2}](string: String) def xxxx(y: ${m3}T${m4}) = ???"""
413+
.definition(m3 to m4, List(m1 to m2))
414+
}
381415
}

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

+27
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
478478
|""".stripMargin
479479
)
480480

481+
@Test def `enum-class-type-param` =
482+
check(
483+
"""|
484+
|enum Options[<<AA>>]:
485+
| case Some(x: A@@A)
486+
| case None extends Options[Nothing]
487+
|""".stripMargin
488+
)
489+
490+
@Test def `enum-class-type-param-covariant` =
491+
check(
492+
"""|
493+
|enum Options[+<<AA>>]:
494+
| case Some(x: A@@A)
495+
| case None extends Options[Nothing]
496+
|""".stripMargin
497+
)
498+
499+
@Test def `enum-class-type-param-duplicate` =
500+
check(
501+
"""|
502+
|enum Testing[AA]:
503+
| case Some[<<AA>>](x: A@@A) extends Testing[AA]
504+
| case None extends Testing[Nothing]
505+
|""".stripMargin
506+
)
507+
481508
@Test def `derives-def` =
482509
check(
483510
"""|

0 commit comments

Comments
 (0)